Search code examples
rloopsmodellm

How to print the name of the indipendent variable into the output of a for loop


This is a chunk of the dataset,

structure(list(gender = structure(c(2L, 1L, 1L, 1L, 1L), .Label = c("1", 
"2"), class = "factor"), Work_less = c(0, 1, 0, 0, NA), happy = c(7, 
8, 7, 6, 7), lifestatisfied = c(8, 8, 9, 9, 7), country = c(8, 
8, 9, 9, 7)), row.names = c(NA, -5L), class = "data.frame")

whose I'm trying to fit different models as follows:

model <- list()
for (i in 2:ncol(dat)) {
  model[[i]] <- lm(lifestatisfied ~  dat[,i], dat)
}

In the output, you might see that the independent variable (dat[, i]) but not the correspondent name of the variable:

[[2]]

Call:
lm(formula = lifestatisfied ~ dat[, i], data = dat)

Coefficients:
(Intercept)     dat[, i]  
     8.6667      -0.6667  


[[3]]

Call:
lm(formula = lifestatisfied ~ dat[, i], data = dat)

Coefficients:
(Intercept)     dat[, i]  
       11.7         -0.5  


[[4]]

Call:
lm(formula = lifestatisfied ~ dat[, i], data = dat)

Coefficients:
(Intercept)     dat[, i]  
  1.589e-15    1.000e+00  


[[5]]

Call:
lm(formula = lifestatisfied ~ dat[, i], data = dat)

Coefficients:
(Intercept)     dat[, i]  
  1.589e-15    1.000e+00 

Could you please suggest a way to print into the output the name of independent to which the index of the model refers to?


Solution

  • We can use:

    xvar <- setdiff(names(dat), "lifestatisfied")
    model <- vector("list", length(xvar))
    for (i in 1:length(xvar)) {
      form <- reformulate(xvar[i], "lifestatisfied")
      model[[i]] <- do.call("lm", list(formula = form, data = quote(dat)))
    }
    

    And the output model is:

    #[[1]]
    #
    #Call:
    #lm(formula = lifestatisfied ~ gender, data = dat)
    #
    #Coefficients:
    #(Intercept)      gender2  
    #       8.25        -0.25  
    #
    #
    #[[2]]
    #
    #Call:
    #lm(formula = lifestatisfied ~ Work_less, data = dat)
    #
    #Coefficients:
    #(Intercept)    Work_less  
    #     8.6667      -0.6667  
    #
    #
    #[[3]]
    #
    #Call:
    #lm(formula = lifestatisfied ~ happy, data = dat)
    #
    #Coefficients:
    #(Intercept)        happy  
    #       11.7         -0.5  
    #
    #
    #[[4]]
    #
    #Call:
    #lm(formula = lifestatisfied ~ country, data = dat)
    #
    #Coefficients:
    #(Intercept)      country  
    #  1.589e-15    1.000e+00  
    

    Remarks

    1. reformulate is useful in a loop where we dynamically create model formulae.

    2. The do.call part is also important. You can try

      model[[i]] <- lm(form, data = dat)
      

      instead to see the difference.

    3. The other answer using broom::tidy only produces a summary of estimated coefficients. It does not return a list of fitted models which facilitate for example, prediction.