Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Iterating and looping over multiple columns in glm in r using a name from another variable

I am trying to iterate over multiple columns for a glm function in R.

view(mtcars)
names <- names(mtcars[-c(1,2)])

for(i in 1:length(names)){
  
  print(paste0("Starting iterations for ",names[i]))
  
  
  model <-  glm(mpg ~ cyl + paste0(names[i]), data=mtcars, family = gaussian())
  summary(model)
  
  print(paste0("Iterations for ",names[i], " finished"))
}

however, I am getting the following error:

[1] "Starting iterations for disp"
Error in model.frame.default(formula = mpg ~ cyl + paste0(names[i]), data = mtcars,  : 
  variable lengths differ (found for 'paste0(names[i])')

Not sure, how I can correct this.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

mpg ~ cyl + paste0(names[i]) or even mpg ~ cyl + names[i] is not a valid syntax for a formula. Use

reformulate(c("cyl", names[i]), "mpg")

instead, which dynamically creates a formula from variable names.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading