[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Unexpected results from "replicate" (compared to saply)



Recall my heteroskedasicity example code "heterosk.R". The management of the Monte Carlo models was a bit tedious.

The following creates a list of regression objects, or at least I wish it would. The result is not a list of 100 regression objects, but rather a 12x100 array in which the row represents the component of model output and the column represents the model.

-----------------------------
# create datasets with 10000 observations each, conduct
# a regression
createOLS <- function(b0=10, b1=13){
 e <- rnorm(10000)
 x <- 10*rnorm(10000)+100
 y <- b0 + b1*x + 1*x*x *e
 myOLS1 <- lm(y~x)
}

oneHundredRegressions <- replicate(100, createOLS(3, 55))

oneHundredBVectors <- oneHundredRegressions[1,]

-----------------------------

oneHundredRegressions appears as a 2 dimensional array, with one column for each run of the model and one row for each element outputted. Example for 4 models:

> oneHundredRegressions
[,1] [,2] [,3] [,4] coefficients Numeric,2 Numeric,2 Numeric,2 Numeric,2 residuals Numeric,10000 Numeric,10000 Numeric,10000 Numeric,10000
effects       Numeric,10000 Numeric,10000 Numeric,10000 Numeric,10000
rank 2 2 2 2 fitted.values Numeric,10000 Numeric,10000 Numeric,10000 Numeric,10000 assign Integer,2 Integer,2 Integer,2 Integer,2 qr List,5 List,5 List,5 List,5 df.residual 9998 9998 9998 9998 xlevels List,0 List,0 List,0 List,0 call Expression Expression Expression Expression terms Expression Expression Expression Expression model List,2 List,2 List,2 List,2
One can obtain the summary output for a particular element by this

summary.lm(oneHundredRegressions[,1])

But
> vcov(oneHundredRegressions[,1])
Error in vcov(oneHundredRegressions[, 1]) :
   no applicable method for "vcov"
> vcov.lm(oneHundredRegressions[,1])
Error: couldn't find function "vcov.lm"
>

On the contrary, I found that if I used the lapply command to replicate, then the results were what I wanted.

oneHundredLapply <- lapply(1:100, function(x) createOLS(4,5))

With that, one can ask for particular regressions easily, as in

oneHundredLapply[[1]]

and vcov and hccm work.


After wresting with this for quite a while, I read the help page for sapply and replicate and realized that the simplify=T option is default for replicate, and that "breaks" the object output into the two dimensional array that I don't want. So if I fix my code by running

oneHundredRegressions <- replicate(100, createOLS(3, 55), simplify=F)

The result is the kind I want. I've put this to use in a revision of my "heterosk.R" example program.