Driss Agramelal
2010-Mar-31 13:13 UTC
[R] for loop; lm() regressions; list of vectors - lapply - accolades and square brackets??
Hello and thank you both for your answers! Dennis, I tried to simply run lm(a ~ b) after re-importing "a" as a matrix, but I get the following error message: Error in model.frame.default(formula = a ~ b, drop.unused.levels = TRUE) : invalid type (list) for variable 'a' so maybe I have to specify something in the arguments? What do you think? David, I tried your syntax as well, and received quasi-the same error statement:> for(i in seq_along(a)) print(r <- lm(a[i] ~ b) )Error in model.frame.default(formula = a[i] ~ b, drop.unused.levels = TRUE) : invalid type (list) for variable 'a[i]' I am not too familiar with the use of accolades, square brackets and parentheses, the order in which they have to come in the function and the role they play, but I think they might be important... I also tried to use "lapply"; it works wonderfully for a basic function like: lapply(a, mean) I get a list of results with names and values..perfect! But with the lm() function... I just don't know how to write the arguments... tried several options without success... Any idea that could help me solve this only seemingly easy task would be most welcome!! Many Thanks, Driss Agramelal 2010/3/30 David Winsemius <dwinsemius@comcast.net>> > On Mar 30, 2010, at 12:42 PM, Driss Agramelal wrote: > > ## Hello everyone, >> ## >> ## I am trying to execute 150 times a lm regression using the 'for' loop, >> with 150 vectors for y, >> ## >> ## and always the same vector for x. >> ## >> ## I have an object with 150 elements named "a", >> ## >> ## and a vector of 60 values named "b". >> ## >> ## Each element in "a" has 60 values plus a header. >> ## >> ## When I type: >> >> r <- lm(i ~ b) >> >> for(i in a) print(r) >> >> >> > Try instead something like this untested modification: > > for(i in seq_along(a)) print(r <- lm(a[i] ~ b) ) > > > >> ## I get 150 times the lm results of the first element of "a" regressed >> with "b", >> ## >> ## whereas I would like to have 150 different regression results from >> each >> element in "a"... >> ## >> ## Can someone please help me with the syntax of my loop please? >> ## >> ## Many Thanks, >> ## >> ## Driss Agramelal >> ## >> ## Switzerland >> ## >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> 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. >> > > David Winsemius, MD > West Hartford, CT > >Hi: Did you try lm(a ~ b) ? According to the lm() help page, if a is a matrix, lm will regress each column of the matrix on the right hand side of the formula. Sounds like what you want... HTH, Dennis [[alternative HTML version deleted]]
David Winsemius
2010-Mar-31 13:42 UTC
[R] for loop; lm() regressions; list of vectors - lapply - accolades and square brackets??
On Mar 31, 2010, at 9:13 AM, Driss Agramelal wrote:> > Hello and thank you both for your answers! > > Dennis, I tried to simply run > > lm(a ~ b) > > after re-importing "a" as a matrix, but I get the following error > message: > > Error in model.frame.default(formula = a ~ b, drop.unused.levels = > TRUE) : > invalid type (list) for variable 'a' > > so maybe I have to specify something in the arguments? What do you > think? > > David, > > I tried your syntax as well, and received quasi-the same error > statement: > > > for(i in seq_along(a)) print(r <- lm(a[i] ~ b) ) > Error in model.frame.default(formula = a[i] ~ b, drop.unused.levels > = TRUE) : > invalid type (list) for variable 'a[i]' > > I am not too familiar with the use of accolades, square brackets and > parentheses, the order in which they have to come > in the function and the role they play, but I think they might be > important... > > I also tried to use "lapply"; it works wonderfully for a basic > function like: > > lapply(a, mean) > > I get a list of results with names and values..perfect! But with the > lm() function... I just > don't know how to write the arguments... tried several options > without success... > > Any idea that could help me solve this only seemingly easy task > would be most welcome!!Do you think we can figure this out when you have provided no sample data and have not provided even the results of str on the data object you are working with? Generally one gains insight by parring the problem down to smaller test cases and working with them. Perhaps the first 5 elements of "a" rather than all 150? -- David.> 2010/3/30 David Winsemius <dwinsemius@comcast.net> > > On Mar 30, 2010, at 12:42 PM, Driss Agramelal wrote: > > ## Hello everyone, > ## > ## I am trying to execute 150 times a lm regression using the 'for' > loop, > with 150 vectors for y, > ## > ## and always the same vector for x. > ## > ## I have an object with 150 elements named "a", > ## > ## and a vector of 60 values named "b". > ## > ## Each element in "a" has 60 values plus a header. > ## > ## When I type: > > r <- lm(i ~ b) > > for(i in a) print(r) > > > > Try instead something like this untested modification: > > for(i in seq_along(a)) print(r <- lm(a[i] ~ b) ) > > > > ## I get 150 times the lm results of the first element of "a" > regressed > with "b", > ## > ## whereas I would like to have 150 different regression results > from each > element in "a"... > ## > ## Can someone please help me with the syntax of my loop please? > ## > ## Many Thanks, > ## > ## Driss Agramelal > ## > ## Switzerland > ## > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. > > David Winsemius, MD > West Hartford, CT > > > > Hi: > > Did you try > > lm(a ~ b) ? > > According to the lm() help page, if a is a matrix, lm will regress > each column of the > matrix on the right hand side of the formula. Sounds like what you > want... > > HTH, > Dennis > >David Winsemius, MD West Hartford, CT [[alternative HTML version deleted]]