使用Python来自statsmodels.formula.api的交互作用图

时间:2019-04-13 08:23:47

标签: python python-3.x plot statsmodels interaction-plot

来自here(我的网站)。我想知道如何从interaction plot个对象中绘制statsmodels.formula.api

Consumption = [51, 52, 53, 54, 56, 57, 55, 56, 58, 59, 62, 63]
Gender  = ["Male", "Male", "Male", "Male", "Male", "Male", "Female", "Female", "Female", "Female", "Female", "Female"]
Income = [80, 80, 90, 90, 100, 100, 80, 80, 90, 90, 100, 100]

import pandas as pd
df6 = pd.DataFrame(
 {
   "Consumption": Consumption
 , "Gender": Gender
 , "Income": Income
 }
 )

print(df6)

from statsmodels.formula.api import ols
from statsmodels.stats.anova import anova_lm
Reg6 = ols(formula = "Consumption ~ Gender + Income", data = df6)
Fit6 = Reg6.fit()

Reg7 = ols(formula = "Consumption ~ Gender*Income", data = df6)
Fit7 = Reg7.fit()

已编辑

我想绘制Reg6Reg7

1 个答案:

答案 0 :(得分:1)

到目前为止,我还看不到如何在同一交互图上绘制Reg6和Reg7。您可以通过这种方式绘制不同的图:

from statsmodels.graphics.factorplots import interaction_plot
fig = interaction_plot(Income, Gender, Consumption,
             colors=['black','gray'], markers=['D','^'], ylabel='Consumption', xlabel='Income')
fig = interaction_plot(Income, Gender, Fit6.fittedvalues,
             colors=['red','blue'], markers=['D','^'], ylabel='Consumption', xlabel='Income')
fig = interaction_plot(Income, Gender, Fit7.fittedvalues,
             colors=['green','orange'], markers=['D','^'], ylabel='Consumption', xlabel='Income')

import matplotlib.pyplot as plt
plt.show()