# Didn't get consistent result of ANOVA from R and Julia

**URL:** https://discourse.julialang.org/t/didnt-get-consistent-result-of-anova-from-r-and-julia/26003
**Category:** General Usage
**Tags:** question, package
**Created:** [July 4, 2019, 7:53am UTC](https://discourse.julialang.org/t/didnt-get-consistent-result-of-anova-from-r-and-julia/26003 "2019-07-04T07:53:53Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Losses](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/losses/32/9175_2.png) [@Losses](https://discourse.julialang.org/u/Losses)
#### Post date: [July 4, 2019, 7:53am UTC](https://discourse.julialang.org/t/didnt-get-consistent-result-of-anova-from-r-and-julia/26003/1 "2019-07-04T07:53:53Z")

</div>

Hello, everyone, I’m a new user of Julia, previously using R/Python. I’m trying to run an ANOVA with Julia but got different result from R.

The test data is extracted from Jamovi: [test\_data.csv - Pastebin.com](https://pastebin.com/TgErRQuh)

The R code is:

```r
data <- read.csv('./data/test_data.csv')
data$cond <- as.factor(data$cond)
data$subj <- as.factor(data$subj)
anova(
  lm(formula = y ~ subj + cond + subj*cond, data = data)
)

```

The result is:

```julia
Analysis of Variance Table

Response: y
            Df Sum Sq Mean Sq F value Pr(>F)
subj 49 13872 283.10 22.1259 < 2.2e-16 ***
cond 1 706 705.54 55.1421 1.466e-13 ***
subj:cond 49 3827 78.11 6.1046 < 2.2e-16 ***
Residuals 2900 37105 12.79
---
Signif. codes: 0 ' ***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

```

Both Jamovi and Pure R returns the same result.

But while I’m trying to run an ANOVA in Julia with the following code:

```nohighlight
using StatsModels, DataFrames, CSV, GLM, ANOVA;  
data = CSV.read("./data/test_data.csv");     
categorical!(data, :cond);
categorical!(data, :subj);
anova(lm(@formula(y~cond+subj+cond*subj),data))

```

It returns a quite different result:

```julia
4×6 DataFrame
│ Row │ Source │ DF │ SS │ MSS │ F │ p │
│ │ String │ Abstract… │ Abstract… │ Abstract… │ Abstract… │ Abstract… │
├─────┼─────────────┼───────────┼───────────┼───────────┼───────────┼─────────────┤
│ 1 │ cond │ 1.0 │ 0.661981 │ 0.661981 │ 0.0517376 │ 0.820083 │
│ 2 │ subj │ 49.0 │ 7980.08 │ 162.859 │ 12.7283 │ 9.31968e-90 │
│ 3 │ cond & subj │ 49.0 │ 3827.32 │ 78.1085 │ 6.10462 │ 2.22324e-35 │
│ 4 │ Residuals │ 2900.0 │ 37105.4 │ 12.795 │ 0.0 │ 0.0 │

```

It’s quite confusing, I have no idea what happened to my code. I would be grateful if I could get your generous help 😀.

---

<div class="post-metadata">

### Author: ![jbrea](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbrea/32/3879_2.png) [@jbrea](https://discourse.julialang.org/u/jbrea)
#### Post date: [July 4, 2019, 9:33am UTC](https://discourse.julialang.org/t/didnt-get-consistent-result-of-anova-from-r-and-julia/26003/2 "2019-07-04T09:33:53Z")

</div>

The [ANOVA readme](https://github.com/marcpabst/ANOVA.jl) states

> Important: Make sure to use EffectsCoding on all your predictors, or results won’t be meaningful.

Did you try this?

---

<div class="post-metadata">

### Author: ![Losses](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/losses/32/9175_2.png) [@Losses](https://discourse.julialang.org/u/Losses)
#### Post date: [July 4, 2019, 10:54am UTC](https://discourse.julialang.org/t/didnt-get-consistent-result-of-anova-from-r-and-julia/26003/3 "2019-07-04T10:54:06Z")

</div>

Oops, sorry, i didn’t follow the correct tutorial…😧

The modified command is:

```julia
 anova(fit(LinearModel, @formula(y~cond+subj+cond*subj), test_data, contrasts = Dict(:cond => EffectsCoding(), :subj => EffectsCoding())))

```

The result is:

```julia
4×6 DataFrame
│ Row │ Source │ DF │ SS │ MSS │ F │ p │
│ │ String │ Abstract… │ Abstract… │ Abstract… │ Abstract… │ Abstract… │
├─────┼─────────────┼───────────┼───────────┼───────────┼───────────┼─────────────┤
│ 1 │ cond │ 1.0 │ 705.541 │ 705.541 │ 55.1421 │ 1.46615e-13 │
│ 2 │ subj │ 49.0 │ 13871.9 │ 283.1 │ 22.1259 │ 1.0026e-162 │
│ 3 │ cond & subj │ 49.0 │ 3827.32 │ 78.1085 │ 6.10462 │ 2.22324e-35 │
│ 4 │ Residuals │ 2900.0 │ 37105.4 │ 12.795 │ 0.0 │ 0.0 │

```

Everything looks well now, thanks for your kindly help!
