Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MixedLM Predict issue #8357

Open
prashanthin opened this issue Aug 2, 2022 · 2 comments
Open

MixedLM Predict issue #8357

prashanthin opened this issue Aug 2, 2022 · 2 comments

Comments

@prashanthin
Copy link

Hi,

I am doing some research on Mixed effects models and using Stats Mixedlm for same. I have Units to be predicted from Price and size at different UPC levels.

So below is the code I used -

md = smf.mixedlm("Units ~ Avg_Units_Price + C_CUSTOM_BASE_SIZE", file3, groups=file3["UPC_10_digi"], re_formula="~Avg_Units_Price + C_CUSTOM_BASE_SIZE")
mdf = md.fit()
print(mdf.summary())

Output

image

mdf.random_effects

image

I am getting moderate avp fit on predicted values however I have below needs -

  1. I want to predict Units using both fixed and random effects as in below case -

image

  • so I want to predict units at size 20 and Price 11.1. I know for predicting these values both fixed effects and random effects would be used, however I am not able to use predict function as I couldnt get clear example/idea for this.
    I have used below code for prediction but its showing multiple arguments error -

image

Can you please help me in this single case prediction using Mixedlm?

Thanks a lot in advance.

@kshedden
Copy link
Contributor

kshedden commented Aug 3, 2022

There is no convenience method for this. You would need to take the BLUPs (from the random_effects attribute as above) and add them to the fitted values from the fixed effects (which is what you get from 'predict'). For example, for all observations in group 7023001154 you would take the fixed effects prediction and add to it -327.6 + 66*Avg_Units_Price - 72*C_CUSTOM_BASE_SIZE.

@snowoody
Copy link

One way to get the random effect is through the predict function in Linear Mixed Effects Model md.

md.predict(mdf.random_effects[group_a1_df_2[0]], exog=a_df)
would get you the random effect prediction.

model.predict(mdf.fe_params, exog=a_df)
would get you the fixed effect prediction.

You probably still need to add them together after the calculation. Got this from https://stackoverflow.com/questions/50300224/statsmodels-mixed-linear-model-predictions. It works for me. But I have to do some preprocessing for exog as it does not take my pandas dataframe directly for categorical values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants