# Randomized Hypothesis Test (row-level analysis): DimensionMismatch ERROR

**URL:** <https://discourse.julialang.org/t/randomized-hypothesis-test-row-level-analysis-dimensionmismatch-error/70663>\
**Category:** New to Julia\
**Tags:** statistics, combinatorics\
**Created:** [October 30, 2021, 2:49pm UTC](https://discourse.julialang.org/t/randomized-hypothesis-test-row-level-analysis-dimensionmismatch-error/70663 "2021-10-30T14:49:58Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![YummyPampers2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yummypampers2/32/27328_2.png) [@YummyPampers2](https://discourse.julialang.org/u/YummyPampers2)\
**Post date:** [October 30, 2021, 2:49pm UTC](https://discourse.julialang.org/t/randomized-hypothesis-test-row-level-analysis-dimensionmismatch-error/70663/1 "2021-10-30T14:49:58Z")

</div>

Hello Everyone,

Using = Pluto 0.17.\*

```julia
using Combinatorics, StatsBase

```

Am working with the following DF

```julia
P1 = DataFrame(Col1 = rand(1:5:100,10), Col2 = rand(1:3:150,10), Col3=rand(1:4:250,10)

```

and another dataframe where the rows  
represent the Mean of P1.

```julia
P2 = DataFrame(Mean = [100,120,125,115,120,110,100,115,130,120]...)

```

I imported and stored my control and series groups

```julia
control = P2.Mean
Col1 = collect(P1[1,:])
Col2 = collect(P1[2,:])
Col3 = collect(P1[3,:])

```

I then concatenated and combined all observations into one array

```julia
subGroups = collect(combinations([control;Col1; Col2;Col3],3))

```

3 represents the number of observations (across the columns)

I calculated the mean for all my records

```julia
meanCol1 = mean(Col1)
meanCol2 = mean(Col2)
meanCol3 = mean(Col3)

```

I encounter an error when I attempt to  
find the p-value for each record using

```julia
pVAL1 = sum([mean(i) >= meanCol1 for i in subGroups])/ length(subGroups)
pVAL2 = sum([mean(i) >= meanCol2 for i in subGroups])/length(subGroups)
pVAL3 = sum([mean(i) >= meanCol3 for i in subGroups])/length(subGroups)

```

The error reads:

```julia
DimensionMismatch("dimensions must match: a has dims (Base.OneTo(10),), b has dims (Base.OneTo(3),), mismatch at 1")

```

If you need the Stacktrace let me know.  
Any suggestions?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 30, 2021, 2:56pm UTC](https://discourse.julialang.org/t/randomized-hypothesis-test-row-level-analysis-dimensionmismatch-error/70663/2 "2021-10-30T14:56:38Z")

</div>

> [@YummyPampers2](#):
>
> ```julia
> subGroups = collect(combinations([control;Col1, Col2, Col3],6))
> 
> ```

```julia

help?> combinations
search:

Couldn't find combinations
Perhaps you meant combine
  No documentation found.

  Binding combinations does not exist.

```

Where does `combinations` come from? Hard to understand issue without an MWE.

```julia
MethodError: no method matching +(::Float64, ::DataFrames.DataFrameRow{DataFrames.DataFrame, DataFrames.Index})

```

This is an intuitive if you are familiar with the fact that `1 + [1, 2, 3]` errors. In Julia, you can’t add scalars and vectors. Instead, you have to broadcast the operation, `1 .+ [1, 2, 3]`. The same applies for `DataFrameRow`s.

---

<div class="post-metadata">

**Author:** ![YummyPampers2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yummypampers2/32/27328_2.png) [@YummyPampers2](https://discourse.julialang.org/u/YummyPampers2)\
**Post date:** [October 30, 2021, 2:58pm UTC](https://discourse.julialang.org/t/randomized-hypothesis-test-row-level-analysis-dimensionmismatch-error/70663/3 "2021-10-30T14:58:40Z")

</div>

I did not include:

```julia
using Combinatorics, StatsBase

```

---

<div class="post-metadata">

**Author:** ![YummyPampers2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yummypampers2/32/27328_2.png) [@YummyPampers2](https://discourse.julialang.org/u/YummyPampers2)\
**Post date:** [October 30, 2021, 3:01pm UTC](https://discourse.julialang.org/t/randomized-hypothesis-test-row-level-analysis-dimensionmismatch-error/70663/4 "2021-10-30T15:01:52Z")

</div>

Also, might you also suggest converting  
my DF into a matrix to avoid  
broadcasting?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 30, 2021, 3:02pm UTC](https://discourse.julialang.org/t/randomized-hypothesis-test-row-level-analysis-dimensionmismatch-error/70663/5 "2021-10-30T15:02:37Z")

</div>

> [@YummyPampers2](#):
>
> ```julia
> P1 = DataFrame(Col1 = rand(1:5:100,10), Col2 = rand(1:3:150,10), Col3=rand(1:4:250,10)
> 
> ```

No, that will not fix your problem because you need broadcasting for array operations as well, as I stated above

> [@pdeffebach](#):
>
> This is an intuitive if you are familiar with the fact that `1 + [1, 2, 3]` errors. In Julia, you can’t add scalars and vectors. Instead, you have to broadcast the operation, `1 .+ [1, 2, 3]` .

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 30, 2021, 3:03pm UTC](https://discourse.julialang.org/t/randomized-hypothesis-test-row-level-analysis-dimensionmismatch-error/70663/6 "2021-10-30T15:03:47Z")

</div>

```julia
julia> subGroups = collect(combinations([control;Col1, Col2, Col3],6))
       
ERROR: syntax: unexpected comma in matrix expression
Stacktrace:
 [1] top-level scope
   @ none:1

```

This code errors as well. It’s important you provide a working MWE, preferrably as a single block of code, to get the best help.

---

<div class="post-metadata">

**Author:** ![YummyPampers2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yummypampers2/32/27328_2.png) [@YummyPampers2](https://discourse.julialang.org/u/YummyPampers2)\
**Post date:** [October 30, 2021, 3:05pm UTC](https://discourse.julialang.org/t/randomized-hypothesis-test-row-level-analysis-dimensionmismatch-error/70663/7 "2021-10-30T15:05:59Z")

</div>

I provided the exact implementation of code above.  
This is not a MWE?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 30, 2021, 3:08pm UTC](https://discourse.julialang.org/t/randomized-hypothesis-test-row-level-analysis-dimensionmismatch-error/70663/8 "2021-10-30T15:08:30Z")

</div>

Your code errors in a way that is _different_ than the error you posted in the first post. So it is not an MWE because it doesn’t reproduce the error you would like help with.

---

<div class="post-metadata">

**Author:** ![YummyPampers2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yummypampers2/32/27328_2.png) [@YummyPampers2](https://discourse.julialang.org/u/YummyPampers2)\
**Post date:** [October 30, 2021, 3:14pm UTC](https://discourse.julialang.org/t/randomized-hypothesis-test-row-level-analysis-dimensionmismatch-error/70663/9 "2021-10-30T15:14:09Z")

</div>

Understood.

I re-wrote my original post. And added  
P2 values to represent the mean of  
each record in P1.

Is this better?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 30, 2021, 3:17pm UTC](https://discourse.julialang.org/t/randomized-hypothesis-test-row-level-analysis-dimensionmismatch-error/70663/10 "2021-10-30T15:17:38Z")

</div>

No. Read my message above. The problem is with the line

```julia
julia> subGroups = collect(combinations([control;Col1, Col2, Col3],6))

```

> [@pdeffebach](#):
>
> ```julia
> julia> subGroups = collect(combinations([control;Col1, Col2, Col3],6))
>        
> ERROR: syntax: unexpected comma in matrix expression
> Stacktrace:
> [1] top-level scope
> @ none:1
> 
> ```
> 
> This code errors as well. It’s important you provide a working MWE, preferrably as a single block of code, to get the best help.

Did you run this code before you posted it?

---

<div class="post-metadata">

**Author:** ![YummyPampers2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yummypampers2/32/27328_2.png) [@YummyPampers2](https://discourse.julialang.org/u/YummyPampers2)\
**Post date:** [October 30, 2021, 3:21pm UTC](https://discourse.julialang.org/t/randomized-hypothesis-test-row-level-analysis-dimensionmismatch-error/70663/11 "2021-10-30T15:21:16Z")

</div>

Oh okay!

The semi-colon needs to be changed to a (,).  
I will make this change above now.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 30, 2021, 3:28pm UTC](https://discourse.julialang.org/t/randomized-hypothesis-test-row-level-analysis-dimensionmismatch-error/70663/12 "2021-10-30T15:28:15Z")

</div>

I believe you have your indexing wrong in creating your `Col1`, `Col2`, and `Col3` objects.

Do get a _column_ you need to do

```julia
Col1 = P1[:,1]

```

_not_

```julia
Col1 = P1[1,:]

```

which is what you are currently doing. Your current indexing selects the first row, not the first column.

---

<div class="post-metadata">

**Author:** ![YummyPampers2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yummypampers2/32/27328_2.png) [@YummyPampers2](https://discourse.julialang.org/u/YummyPampers2)\
**Post date:** [October 30, 2021, 3:43pm UTC](https://discourse.julialang.org/t/randomized-hypothesis-test-row-level-analysis-dimensionmismatch-error/70663/13 "2021-10-30T15:43:10Z")

</div>

Yes – I am analyzing across the rows in this  
example. (I will add to the example)

I need to convert the DataFrameRow type into  
a vector.

I found [this thread](https://discourse.julialang.org/t/cleanest-way-to-convert-dataframe-row-into-a-vector/5531)

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 30, 2021, 3:47pm UTC](https://discourse.julialang.org/t/randomized-hypothesis-test-row-level-analysis-dimensionmismatch-error/70663/14 "2021-10-30T15:47:05Z")

</div>

Just use `collect`.

```julia
julia> Col3 = P1[3,:]
DataFrameRow
 Row │ Col1 Col2 Col3  
     │ Int64 Int64 Int64 
─────┼─────────────────────
   3 │ 16 64 5

julia> collect(Col3)
3-element Vector{Int64}:
 16
 64
  5

```

---

<div class="post-metadata">

**Author:** ![YummyPampers2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yummypampers2/32/27328_2.png) [@YummyPampers2](https://discourse.julialang.org/u/YummyPampers2)\
**Post date:** [October 30, 2021, 4:38pm UTC](https://discourse.julialang.org/t/randomized-hypothesis-test-row-level-analysis-dimensionmismatch-error/70663/17 "2021-10-30T16:38:27Z")

</div>

@pdeffebach

The issue I am encountering now stems from  
the control = P2.Mean, step. When I apply the  
collect() method, I get the same error. I also  
attempted to (.) broadcast, but am still seeing  
the DimensionMismatch Error.

Any suggestions?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 30, 2021, 5:04pm UTC](https://discourse.julialang.org/t/randomized-hypothesis-test-row-level-analysis-dimensionmismatch-error/70663/18 "2021-10-30T17:04:22Z")

</div>

I cannot debug your program line-by-line for you, unfortunately.

I suggest you work through more [Julia tutorials](https://julialang.org/learning/tutorials/) to learn how to better navigate these errors.

---

<div class="post-metadata">

**Author:** ![YummyPampers2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yummypampers2/32/27328_2.png) [@YummyPampers2](https://discourse.julialang.org/u/YummyPampers2)\
**Post date:** [November 1, 2021, 3:47pm UTC](https://discourse.julialang.org/t/randomized-hypothesis-test-row-level-analysis-dimensionmismatch-error/70663/20 "2021-11-01T15:47:49Z")

</div>

@pdeffebach

Thanks for your time and sharing the resource.

I did not want you to debug the code. But  
the solutions you presented created NEW  
challenges that no longer were of my  
construction, but rather yours.

**Solution:**

1. Reconstruct the DF from the source so that  
it was not a cross-tab but single-attribute DF.
2. I used semi-colons in the combination()  
method to concatenate all array elements.
