MBl
MBl

Reputation: 35

lm formula variable lengths differ

I would like to calculate an r square coefficient for each ggplot in facetwrap. I thought of a solution using a for loop and save the coeficients in a list and than assign the items of the list to each facet. However, I cannot calculate the coefficients in the for loop. I get an error, that says:

Error in model.frame.default(formula = H60percent ~ choice, data = DATA50, : variable lengths differ (found for 'choice')

Here is my code:

xvalue <- c("Jnr3250","Jnr6450","Jnr12850","Jnr25650")
Rcoef_list <- list()
for (i in 1:length(xvalue)) {
  #print i as a number
  print(i)
  #choose elemnt from from xvalue according to the i (number)
  choice <-  noquote(xvalue[i], right = FALSE)
  print(choice)
  # counts R2
  LM1 =  lm(formula = H60percent ~ choice, data = DATA50)
  Rvalue <-round(as.numeric(summary(LM1)$r.squared[1]),2) 
  R2 <- paste("r^2 == ", Rvalue)
  print(R2)
  #put each R2 in a list
  Rcoef_list[[i]] <- R2
}

if I write the actual name of a column (for example Jnr3250) instead of choice in lm function it works (but only for one value obviously). I tried paste0(choice) and the error is the same. I will be glad for any tips or if someone could just point me in the right direction.

Upvotes: 1

Views: 697

Answers (2)

Chase
Chase

Reputation: 69161

Your question isn't reproducible, but I think you'll have better luck creating a formula() object to pass into lm(). Here's an example with mtcars data, regressing mpg against three of the other variables in the dataset:

xcols <- c("disp", "hp", "qsec")
Rcoef_list <- list()

for (i in 1:length(xcols)){
  f <- as.formula(paste0("mpg ~", xcols[i]))
  LM1 <- lm(formula = f, data = mtcars)
  Rvalue <-round(as.numeric(summary(LM1)$r.squared[1]),2) 
  R2 <- paste("r^2 == ", Rvalue) 
  Rcoef_list[[i]] <- R2  
}
Rcoef_list
#> [[1]]
#> [1] "r^2 ==  0.72"
#> 
#> [[2]]
#> [1] "r^2 ==  0.6"
#> 
#> [[3]]
#> [1] "r^2 ==  0.18"

Created on 2019-01-27 by the reprex package (v0.2.1)

Upvotes: 0

Ben Bolker
Ben Bolker

Reputation: 226087

Are your variables separate predictor values, or values of a grouping variable? The answer below assumes the former; if it's the latter, use lme4::lmList() or nlme::lmList() ...

This is pretty close to a duplicate ... I haven't dissected your example to see exactly where you got in trouble, but it's usually best to do these kinds of problems with reformulate(). Stripped down, your list would look like this:

xvalue <- c("Jnr3250","Jnr6450","Jnr12850","Jnr25650")
Rcoef_list <- list()
for (x in xvalue) {
  form <- reformulate(x, response="H60percent")
  LM1 <- lm(form, data=DATA50)
  Rcoef_list[[x]] <- summary(LM1)$r.squared[1]
}

If you want the r^2 == stuff, you can use sprintf("r^2 == %1.1f", unlist(Rcoef_list)) after running the loop.

Upvotes: 2

Related Questions