Problem with loop using variables with spaces in name

I am making a loop to do logistic regressions by changing both outcome and predictor variables. The loop works fine except when one of the predictor variables has spaces in the name.
I already tried putting the name with inverted quotes (`) but it doesn't work.
I could change the name of this variable but I would prefer not to do it.
Does anyone know how I can make it work?
Here is a reprex

structure(list(a = c("red", "blue", "red", "blue", "red", "blue", 
"blue", "blue", "blue", "blue", "red", "blue", "blue", "red", 
"blue"), b = c("car", "car", "bike", "car", "bike", "car", "car", 
"bike", "car", "bike", "car", "bike", "car", "bike", "car"), 
    var1 = c(0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0), var2 = c("histo", 
    "histo", "histo", "histo", "histo", "histo", "histo", "math", 
    "histo", "histo", "histo", "histo", "histo", "histo", "histo"
    ), `tam r1` = c(FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, 
    FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE), `tam r2` = c(TRUE, 
    TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, TRUE, 
    TRUE, TRUE, TRUE, FALSE, FALSE)), class = c("tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -15L))
head(df)
# A tibble: 6 × 6
  a     b      var1 var2  `tam r1` `tam r2`
  <chr> <chr> <dbl> <chr> <lgl>    <lgl>   
1 red   car       0 histo FALSE    TRUE    
2 blue  car       0 histo TRUE     TRUE    
3 red   bike      1 histo TRUE     TRUE    
4 blue  car       0 histo FALSE    FALSE   
5 red   bike      1 histo TRUE     FALSE   
6 blue  car       1 histo TRUE     TRUE 

Here is the code:


variables_resultado <- c("a", "b")

variables_predictoras_lista <- list(
  "var1",
  "var2",
  "tam r1",
  "tam r2"
)

resultados_modelos <- list()

for (variable_resultado in variables_resultado) {
  for (variables_predictoras in variables_predictoras_lista) {
    tablamxsitam <- df %>%
      select(c(!!sym(variable_resultado),  
               variables_predictoras)) %>%
      mutate_all(as.factor) 
    
    formula_regresion <- formula(paste(variable_resultado, "~", paste(variables_predictoras, collapse = "+")))
    
    modelo <- glm(formula = formula_regresion, data = tablamxsitam, family = binomial(link = "logit"))
    
    resultados_modelos[[paste(variable_resultado, paste(variables_predictoras, collapse = "_"))]] <- broom::tidy(modelo, exponentiate = TRUE, conf.int = TRUE) %>%
      mutate(across("p.value",
                    function(x) {
                      ifelse(x < 0.001, "< 0.001", sprintf("%.3f", x))
                    })) %>%
      mutate(across(where(is.numeric), round, 3)) %>%
      flextable::flextable() %>%
      flextable::theme_box() 
    
  }
}

The code works fine with only "var1" and "var2"

inner for loop :


    tablamxsitam <- df %>%
      select(c(!!sym(variable_resultado),  
               variables_predictoras)) %>%
      mutate_all(as.factor) 
    
    #added this
    vq <- paste0("`",variables_predictoras,"`")
    mytext <- paste(variable_resultado, "~", paste(vq, collapse = "+"))
    print(mytext)

    formula_regresion <- formula(mytext)
     # to here 

    modelo <- glm(formula = formula_regresion, data = tablamxsitam, family = binomial(link = "logit"))
    
    resultados_modelos[[paste(variable_resultado, paste(variables_predictoras, collapse = "_"))]] <- broom::tidy(modelo, exponentiate = TRUE, conf.int = TRUE) %>%
      mutate(across("p.value",
                    function(x) {
                      ifelse(x < 0.001, "< 0.001", sprintf("%.3f", x))
                    })) %>%
      mutate(across(where(is.numeric), round, 3)) %>%
      flextable::flextable() %>%
      flextable::theme_box()

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.