5

I'm trying to calculate sample covariance between these two vectors. I defined a function with two input variables. I don't know if it is correct? Also my formula for sample covariance won't run. Can anyone help me write it out in R?

  xv = c(1., 5.5, 7.8, 4.2, -2.7, -5.4, 8.9)
  yv = c(0.1, 1.5, 0.8, -4.2, 2.7, -9.4, -1.9)
  sampleCov= function(x,y){ 
    cov(xv,yv) = frac{sum_{i=1}^{n}(x_i-\mu_x)(y_i-\mu_y)}{n-1}].
    return (Cov(xv,yv)
  }
Julius Vainora
  • 47,421
  • 9
  • 90
  • 102
Bill
  • 275
  • 2
  • 5
  • 9
  • 5
    Your function doesn't work because it has syntax errors. Fix those first. Have you tried the `cov` function? – Joshua Ulrich Oct 17 '12 at 19:35
  • To be honest i'm very new to programming and dont know how to fix them. I've been stuck on this for a while. How can I improve my function? – Bill Oct 17 '12 at 19:40
  • 1
    You can improve your function by reading the [introductory manual](http://cran.r-project.org/doc/manuals/R-intro.html), especially [Section 10, Writing your own functions](http://cran.r-project.org/doc/manuals/R-intro.html#Writing-your-own-functions). – Joshua Ulrich Oct 17 '12 at 19:44
  • 2
    For starters, don't use LaTeX markup as R code; it just won't parse. It is hard to know where to begin here because you have got totally the wrong end of the stick if you thought R could even begin to understand that code. – Gavin Simpson Oct 17 '12 at 19:44

3 Answers3

6

There is a base function in R called cov that does exactly what you want, but if you want to write a function (no need to do that) you can try the following:

COV<- function(x,y) {
  if(length(x)!=length(y)) {stop('x must have the same length as y ')}
  x.bar <- mean(x)
  y.bar <- mean(y)
  N <- length(x)

  Cov <- (sum((x-x.bar)*(y-y.bar))) / (N-1)
  return(Cov)
}

COV(xv, yv)
[1] 8.697381

cov(xv, yv)
[1] 8.697381

As you can see COV gives the same result as cov so you don't have to write a function for that.

Besides, the body of your function doesn't have R syntax, instead you wrote LaTex syntax which are not the same.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
5

Just use the internal cov() function:

xv <- c(1., 5.5, 7.8, 4.2, -2.7, -5.4, 8.9)
yv <- c(0.1, 1.5, 0.8, -4.2, 2.7, -9.4, -1.9)
cov(xv, yv)

R> cov(xv, yv)
[1] 8.697381

If you really want to reinvent the wheel:

sampleCov <- function(x,y){
    stopifnot(identical(length(x), length(y)))
    sum((x - mean(x)) * (y - mean(y))) / (length(x) - 1)
}

R> sampleCov(xv, yv)
[1] 8.697381
Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
0

I know this is old, but you can compute the covariance of two matrices using the following formula:

  cov_xv_yv <- 1/(length(xv)-1) * t(xv) %*% yv

Which is 1/(N-1) times the matrix product of the transpose of matrix xv and the matrix yv.

This form is easily extendable to many dimensions.

Connor Dibble
  • 517
  • 3
  • 13