[R] how to calculate covariance matrix in R? why cov doesn't work

0 views
Skip to first unread message

aegea

unread,
Dec 3, 2009, 11:29:04 PM12/3/09
to r-h...@r-project.org

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-h...@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.

Jorge Ivan Velez

unread,
Dec 4, 2009, 1:01:42 AM12/4/09
to aegea, r-h...@r-project.org
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

[[alternative HTML version deleted]]

aegea

unread,
Dec 4, 2009, 3:54:30 PM12/4/09
to r-h...@r-project.org

Jorge,

It really helps. I appreciate your help a lot!

Chen

On Fri, Dec 4, 2009 at 12:04 AM, Jorge Ivan Velez [via R] <
ml-node+9482...@n4.nabble.com<ml-node%2B948281-...@n4.nabble.com>
> wrote:

> > [hidden email]<http://n4.nabble.com/user/SendEmail.jtp?type=node&node=948281&i=0>mailing list


> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide

> > http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>


> > and provide commented, minimal, self-contained, reproducible code.
> >
>
> [[alternative HTML version deleted]]
>
> ______________________________________________

> [hidden email]<http://n4.nabble.com/user/SendEmail.jtp?type=node&node=948281&i=1>mailing list


> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide

> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>


> and provide commented, minimal, self-contained, reproducible code.
>
>

> ------------------------------
> View message @
> http://n4.nabble.com/how-to-calculate-covariance-matrix-in-R-why-cov-doesn-t-work-tp948253p948281.html
> To unsubscribe from how to calculate covariance matrix in R? why cov
> doesn't work, click here< (link removed) ==>.
>
>
>


--
Best,
Chen

--
View this message in context: http://n4.nabble.com/how-to-calculate-covariance-matrix-in-R-why-cov-doesn-t-work-tp948253p948858.html


Sent from the R help mailing list archive at Nabble.com.

[[alternative HTML version deleted]]

Reply all
Reply to author
Forward
0 new messages