4

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()
Roald Schuring
  • 179
  • 1
  • 3
  • 13

2 Answers2

7

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)
DJV
  • 4,743
  • 3
  • 19
  • 34
sukhbinder
  • 1,001
  • 10
  • 9
  • if you can see this, do you know if the approach make the prediction based on the fixed effect or based on both the fixed and the random effect so that the prediction is specific to each group? If it takes both fixed and random effects into account, how does it know which individual in which group it is predicting? – user11806155 Jan 16 '21 at 03:13
  • The documentation of the predict function states as follows: `An array of fitted values. Note that these predicted values only reflect the fixed effects mean structure of the model.` – Berkan Erol Mar 14 '22 at 15:57
1

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.

Yingxu He
  • 130
  • 1
  • 6