I am unable to perform annova on a linear regression model. I have attached the simplified code below. Please let me know how to fix it.
import statsmodels.api as sm
import numpy as np
# define the data
x1 = np.random.rand(100)
x2 = np.random.rand(100)
y = 2*x1 + 3*x2 + np.random.normal(size=100)
# build the model with all independent variables
X = sm.add_constant(np.column_stack((x1, x2)))
model = sm.OLS(y, X).fit()
# perform the F-test
f_value, p_value, _ = sm.stats.anova_lm(model, typ=1)
From
anova_lm
documentation:You would need to use the formula API when defining your model
Additionally I'm not sure why you're adding a constant in
X
. In this way your fitted model would end up having two intercepts. I guess what you were actually trying to achieve was