perception
之间有什么互动, outcome
, 和 outlook
?
我已将它们带入分类变量以 [潜在地] 简化事情。
import pandas as pd
import numpy as np
high, size = 100, 20
df = pd.DataFrame({'perception': np.random.randint(0, high, size),
'age': np.random.randint(0, high, size),
'smokes_cat': pd.Categorical(np.tile(['lots', 'little', 'not'],
size//3+1)[:size]),
'outcome': np.random.randint(0, high, size),
'outlook_cat': pd.Categorical(np.tile(['positive', 'neutral',
'negative'],
size//3+1)[:size])
})
df.insert(2, 'age_cat', pd.Categorical(pd.cut(df.age, range(0, high+5, size//2),
right=False, labels=[
"{0} - {1}".format(i, i + 9)
for i in range(0, high, size//2)])))
def tierify(i):
if i <= 25:
return 'lowest'
elif i <= 50:
return 'low'
elif i <= 75:
return 'med'
return 'high'
df.insert(1, 'perception_cat', df['perception'].map(tierify))
df.insert(6, 'outcome_cat', df['outcome'].map(tierify))
np.random.shuffle(df['smokes_cat'])
在线运行:http://ideone.com/fftuSv或 https://repl.it/repls/MicroLeftSequences
这是伪造的数据,但应该提出这个想法。个人看法
perception
,然后他们会看到实际的 outcome
,从中可以决定他们的outlook
.使用 Python( Pandas ,或任何真正开源的东西),我如何显示概率 — 和 p 值— 这些之间的相互作用 3 依赖 栏目 (可能使用
age
、 smokes_cat
作为潜在的混杂因素)?
最佳答案
您可以为此特定目的使用交互图。这非常适合您的情况。我会为你的数据使用这样的情节。我已经针对问题中生成的虚拟数据进行了尝试,您可以编写如下代码。虽然将其视为伪代码,但您必须根据需要定制代码。
以其简单的形式:
然而,为了获得更多和更深入的理解,我放置了一些您可以查看的链接。
代码
... # The rest of the code in the question.
# Interaction plot
import matplotlib.pyplot as plt
from statsmodels.graphics.factorplots import interaction_plot
p = interaction_plot(
x = df['perception'],
trace=df['outlook_cat'],
response= df['outcome']
)
plt.savefig('./my_interaction_plot.png') # or plt.show()
您可以找到
interaction_plot()
的文档here .此外,我还建议您运行方差分析。进一步阅读
您可以查看这些链接:
关于statistics - 统计测试 : how do (perception; actual results; and next) interact?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57536795/