Noahp555
Noahp555

Reputation: 25

How do I export coefficients from a lm() object containing multiple lm()?

I have an object (S3; lm) that contains the linear regression outputs of 471 different models. I am trying to extract the standard error of a specific variable in each model but I'm unsure how to do so, can anyone help? Specifically, I want to extract the standard error for the variable "p" for EACH of the 471 models saved in the "fit" object.

varnames = names(merged1)[2036:2507]
          fit <- lapply(varnames, 
          FUN=function(p) lm(formula(paste("Dx ~ x + y + z + q +", p)),data=merged1))
names(fit) <- varnames

Thank you so much!

Note Edited to reflect the anonymous function p, rather than x, as stated previously.

Upvotes: 1

Views: 212

Answers (2)

G. Grothendieck
G. Grothendieck

Reputation: 269396

Using fit shown reproducibly in the Note at the end invoke map_dfr on that with tidy which will give a data frame containing coefficients and associated statistics. We filter out the rows we want.

library(broom) # tidy
library(dplyr)
library(purrr) # map_dfr

fit %>%
  map_dfr(tidy, .id = "variable") %>%
  filter(term == variable)

giving:

# A tibble: 8 x 6
  variable term  estimate std.error statistic p.value
  <chr>    <chr>    <dbl>     <dbl>     <dbl>   <dbl>
1 hp       hp     -0.0147    0.0147    -1.00  0.325  
2 drat     drat    1.21      1.50       0.812 0.424  
3 wt       wt     -3.64      1.04      -3.50  0.00160
4 qsec     qsec   -0.243     0.402     -0.604 0.551  
5 vs       vs     -0.634     1.90      -0.334 0.741  
6 am       am      1.93      1.34       1.44  0.161  
7 gear     gear    0.158     0.910      0.174 0.863  
8 carb     carb   -0.737     0.393     -1.88  0.0711 

Note

We compute fit reproducibly using mtcars which is built into R.

data <- mtcars
resp <- "mpg" # response
fixed <- c("cyl", "disp")  # always include these
varnames <- setdiff(names(data), c(resp, fixed)) # incl one at a time

fit <- Map(function(v) {
  fo <- reformulate(c(fixed, v), resp)
  lm(fo, data)
}, varnames)

Updated

Significantly revised.

Upvotes: 1

Samet S&#246;kel
Samet S&#246;kel

Reputation: 2660

sapply(fit,function(x) summary(x)$coefficients[p,][2],simplify = F)

subsetting to 2nd element serves standard error for a variable.

Upvotes: 1

Related Questions