[R] rep() inside of lm()?

David Winsemius dwinsemius at comcast.net
Sun Jan 1 17:37:03 CET 2012


On Jan 1, 2012, at 9:13 AM, iliketurtles wrote:

> HI all,
> I'm new to R.
>
> Say I have a multi-layered list called newlist.

Which you have not provided in a form that lends itself well to  
constructing worked examples.

> ############
>> str(newlist)
> List of 2
> $ :List of 5
>  ..$ : num [1:8088] NA 464 482 535 557 ...
>  ..$ : num [1:8088, 1:2] NA 464 482 535 557 ...
>  ..$ : num [1:8088, 1:3] NA 464 482 535 557 ...
>  ..$ : num [1:8088, 1:4] NA 464 482 535 557 ...
>  ..$ : num [1:8088, 1:5] NA 464 482 535 557 ...
> $ :List of 3
>  ..$ : num [1:8088] NA NA 1386 464 482 ...
>  ..$ : num [1:8088] NA NA NA 1386 464 ...
>  ..$ : num [1:8088] NA NA NA NA 1386 ...
> ############
> Let's say this list has 2 entries. i.e. length(newlist)=2. However,  
> in my
> function this list can be any number of entries, 1 to N. I want to  
> regress
> each newlist[[N]] in a multiple linear regression. The problem is I  
> don't
> know how to make a non-character or a non-numeric repeat.
>
> So I'd want something like:
> #---------------------------
> dumplist<-list()
> for(a in 1:length(newlist[[1]])){
> for(b in 1:length(newlist[[2]]){
> for(terminal in 1:length(newlist[[length(newlist)]])){

Most problems if this sort arise from failing to understand that  
formulas are not built up like that. The lm function is not going to  
interpret your character values inside the expressions linked with  
"+"'s and "~".  If you want to make a formula you will need to use  
paste() to construct the approriate string and then formula() or  
as.formula() to make it the appropriate mode ( and do this before  
passing to lm(). There are many, many worked examples in the rhelp   
archives.

-- 
David

>
> dumlist[[..]]<-lm(y~newlist[[1]][a] + newlist[[2]]][b] + ....... +
> newlist[[N]][terminal]).
>
> }}}
> #---------------------------
> and I want the above to adjust to whatever N is.
>
> I'm new to R so this kind of thing is very difficult. I'm  
> experimenting with
> eval(), parse(), paste(), expression(), letters[] and other stuff  
> but I'm
> not getting far and I would greatly appreciation any guidance.
>
> ----
>
> Isaac
> Research Assistant
> Quantitative Finance Faculty, UTS
> --
> View this message in context: http://r.789695.n4.nabble.com/rep-inside-of-lm-tp4250712p4250712.html
> Sent from the R help mailing list archive at Nabble.com.

You might consider subscribing rather than using Nabble which is  
neither r-help nor is it an archive.

-- 

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list