1
$\begingroup$

I've done multiple regression modeling in R using the below script. Now I'm wondering how can I make a table for the coefficients of each model of all models from lm, glm, and rf separately.


all_variables <- names(PaC09c_w20_dat3)
response_variables <- all_variables[c(1:3)]
predictors <- all_variables[-c(1:3)]

lm_model <- lapply(
  response_variables,
  function(x) lm(reformulate(termlabels = predictors, response = x), data = PaC09c_w20_dat3)
) |>
  setNames(response_variables)

glm_model <- lapply(
  response_variables,
  function(x) glm(reformulate(termlabels = predictors, response = x), data = PaC09c_w20_dat3)
) |>
  setNames(response_variables)

rf_ranger <- lapply(
  response_variables,
  function(x) ranger(reformulate(termlabels = predictors, response = x), data = PaC09c_w20_dat3, importance="impurity")
) |>
  setNames(response_variables)

save.image(file="PaC09c_w20_Reg.RData")

Input data looks like (Original Table is very large),

  head(PaC09c_w20_dat3)
        EE87865ln1   EE87866ln1   EE87895ln1  blood_vessel_w20 adrenal_gland_w20 bone_element_w20 bronchus_w20 
               <dbl>       <dbl>      <dbl>     <dbl>             <dbl>            <dbl>        <dbl>         
        1    0.00391     0.00326    0.00332       0                 0                1            0             
        2    0.00139     0.00116    0.00132       0                 0                0            0             
        3    0.00360     0.00270    0.00469       1                 1                0            1             
        4    0.00323     0.00348    0.00339       0                 0                1            0             
        5    0.00323     0.00330    0.00382       0                 1                0            0             
        6    0.00278     0.00208    0.00214       0                 0                1            0   

The expected output would look like this,

                           EE87865_lmcof   EE87866_lmcof
blood_vessel_w20           6.332633e-06    5.916615e-06
adrenal_gland_w20         -2.159470e-06   -2.399176e-06
bone_element_w20           8.578804e-06    8.106153e-06
bronchus_w20              -2.956291e-05   -2.152231e-05       
$\endgroup$
1
  • 1
    $\begingroup$ Your code is neither reproducible (one would need the bed file) nor small enough to mentally compute what could be in each variable. How do you expect people on the forum to help you get to a random goal you have? $\endgroup$
    – Ram RS
    Commented Aug 9, 2023 at 16:02

2 Answers 2

1
$\begingroup$

To get the coefficients alone

lm_model$coefficients
glm_model$coefficients

Everything including coefficients its,

summary(lm_model)
summary(glm_model)

I assume this is the same for summary(rf_ranger), albeit not sure whether coefficients would work here

$\endgroup$
2
  • $\begingroup$ lm_model$coefficients and glm_model$coefficients produces NULL. As you can see every model object (lm_model, glm_model, and rf_ranger) consists of multiple regression models of samples (e.g., EE...). $\endgroup$
    – Deb
    Commented Aug 10, 2023 at 2:10
  • 1
    $\begingroup$ Given how OP has made these things lists, we'll probably need an lapply there. $\endgroup$
    – Ram RS
    Commented Aug 10, 2023 at 22:04
1
$\begingroup$

Well, I found a solution,

 coefs_lm<-as.data.frame(lapply(lm_model,function(x)coef(x)[2:5]))

 coefs_glm<-as.data.frame(lapply(glm_model,function(x)coef(x)[2:5]))

var_imp <- function(object, ...) {
  var_imp_r <- object$variable.importance
  var_imp_r
}

coefs_rf<-as.data.frame(lapply(rf_ranger,function(x)var_imp(x)[1:4]))

2:5 for lm and glm is the number of samples EE.., where 1 is intercept.

$\endgroup$
2
  • 1
    $\begingroup$ What is the 2:28 part? Is it specific to your dataset(s)? $\endgroup$
    – Ram RS
    Commented Aug 10, 2023 at 22:03
  • $\begingroup$ Sorry, it should be 2:5 for this toy dataset in this example. $\endgroup$
    – Deb
    Commented Aug 11, 2023 at 0:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.