Search code examples
rcovariancepearson-correlation

Pearson Correlation Covariance Showing Positive when Negative Correlated


I'm trying to compute Pearson correlation for these two below. And it fits a linear line y=1/3*(x). When I calculate covariance, I got 4.5 and standard deviation for x and y are 4.74 and 1.58 respectively, which ends up a positive coefficient. However, the slides I have tells me the covariance is -7.5 and the coefficient -1, which confuses me. Who is actually correct on this?

x<-c(-3,-6,0,3,6)

y<-c(1,-2,0,-1,2)


Solution

  • x<-c(-3,-6,0,3,6)
    y<-c(1,-2,0,-1,2)
    
    cor(x,y, method = "pearson") # pearson correlation
    [1] 0.6
    
    cov(x,y) # covariance
    [1] 4.5
    

    you can also compute the covariance and pearson correlation manually to double check:

    N <- length(x)
    x_ <- mean(x)
    y_ <- mean(y)
    
    cova <- 0
    for(i in 1:length(x))
    {
      cova <- cova + ((x[i]-x_)*(y[i]-y_))/(N-1)
    }
    
    cova
    [1] 4.5
    

    or:

    cova <- as.numeric((x-x_)%*%(y-y_)/(N-1))
    
    cova
    [1] 4.5
    

    for the pearson correlation:

    cova / (sqrt(var(x)*var(y)))
    [1] 0.6