0

I was looking at this post on extracting coefficients from a regression loop. I was wondering how I would extract the coefficient and standard error? I thought it would be something like the following, but that appears to not be it:

data <- mtcars[, c("mpg", "cyl", "disp", "hp", "drat", "wt")]
col10 <- names(data)[-1]

lm.test <- vector("list", length(col10))

for(i in seq_along(col10)){
  lm.test[[i]] <- lm(reformulate(col10[i], "mpg"), data = data)
}

lm.test

cfs <- lapply(lm.test, coef[1:2])
hy9fesh
  • 589
  • 2
  • 15
  • Isn't `cfs <- lapply(lm.test, coef)` already gives what you want? Why put additional `[1:2]` there? BTW what you want can be achieved by using an anonymous function `lapply(lm.test, function(x) coef(x)[1:2])` – Ronak Shah Jan 27 '21 at 08:36

2 Answers2

0

Ah, you got the famous "object of type closure is not subsettable" error.

We can use some [[ magic to help us here:

cfs <- lapply(lm.test, `[[`, "coefficients")

The double bracket is a function that extracts a variable name - this effectively loops over the following code:

cf_i <- lm.test[[i]][["coefficients"]]

where i is the number of models in lm.test

EDIT:

More simply,

cfs <- lapply(lm.test, coef)
Dubukay
  • 1,764
  • 1
  • 8
  • 13
  • I'm getting the following for cf_i: ```(Intercept) wt 37.285126 -5.344472``` and not the SE. – hy9fesh Jan 27 '21 at 00:03
  • The SE is a separate calculation and not a default part of `lm()`'s output, so it's not possible to extract it directly from the object you've created afaik – Dubukay Jan 27 '21 at 00:16
0

There is probably a more elegant way for me to extract the coefficient and SE, but this is what I did:

data <- mtcars[, c("mpg", "cyl", "disp", "hp", "drat", "wt")]
col10 <- names(data)[-1]

lm.test <- vector("list", length(col10))

for(i in seq_along(col10)){
  lm.test[[i]] <- lm(reformulate(col10[i], "mpg"), data = data)
}

lm.test_coef <- lapply(lm.test, summary)

# extract the coefficient (X2)
lm.test_coef_df <- data.frame(matrix(unlist(lm.test_coef), ncol = max(lengths(lm.test_coef)), byrow = TRUE))

# turn list into dataframe
lm.test_coef_df <- as.data.frame(t(simplify2array(lm.test_coef)))

# extract coefficients column
lm.test_coef_df_i <- dplyr::select(lm.test_coef_df, terms, coefficients)

# flatten list
lm.test_coef_df_i <- apply(lm.test_coef_df_i,2,as.character)
View(lm.test_coef_df_i)
lm.test_coef_df_i <- as.data.frame(lm.test_coef_df_i)

# remove strings
lm.test_coef_df_i$coefficients <- stringr::str_replace(lm.test_coef_df_i$coefficients, '\\c', '')
lm.test_coef_df_i$coefficients <- stringr::str_replace(lm.test_coef_df_i$coefficients, '\\(', '')
lm.test_coef_df_i$coefficients <- stringr::str_replace(lm.test_coef_df_i$coefficients, '\\)', '')


lm.test_coef_df_i <- cSplit(lm.test_coef_df_i, "coefficients", sep=",")

lm.coef_sd <- dplyr::select(lm.test_coef_df_i, coefficients_2, coefficients_4)
View(lm.coef_sd)
hy9fesh
  • 589
  • 2
  • 15