Interaction Plot from statsmodels.formula.api using Python
Asked Answered
C

1

5

Taken from here (my website). I wonder how to plot interaction plot from statsmodels.formula.api objects.

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()

Edited

I want to plot Reg6 and Reg7.

Camara answered 13/4, 2019 at 8:23 Comment(1)
Can you please explain what an interaction plot of Reg6 and Reg7 means? What is on the Y-Axis/X-Axis?Towe
K
7

I cannot see - so far - how to plot both Reg6 and Reg7 on the same interaction plot. You can take different plots that way:

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()
Kozloski answered 15/4, 2019 at 13:13 Comment(1)
Thanks @Kozloski for very useful answer. ThanksCamara

© 2022 - 2024 — McMap. All rights reserved.