Page 49, Listing 2.1:
While not a true error, it has been pointed out the model would make more sense if we add a constant to the formula. For demonstration purposes
we left this out but if you like, you can do so with minimum effort. The following code:
import statsmodels.api as sm
import numpy as np
predictors = np.random.random(1000).reshape(500,2)
target = predictors.dot(np.array([0.4, 0.6])) + np.random.random(500)
lmRegModel = sm.OLS(target,predictors)
result = lmRegModel.fit()
result.summary()
Now becomes:
import statsmodels.api as sm
import numpy as np
predictors = np.random.random(1000).reshape(500,2)
target = predictors.dot(np.array([0.4, 0.6])) + np.random.random(500)
predictors = sm.add_constant(predictors)
lmRegModel = sm.OLS(target,predictors)
result = lmRegModel.fit()
result.summary()
A single line of code is sufficient to add this constant. Our summary now shows coefficients way closer to the true imputed values.