If I have a model line = lm(y~x1) and I want to use a for loop to change the number of explanatory variables, how would I do this? So for example I want to store the model objects in a list. model1 = lm(y~x1) model2 = lm(y~x1+x2) model3 = lm(y~x1+x2+x3) model4 = lm(y~x1+x2+x3+x4) model5 = lm(y~x1+x2+x3+x4+x5)... model10. model_function = function(x){ for(i in 1:x) { } If x =1, then the list will only add model1. If x =2, then the list will add both model1 and model2. If x=3, then the list will add model1 model 2 and model3 and so on. How do I translate this into code? -- View this message in context: http://r.789695.n4.nabble.com/How-to-iterate-through-different-arguments-tp2953511p2953511.html Sent from the R help mailing list archive at Nabble.com.
any solutions? -- View this message in context: http://r.789695.n4.nabble.com/How-to-iterate-through-different-arguments-tp2953511p2953608.html Sent from the R help mailing list archive at Nabble.com.
There are several ways to do this. The following is only one of the ways. One of the advantages of this approach is that it allows including both continuous and categorical variables. I'll demonstrate with the iris dataset. Place your variables in a dataframe with the y variable in the first column. Then, out<-list() vars<-names(iris) out[[1]]<-lm(Sepal.Length~1,data=iris) for(k in 2:length(vars)){ out[[k]]<-update(out[[k-1]],as.formula(paste(".~.+",vars[k],sep=""))) } On Sun, Oct 3, 2010 at 4:29 PM, lord12 <trexinsd@yahoo.com> wrote:> > If I have a model line = lm(y~x1) and I want to use a for loop to change > the > number of explanatory variables, how would I do this? > > So for example I want to store the model objects in a list. > > model1 = lm(y~x1) > model2 = lm(y~x1+x2) > model3 = lm(y~x1+x2+x3) > model4 = lm(y~x1+x2+x3+x4) > model5 = lm(y~x1+x2+x3+x4+x5)... > model10. > > model_function = function(x){ > for(i in 1:x) { > } > If x =1, then the list will only add model1. If x =2, then the list will > add > both model1 and model2. If x=3, then the list will add model1 model 2 and > model3 and so on. How do I translate this into code? > -- > View this message in context: > http://r.789695.n4.nabble.com/How-to-iterate-through-different-arguments-tp2953511p2953511.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
On Sun, Oct 3, 2010 at 4:29 PM, lord12 <trexinsd at yahoo.com> wrote:> > If I have a model line = lm(y~x1) and I want to use a for loop to change the > number of explanatory variables, how would I do this? > > So for example I want to store the model objects in a list. > > model1 = lm(y~x1) > model2 = lm(y~x1+x2) > model3 = lm(y~x1+x2+x3) > model4 = lm(y~x1+x2+x3+x4) > model5 = lm(y~x1+x2+x3+x4+x5)... > model10. > > model_function = function(x){ > for(i in 1:x) { > } > If x =1, then the list will only add model1. If x =2, then the list will add > both model1 and model2. If x=3, then the list will add model1 model 2 and > model3 and so on. How do I translate this into code?Here are a couple of approaches. The first one is simpler and may be adequate. The second has the advantage that it writes out the formula in full, fo, which is shown in the output: lapply(1:4, function(i) lm(y1 ~., anscombe[c(1:i, 5)])) lapply(1:4, function(i) { fo <- formula(model.frame(y1 ~., anscombe[c(1:i, 5)])) do.call("lm", list(fo, quote(anscombe))) }) -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Thanks a lot! I have one other question,however. If I want to have two garchFits, garchFit(~aparch(1,1), data = sunspot.year) and garchFit(~arma(2,1)+apaarch(1,1), data = sunspot.year), how do I automate the creation of these models? for(k in 1:2) { garchFit~...,data = sunspot.year), } I am not sure what to put in the ... -- View this message in context: http://r.789695.n4.nabble.com/How-to-iterate-through-different-arguments-tp2953511p2965042.html Sent from the R help mailing list archive at Nabble.com.
On Wed, Oct 6, 2010 at 10:16 AM, lord12 <trexinsd at yahoo.com> wrote:> > Thanks a lot! I have one other question,however. If I want to have two > garchFits, ?garchFit(~aparch(1,1), data = sunspot.year) and > garchFit(~arma(2,1)+apaarch(1,1), data = sunspot.year), how do I automate > the creation of these models? > > for(k in 1:2) > { > ?garchFit~...,data = sunspot.year), > } > > I am not sure what to put in the ...Set up a character vector of terms, Terms, such as Terms <- c("x1", "x2", "x3", "x4") and then replace the fo<- line in my second solution with: fo <- as.formula(paste("y1", paste(Terms[1:k], collapse = "+"), sep = "~")) -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com