如何解决在 Python 中绑定 GLM 公式中的列
我有以下数据(避孕药使用):
import statsmodels.api as sm
import pandas as pd
data = pd.read_table('https://data.princeton.edu/wws509/datasets/cuse.dat',sep="\s+",header = 0)
data.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 16 entries,0 to 15
Data columns (total 5 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 age 16 non-null object
1 education 16 non-null object
2 wantsMore 16 non-null object
3 notUsing 16 non-null int64
4 using 16 non-null int64
dtypes: int64(2),object(3)
memory usage: 768.0+ bytes
这是数据:
data.head()
age education wantsMore notUsing using
1 <25 low yes 53 6
2 <25 low no 10 4
3 <25 high yes 212 52
4 <25 high no 50 10
5 25-29 low yes 60 14
我想使用 GLM 创建一个模型。当我使用 R 时,这是我的方法,其中响应(使用 + notUsing)取决于年龄、教育和需求更多:
lrfit = glm( cbind(using,notUsing) ~ age + education + wantsMore,data = data,family = binomial)
在这种情况下,我需要在公式中绑定列。但是对于 python 我已经尝试过这个:
model = sm.formula.glm("notUsing + using ~ C(age,education,wantsMore)",family=sm.families.Binomial(),data=data).fit()
但是出现了错误AttributeError: 'Series' object has no attribute 'code_without_intercept'
,我猜是因为notUsing和using的绑定。请你帮我看看这个公式吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com(将#修改为@)