Home > Enterprise >  How to find confidence interval from the simulation of linear model
How to find confidence interval from the simulation of linear model

Time:10-15

I have used the arm package, sim() function, to create a simulation of linear model. The simulation result contains the coefficients and residual error. I would like to find the 95% confidence interval for the coefficients. 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)

CodePudding user response:

You can retrieve approximate CIs by computing quantiles of the coefficients simulated by arm::sim():

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)

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). 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
> 

CodePudding user response:

Is this what you're looking for? 95% CI for coefficients with data iris, replace petal.length with your coefficients of choice

  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
  • Related