Weighted correlation in julia

Viewed 86

How can I run a weighted correlation in Julia? In Stata, you can run corr x y [aw=weight] to find correlations between columns x and y using weight as the weights. I can't find the same functionality in Julia.

1 Answers

This is the way to do it:

julia> using Statistics, StatsBase

julia> x = [1 2
            3 4
            1 -2
            3 -4
            5 -6]
5×2 Matrix{Int64}:
 1   2
 3   4
 1  -2
 3  -4
 5  -6

julia> cor(x, Weights([1,1,0,0,0]))
2×2 Matrix{Float64}:
 1.0  1.0
 1.0  1.0

julia> cor(x, Weights([0,0,1,1,1]))
2×2 Matrix{Float64}:
  1.0  -1.0
 -1.0   1.0
Related