alkamid
alkamid

Reputation: 7750

Concatenating DataFrame columns for statsmodels.OLS

If I want to build a model based on the logarithm of my Y and X2, I would do:

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()

How to do the same with statsmodels.api? I know I could concatenate the df but there surely is a simpler method.

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()

at x = df... (the commented line) I get:

TypeError: Not implemented for this type

Upvotes: 2

Views: 909

Answers (1)

unutbu
unutbu

Reputation: 880667

You could build x using pd.DataFrame:

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

instead of

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())

Upvotes: 2

Related Questions