Difference in Python statsmodels OLS and R's lm

浪尽此生 提交于 2019-11-30 01:43:00

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 ols

results = 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!