# Problems with tuples in Statistics function

**URL:** <https://discourse.julialang.org/t/problems-with-tuples-in-statistics-function/60754>\
**Category:** Statistics\
**Created:** [May 8, 2021, 2:19am UTC](https://discourse.julialang.org/t/problems-with-tuples-in-statistics-function/60754 "2021-05-08T02:19:24Z")\
**Posts on this page:** 20\
**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:** [May 8, 2021, 2:19am UTC](https://discourse.julialang.org/t/problems-with-tuples-in-statistics-function/60754/1 "2021-05-08T02:19:24Z")

</div>

```julia
using Statistics
using Distributions
x=[1,2,3,4,5,6,7,8]
y=[8,4,5,5,6,5,6,5]
function TwoSampleT2Test(X,Y)
    nx, p = size(X)
    ny, _ = size(Y)
    δ = mean(X, dims=1) - mean(Y, dims=1)
    Sx = cov(X)
    Sy = cov(Y)
    S_pooled = ((nx-1)*Sx + (ny-1)*Sy)/(nx+ny-2)
    t_squared = (nx*ny)/(nx+ny) * δ * inv(S_pooled) * transpose(δ)
    statistic = t_squared[1,1] * (nx+ny-p-1)/(p*(nx+ny-2))
    F = FDist(p, nx+ny-p-1)
	p_value= 1 - pvalue(Kolmogorov(), sqrt(x.n)*x.δ; tail=:right)
    println("Test statistic: $(statistic)\nDegrees of freedom: $(p) and $(nx+ny-p-1)\np-value: $(p_value)")
    return([statistic, p_value])
end
TwoSampleT2Test(x,y)

```

I get a bounds error, something to do with selecting an invalid value of a tuple. Is there a problem with the function, or is it the array/tuple that I was using as a an example?

I also tried to use Rdatasets, but

```julia
using Rdatasets
iris = dataset("datasets", "iris")

versicolor = convert(Matrix, iris[iris.Species .== "versicolor", 1:2])

virginica = convert(Matrix, iris[iris.Species .== "virginica", 1:2])

```

gave me a multiple definition error in Pluto.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [May 8, 2021, 3:54am UTC](https://discourse.julialang.org/t/problems-with-tuples-in-statistics-function/60754/2 "2021-05-08T03:54:59Z")

</div>

please post your code like they are code

---

<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:** [May 8, 2021, 5:18am UTC](https://discourse.julialang.org/t/problems-with-tuples-in-statistics-function/60754/3 "2021-05-08T05:18:26Z")

</div>

sorry I used quotation marks by mistake.

---

<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:** [May 8, 2021, 8:54am UTC](https://discourse.julialang.org/t/problems-with-tuples-in-statistics-function/60754/4 "2021-05-08T08:54:11Z")

</div>

Your error has nothing to do with statistics. The MWE would be:

```julia
julia> x=[1,2,3,4,5,6,7,8]
8-element Vector{Int64}:
 1
 2
 3
 4
 5
 6
 7
 8

julia> nx, p = size(x)
ERROR: BoundsError: attempt to access Tuple{Int64} at index [2]

```

You are calling `size` on a vector, which gives you back a 1-element tuple (as a vector is one-dimensional, it has only length). You then try to destructure that by assigning it to two variables (`nx, p`) but that doesn’t work as there’s only one element in the tuple.

---

<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:** [May 8, 2021, 4:08pm UTC](https://discourse.julialang.org/t/problems-with-tuples-in-statistics-function/60754/5 "2021-05-08T16:08:20Z")

</div>

Ok, do I need to assign the tuple as an array? How do I fix this?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [May 8, 2021, 4:09pm UTC](https://discourse.julialang.org/t/problems-with-tuples-in-statistics-function/60754/6 "2021-05-08T16:09:23Z")

</div>

what do you expect `nx` and `p` to be respectively?

---

<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:** [May 8, 2021, 4:38pm UTC](https://discourse.julialang.org/t/problems-with-tuples-in-statistics-function/60754/7 "2021-05-08T16:38:42Z")

</div>

I don’t know, I was trying to get someone elses function to work. I instead found tho Hotelling Test in HypothesisTests.jl , but I’m not sure how to enter it.

```julia
using HypothesisTests

x = randn(100_000); y = randn(100_000);
BartlettTest(x,y) #not the Hotelling test, but I need to understand how to use matrix variables first

```

tells me that there is no method matching. Instead i need to use an abstract matrix.  
I know that a matirx is an array of arrays, and that an array is a matrix with 1 row, but I’m not sure if this is a formatting problem, ie. do I need to change type, or arrange both x and y into a single maxrix ?

Or, if I need more tests.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [May 8, 2021, 4:53pm UTC](https://discourse.julialang.org/t/problems-with-tuples-in-statistics-function/60754/8 "2021-05-08T16:53:42Z")

</div>

> [@brett\_knoss](#):
>
> a matirx is an array of arrays

no, a `Matrix` is a `Array` with dimension equals 2.

```julia
julia> Matrix
Matrix{T} where T (alias for Array{T, 2} where T)

```

* * *

> [@brett\_knoss](#):
>
> and that an array is a matrix with 1 row,

also no, a `Vector` is probably what you’re thinking here and that is a column vector, not a row vector:

```julia
julia> a = rand(3)
3-element Vector{Float64}:
 0.9434075399317472
 0.42470401598192176
 0.4838410682979659

julia> a' * a
1.3044934669629604

```

---

<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:** [May 9, 2021, 7:06pm UTC](https://discourse.julialang.org/t/problems-with-tuples-in-statistics-function/60754/9 "2021-05-09T19:06:23Z")

</div>

I took linear algebra, so I should know this, but what does a\_prime and a mean?

---

<div class="post-metadata">

**Author:** ![AndiMD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andimd/32/6531_2.png) [@AndiMD](https://discourse.julialang.org/u/AndiMD)\
**Post date:** [May 9, 2021, 7:37pm UTC](https://discourse.julialang.org/t/problems-with-tuples-in-statistics-function/60754/10 "2021-05-09T19:37:36Z")

</div>

`a'` is short for `adjoint(a)`.  
You can find this information in the REPL help with `?something`, for example `?'`.

---

<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:** [May 9, 2021, 8:35pm UTC](https://discourse.julialang.org/t/problems-with-tuples-in-statistics-function/60754/11 "2021-05-09T20:35:38Z")

</div>

So is there a way to put two arrays into a two-dimensional matrix?

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [May 9, 2021, 9:31pm UTC](https://discourse.julialang.org/t/problems-with-tuples-in-statistics-function/60754/12 "2021-05-09T21:31:50Z")

</div>

If you mean to put the first as first colum and the second as the secon colum it is `hcat(vector1,vector2)`

Have a look on my tutorial: [2 - Data types - Julia language: a concise tutorial](https://syl1.gitbook.io/julia-language-a-concise-tutorial/language-core/data-types)

---

<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:** [May 9, 2021, 11:15pm UTC](https://discourse.julialang.org/t/problems-with-tuples-in-statistics-function/60754/13 "2021-05-09T23:15:43Z")

</div>

OK, I got this, now I get that a zero vector is a point, but is mu[0] a common mean, between two ranges?

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [May 10, 2021, 6:16am UTC](https://discourse.julialang.org/t/problems-with-tuples-in-statistics-function/60754/14 "2021-05-10T06:16:51Z")

</div>

Not sure here what you mean here . For “zero vector” do you mean a vecor of zeros or a empty vector . but in both point it is hard to interpret as a “point”.

`mu[0]` looks odds in Julia, as vector indices start from 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:** [May 10, 2021, 3:05pm UTC](https://discourse.julialang.org/t/problems-with-tuples-in-statistics-function/60754/15 "2021-05-10T15:05:13Z")

</div>

from [Juliastat.org](http://Juliastat.org)  
[Multivariate tests · HypothesisTests.jl](https://juliastats.org/HypothesisTests.jl/stable/multivariate/)

```julia
OneSampleHotellingT2Test(X::AbstractMatrix, Y::AbstractMatrix, μ₀=<zero vector>)

```

and here is my understanding of a zero vector

> **[The zero vector - Math Insight](https://mathinsight.org/zero_vector)**
>
> The zero vector is the unique vector having zero length. The direction of the zero vector is undefined.

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [May 10, 2021, 4:39pm UTC](https://discourse.julialang.org/t/problems-with-tuples-in-statistics-function/60754/16 "2021-05-10T16:39:01Z")

</div>

The line from the docs below tells you what you need to know. Where it says `μ₀=<zero vector>`, that means that the default value of `μ₀` if not specified, is a vector containing all zeros ([Functions · The Julia Language](https://docs.julialang.org/en/v1/manual/functions/#Optional-Arguments)). Since the docs don’t tell you what the function returns, click on the source link and you can clearly see that it returns an instance of the `OneSampleHotellingT2Test` struct.

> test of the hypothesis that the vector of mean column differences between `X` and `Y` is equal to `μ₀` .

---

<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:** [May 10, 2021, 4:43pm UTC](https://discourse.julialang.org/t/problems-with-tuples-in-statistics-function/60754/17 "2021-05-10T16:43:01Z")

</div>

Ok, I think I get that, so what do I enter there? An array of N number of zeros?

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [May 10, 2021, 4:45pm UTC](https://discourse.julialang.org/t/problems-with-tuples-in-statistics-function/60754/18 "2021-05-10T16:45:20Z")

</div>

You pass a vector that you want to test the mean column differences against. That’s up to your problem. If you want to test whether the column means are equal, ie., their differences are equal, don’t pass anything so that it uses the default value.

---

<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:** [May 10, 2021, 7:39pm UTC](https://discourse.julialang.org/t/problems-with-tuples-in-statistics-function/60754/19 "2021-05-10T19:39:50Z")

</div>

OK, I got that, so what would I enter, so that it knows I want default?

---

<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:** [May 10, 2021, 7:49pm UTC](https://discourse.julialang.org/t/problems-with-tuples-in-statistics-function/60754/20 "2021-05-10T19:49:18Z")

</div>

The default value for a keyword argument is the value that the argument will take is it is not supplied when the function is called. This is the same as what I showed you with `tail` in the p value calculations before.

[Next page](https://discourse.julialang.org/t/problems-with-tuples-in-statistics-function/60754.md?page=2)
