Skip to content
This repository has been archived by the owner on May 4, 2019. It is now read-only.

Correlation that handles NA #236

Open
Unoqualsiasi opened this issue Feb 13, 2017 · 0 comments
Open

Correlation that handles NA #236

Unoqualsiasi opened this issue Feb 13, 2017 · 0 comments

Comments

@Unoqualsiasi
Copy link

Unoqualsiasi commented Feb 13, 2017

@johnmyleswhite maybe it's useful to someone i don't know. This function handles NA.

function PearsonCorr(x::DataArray, y::DataArray)
    Corr = 0.0
    Xavg = 0.0
    Yavg = 0.0
    Xavg2 = 0.0
    Yavg2 = 0.0
    covXY = 0.0
    n = 0.0
    for i in 1:length(x)
        if !isna(x,i) && !isna(y,i)
            n +=1
            Xavg += x[i]
            Yavg += y[i]
            Xavg2 += x[i] * x[i]
            Yavg2 += y[i] * y[i]
            covXY += x[i] * y[i]
        end
    end
    Xavg /=  n
    Yavg /=  n
    covXY = covXY - n * Xavg * Yavg
    Xstd = Xavg2 - n * Xavg * Xavg
    Ystd = Yavg2 - n * Yavg * Yavg
    Corr = covXY / sqrt(Xstd * Ystd)
    return Corr
end
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant