简体   繁体   English

Statsmodels 混合线性 Model 预测

[英]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.我正在使用 Python 中的 statsmodels MixedLM package 估算混合线性 Model。拟合 model 后,我现在想进行预测,但正在努力理解“预测”方法。

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. statsmodels 文档 ( http://www.statsmodels.org/dev/generated/statsmodels.regression.mixed_linear_model.MixedLM.predict.html ) 建议 predict 方法采用包含已估计的 model 参数的数组。 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()

I know I am late by few months but its good to answer if someone lese is having the same question. 我知道我要迟到几个月,但是如果有人遇到同样的问题,可以很好地回答。 The params required are available in the result object. 所需的参数在结果对象中可用。 They are result.fe_params 它们是result.fe_params

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

or with result object 或与结果对象

result.predict(exog=xtest)

To answer the user11806155's question, to make predictions purely on fixed effects, you can do要回答 user11806155 的问题,要纯粹根据固定效果进行预测,您可以这样做

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 (eg "group1")要预测随机效应,您只需更改参数并指定特定的组名(例如“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.您可以将它们加在一起以获得专门针对某个组的预测。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python Statsmodels Mixedlm(混合线性模型)随机效应 - Python Statsmodels Mixedlm (Mixed Linear Model) random effects 如何在Python statsmodels线性混合效果模型中拥有多个组? - How to have multiple groups in Python statsmodels linear mixed effects model? 在 statsmodels 的混合线性模型中指定交叉效应的正确方法是什么? - Which is the correct way to specify crossed effects in a mixed linear model in statsmodels? 当我尝试拟合线性混合效果模型时,为什么statsmodels会抛出IndedxError? - Why is statsmodels throwing an IndedxError when I try to fit a linear mixed-effect model? Python Statsmodels中面板数据的线性混合模型和时间自相关 - Linear Mixed Models and time autocorrelation for Panel Data in Python Statsmodels 使用Seaborn和Statsmodels在一个图中显示数据和模型预测 - Showing data and model predictions in one plot using Seaborn and Statsmodels ARIMA(python statsmodels)的预测 - Predictions with ARIMA (python statsmodels) Plot x 对 y Statsmodels 混合模型结果 - Plot x against y Statsmodels mixed-model result statsmodels线性回归-包括模型中所有预测变量的patsy公式 - statsmodels linear regression - patsy formula to include all predictors in model “statsmodels.regression.linear_model如何? WLS“工作? - How does “statsmodels.regression.linear_model. WLS” work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM