Home > Net >  Extract lm coefficient and significance in a for loop
Extract lm coefficient and significance in a for loop

Time:11-20

Let's say I have a model like this.

set.seed(416)
N = 100
X  = data.frame(
  x1 <- rnorm(N,0,1),
  x2 <- rnorm(N,0,1)
)
X$y <- 2*X$x1   3*X$x2   rnorm(N)

model <- lm(y ~ x1   x2, data=X)

I want to create a named list of the Estimate, P-Value, and Sig (whether p < 0.05) for each variable. Here's how I do it now. But is there a way to do this in a for loop so I can easily add more variables to model?

# Inefficient
model_data <- (c(
         est_x1= coef(summary(model))['x1', 'Estimate'], 
         p_x1=coef(summary(model))['x1', 'Pr(>|t|)'], 
         sig_x1=coef(summary(model))['x1', 'Pr(>|t|)'] < 0.05, 
         est_x2= coef(summary(model))['x2', 'Estimate'], 
         p_x2= coef(summary(model))['x2', 'Pr(>|t|)'], 
         sig_x2=coef(summary(model))['x2', 'Pr(>|t|)'] < 0.05
         ))


est_x1         p_x1       sig_x1       est_x2         p_x2       sig_x2 
1.947646e 00 1.204075e-34 1.000000e 00 3.008251e 00 1.158695e-51 1.000000e 00

CodePudding user response:

The broom package can take the model and put the results into a data.frame

model <- lm(y ~ x1   x2, data=X)

library(broom)
summary_table <- tidy(model)
summary_table

# A tibble: 3 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)    0.106    0.101       1.05 2.94e- 1
2 x1             1.95     0.102      19.1  1.20e-34
3 x2             3.01     0.0982     30.6  1.16e-51

To create the named vector:

answer <- c(summary_table$estimate, summary_table$std.error, summary_table$p.value)

names <- expand.grid(summary_table$term, c("estimate", "std.error", "p.value"), stringsAsFactors = FALSE) 

names(answer) <- paste(names$Var2, names$Var1)
answer

 estimate (Intercept)           estimate x1           estimate x2 std.error (Intercept)          std.error x1          std.error x2   p.value (Intercept) 
         1.059576e-01          1.947646e 00          3.008251e 00          1.005028e-01          1.019442e-01          9.822749e-02          2.943759e-01 
           p.value x1            p.value x2 
         1.204075e-34          1.158695e-51 
  • Related