# Spearman Correlation, How do I find rho?

**URL:** <https://discourse.julialang.org/t/spearman-correlation-how-do-i-find-rho/36291>\
**Category:** Statistics\
**Created:** [March 21, 2020, 5:43am UTC](https://discourse.julialang.org/t/spearman-correlation-how-do-i-find-rho/36291 "2020-03-21T05:43:49Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [March 21, 2020, 5:43am UTC](https://discourse.julialang.org/t/spearman-correlation-how-do-i-find-rho/36291/1 "2020-03-21T05:43:49Z")

</div>

```julia
B= df.Crude_Birth_Rate

Employment_df = DataFrame(A=df.Female_Employment,B=df.Crude_Birth_Rate, )corspearman(x=A, y=B) = cor(tiedrank(A), tiedrank(B))

```

I’m trying to do a Spearman Correlation Test, because I know from my regression test, that the data is polynomial, not linear. But, when I run the test, it just says corspearman, how do I enter the variables from my dataframe, into the test?

---

<div class="post-metadata">

**Author:** ![junder873](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@junder873](https://discourse.julialang.org/u/junder873)\
**Post date:** [March 22, 2020, 10:52pm UTC](https://discourse.julialang.org/t/spearman-correlation-how-do-i-find-rho/36291/2 "2020-03-22T22:52:01Z")

</div>

I am not aware of a package that implements p-values of correlation tests (this [stackoverflow](https://stackoverflow.com/questions/53345724/how-to-use-julia-to-compute-the-pearson-correlation-coefficient-with-p-value) question and answer might be useful though).

If your main question is how to calculate the correlation, just put the vectors into the function:

```julia
corspearman(df.Female_Employment, df.Crude_Birth_Rate)

```

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [March 23, 2020, 3:30am UTC](https://discourse.julialang.org/t/spearman-correlation-how-do-i-find-rho/36291/3 "2020-03-23T03:30:31Z")

</div>

Yeah that was a mistake, I just want to know how to find rho.

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [March 24, 2020, 12:56am UTC](https://discourse.julialang.org/t/spearman-correlation-how-do-i-find-rho/36291/4 "2020-03-24T00:56:41Z")

</div>

Ok I entered it the way you did and it worked.

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [March 24, 2020, 12:58am UTC](https://discourse.julialang.org/t/spearman-correlation-how-do-i-find-rho/36291/5 "2020-03-24T00:58:13Z")

</div>

What would I have to do to run a Kendall tests

```julia
corkendall(df.Female_Employment, df.Crude_Birth_Rate)

```

```julia
`float` not defined on abstractly-typed arrays; please convert to a more specific type
error(::String) at error.jl:33
float(::Array{Real,1}) at float.jl:894
corkendall(::Array{Real,1}, ::Array{Real,1}) at rankcorr.jl:103
top-level scope at FemLabour.jl:29

```

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [March 24, 2020, 12:58am UTC](https://discourse.julialang.org/t/spearman-correlation-how-do-i-find-rho/36291/6 "2020-03-24T00:58:51Z")

</div>

I’m thinking I need to change the number type.

---

<div class="post-metadata">

**Author:** ![junder873](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@junder873](https://discourse.julialang.org/u/junder873)\
**Post date:** [March 24, 2020, 1:57am UTC](https://discourse.julialang.org/t/spearman-correlation-how-do-i-find-rho/36291/7 "2020-03-24T01:57:16Z")

</div>

What are the types of the dataframe columns? If you type in the name of the DataFrame in the REPL and press enter, the top of each column will be the types.

You can also see the types of the columns by using the describe function, `describe(df)`

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [March 24, 2020, 3:26am UTC](https://discourse.julialang.org/t/spearman-correlation-how-do-i-find-rho/36291/8 "2020-03-24T03:26:06Z")

</div>

Real

---

<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:** [March 24, 2020, 8:17am UTC](https://discourse.julialang.org/t/spearman-correlation-how-do-i-find-rho/36291/9 "2020-03-24T08:17:39Z")

</div>

It looks like you’re running into trouble with typing from XLSX again - I would therefore again suggest you put your tabluar data in csv format, which will make life a lot easier.

Apart from that you can try `corkendall(Float64.(df.Female_Employment), Float64.(df.Crude_Birth_Rate))`

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [March 24, 2020, 1:31pm UTC](https://discourse.julialang.org/t/spearman-correlation-how-do-i-find-rho/36291/10 "2020-03-24T13:31:36Z")

</div>

I’d have to get the CSV to work, besides I’m using Excel at the same time, and doing charts on Excel instead of plots.

---

<div class="post-metadata">

**Author:** ![junder873](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@junder873](https://discourse.julialang.org/u/junder873)\
**Post date:** [March 24, 2020, 5:54pm UTC](https://discourse.julialang.org/t/spearman-correlation-how-do-i-find-rho/36291/11 "2020-03-24T17:54:29Z")

</div>

If it still is not working, can you post some sample data and the code you are using so we can better help you?

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [March 24, 2020, 6:16pm UTC](https://discourse.julialang.org/t/spearman-correlation-how-do-i-find-rho/36291/12 "2020-03-24T18:16:33Z")

</div>

> [@nilshg](#):
>
> `corkendall(Float64.(df.Female_Employment), Float64.(df.Crude_Birth_Rate))`

Sorry, yes I got it fixed.

---

<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:** [March 25, 2020, 7:43am UTC](https://discourse.julialang.org/t/spearman-correlation-how-do-i-find-rho/36291/13 "2020-03-25T07:43:31Z")

</div>

“Getting the csv to work” is as easy as doing “Save as” in Excel and changing the file format to csv. That obviously doesn’t preclude you from keeping an xlsx version around if you want to make charts in Excel, although I’d say here’s a great opportunity to learn how to make Plots in Julia 🙂

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [March 25, 2020, 6:29pm UTC](https://discourse.julialang.org/t/spearman-correlation-how-do-i-find-rho/36291/14 "2020-03-25T18:29:53Z")

</div>

True, I probably will learn plots next thing. I already started doing some.

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [March 25, 2020, 8:16pm UTC](https://discourse.julialang.org/t/spearman-correlation-how-do-i-find-rho/36291/15 "2020-03-25T20:16:42Z")

</div>

Right now I need to export a matrix to Excel. I would like to do this as an XLSX, but a CSV should work too.

---

<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:** [March 26, 2020, 6:12am UTC](https://discourse.julialang.org/t/spearman-correlation-how-do-i-find-rho/36291/16 "2020-03-26T06:12:31Z")

</div>

You can export it via XLSX, see the docs [here](https://felipenoris.github.io/XLSX.jl/stable/tutorial/#Create-New-Files-1).

Alternatively, you can just wrap your Matrix in a `DataFrame` and write it with `CSV.jl`:

```julia
julia> using CSV, DataFrames

julia> my_matrix = rand(5, 5); 

julia> CSV.write("C:/Users/brett/OneDrive/Documents/Soc323/test.csv", my_matrix)

```

You can of course then open this csv with Excel and save it in XLSX format from within Excel if you need that.

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [March 26, 2020, 4:15pm UTC](https://discourse.julialang.org/t/spearman-correlation-how-do-i-find-rho/36291/17 "2020-03-26T16:15:59Z")

</div>

```julia
ERROR: ArgumentError: a 'Array{Real,2}' is not a table; see `?Tables.table` for ways to treat an AbstractMatrix as a table

```

I tried to look up Tables.table in help, but coludn’t find anything. How do I change this into a usable table?

---

<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:** [March 27, 2020, 8:26am UTC](https://discourse.julialang.org/t/spearman-correlation-how-do-i-find-rho/36291/18 "2020-03-27T08:26:57Z")

</div>

My apologies, I screwed up my own MWE above: I added `using DataFrames` at the start clearly because I was going to actually use it in the example…

As the error says, a `Matrix` is not a `Table`, so you need to convert it into one - an easy way to do it is to pass it to the `DataFrame` constructor. Replace the second line with:

```julia
julia> my_matrix = DataFrame(rand(5, 5))

```
