Re: [R] Apply same linear model to subset of dataframe

2012-11-08 Thread David Winsemius
", "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

Re: [R] Apply same linear model to subset of dataframe

2012-11-08 Thread Ross Ahmed
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"

Re: [R] Apply same linear model to subset of dataframe

2012-11-06 Thread Ross Ahmed
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",

Re: [R] Apply same linear model to subset of dataframe

2012-11-06 Thread Jean V Adams
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

Re: [R] Apply same linear model to subset of dataframe

2012-11-06 Thread Ross Ahmed
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 <

Re: [R] Apply same linear model to subset of dataframe

2012-11-06 Thread Jean V Adams
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

[R] Apply same linear model to subset of dataframe

2012-11-04 Thread Ross Ahmed
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