Difference in Python statsmodels OLS and R's lm Difference in Python statsmodels OLS and R's lm r r

Difference in Python statsmodels OLS and R's lm


Looks like Python does not add an intercept by default to your expression, whereas R does when you use the formula interface..

This means you did fit two different models. Try

lm( y ~ x - 1, data)

in R to exclude the intercept, or in your case and with somewhat more standard notation

lm(num_rx ~ ridageyr - 1, data=demoq)


Note that you can still use ols from statsmodels.formula.api:

from statsmodels.formula.api import olsresults = ols('num_rx ~ ridageyr', demoq).fit()results.summary()

I think it uses patsy in the backend to translate the formula expression, and intercept is added automatically.