# StatsBase.jl sample size \> 31 throws error

**URL:** https://discourse.julialang.org/t/statsbase-jl-sample-size-31-throws-error/34484
**Category:** Statistics
**Created:** [February 11, 2020, 7:17pm UTC](https://discourse.julialang.org/t/statsbase-jl-sample-size-31-throws-error/34484 "2020-02-11T19:17:36Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)
#### Post date: [February 11, 2020, 7:17pm UTC](https://discourse.julialang.org/t/statsbase-jl-sample-size-31-throws-error/34484/1 "2020-02-11T19:17:36Z")

</div>

I’m running into an issue that I’m hoping someone can help me understand. I have a 1308 x 22 DataFrame that I’m trying to sample from with StatsBase. All is well and fine as long as `n <= 31`.

The code looks like this:

```julia
julia> df
1308×22 DataFrame. Omitted printing of 8 columns
...

julia> wv = FrequencyWeights(df.PWORWGT)
1308-element FrequencyWeights{Int64,Int64,Array{Int64,1}}:
...

julia> sample(axes(df,1), wv, 31)
31-element Array{Int64,1}:
...

# If I change 31 to 32, I get this:

julia> sample(axes(df,1), wv, 32)
ERROR: MethodError: no method matching make_alias_table!(::Array{Int64,1}, ::Int64, ::Array{Float64,1}, ::Array{Int64,1})
Closest candidates are:
  make_alias_table!(::AbstractArray{Float64,1}, ::Float64, ::AbstractArray{Float64,1}, ::AbstractArray{Int64,1}) at C:\Users\mthel\.julia\packages\StatsBase\DyWPR\src\sampling.jl:471
Stacktrace:
 [1] alias_sample!(::Random._GLOBAL_RNG, ::Base.OneTo{Int64}, ::FrequencyWeights{Int64,Int64,Array{Int64,1}}, ::Array{Int64,1}) at C:\Users\mthel\.julia\packages\StatsBase\DyWPR\src\sampling.jl:533
 [2] #sample!#153(::Bool, ::Bool, ::typeof(sample!), ::Random._GLOBAL_RNG, ::Base.OneTo{Int64}, ::FrequencyWeights{Int64,Int64,Array{Int64,1}}, ::Array{Int64,1}) at C:\Users\mthel\.julia\packages\StatsBase\DyWPR\src\sampling.jl:768
 [3] #sample#154 at .\none:0 [inlined]
 [4] #sample at .\none:0 [inlined]
 [5] #sample#155 at C:\Users\mthel\.julia\packages\StatsBase\DyWPR\src\sampling.jl:789 [inlined]
 [6] sample(::Base.OneTo{Int64}, ::FrequencyWeights{Int64,Int64,Array{Int64,1}}, ::Int64) at C:\Users\mthel\.julia\packages\StatsBase\DyWPR\src\sampling.jl:789
 [7] top-level scope at REPL[29]:1

```

Is this a bug that I should report, or am I missing something? If I remove the weight vector, it works fine, regardless of what number I pick for `n`.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [February 12, 2020, 6:51am UTC](https://discourse.julialang.org/t/statsbase-jl-sample-size-31-throws-error/34484/2 "2020-02-12T06:51:03Z")

</div>

An MWE would be helpful (you don’t need the `df`, since you just use its dimension information).

---

<div class="post-metadata">

### Author: ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)
#### Post date: [February 12, 2020, 12:45pm UTC](https://discourse.julialang.org/t/statsbase-jl-sample-size-31-throws-error/34484/3 "2020-02-12T12:45:37Z")

</div>

Here’s a MWE:

```julia
using DataFrames
using Random
using StatsBase

df=DataFrame(a=rand(1308), b=rand(1308), c=rand(1308), d=collect(1:2:2616))
wv = FrequencyWeights(df.d)

```

With `n=31` it works just fine:

```julia
julia> sample(axes(df,1), wv, 31)
31-element Array{Int64,1}:
  807
  283
 1219
  793
 1180
  966
  510
  997
  647
 1052
 1181
  952
    ⋮
 1012
  507
  994
  447
 1297
 1293
  768
  147
  667
 1248
  924
  477

```

At `n=32`, the error:

```julia
julia> sample(axes(df,1), wv, 32)
ERROR: MethodError: no method matching make_alias_table!(::Array{Int64,1}, ::Int64, ::Array{Float64,1}, ::Array{Int64,1})
Closest candidates are:
  make_alias_table!(::AbstractArray{Float64,1}, ::Float64, ::AbstractArray{Float64,1}, ::AbstractArray{Int64,1}) at C:\Users\mthel\.julia\packages\StatsBase\DyWPR\src\sampling.jl:471
Stacktrace:
 [1] alias_sample!(::Random._GLOBAL_RNG, ::Base.OneTo{Int64}, ::FrequencyWeights{Int64,Int64,Array{Int64,1}}, ::Array{Int64,1}) at C:\Users\mthel\.julia\packages\StatsBase\DyWPR\src\sampling.jl:533
 [2] #sample!#153(::Bool, ::Bool, ::typeof(sample!), ::Random._GLOBAL_RNG, ::Base.OneTo{Int64}, ::FrequencyWeights{Int64,Int64,Array{Int64,1}}, ::Array{Int64,1}) at C:\Users\mthel\.julia\packages\StatsBase\DyWPR\src\sampling.jl:768
 [3] #sample#154 at .\none:0 [inlined]
 [4] #sample at .\none:0 [inlined]
 [5] #sample#155 at C:\Users\mthel\.julia\packages\StatsBase\DyWPR\src\sampling.jl:789 [inlined]
 [6] sample(::Base.OneTo{Int64}, ::FrequencyWeights{Int64,Int64,Array{Int64,1}}, ::Int64) at C:\Users\mthel\.julia\packages\StatsBase\DyWPR\src\sampling.jl:789
 [7] top-level scope at REPL[16]:1

```

---

<div class="post-metadata">

### Author: ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)
#### Post date: [February 12, 2020, 12:52pm UTC](https://discourse.julialang.org/t/statsbase-jl-sample-size-31-throws-error/34484/4 "2020-02-12T12:52:16Z")

</div>

Nevermind, my brain was too exhausted yesterday. The error message tells me what I need to do:

```julia
 # changed column d to Float
df=DataFrame(a=rand(1308), b=rand(1308), c=rand(1308), d=collect(1.0:2.0:2616.0))
wv = FrequencyWeights(df.d)

```

Now,

```julia
julia> sample(axes(df,1), wv, 32)
32-element Array{Int64,1}:
  118
  996
 1255
 1148
 1071
  988
  573
  432
  700
 1067
  721
  842
    ⋮
  669
  615
  524
  896
 1147
 1277
 1020
 1042
 1190
 1236
 1084
 1021

```

Seems like it should just work though, regardless of whether or not my data are Int/Float.
