Reputation: 65
I have data as x and y variables in python stored as a list. How can i import this to python to run through statsmodels.
from __future__ import print_function
import statsmodels.api as sm
import statsmodels.formula.api as smf
import pandas as pd
x = [1,1,2,3]
y=[1,0,0,0]
data = pd.DataFrame(x,y) #to merge the two side by side
star98 = sm.datasets.star98.load_pandas().data
formula = 'x ~ y'
pd.options.mode.chained_assignment = None # default='warn'
mod1 = smf.glm(formula=formula, data=data, family=sm.families.Binomial()).fit()
x = mod1.summary()
ValueError: The first guess on the deviance function returned a nan. This could be a boundary problem and should be reported
Upvotes: 0
Views: 1748
Reputation: 3947
You had a couple of minor problems. First, the way you were building your data, y
was actually interpreted as the index of the dataframe:
In [3]:
x = [1,1,2,3]
y=[1,0,0,0]
data = pd.DataFrame(x,y) #to merge the two side by side
data
Out[3]:
0
1 1
0 1
0 2
0 3
Instead, you have to pass both as columns and make sure that they get column names; the easier is probably with a dictionary:
In [13]:
x = [1,1,2,3]
y = [1,0,0,0]
data = pd.DataFrame({'x' : x, 'y' : y}) #to merge the two side by side
data
Out[13]:
x y
0 1 1
1 1 0
2 2 0
3 3 0
Secondly, your formula was wrong (since I guess you are trying to classify y
from the data in x
), it should be,
formula = 'y ~ x'
If you fit it that way with the rest of your code, you'll get some better results.
In [21]:
x
Out[21]:
Generalized Linear Model Regression Results
Dep. Variable: y No. Observations: 4
Model: GLM Df Residuals: 2
Model Family: Binomial Df Model: 1
Link Function: logit Scale: 1.0
Method: IRLS Log-Likelihood: -1.3863
Date: Mon, 28 Mar 2016 Deviance: 2.7726
Time: 15:34:32 Pearson chi2: 2.00
No. Iterations: 22
coef std err z P>|z| [95.0% Conf. Int.]
Intercept 22.1423 3.9e+04 0.001 1.000 -7.64e+04 7.64e+04
x -22.1423 3.9e+04 -0.001 1.000 -7.64e+04 7.64e+04
Hope it helps.
Upvotes: 4