This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Linear Model Loop in R

Hello,

I have a list of linear model outputs in R:

Models <- c('FGm', 'ECm', 'BWm', 'Mm', 'BDm', 'Cm', 'WCm', 'FCm',
                'DPC1m','DPC2m','DPC3m','DPC4m','DPC5m','DPC6m','DPC7m',
                'DPC8m','DPC9m','DPC10m','SPC1m','SPC2m','SPC3m','SPC4m',
                'SPC5m','SPC6m','SPC7m','SPC8m','SPC9m','SPC10m', 'Ipom', 
                'Mertm', 'Seasm')

I am trying to generate a column with residual and predicted values in the dataframe:

for (i in Models){
  res <- paste('Data$', i,".res", sep="")
  pred <- paste('Data$', i,".pred", sep="")
  res <- residuals(i)
  pred <- predict(i)
}

I keep getting the following error, can someone help with this?

Error: $ operator is invalid for atomic vectors
r

Try:

res <- Data[ paste0(i,".res") ]
pred <- Data[ paste0(i,".pred") ]

That is helpful, thank you! The problem here is that the following concatenated list is in character format, but I want each item in this list to be treated as an lm type object. So, not sure how to just get these read in as lm objects.

Models <- c('FGm', 'ECm', 'BWm', 'Mm', 'BDm', 'Cm', 'WCm', 'FCm',
                'DPC1m','DPC2m','DPC3m','DPC4m','DPC5m','DPC6m','DPC7m',
                'DPC8m','DPC9m','DPC10m','SPC1m','SPC2m','SPC3m','SPC4m',
                'SPC5m','SPC6m','SPC7m','SPC8m','SPC9m','SPC10m', 'Ipom', 
                'Mertm', 'Seasm')

for (i in Models){
  Data[ paste0(i,".res") ] <- residuals(i)
}

Beyond that, are you aware that the first res and pred are being overwritten in the 3th and 4th line of the for loop body without seemingly being used before?

So here was my rationale here:

Models <- list(FGm, ECm, BWm, Mm, BDm, Cm, WCm, FCm, 
                DPC1m,DPC2m,DPC3m,DPC4m,DPC5m,DPC6m,DPC7m,
                DPC8m,DPC9m,DPC10m,SPC1m,SPC2m,SPC3m,SPC4m,
                SPC5m,SPC6m,SPC7m,SPC8m,SPC9m,SPC10m, Ipom, 
                Mertm, Seasm) #list of lm type objects

Sorry, I realized the overwrite. The following should illustrate what I want to do:

for (i in Models){
  paste0('Data$', names(Models)[i],".res", sep="")  <- residuals(i) 
}

The problem here is that I'm unable to access the names of the list items from above. This term "names(Models)[i]" won't work unfortunately.

1 answer

Suppose you have a list of lm models.

Models <- list(lm(mpg~cyl, mtcars), lm(hp~drat, mtcars))

When you have a list, you generally use lapply to iterate over it and apply a function.

Data <- lapply(Models, function(x) data.frame(res=residuals(x), pred=predict(x)))

I wasn't completely sure from your description what you wanted as an output, but I output a list of data.frames containing the residuals and predicted values for each model.

> lapply(Data, head)
[[1]]
                         res     pred
Mazda RX4          0.3701643 20.62984
Mazda RX4 Wag      0.3701643 20.62984
Datsun 710        -3.5814159 26.38142
Hornet 4 Drive     0.7701643 20.62984
Hornet Sportabout  3.8217446 14.87826
Valiant           -2.5298357 20.62984

[[2]]
                         res     pred
Mazda RX4         -19.226118 129.2261
Mazda RX4 Wag     -19.226118 129.2261
Datsun 710        -39.103380 132.1034
Hornet 4 Drive    -66.413209 176.4132
Hornet Sportabout   2.614957 172.3850
Valiant           -89.827684 194.8277

You are an amazing human, thank you so much. This is perfect.

Log in to answer this question.