Kristi Glover
2013-May-13 15:17 UTC
[R] How can I make a loop to do multiple regression for more than 3000 dependent variables?
Hi R- User, I am just wondering how I can make a loop to repeat multiple regression. I do have more than 3000 dependent variables (example S1, S2......Sn) and put in different columns but the explanatory variables are the same for all these dependent variables. I have given an example below. In this example, S1, S2,....Sn are the dependent variables and; en1_predictor, en2_predictor, en3_predictor are the predictors for the dependent variables. I wanted to make a multiple regression for each dependent variable using these 4 predictors and make two tables: table 1 is: predicted value for each dependent variable and; the table 2: is the R2 adjusted and associated p value of each model (each dependent variable). I have been doing it (multiple regression) for each dependent variable manually but taking so much time. I am just wondering how I can make a loop from which I can extract these information in two tables.> dput(dat)dat<-structure(list(sites = 1:22, S1 = c(4L, 7L, 9L, 10L, 10L, 8L, 8L, 8L, 17L, 18L, 13L, 13L, 11L, 1L, 10L, 20L, 22L, 20L, 18L, 16L, 7L, 20L), S2 = c(5L, 8L, 12L, 9L, 12L, 5L, 5L, 7L, 21L, 12L, 17L, 17L, 10L, 2L, 9L, 21L, 19L, 21L, 15L, 23L, 12L, 18L ), Sn = c(7L, 12L, 23L, 7L, 24L, 9L, 2L, 6L, 19L, 21L, 32L, 12L, 54L, 12L, 32L, 35L, 21L, 34L, 25L, 27L, 21L, 32L), en1_predictor = c(-2.5767, -2.5767, -2.5767, -2.5767, -2.5767, -2.5767, -2.5767, -2.5347, -2.5347, -2.5347, -2.5347, -2.5347, -2.5347, -2.4939, -2.4939, -2.4939, -2.4939, -2.4939, -2.4939, -2.4939, -2.4543, -2.4543 ), en2_predictor = c(-1.1785, -0.6596, -0.6145, -0.6437, -0.6593, -0.7811, -1.1785, -1.1785, -1.1785, -0.6596, -0.6145, -0.6437, -0.6593, -1.1785, -0.1342, -0.2085, -0.4428, -0.5125, -0.8075, -1.1785, -1.1785, -0.1342), en3_predictor = c(-1.4445, -1.3645, -1.1634, -0.7735, -0.6931, -1.1105, -1.4127, -1.5278, -1.4445, -1.3645, -1.1634, -0.7735, -0.6931, -1.0477, -0.8655, -0.1759, 0.1203, -0.2962, -0.4473, -1.0436, -0.9705, -0.8953), en4_predictor = c(-0.4783, -0.3296, -0.2026, -0.3579, -0.5154, -0.5726, -0.6415, -0.3996, -0.4529, -0.5762, -0.561, -0.6891, -0.7408, -0.6287, -0.4337, -0.4586, -0.5249, -0.6086, -0.7076, -0.7114, -0.4952, 0.1091)), .Names = c("sites", "S1", "S2", "Sn", "en1_predictor", "en2_predictor", "en3_predictor", "en4_predictor"), class = "data.frame", row.names = c(NA, -22L )) Your help is highly appreciated. Cheers, Kristi ================ [[alternative HTML version deleted]]
Jeff Newmiller
2013-May-13 16:02 UTC
[R] How can I make a loop to do multiple regression for more than 3000 dependent variables?
Please don't post In HTML. It mangles your code. Figure out how to adjust your email software. Your example is incomplete, in that you have not indicated how you are doing the regression manually (I.e what R code steps are you using?). If your model is linear, you could do: deps <- as.matrix( dat[,2:4] ) inds <- as.matrix( dat[,5:8]) lm( deps~inds ) --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. Kristi Glover <kristi.glover at hotmail.com> wrote:>Hi R- User, >I am just wondering how I can make a loop to repeat multiple >regression. >I do have more than 3000 dependent variables (example S1, S2......Sn) >and put in different columns but the explanatory variables are the same >for all these dependent variables. I have given an example below. In >this example, S1, S2,....Sn are the dependent variables and; >en1_predictor, en2_predictor, en3_predictor are the predictors for the >dependent variables. > >I wanted to make a multiple regression for each dependent variable >using these 4 predictors and make two tables: table 1 is: predicted >value for each dependent variable and; the table 2: is the R2 adjusted >and associated p value of each model (each dependent variable). > >I have been doing it (multiple regression) for each dependent variable >manually but taking so much time. I am just wondering how I can make >a loop from which I can extract these information in two tables. > >> dput(dat) >dat<-structure(list(sites = 1:22, S1 = c(4L, 7L, 9L, 10L, 10L, 8L, >8L, 8L, 17L, 18L, 13L, 13L, 11L, 1L, 10L, 20L, 22L, 20L, 18L, >16L, 7L, 20L), S2 = c(5L, 8L, 12L, 9L, 12L, 5L, 5L, 7L, 21L, >12L, 17L, 17L, 10L, 2L, 9L, 21L, 19L, 21L, 15L, 23L, 12L, 18L >), Sn = c(7L, 12L, 23L, 7L, 24L, 9L, 2L, 6L, 19L, 21L, 32L, 12L, >54L, 12L, 32L, 35L, 21L, 34L, 25L, 27L, 21L, 32L), en1_predictor >c(-2.5767, >-2.5767, -2.5767, -2.5767, -2.5767, -2.5767, -2.5767, -2.5347, >-2.5347, -2.5347, -2.5347, -2.5347, -2.5347, -2.4939, -2.4939, >-2.4939, -2.4939, -2.4939, -2.4939, -2.4939, -2.4543, -2.4543 >), en2_predictor = c(-1.1785, -0.6596, -0.6145, -0.6437, -0.6593, >-0.7811, -1.1785, -1.1785, -1.1785, -0.6596, -0.6145, -0.6437, >-0.6593, -1.1785, -0.1342, -0.2085, -0.4428, -0.5125, -0.8075, >-1.1785, -1.1785, -0.1342), en3_predictor = c(-1.4445, -1.3645, >-1.1634, -0.7735, -0.6931, -1.1105, -1.4127, -1.5278, -1.4445, >-1.3645, -1.1634, -0.7735, -0.6931, -1.0477, -0.8655, -0.1759, >0.1203, -0.2962, -0.4473, -1.0436, -0.9705, -0.8953), en4_predictor >c(-0.4783, >-0.3296, -0.2026, -0.3579, -0.5154, -0.5726, -0.6415, -0.3996, >-0.4529, -0.5762, -0.561, -0.6891, -0.7408, -0.6287, -0.4337, >-0.4586, -0.5249, -0.6086, -0.7076, -0.7114, -0.4952, 0.1091)), .Names >= c("sites", >"S1", "S2", "Sn", "en1_predictor", "en2_predictor", "en3_predictor", >"en4_predictor"), class = "data.frame", row.names = c(NA, -22L >)) > >Your help is highly appreciated. >Cheers, >Kristi >================> > > [[alternative HTML version deleted]] > >______________________________________________ >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.