I think what's happening is the process by which you make your list:
> *loop
> var <- vcov(out)
> varmat<- list(varmat, var)
I assume that before you run this loop, you write:
varmat <- list()
So, when you run the loop, each time you do the command varmat <-
list(varmat, var), you make a new list whose first element is a list
of lists ("varmat") and the second element is your vcov matrix. This
will lead to a pretty confusing data object.
Here's a simpler way to make your list:
varmat <- list()
for (i in 1:whatever){
var <- vcov(out[i])
varmat[[i]] <- var
}
Or if you want to go the apply route, assuming that you can make "out"
a list of regressions:
varmat <- lapply(out, function(x) vcov(x))
-Michael
On Apr 8, 2010, at 9:30 AM, Carly Knight wrote:
> Hi Michael,
>
> Thanks! I did try the [[x]] notation. I may be missing something but
> I still can't the result to be a matrix (and it has to be a matrix
> object to be fed into the other function). This is the code I am
> getting:
>
> var1<- varmat[[1]]
> > is.matrix(var1)
> [1] FALSE
>
>
> > as.matrix(var1)
> [,1]
> [1,] List,2
> [2,] ?
> >
>
> I'm probably missing something simple but do you know what I am
> doing wrong?
>
>
>
>
> On Thu, Apr 8, 2010 at 9:20 AM, Michael Barnett
> <mlbarnett at gmail.com> wrote:
> Carly -
>
> To properly index a list element, you need to type "[[x]]", not
> "[x]", so if you use varmat[[1]], that will return the first element
> of the list as a matrix object.
>
> -Michael
>
> On Apr 8, 2010, at 9:18 AM, Carly Knight wrote:
>
>> Hey everyone,
>>
>> I have a question not related to the problem set. For the
>> replication I have created a loop to run through a bunch of my
>> regressions. I've stored all the variance-covariance matrices in a
>> list object. Now that I have them, I want to extract each matrix to
>> put into my mvrnormal() funciton. however, I can't seem to turn it
>> back into a matrix. I've tried unlisting, calling a specific
>> element of the list, and everytime I try to do this or apply
>> as.matrix() I end up getting an error or something odd. This is the
>> code I am working with. Does anyone know how to properly unlist?
>>
> *loop
> var <- vcov(out)
> varmat<- list(varmat, var)
>> }
>>
>>
>> My code to try to unlist:
>> x<-as.matrix(unlist(lapply(varmat, t))[1])
>>
>> I've also tried:
>> unlist(varmat[1])
>> unlist(varmat)[1]
>>
>> Any help would be very much appreciated!
>>
>> - Carly
>>
>>
>> --
>> Carly Knight
>> Ph.D. Student
>> Department of Sociology
>> Harvard University
>> Phone: (281) 682-4063
>> E-Mail: crknight at
fas.harvard.edu
>> _______________________________________________
>> gov2001-l mailing list
>> gov2001-l at
lists.fas.harvard.edu
>>
http://lists.fas.harvard.edu/mailman/listinfo/gov2001-l
>
>
> _______________________________________________
> gov2001-l mailing list
> gov2001-l at
lists.fas.harvard.edu
>
http://lists.fas.harvard.edu/mailman/listinfo/gov2001-l
>
>
>
>
> --
> Carly Knight
> Ph.D. Student
> Department of Sociology
> Harvard University
> Phone: (281) 682-4063
> E-Mail: crknight at
fas.harvard.edu
> _______________________________________________
> gov2001-l mailing list
> gov2001-l at
lists.fas.harvard.edu
>
http://lists.fas.harvard.edu/mailman/listinfo/gov2001-l