Hi All, I had posted a question on a similar topic, but I think it was not focused. I am posting a modification that I think better accomplishes this. I hope this is ok, and I apologize if it is not. :) I am looping through variables and running several regressions. I have reason to believe that the data is being duplicated because I have been monitoring the memory use on unix. How can I avoid this? Here is an example of how I am going about this. For lm, I also tried model=FALSE, but this did not seem to do the job. Any ideas? Thanks for your time. Regards, Juliet # create data set.seed(1) response <- rnorm(50) var1 <- rnorm(50) var2 <- rnorm(50) var3 <- rnorm(50) myData <- data.frame(response,var1,var2,var3) var.names <- names(myData)[2:4] numVars <- length(var.names) betas <- rep(-1,numVars) names(betas) <- var.names #run regression on var1 through var3. for (Var_num in 1:numVars) { col.name <- var.names[Var_num] mylm <- lm(response ~ get(col.name),data=myData,model=FALSE) betas[Var_num] <- coef(mylm)[2] }
Jorge Ivan Velez
2009-Jan-24 05:44 UTC
[R] how to prevent duplications of data within a loop
Dear Juliet, Why don't you use *apply() instead a for() loop? Here is a starting point:> apply(myData[,-1],2,function(x) coefficients(lm(response~x)))var1 var2 var3 (Intercept) 0.10438369 0.10415221 0.1176728 x -0.03354243 0.02429041 -0.2240759 Note that the second row of the code above is the same you get with you procedure. See ?apply for more information. HTH, Jorge On Sat, Jan 24, 2009 at 12:30 AM, Juliet Hannah <juliet.hannah@gmail.com>wrote:> Hi All, > > I had posted a question on a similar topic, but I think it was not > focused. I am posting a modification that I think better accomplishes > this. > I hope this is ok, and I apologize if it is not. :) > > I am looping through variables and running several regressions. I have > reason to believe that the data is being duplicated because I have > been > monitoring the memory use on unix. > > How can I avoid this? Here is an example of how I am going about this. > For lm, I also tried model=FALSE, but this did not seem to do the job. > Any ideas? > Thanks for your time. > > Regards, > > Juliet > > # create data > set.seed(1) > response <- rnorm(50) > var1 <- rnorm(50) > var2 <- rnorm(50) > var3 <- rnorm(50) > myData <- data.frame(response,var1,var2,var3) > var.names <- names(myData)[2:4] > > > numVars <- length(var.names) > betas <- rep(-1,numVars) > names(betas) <- var.names > > #run regression on var1 through var3. > > for (Var_num in 1:numVars) > { > col.name <- var.names[Var_num] > mylm <- lm(response ~ get(col.name),data=myData,model=FALSE) > betas[Var_num] <- coef(mylm)[2] > } > > ______________________________________________ > 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]]
David Freedman
2009-Jan-24 17:46 UTC
[R] how to prevent duplications of data within a loop
or how about lm(myData$response~as.matrix(myData[2:4])) hth, david Juliet Hannah wrote:> > Hi All, > > I had posted a question on a similar topic, but I think it was not > focused. I am posting a modification that I think better accomplishes > this. > I hope this is ok, and I apologize if it is not. :) > > I am looping through variables and running several regressions. I have > reason to believe that the data is being duplicated because I have > been > monitoring the memory use on unix. > > How can I avoid this? Here is an example of how I am going about this. > For lm, I also tried model=FALSE, but this did not seem to do the job. > Any ideas? > Thanks for your time. > > Regards, > > Juliet > > # create data > set.seed(1) > response <- rnorm(50) > var1 <- rnorm(50) > var2 <- rnorm(50) > var3 <- rnorm(50) > myData <- data.frame(response,var1,var2,var3) > var.names <- names(myData)[2:4] > > > numVars <- length(var.names) > betas <- rep(-1,numVars) > names(betas) <- var.names > > #run regression on var1 through var3. > > for (Var_num in 1:numVars) > { > col.name <- var.names[Var_num] > mylm <- lm(response ~ get(col.name),data=myData,model=FALSE) > betas[Var_num] <- coef(mylm)[2] > } > > ______________________________________________ > R-help at 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. > >-- View this message in context: http://www.nabble.com/how-to-prevent-duplications-of-data-within-a-loop-tp21637431p21642232.html Sent from the R help mailing list archive at Nabble.com.