Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I store "arrays" of statistical models?

Tags:

r

Is there an R data structure into which I can store a number of lm or lmer or gam objects? J has boxed arrays, and one can put pretty much anything into the cells of such a boxed array. I think that's what I'm looking for in R.

I've tried lists and data frames, to no avail; I thought lists might work.

> testlist <- list()
> testlist[1] <- subject1.2008.gam
Warning message:
In testlist[1] <- subject1.2008.gam :
  number of items to replace is not a multiple of replacement length
> 

Alternatively, is there a way to create and use a variable name on the LHS of <-?

Finally, perhaps you have a better idiom for me to consider. I'm trying to create a collection of GAM models over a set of subjects and years, for example. Later, I want to be able to plot or predict from those models, so I think I need to keep the full model around. Because I want to be able to use this code with different data sets later, I'd like not to hard-code the names of the gam objects nor their number.

While I started by putting the gam() call in a loop, I think one of the apply() functions might work better, but I still need a place to store the output.

like image 789
Bill Avatar asked Apr 08 '11 19:04

Bill


1 Answers

You need the [[ operator for lists, try

testlist[[1]] <- subject1.2008.gam

The other usual tip is that you may want to pre-allocate if you know how many elements you may have, I often do

testlist <- vector(mode="list", length=N)

for a given N.

like image 173
Dirk Eddelbuettel Avatar answered Nov 16 '22 00:11

Dirk Eddelbuettel