[R] Very strange function() behaviour.

Bert Gunter gunter.berton at gene.com
Mon Jan 2 16:34:45 CET 2012


(To me, anyway) Incoherent. Reproducible code needed.

-- Bert

On Mon, Jan 2, 2012 at 1:59 AM, iliketurtles <isaacm200 at gmail.com> wrote:
> Sorry if there's an easy answer to this problem, but here goes..
> *
> INTRODUCTION & CONTEXT*
>
> I'm creating a function where the number of entries in the lm(y~.......)
> varies. i.e. depending on the function input I want lm(y~x1+x2+x3),
> sometimes I'll want lm(y~x1) only, et cetera.
>
> I've completed this with paste(..,sep="") and as.formula().
>
> *The problem*
>
> Many of these possible entries [i.e. the x1, x2, x3] need their own lapply
> for the lm() to work, as they're all lists or matrices with many dimensions
> [I'm iterating along the dim of each X, which is why I need separate
> lapply() for each]. Except, I can't do as.formula("..") with lapply(),
> therefore I'm stuffed!
>
> This is the problem area:
>
> #--------------------------------------------------
> rl<-list()
> for(Q in 1:ncol(y)){
> rl[[colnames(y)[Q]]]<-
> lapply(1:length(x1),function(N){lm(y[,Q]~x1[[N]])}
> }
> #-------------------------------------------------
>
> This is the correct code for a case of 1 independent variable. But if the
> preceding code wants to put in x2 or x3 (which it can do fine), how do I get
> more lapplys to automatically layer on top of the above code, responsive to
> the number of x? [[Note I can't layer loops on top of it... as I'm shoving
> the lm() into a list(), lapply() puts the lm() in the correct dimensions of
> the list() but for() loops do not.]]
>
> ##P.S. just consider y to be:
> y<-matrix(rnorm(100),ncol=50,nrow=100)
> ##x1, x2, x3, etc are either 1 or 2 dimensional lists... Just know that
> there needs to be some lapply()- or loop-esque "thingy" that deals with each
> one, otherwise it won't work.
>
>
> Thank you everyone.
>
>
> -----
> ----
>
> Isaac
> Research Assistant
> Quantitative Finance Faculty, UTS
> --
> View this message in context: http://r.789695.n4.nabble.com/Very-strange-function-behaviour-tp4252877p4252877.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm



More information about the R-help mailing list