", "drat", "gear")
> IV <- list(c("cyl", "disp", "hp"), c("wt", "qsec"), c("carb", "hp"))
> fits <- vector("list", length(DV))
>
> for(i in seq(DV)) {
>fi
quot;, "qsec"), c("carb", "hp"))
fits <- vector("list", length(DV))
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"
Superb Jean, many thanks
Ross
From: Jean V Adams
Date: Tuesday, 6 November 2012 19:20
To: Ross Ahmed
Cc:
Subject: Re: [R] Apply same linear model to subset of dataframe
DV <- c("mpg", "drat", "gear")
IV <- list(c("cyl", "disp",
ion(x) plot(x, fit$resid))
fits[[i]] <- fit
}
Jean
Ross Ahmed wrote on 11/06/2012 09:25:13 AM:
>
> Thanks Jean
>
> This works for the plots, but it only stores the last lm() computed
>
> Ross
>
> From: Jean V Adams
> Date: Tuesday, 6 November
Thanks Jean
This works for the plots, but it only stores the last lm() computed
Ross
From: Jean V Adams
Date: Tuesday, 6 November 2012 14:12
To: Ross Ahmed
Cc:
Subject: Re: [R] Apply same linear model to subset of dataframe
Ross,
Here's one way to condense the code ...
DV <
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, m
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
7 matches
Mail list logo