# Why does shuffling rows change the estimates?

**URL:** <https://discourse.julialang.org/t/why-does-shuffling-rows-change-the-estimates/52305>\
**Category:** General Usage\
**Tags:** question, turing\
**Created:** [December 23, 2020, 11:14pm UTC](https://discourse.julialang.org/t/why-does-shuffling-rows-change-the-estimates/52305 "2020-12-23T23:14:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [December 23, 2020, 11:14pm UTC](https://discourse.julialang.org/t/why-does-shuffling-rows-change-the-estimates/52305/1 "2020-12-23T23:14:11Z")

</div>

This is a small model with fake data in a linear regression. Unexpectedly, shuffling the rows of the dataframe changes the estimates.

```julia
import Random
using DataFrames
using Pipe: @pipe
using StatsBase: StatsBase, mad, median, percentile, sample, shuffle

using Distributions
using Turing

Random.seed!(1)
##

df, parameters = let
    β = (
        β_0 = 7,
        β_1 = 0.05,
        β_2 = 0.10,
        β_3 = 0.15,
        β_4 = 0.20,
    )
    σ = 2
    parameters = (β..., σ=σ)
    parameters_df = DataFrame(
        parameter=collect(keys(parameters)),
        value=collect(values(parameters)),
    )

    N = 100_000
    X = DataFrame(
        x_0=fill(1, N),
        x_1=rand([1,2], N),
        x_2=rand(1:1:10, N),
        x_3=rand([0,1], N),
        x_4=rand(0:1:10, N),
    )
    df = transform(X)
    μ = Matrix(df) * collect(β)
    ϵ = rand(Normal(0, σ), N)
    y = μ .+ ϵ

    (df = transform(
        X,
        [] => (() -> μ) => :μ,
        [] => (() -> ϵ) => :ϵ,
        [] => (() -> y) => :y,
        ),
     parameters = parameters_df, )
end

##
@model function linear_outcome_model(X, y)
    β_0 ~ Normal(mean(y), 2std(y))
    β_1 ~ Normal(0, .5)
    β_2 ~ Normal(0, .5)
    β_3 ~ Normal(0, .5)
    β_4 ~ Normal(0, .5)

    μ = (
            β_0 .* X.x_0
            .+ β_1 .* X.x_1
            .+ β_2 .* X.x_2
            .+ β_3 .* X.x_3
            .+ β_4 .* X.x_4

    )
    σ ~ truncated(Normal(0, 2std(y)), 0, Inf)
    y ~ MvNormal(μ, σ)
end

fields = [
    :x_0,
    :x_1,
    :x_2,
    :x_3,
    :x_4,

]

samples_original = @pipe df |>
linear_outcome_model(_[:, fields], _.y) |>
sample(_, NUTS(0.65), 3000)

samples_shuffled = @pipe df |>
DataFrame(shuffle(eachrow(_))) |>
linear_outcome_model(_[:, fields], _.y) |>
sample(_, NUTS(0.65), 3000)

function compare_samples(samples)
    @pipe DataFrame(summarize(samples, mean, std)) |>
    rename(_, :parameters => :parameter) |>
    innerjoin(_, parameters, on=:parameter)
end

comparison_original = compare_samples(samples_original)

#comparison_original
#6×4 DataFrame
# Row │ parameter mean std value 
# │ Symbol Float64 Float64 Real  
#─────┼─────────────────────────────────────────
# 1 │ β_0 6.97052 0.0266058 7
# 2 │ β_1 0.0417781 0.0130792 0.05
# 3 │ β_2 0.101241 0.00227608 0.1
# 4 │ β_3 0.170157 0.012559 0.15
# 5 │ β_4 0.203193 0.00201488 0.2
# 6 │ σ 1.99352 0.00446498 2

comparison_shuffled = compare_samples(samples_shuffled)

# 6×4 DataFrame
# Row │ parameter mean std value 
# │ Symbol Float64 Float64 Real  
# ─────┼───────────────────────────────────────────
# 1 │ β_0 8.70263 0.0270448 7
# 2 │ β_1 -0.00431323 0.0133114 0.05
# 3 │ β_2 -0.00108792 0.00232939 0.1
# 4 │ β_3 0.0106373 0.0134052 0.15
# 5 │ β_4 -0.00122068 0.00215966 0.2
# 6 │ σ 2.11683 0.0047302 2
```

---

<div class="post-metadata">

**Author:** ![cpfiffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpfiffer/32/208747_2.png) [@cpfiffer](https://discourse.julialang.org/u/cpfiffer)\
**Post date:** [December 24, 2020, 4:34am UTC](https://discourse.julialang.org/t/why-does-shuffling-rows-change-the-estimates/52305/2 "2020-12-24T04:34:57Z")

</div>

Try setting a seed with `Random.seed!` first – I think this is a seeding thing and has nothing to do with shuffling.

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [December 24, 2020, 4:50am UTC](https://discourse.julialang.org/t/why-does-shuffling-rows-change-the-estimates/52305/3 "2020-12-24T04:50:11Z")

</div>

Unfortunately that doesn’t do it. I still get wrong results when shuffling and right results otherwise.

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [December 25, 2020, 7:15am UTC](https://discourse.julialang.org/t/why-does-shuffling-rows-change-the-estimates/52305/4 "2020-12-25T07:15:44Z")

</div>

Turns out I was using `@pipe` wrong. Switching to `@chain` solves the problem.
