Answers.Body: <p>Well, I found a solution,</p>
  <pre><code> coefs_lm&lt;-as.data.frame(lapply(lm_model,function(x)coef(x)[2:5]))
  
   coefs_glm&lt;-as.data.frame(lapply(glm_model,function(x)coef(x)[2:5]))
  
  var_imp &lt;- function(object, ...) {
    var_imp_r &lt;- object$variable.importance
    var_imp_r
  }
  
  coefs_rf&lt;-as.data.frame(lapply(rf_ranger,function(x)var_imp(x)[1:4]))
  </code></pre>
  <p><code>2:5</code> for <code>lm</code> and <code>glm</code> is the number of samples <code>EE..</code>, where 1 is intercept.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-08-10T22:03:04.870Z
Answers.Text: What is the `2:28` part? Is it specific to your dataset(s)?
Answers.UserDisplayName: Ram RS
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-08-11T00:47:26.287Z
Answers.Text: Sorry, it should be `2:5` for this toy dataset in this example.
Answers.UserDisplayName: Deb
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-08-10T04:32:36.290Z
Answers.LastActivityDate: 2023-08-11T00:46:39.117Z
Answers.LastEditDate: 2023-08-11T00:46:39.117Z
Answers.UserDisplayName: Deb
Answers.Body: <p>To get the coefficients alone</p>
  <pre><code>lm_model<span class="math-container">$coefficients
  glm_model$</span>coefficients
  </code></pre>
  <p>Everything including coefficients its,</p>
  <pre><code>summary(lm_model)
  summary(glm_model)
  </code></pre>
  <p>I assume this is the same for <code>summary(rf_ranger)</code>, albeit not sure whether <code>coefficients</code> would work here</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-08-10T02:10:11.000Z
Answers.Text: `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...`).
Answers.UserDisplayName: Deb
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-08-10T22:04:52.377Z
Answers.Text: Given how OP has made these things lists, we'll probably need an `lapply` there.
Answers.UserDisplayName: Ram RS
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-08-09T20:37:38.723Z
Answers.LastActivityDate: 2023-08-09T20:37:38.723Z
Answers.UserDisplayName: M__
Body: <p>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.</p>
  <pre class="lang-r prettyprint-override"><code>
  all_variables &lt;- names(PaC09c_w20_dat3)
  response_variables &lt;- all_variables[c(1:3)]
  predictors &lt;- all_variables[-c(1:3)]
  
  lm_model &lt;- lapply(
    response_variables,
    function(x) lm(reformulate(termlabels = predictors, response = x), data = PaC09c_w20_dat3)
  ) |&gt;
    setNames(response_variables)
  
  glm_model &lt;- lapply(
    response_variables,
    function(x) glm(reformulate(termlabels = predictors, response = x), data = PaC09c_w20_dat3)
  ) |&gt;
    setNames(response_variables)
  
  rf_ranger &lt;- lapply(
    response_variables,
    function(x) ranger(reformulate(termlabels = predictors, response = x), data = PaC09c_w20_dat3, importance=&quot;impurity&quot;)
  ) |&gt;
    setNames(response_variables)
  
  save.image(file=&quot;PaC09c_w20_Reg.RData&quot;)
  </code></pre>
  <p>Input data looks like (Original Table is very large),</p>
  <pre><code>  head(PaC09c_w20_dat3)
          EE87865ln1   EE87866ln1   EE87895ln1  blood_vessel_w20 adrenal_gland_w20 bone_element_w20 bronchus_w20 
                 &lt;dbl&gt;       &lt;dbl&gt;      &lt;dbl&gt;     &lt;dbl&gt;             &lt;dbl&gt;            &lt;dbl&gt;        &lt;dbl&gt;         
          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   
  </code></pre>
  <p>The expected output would look like this,</p>
  <pre><code>                           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       
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-08-09T16:02:16.753Z
Comments.Text: 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?
Comments.UserDisplayName: Ram RS
Tags: <r><linear-regression>
Title: How to get coefficient tables from multiple regression model result!
ThreadText: # How to get coefficient tables from multiple regression model result!
  
  (Asked by: username_0 on 2023-08-09)
  
  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.
  
  ```r
  
  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       
  
  ```
  
  **Tags:** r, linear-regression
  
  > **Comments:**
  > **username_1**: 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?  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2023-08-10. Score: 1 | Accepted answer
  
  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.
  
  > **Comments:**
  > **username_1**: What is the `2:28` part? Is it specific to your dataset(s)?  
  > **username_0**: Sorry, it should be `2:5` for this toy dataset in this example.  
  
  ---
  
  ### Answer by username_2 on 2023-08-09. Score: 1
  
  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
  
  > **Comments:**
  > **username_0**: `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...`).  
  > **username_1**: Given how OP has made these things lists, we'll probably need an `lapply` there.  
  
  ---
  
  