aegea
2009-Dec-04 04:29 UTC
[R] how to calculate covariance matrix in R? why cov doesn't work
Hello, Sorry. It may be a stupid question. I have two vectors a<-c(9,3,5) b<-c(3,4,1) How can I get the variance-covariance matrix of these two vectors? I tried cov(a,b), I got a number not a matrix. I tried to transpose vector a and b as t(a) and t(b), it still cannot work. Any suggestions? Thank a lot! -- View this message in context: http://n4.nabble.com/how-to-calculate-covariance-matrix-in-R-why-cov-doesn-t-work-tp948253p948253.html Sent from the R help mailing list archive at Nabble.com.
Jorge Ivan Velez
2009-Dec-04 06:01 UTC
[R] how to calculate covariance matrix in R? why cov doesn't work
Try this: a <- c(9,3,5) b<-c(3,4,1) cbind(a, b) # a b # [1,] 9 3 # [2,] 3 4 # [3,] 5 1 cov(cbind(a, b)) # a b # a 9.3333333 -0.6666667 # b -0.6666667 2.3333333 HTH, Jorge On Thu, Dec 3, 2009 at 11:29 PM, aegea <> wrote:> > Hello, > > Sorry. It may be a stupid question. > I have two vectors > a<-c(9,3,5) > b<-c(3,4,1) > How can I get the variance-covariance matrix of these two vectors? > I tried cov(a,b), I got a number not a matrix. > I tried to transpose vector a and b as t(a) and t(b), it still cannot work. > > Any suggestions? Thank a lot! > -- > View this message in context: > http://n4.nabble.com/how-to-calculate-covariance-matrix-in-R-why-cov-doesn-t-work-tp948253p948253.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]]