Question: Linear Model Loop in R
0
gravatar for selplat21
17 days ago by
selplat2120
selplat2120 wrote:

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
bioinformatics R • 104 views
ADD COMMENTlink modified 17 days ago by rpolicastro2.4k • written 17 days ago by selplat2120

Try:

res <- Data[ paste0(i,".res") ]
pred <- Data[ paste0(i,".pred") ]
ADD REPLYlink written 17 days ago by zx87549.7k

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)
}
ADD REPLYlink written 17 days ago by selplat2120

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?

ADD REPLYlink written 17 days ago by ATpoint42k

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.

ADD REPLYlink modified 17 days ago • written 17 days ago by selplat2120
1
gravatar for rpolicastro
17 days ago by
rpolicastro2.4k
rpolicastro2.4k wrote:

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
ADD COMMENTlink modified 17 days ago • written 17 days ago by rpolicastro2.4k

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

ADD REPLYlink written 16 days ago by selplat2120
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1297 users visited in the last hour