简体   繁体   中英

Multiple Linear Regression in Python (PatsyError: model is missing required outcome variables)

I am running the following code for regression in Python and I get the error (PatsyError: model is missing required outcome variables). How do I fix it? Thanks

Y = spikers['grade'] 
X = spikers[['num_pageview', 'num_video_play_resume', 'eng_proficiency', 'english']] 
model = smf.ols(Y,X).fit() 
model.summary()

You should use the following commands:

df = pd.DataFrame({'x':X, 'y':Y})
model = smf.ols('y~x', data=df).fit()

In which df is your DataFrame type data.

I had a very similar problem trying to run sm.logit on an outcome variable 'y' that is binary (0s or 1s): let all my data be in a pandas data frame called 'data:

import statsmodels.formula.api as sm

X = ['Age','Sex','x1','x2','x3','x4']
logit = sm.logit(data['y'],data[X])
result = logit.fit()
print result.summary()

Traceback (most recent call last):

  File "<ipython-input-XXX>", line 1, in <module>
    logit = sm.logit(data['y'],data[X])

  File "C:\...\statsmodels\base\model.py", line 147, in from_formula
    missing=missing)

  File "C:\...\statsmodels\formula\formulatools.py", line 68, in handle_formula_data
    NA_action=na_action)

  File "C:\...\patsy\highlevel.py", line 312, in dmatrices
    raise PatsyError("model is missing required outcome variables")

PatsyError: model is missing required outcome variables

I was getting this error message displayed above. I managed to fix that and pull out some sensible results by using this notation instead:

f1 = 'y ~ Age+Sex+x1+x2+x3+x4'
logit = sm.logit(formula = f1, data = data)
result = logit.fit()

This kind of notational use of the statsmodels.formula.api is usually preferred, as far as I can tell

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM