[英]How to find confidence interval from the simulation of linear model
I have used the arm package, sim() function, to create a simulation of linear model.我使用了 arm 包 sim() 函数来创建线性模型的模拟。 The simulation result contains the coefficients and residual error.
仿真结果包含系数和残差。 I would like to find the 95% confidence interval for the coefficients.
我想找到系数的 95% 置信区间。 How do I find it?
我如何找到它?
The codes I have used to run the simulation are provided below.下面提供了我用来运行模拟的代码。
mod <- lm(y ~ normal_dist + uniform_dist)
sims <- arm::sim(mod, n = 1000)
You can retrieve approximate CIs by computing quantiles of the coefficients simulated by arm::sim()
:您可以通过计算
arm::sim()
模拟的系数的分位数来检索近似 CI:
library(arm)
t(apply(sims@coef, 2, quantile, c(0.025, 0.975)))
2.5% 97.5%
(Intercept) 33.394308 42.024628
cyl -3.485516 -2.169014
( @coef
retrieves the coefficients; apply(., 2, quantile, c(0.025, 0.975))
computes the quantiles for each column; t()
transposes the result) (
@coef
检索系数; apply(., 2, quantile, c(0.025, 0.975))
计算每列的分位数; t()
转置结果)
However, this is very inefficient and a bit backwards as the simulations are derived by drawing multivariate normal simulations based on the sampling covariance matrix of the coefficients (as such, these limits also won't include the finite-size correction that uses the t distribution instead of the Normal).然而,这是非常低效的并且有点倒退,因为模拟是通过基于系数的采样协方差矩阵绘制多元正态模拟得出的(因此,这些限制也不包括使用 t 分布的有限大小校正而不是正常)。 You can get the confidence intervals much more easily:
您可以更轻松地获得置信区间:
> confint(mod)
2.5 % 97.5 %
(Intercept) 33.649223 42.119930
cyl -3.534237 -2.217343
>
Is this what you're looking for?这是你要找的吗? 95% CI for coefficients with data iris, replace petal.length with your coefficients of choice
带有数据虹膜的系数的 95% CI,用您选择的系数替换 petal.length
fit <- lm(Petal.Width ~Petal.Length,iris)
confint(fit,'Petal.Length',level=0.95)
Output输出
2.5 % 97.5 %
Petal.Length 0.3968193 0.4346915
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.