[R] for loop; lm() regressions; list of vectors - lapply - accolades and square brackets??
ehlers at ucalgary.ca
Wed Mar 31 17:11:35 CEST 2010
David is right - you should include data/code.
Nevertheless, the error you get suggests that what you
call a "matrix" is in fact a data.frame; that's usually
a crucial distinction. There are two things you might try:
lm(as.matrix(a) ~ b)
for(i in seq_along(a)) print(r<- lm(a[ ,i] ~ b) )
#Note the comma!
On 2010-03-31 7:42, David Winsemius wrote:
> 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
>> 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
>> I tried your syntax as well, and received quasi-the same error
>>> 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
>> 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?
University of Calgary
More information about the R-help