获得混合LM预测的置信区间

问题描述 投票:0回答:1
ggeffects

ggpredict
的帮助下,您可以在使用
plot_model()
函数拟合线性混合模型后轻松地生成一系列预测及其置信区间。以下是结果之一:

我正在尝试产生与python相似的结果。 Python

statsmodels

请勿返回预测的标准错误,因此我尝试从我在线找到的一个公式中进行对其进行计算,但结果完全不同。 到目前为止,我有:enter image description here

我确定我遵循了公式错误,因为该公式是用于单个变量回归的公式,而我的模型包含许多因变量,并且我在Python软件包中找到一个函数而失去了希望,这将使我互相间隔。
我应该从哪里开始?我想坚持使用StatsModels,但将不胜感激。

enter image description here我相信您不能直接做到这一点,但是您可以估计效果的置信区间如下。 由于您没有指定它,也没有为您的R示例共享任何代码,因此我假设您需要置信区间(不是预测间隔)。

import numpy as np import pandas as pd import statsmodels.formula.api as smf from patsy import dmatrix from plotnine import * # random data np.random.seed(42) n = 200 df = pd.DataFrame({ "X": np.random.uniform(60, 90, n), "Z": np.random.choice(["Z1", "Z2"], n), "group": np.random.choice(range(10), n), # 10 groups }) df["y"] = 0.5 * df["X"] + 1 * (df["Z"] == "Z2") + np.random.normal(0, 1, n) model = smf.mixedlm("y ~ X * C(Z)", df, groups=df["group"]) result = model.fit() plot_df = pd.DataFrame({ "X": np.linspace(60, 90, num=20), "Z": ["Z1", "Z2"] * 10 # Repeat categories for each value }) # transform into design matrix using patsy and compute confidence intervals design_matrix = dmatrix("X * C(Z)", plot_df) confidence_interval = result.t_test(design_matrix).conf_int() confidence_interval = pd.DataFrame(confidence_interval, columns=["low", "high"], index=plot_df.index) confidence_interval["mean"] = result.t_test(design_matrix).effect plot_df = pd.concat([plot_df, confidence_interval], axis=1) plot = ( ggplot(plot_df, aes(x="X", y="mean", color="Z")) + geom_line() + # Mean prediction line geom_ribbon(aes(ymin="low", ymax="high", fill="Z"), alpha=0.2) + theme_minimal() + theme(rect=element_rect(fill="white")) + labs(title="MixedLM Predictions with Confidence Intervals", x="X", y="Predicted Y", color="Z", fill="Z") + geom_point(data=df, mapping=aes(x="X", y="y"), alpha=0.5) ) plot

预期结果:

python r statistics statsmodels mixed-models
1个回答
0
投票

	

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.