Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
poisson regression
#1
I am working with poisson regression with pandas
and I have a question about this specific command
poisson(y,x)
.
with y,x being respectively the dependent, independent variables.

the following code takes two columns from an excel file and works properly

import pandas as pd
from statsmodels.formula.api import poisson
from statsmodels.formula.api import negativebinomial

data = pd.read_csv(r'file.csv')
varcomp = 'y ~ x'
model_1 = poisson(varcomp, data).fit()
print( model_1.summary())


but now I want x and y to be two rows instead of columns, but I am a bit lost
and don't know how to proceed.

I have tried to convert those two columns into lists, since I will be able to do the same with rows,
but once I implement them into the
poisson
command, I get an error.
Thank you in advance.

x_list=data['x'].tolist()
y_list=data['y'].tolist()

model_1 = poisson('y_list ~ x_list', data).fit()
print( model_1.summary())
Error:
File "C:\Users\lib\site-packages\patsy\highlevel.py", line 312, in dmatrices raise PatsyError("model is missing required outcome variables") patsy.PatsyError: model is missing required outcome variables
Reply
#2
Did you try transposing the dataframe?
tdata = data.transpose()
Reply
#3
I was certainly not aware of that option, and thanks for letting me know.
However, if possible, I would like to stick to lists, since it gives me
more freedom to do what I need to do,
for instance in some cases, I have to sum the numbers from different cells
before starting to work with them, and I am not sure how else to do it.
Any ideas?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020