Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Statsmodels Mixed Linear Model predictions

I am estimating a Mixed Linear Model using the statsmodels MixedLM package in Python. After fitting the model, I now want to make predictions but am struggling to understand the 'predict' method.

The statsmodels documentation (http://www.statsmodels.org/dev/generated/statsmodels.regression.mixed_linear_model.MixedLM.predict.html) suggests that the predict method takes an array containing the parameters of the model that has been estimated. How can I retrieve this array?

y = raw_data['dependent_var']
X = raw_data[['var1', 'var2', 'var3']]
groups = raw_data['person_id']

model = sm.MixedLM(endog=y, exog=X, groups=groups)
result = model.fit()
like image 984
Roald Schuring Avatar asked Oct 18 '25 07:10

Roald Schuring


2 Answers

I know I am late by few months but it's good to answer if someone else is having the same question. The params required are available in the result object. They are result.fe_params

model.predict(reresult.fe_params, exog=xest)

or with result object

result.predict(exog=xtest)
like image 173
sukhbinder Avatar answered Oct 19 '25 19:10

sukhbinder


To answer the user11806155's question, to make predictions purely on fixed effects, you can do

model.predict(reresult.fe_params, exog=xtest)

To make predictions on random effects, you can just change the parameters with specifying the particular group name (e.g. "group1")

model.predict(reresult.random_effects["group1"], exog=xtest). 

I assume the order of features in the test data should follow the same order as what you give as the model's parameters. You can add them together to get the prediction specifically for a group.

like image 21
Yingxu He Avatar answered Oct 19 '25 20:10

Yingxu He



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!