Ignoring missing values in multiple OLS regression with statsmodels

浪子不回头ぞ 提交于 2019-12-04 02:16:22

You answered your own question. Just pass

missing = 'drop'

to ols

import statsmodels.formula.api as smf
...
results = smf.ols(formula = "da ~ cfo + rm_proxy + cpi + year", 
                 data=df, missing='drop').fit()

If this doesn't work then it's a bug and please report it with a MWE on github.

FYI, note the import above. Not everything is available in the formula.api namespace, so you should keep it separate from statsmodels.api. Or just use

import statsmodels.api as sm
sm.formula.ols(...)

The answer from jseabold works very well, but it may be not enough if you the want to do some computation on the predicted values and true values, e.g. if you want to use the function mean_squared_error. In that case, it may be better to get definitely rid of NaN

df = pd.read_csv('cl_030314.csv')
df_cleaned = df.dropna()
results = sm.ols(formula = "da ~ cfo + rm_proxy + cpi + year", data=df_cleaned).fit()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!