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

Joshua Wiley jwiley.psych at gmail.com
Sun Jan 1 18:24:16 CET 2012


Hi Isaac,

This is my best understanding of what you are after.  A small worked
example would have been helpful (even if you had to manually write the
code, we would at least have a 'gold standard' for what we were trying
to automate to take lists of arbitrary size).  This is not necessarily
an efficient solution, but it is relatively simple and efficiency can
be dealt with later if this is in fact what you are trying to do.  I
tried to write it up in words, but that just ended up being confusing,
so hopefully this code is comprehendable:

############################################
## sample data
## an outer list of arbitrary length, each element of which
## is itself a list of arbitrary length each element of which
## is a numeric vector
set.seed(1)
newlist <- lapply(seq.int(sample(9, 1)), FUN = function(n) {
  lapply(seq.int(sample(5, 1)), function(k) rnorm(100))
})
str(newlist)

## you want all possible combinations choosing
## one element at a time from each element of newlist
## this is the index of which elements to extract from
## each sublist of newlist
index <- expand.grid(sapply(newlist, seq_along))

## create a list (length 20) each element of which
## is a matrix of the predictors
tmp <- lapply(1:nrow(index), function(i) {
  do.call("cbind", sapply(seq_along(newlist), function(j) {
    newlist[[j]][index[i, j]]}))
})

y <- 1:100

## calculate all the regressions
results <- lapply(tmp, function(x) lm(y ~ x))

## summary of the first one
summary(results[[1]])
############################################

Cheers,

Josh

On Sun, Jan 1, 2012 at 6:13 AM, iliketurtles <isaacm200 at gmail.com> wrote:
> HI all,
> I'm new to R.
>
> Say I have a multi-layered list called newlist.
> ############
>> 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)]])){
>
> 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.
>
> Thank you.
>
> -----
> ----
>
> 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.
>
> ______________________________________________
> 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.



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/



More information about the R-help mailing list