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

标签 python python-3.x plot statsmodels

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

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

最佳答案

到目前为止,我看不出如何在同一个交互图上同时绘制 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()

关于python - 来自 statsmodels.formula.api 使用 Python 的交互图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55663474/

相关文章:

matlab - 在 MATLAB 中,如何更改子图的背景颜色?

python - Matplotlib:仅一个子图具有相同的纵横比

python - 根据值列 a 映射到列 b 获取列 c 创建新列

python - 在 Django 模型中使用请求

python - 如何为 range() 使用小数步长值?

c++ - ValueError : ['path' ] when installing pyfim

python - 由 matplotlib 创建的 eps 文件的 LaTeX 文档和 BoundingBox

python - 列表理解中的调用函数

python - 使用二维 numpy 数组有效填充 pandas 数据框

r - 绘制折线图,​​xy.coords(x,y,xlabel,ylabel,log)中的错误: 'x' and 'y' lengths differ