[英]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.