Ross,
Here's one way to condense the code ...
DV <- c("mpg", "drat", "gear")
IV <- list(c("cyl", "disp", "hp"),
c("wt", "qsec"), c("carb", "hp"))
for(i in seq(DV)) {
fit <- lm(formula=paste(DV[i], paste(IV[[i]],
collapse="+"),
sep="~"), data=mtcars)
plot(fit$fitted, fit$resid, main=paste("DV", DV[i],
sep="="))
lapply(fit$model[, -1], function(x) plot(x, fit$resid))
}
Jean
Ross Ahmed <rossahmed@googlemail.com> wrote on 11/04/2012 09:57:34
AM:>
> I have applied the same linear model to several different subsets of a
> dataset. I recently read that in R, code should never be repeated. I
feel my> code as it currently stands has a lot of repetition, which could be
> condensed into fewer lines. I will use the mtcars dataset to replicate
what> I have done. My question is: how can I use fewer lines of code (for
example> using a for loop, a function or plyr) to achieve the same output as
below?>
> data(mtcars)
>
> # Apply the same model to the dataset but choosing different
combinations of> dependent (DV) and independent (IV) variables in each case:
> lm.mpg= lm(mpg~cyl+disp+hp, data=mtcars)
> lm.drat = lm(drat~wt+qsec, data=mtcars)
> lm.gear = lm(gear~carb+hp, data=mtcars)
>
> # Plot residuals against fitted values for each model
> plot(lm.mpg$fitted,lm.mpg$residuals, main = "lm.mpg")
> plot(lm.drat$fitted,lm.drat$residuals, main = "lm.drat")
> plot(lm.gear$fitted,lm.gear$residuals, main = "lm.gear")
>
> # Plot residuals against IVs for each model
> plotResIV <- function (IV,lmResiduals)
> {
> lapply(IV, function (x) plot(x,lmResiduals))
> }
>
> plotResIV(lm.mpg$model[,-1],lm.mpg$residuals)
> plotResIV(lm.drat$model[,-1],lm.drat$residuals)
> plotResIV(lm.gear$model[,-1],lm.gear$residuals)
>
> Many thanks
> Ross Ahmed
[[alternative HTML version deleted]]