cov(A) will compute a covariance matrix of the columns of a matrix A.
 
cov(a, b) will compute a covariance of the vectors a and b.
 
here are some examples:
 
#create random vectors
a=rnorm(10)
b=rnorm(10)
 
 
> a
 [1] -0.3110384 -2.6201009 -1.3078118  1.3384743  2.4032381 -0.7245201
 [7] -0.2274577 -1.4619624  2.2165118  1.3621709
 
> b
 [1] -1.1648495 -2.3013411  1.5335541 -0.2165172  1.1157774  1.0360323
 [7] -0.8309283 -0.3403514  0.3640117 -0.6497384

#create a matrix
A=cbind(a,b)  
 
> A
               a          b
 [1,] -0.3110384 -1.1648495
 [2,] -2.6201009 -2.3013411
 [3,] -1.3078118  1.5335541
 [4,]  1.3384743 -0.2165172
 [5,]  2.4032381  1.1157774
 [6,] -0.7245201  1.0360323
 [7,] -0.2274577 -0.8309283
 [8,] -1.4619624 -0.3403514
 [9,]  2.2165118  0.3640117
[10,]  1.3621709 -0.6497384
 
cov(a,b)
 
Covariance
> cov(a,b)
[1] 0.7481056
 
#Covariance matrix
cov(A)
 
> cov(A)
          a         b
a 2.8529990 0.7481056
b 0.7481056 1.3910519
 
By the way, the covariance of two scalars will be zero, since the scalars are constants.
 
 
I hope this helps.
 
Sincerely yours,
 
Mark J. Lamias


________________________________
From: huang jialin <huangpsych@gmail.com>
To: "Doran, Harold" <HDoran@air.org> 
Cc: "r-help@r-project.org" <r-help@r-project.org> 
Sent: Monday, March 12, 2012 12:08 PM
Subject: Re: [R] how to calculate a variance and covariance matrix for a vector

Doran,

Thanks for your advice.

Sincerely,
Jialin Huang


On Mon, Mar 12, 2012 at 10:56 AM, Doran, Harold <HDoran@air.org> wrote:

> Always start with the help page. But, the best way to get help on this
> list is to construct some toy data. Even if imperfect, people will work to
> help you. Maybe show us some of your actual data.****
>
> ** **
>
> Of course, if a and b1 are really scalars, I don’t think there is such a
> thing as the covariance between two scalars****
>
> ** **
>
> ** **
>
> * *
>
> ** **
>
> *From:* huang jialin [mailto:huangpsych@gmail.com]
> *Sent:* Monday, March 12, 2012 11:53 AM
> *To:* Doran, Harold
> *Cc:* r-help@r-project.org
> *Subject:* Re: [R] how to calculate a variance and covariance matrix for
> a vector****
>
> ** **
>
> Hi,****
>
> ** **
>
> Thanks for your response. ****
>
> ** **
>
> I tried to use cov() before, but got NA all over the place.****
>
> ** **
>
> a and b1 are scalars in the example. So that is the problem. I will start
> over from the formula. ****
>
> ** **
>
> Thanks again.****
>
> ** **
>
> Sincerely,****
>
> Jialin Huang****
>
> ** **
>
> On Mon, Mar 12, 2012 at 10:47 AM, Doran, Harold <HDoran@air.org> wrote:***
> *
>
> Can you provide sample data? This suggests that a and b1, for example, are
> scalars. If that is what you actually have then you cannot produce a
> covariance between two scalars.****
>
>
> > -----Original Message-----
> > From: r-help-bounces@r-project.org [mailto:r-help-bounces@r-project.org]
> On
> > Behalf Of huang jialin
> > Sent: Monday, March 12, 2012 11:43 AM
> > To: r-help@r-project.org
> > Subject: [R] how to calculate a variance and covariance matrix for a
> vector
> >
> > Hello,
> >
> > I have a vector {a, b1, b2, b3, b4}. How can I calculate the following
> > matrix:
> >
> > var(a)  cov(a, b1)  cov(a, b2)  cov(a, b3)  cov(a, b4)
> > cov(a, b1) var(b1)  cov(a, b2)  cov(a, b3)  cov(a, b4)
> > ...
> > ...
> > cov(a, b1)  cov(a, b2)  cov(a, b3)  cov(a, b4) var(b4)
> >
> > I would very appreciate your inputs. Thank you very much.
> >
> > Sincerely,
> > Jialin Huang
> >****
>
> >      [[alternative HTML version deleted]]****
>
> >
> > ______________________________________________
> > 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]]


______________________________________________
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]]

