连接statsmodels.OLS的DataFrame列

时间:2015-03-22 11:23:55

标签: numpy pandas statsmodels

如果我想根据Y和X2的对数构建模型,我会这样做:

import statsmodels.formula.api as smf
import numpy as np
import pandas as pd

d = {'Y': [1,2,3,4], 'X1': [5,6,7,8], 'X2': [9,10,11,12]}
df = pd.DataFrame(d)
model = smf.ols(formula='np.log(Y) ~ X1 + np.log(X2)', data=df).fit()

如何对statsmodels.api执行相同的操作?我知道我可以连接df,但肯定有一个更简单的方法。

import statsmodels.api as sm
import numpy as np
import pandas as pd

d = {'Y': [1,2,3,4], 'X1': [5,6,7,8], 'X2': [9,10,11,12]}
df = pd.DataFrame(d)
y = np.log(df['Y'])
x = pd.DataFrame()
x['X1'] = d['X1']
x['logX2'] = np.log(d['X2'])
#x = df[['X1', np.log('X2')]] # I'd like to type sth like this
x = sm.add_constant(x)
model = sm.OLS(y, x).fit()
model.summary()

x = df...(注释行)我得到了:

TypeError: Not implemented for this type

1 个答案:

答案 0 :(得分:2)

您可以使用x构建pd.DataFrame

x = pd.DataFrame({'X1': df['X1'], 'log(X2)': np.log(df['X2'])})

而不是

x = pd.DataFrame()
x['X1'] = d['X1']
x['logX2'] = np.log(d['X2'])

import numpy as np
import pandas as pd
import statsmodels.api as sm

d = {'Y': [1,2,3,4], 'X1': [5,6,7,8], 'X2': [9,10,11,12]}
df = pd.DataFrame(d)
y = np.log(df['Y'])
x = pd.DataFrame({'X1': df['X1'], 'log(X2)': np.log(df['X2'])})
x = sm.add_constant(x)
model = sm.OLS(y, x).fit()
print(model.summary())