Your code seems generally ok (though I wonder why you define a function
Var() to do exactly the same thing as var) but your problem is with your use
of mean, not in the bootstrap code.
It collapses things into a vector before taking a mean -- c.f.,
R> x = array(1:27,c(3,3,3))
R> mean(x)
[1] 14
Playing around with things, this seems to do what you would want:
apply(x, -1, mean)
Though I've never used *apply with the margin argument negative so no
promises that this works...
To make your code a little easier, you might want to make use of the
replicate() function in something like this
Michael Weylandt
On Sat, Sep 3, 2011 at 5:34 PM, meddee <meddee1000@gmail.com> wrote:
> Dear all I am a bit new to R so please keep your swords sheathed!
>
> I would simply like to bootstrap a covariance matrix from a multivariate
> gaussian density. At face value that seemed like a very straightforward
> problem to solve but I somehow could not get the boot package to work and
> did not really understand the documentation so I tried to do the bootstrap
> manually. Hence:
>
> x<-rmvnorm(n = 5, mean, diag(1,length(mean)))
> Var<-function(a) var(a)
> Var(x)
> sample<-matrix(sample(x,replace=T),ncol=length(mean))#single BS sample
> Var(sample)# sqr matrix of length(mean)
>
> #generate 1000 bootstrap samples
> boot <- array(NA, c(1000, 3, 3))
> #assign the var for bootstrap sample i as the ith element in the vector
> boot, using a for loop
> for (i in 1:1000) boot[i,,] <- Var(sample)
> mean(boot)
>
> For output I expect to see a 3x3 covariance matrix but i am getting a
> single
> scalar value.
>
> So can some person(s) do either (or all) of the following:
> - point out how I can get the intended result from the above code
> - point out how the boot function can be used to to solve this problem
> - point me to further documentation for the boot function
>
> p.s: rmvnorm is from the mvtnorm package.
>
> Thanks in advance!
>
>
>
> --
> View this message in context:
>
http://r.789695.n4.nabble.com/Bootstrapping-a-covariance-matrix-tp3788553p3788553.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.
>
[[alternative HTML version deleted]]