Reputation: 71
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()
Upvotes: 1
Views: 22874
Reputation: 51
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.
Upvotes: 5
Reputation: 31
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
Upvotes: 2