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@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.

Reply via email to