# Whats the easiest way to create correlation matrices in Julia?

**URL:** <https://discourse.julialang.org/t/whats-the-easiest-way-to-create-correlation-matrices-in-julia/71006>\
**Category:** New to Julia\
**Tags:** question, statistics\
**Created:** [November 5, 2021, 1:34pm UTC](https://discourse.julialang.org/t/whats-the-easiest-way-to-create-correlation-matrices-in-julia/71006 "2021-11-05T13:34:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![SubTer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/subter/32/28830_2.png) [@SubTer](https://discourse.julialang.org/u/SubTer)\
**Post date:** [November 5, 2021, 1:34pm UTC](https://discourse.julialang.org/t/whats-the-easiest-way-to-create-correlation-matrices-in-julia/71006/1 "2021-11-05T13:34:05Z")

</div>

Basically what the topic title says - suppose I have an array/df thats 5 columns (5 variables) by 100 rows of data values.

I got it to do a good job doing covariance matrix output with the GLM package:

```julia
lm1 = lm(@formula(Y ~ X1 + X2 + X3 + X4), DataVarArray)

```

and then calling for vcov:

```julia
println("Covariance matrix: ",vcov(lm1))

```

but cant find a simple way to do a correlation matrix on same df… some pointers would be appreciated.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [November 5, 2021, 1:43pm UTC](https://discourse.julialang.org/t/whats-the-easiest-way-to-create-correlation-matrices-in-julia/71006/2 "2021-11-05T13:43:44Z")

</div>

You mean this?

```julia
julia> using DataFrames

julia> df = rand(10, 5);

julia> using Statistics

julia> cor(df)
5×5 Matrix{Float64}:
 1.0 0.480836 0.226642 0.361239 0.192271
 0.480836 1.0 0.0860495 0.841819 -0.113863
 0.226642 0.0860495 1.0 -0.0617229 -0.26865
 0.361239 0.841819 -0.0617229 1.0 0.158675
 0.192271 -0.113863 -0.26865 0.158675 1.0

```

---

<div class="post-metadata">

**Author:** ![SubTer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/subter/32/28830_2.png) [@SubTer](https://discourse.julialang.org/u/SubTer)\
**Post date:** [November 5, 2021, 1:50pm UTC](https://discourse.julialang.org/t/whats-the-easiest-way-to-create-correlation-matrices-in-julia/71006/3 "2021-11-05T13:50:00Z")

</div>

Yes - actually first thing I tried, but kept getting this error:

```julia
LoadError: MethodError: no method matching cor(::DataFrame)

```

so thought I am just not understanding that function… but your toy example works fine for me as well… hmm… wonder why it’s not happy with the matrix I tried passing it… i wonder if it’s because when i use hcat to put it together it adds a Row column?

---

<div class="post-metadata">

**Author:** ![SubTer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/subter/32/28830_2.png) [@SubTer](https://discourse.julialang.org/u/SubTer)\
**Post date:** [November 5, 2021, 1:53pm UTC](https://discourse.julialang.org/t/whats-the-easiest-way-to-create-correlation-matrices-in-julia/71006/4 "2021-11-05T13:53:34Z")

</div>

ahh got it! it really wants a “true” matrix…  
when i cast it as:

```julia
df1 = Matrix(VarArray)

```

and then pass it to cor(df1), it finally works as expected!

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [November 5, 2021, 2:08pm UTC](https://discourse.julialang.org/t/whats-the-easiest-way-to-create-correlation-matrices-in-julia/71006/5 "2021-11-05T14:08:13Z")

</div>

Ah yes sorry I cheated a bit above which probably added to your confusion. `rand(10, 5)` creates a matrix, which is what `cor` requires. A `DataFrame` isn’t an `AbstractMatrix` but as you found can be cast as one by just calling the `Matrix` constructor.

---

<div class="post-metadata">

**Author:** ![SubTer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/subter/32/28830_2.png) [@SubTer](https://discourse.julialang.org/u/SubTer)\
**Post date:** [November 5, 2021, 2:09pm UTC](https://discourse.julialang.org/t/whats-the-easiest-way-to-create-correlation-matrices-in-julia/71006/6 "2021-11-05T14:09:55Z")

</div>

all good, i learned something new today!
