192
An Introduction to ANOVA in R. Daniel Faso Derek Beaton Noah Sasson Hervé Abdi

An Introduction to ANOVA in R

  • Upload
    harley

  • View
    42

  • Download
    0

Embed Size (px)

DESCRIPTION

An Introduction to ANOVA in R. Daniel FasoDerek Beaton Noah Sasson Hervé Abdi. An Introduction to ANOVA in R. Daniel FasoDerek Beaton Noah Sasson Hervé Abdi. An Introduction to ANOVA in R. Daniel Faso Derek Beaton Noah Sasson Hervé Abdi. An Introduction to ANOVA in R. - PowerPoint PPT Presentation

Citation preview

Page 1: An Introduction to ANOVA in R

An Introduction to ANOVA in R.

Daniel Faso

Derek Beaton

Noah Sasson

Hervé Abdi

Page 2: An Introduction to ANOVA in R

An Introduction to ANOVA in R.

Daniel Faso

Derek Beaton

Noah Sasson

Hervé Abdi

Page 3: An Introduction to ANOVA in R

An Introduction to ANOVA in R.

Daniel Faso

Derek Beaton

Noah Sasson

Hervé Abdi

Page 4: An Introduction to ANOVA in R

An Introduction to ANOVA in R.

Daniel Faso

Derek Beaton

Noah Sasson

Hervé Abdi

Page 5: An Introduction to ANOVA in R

An Introduction to ANOVA in R.

Daniel Faso

Derek Beaton

Noah Sasson

Hervé AbdiJoseph Dunlop

Page 6: An Introduction to ANOVA in R

Outline

• We have a lot to talk about!

–What is, and why use R?

– All sorts of ANOVAs

• And (most) everything to go with them!

Page 7: An Introduction to ANOVA in R

Outline

• We have a lot to talk about!

–What is, and why use R?

– All sorts of ANOVAs

• And (most) everything to go with them!

Page 8: An Introduction to ANOVA in R

R

• Stats or Programming?

• Gratis vs. Libre?

Page 9: An Introduction to ANOVA in R

R

• Stats or Programming?

• Gratis vs. Libre?

Page 10: An Introduction to ANOVA in R

R

• Stats and Programming

• Gratis and Libre

Page 11: An Introduction to ANOVA in R

R

• Stats and Programming

– R is a language

– R is an environment

• Gratis and Libre

Page 12: An Introduction to ANOVA in R

R

• Stats and Programming

• Gratis and Libre

– Free (as in beer)

– Free (as in speech)

– No cost, no restrictions

Page 13: An Introduction to ANOVA in R

R Communities

• Several major ones:

– CRAN

– BioConductor

– R-Forge

Page 14: An Introduction to ANOVA in R

We promise!

Page 15: An Introduction to ANOVA in R

R Communities

• Community provides add-ons

– Called packages

–March, 2013: 4380 packages (CRAN)

– February, 2014: 5206 packages (CRAN)

Page 16: An Introduction to ANOVA in R

R is a language

• What if something doesn’t exist?

–Make it yourself!

– R is Turing Complete

Page 17: An Introduction to ANOVA in R

R as a language

• Syntax comes from S

– R syntax is a bit similar to Matlab

• But with some special features

specifically for “speaking stats”

Page 18: An Introduction to ANOVA in R

All sorts of interfaces

• R is ugly.

– And sometimes slow.

• But people are changing that!

– Remember: beer and speech!

Page 19: An Introduction to ANOVA in R

Yuck!

Page 20: An Introduction to ANOVA in R

Less yuck

Page 21: An Introduction to ANOVA in R

SPSS like

Page 22: An Introduction to ANOVA in R

SPSS like

Page 23: An Introduction to ANOVA in R

Matlab like

Page 24: An Introduction to ANOVA in R

And many more• RED

• TinnR

• RevoR

– A commercial version with free

academic license

• Which means it’s faster and comes with

support!

Page 25: An Introduction to ANOVA in R

Moving on

• For today, we’ll stick with regular

ugly R.

Page 26: An Introduction to ANOVA in R

Outline

• We have a lot to talk about!

–What is, and why use R?

– All sorts of ANOVAs

• And (most) everything to go with them!

Page 27: An Introduction to ANOVA in R

All sorts of ANOVAs

• S = Subjects

• A = independent variable A

• a = level of A

• S(A) = one factor between

• S x A = one factor within (repeated)

• y = Dependent Variables

Page 28: An Introduction to ANOVA in R

All sorts of ANOVAs

• S = Subjects

• A = independent variable A

• a = level of A

• S(A) = one factor between

• S x A = one factor within (repeated)

• y = Dependent Variables

Page 29: An Introduction to ANOVA in R

All sorts of ANOVAs

• S = Subjects

• A = independent variable A

• a = level of A

• S(A) = one factor between

• S x A = one factor within (repeated)

• y = Dependent Variables

Page 30: An Introduction to ANOVA in R

All sorts of ANOVAs

• S = Subjects

• A = independent variable A

• a = level of A

• S(A) = one factor between

• S x A = one factor within (repeated)

• y = Dependent Variables

Page 31: An Introduction to ANOVA in R

All sorts of ANOVAs

• S = Subjects

• A = independent variable A

• a = level of A

• S(A) = one factor between

• S x A = one factor within (repeated)

• y = Dependent Variables

Page 32: An Introduction to ANOVA in R

All sorts of ANOVAs

• S = Subjects

• A = independent variable A

• a = level of A

• S(A) = one factor between

• S x A = one factor within (repeated)

• y = Dependent Variables

Page 33: An Introduction to ANOVA in R

All sorts of ANOVAs

• S = Subjects

• A = independent variable A

• a = level of A

• S(A) = one factor between

• S x A = one factor within (repeated)

• y = Dependent Variables

Page 34: An Introduction to ANOVA in R

All sorts of ANOVAs

• S(A)

• S(A x B) – balanced and unbalanced

• S(A) x B – balanced and unbalanced

• S(A x B) x C

Page 35: An Introduction to ANOVA in R

All sorts of ANOVAs

• S(A)

• S(A x B) – balanced and unbalanced

• S(A) x B – balanced and unbalanced

• S(A x B) x C

Page 36: An Introduction to ANOVA in R

All sorts of ANOVAs

• S(A)

• S(A x B) – balanced and unbalanced

• S(A) x B – balanced and unbalanced

• S(A x B) x C

Page 37: An Introduction to ANOVA in R

All sorts of ANOVAs

• S(A)

• S(A x B) – balanced and unbalanced

• S(A) x B – balanced and unbalanced

• S(A x B) x C

Page 38: An Introduction to ANOVA in R

All sorts of ANOVAs

• S(A)

• S(A x B) – balanced and unbalanced

• S(A) x B – balanced and unbalanced

• S(A x B) x C

Page 39: An Introduction to ANOVA in R

Outline

• We have a lot to talk about!

–What is, and why use R?

– All sorts of ANOVAs

• And (most) everything to go with them!

Page 40: An Introduction to ANOVA in R

(Most) Everything

• Transforming data

• Plotting results

• Saving results

• Post hoc tests

• And (maybe) many more!

Page 41: An Introduction to ANOVA in R

(Most) Everything

• Transforming data

• Plotting results

• Saving results

• Post hoc tests

• And (maybe) many more!

Page 42: An Introduction to ANOVA in R

(Most) Everything

• Transforming data

• Plotting results

• Saving results

• Post hoc tests

• And (maybe) many more!

Page 43: An Introduction to ANOVA in R

(Most) Everything

• Transforming data

• Plotting results

• Saving results

• Post hoc tests

• And (maybe) many more!

Page 44: An Introduction to ANOVA in R

(Most) Everything

• Transforming data

• Plotting results

• Saving results

• Post hoc tests

• And (maybe) many more!

Page 45: An Introduction to ANOVA in R

Quick background

• Two important concepts:

– Variables

– Functions

• These are how R works

Page 46: An Introduction to ANOVA in R

Quick background

• Two important concepts:

– Variables

– Functions

• These are how R works

Page 47: An Introduction to ANOVA in R

Variables

• Called so because they can change

– But they only change when you make

them change

Page 48: An Introduction to ANOVA in R

Variables

• Look like this:

> save.this <- from.that

Page 49: An Introduction to ANOVA in R

Variables

• Look like this:

> save.this <- from.that

Page 50: An Introduction to ANOVA in R

Variables

• Look like this:

> save.this <- from.that

Page 51: An Introduction to ANOVA in R

Variables

• Look like this:

> save.this <- from.that

Page 52: An Introduction to ANOVA in R

Variables

• Look like this:

> save.this = from.that

Page 53: An Introduction to ANOVA in R

Variables

• Look like this:

> save.this <- from.that

Page 54: An Introduction to ANOVA in R

Quick background

• Two important concepts:

– Variables

– Functions

• These are how R works

Page 55: An Introduction to ANOVA in R

Quick background

• Two important concepts:

– Variables

– Functions

• These are how R works

Page 56: An Introduction to ANOVA in R

Functions

y = f(x) – Ew, math.

Page 57: An Introduction to ANOVA in R

Functions

• Same idea

Page 58: An Introduction to ANOVA in R

Functions

y = f(x)

Page 59: An Introduction to ANOVA in R

Functions

y = f(x)

Page 60: An Introduction to ANOVA in R

Stuff goes in

y = f(x)

Page 61: An Introduction to ANOVA in R

Magic

y = f(x)

Page 62: An Introduction to ANOVA in R

Save that magic!

y = f(x)

Page 63: An Introduction to ANOVA in R

R does the same thing

• Say f(x) is √x

Page 64: An Introduction to ANOVA in R

f(x)

• If x is 4

• f(x) is 2

Page 65: An Introduction to ANOVA in R

In R?

>sqrt

Page 66: An Introduction to ANOVA in R

In R?

>sqrt

– But we require ()

Page 67: An Introduction to ANOVA in R

In R?

>sqrt()

Page 68: An Introduction to ANOVA in R

But really…

>sqrt(4)

Page 69: An Introduction to ANOVA in R

But really…

>sqrt(4)

[1] 2

Page 70: An Introduction to ANOVA in R

Or…

Page 71: An Introduction to ANOVA in R

Or…

Page 72: An Introduction to ANOVA in R

Or…

Page 73: An Introduction to ANOVA in R

Phew.

Page 74: An Introduction to ANOVA in R

Oh…

• We still have to talk about ANOVAs!

Page 75: An Introduction to ANOVA in R

The real presentation!

• Some back and forth

Page 76: An Introduction to ANOVA in R

Some back and forth

• We need slides & R to show

everything today

– Follow along as best you can.

– If you get lost, we’ll try to help

Page 77: An Introduction to ANOVA in R

Basics of R

• How can I transition to R?

Page 78: An Introduction to ANOVA in R

See: very ugly.

Page 79: An Introduction to ANOVA in R

How to use R

Page 80: An Introduction to ANOVA in R

Some basics

• “Working directory”

–What it means

Page 81: An Introduction to ANOVA in R

getwd()

• get working directory – the folder you’re

currently in

Page 82: An Introduction to ANOVA in R

setwd()

• set working directory – the folder you

want to change to

Page 83: An Introduction to ANOVA in R

Let’s get & set!

Page 84: An Introduction to ANOVA in R

ls()

• Lists variables in R’s workspace

Page 85: An Introduction to ANOVA in R

ls()

• Lists variables in R’s workspace

Page 86: An Introduction to ANOVA in R

ls()

• Lists variables in R’s workspace

Page 87: An Introduction to ANOVA in R

Storing variables

• my.var <- 10 + 12

Page 88: An Introduction to ANOVA in R

R is a fancy calculator

Page 89: An Introduction to ANOVA in R

(Not) Storing variables

Page 90: An Introduction to ANOVA in R

(Not Yet) Storing variables

Page 91: An Introduction to ANOVA in R

ls()

• Lists variables in R’s workspace

Page 92: An Introduction to ANOVA in R

ls()

• Lists variables in R’s workspace

Page 93: An Introduction to ANOVA in R

(Now) Storing variables

• my.var <- 10 + 12

Page 94: An Introduction to ANOVA in R

Storing variables

• my.var <- 10 + 12

Page 95: An Introduction to ANOVA in R

Storing variables

• my.var <- 10 + 12

Page 96: An Introduction to ANOVA in R

ls()

• Lists variables in R’s workspace

Page 97: An Introduction to ANOVA in R

ls()

• Lists variables in R’s workspace

Page 98: An Introduction to ANOVA in R

ls()

• Lists variables in R’s workspace

Page 99: An Introduction to ANOVA in R

Storing variables

• my.var <- 10 + 12

–We’ll be doing this a lot

Page 100: An Introduction to ANOVA in R

rm()

• What about getting rid of everything?

– rm(list=ls())

• BE CAREFUL USING THIS!

Page 101: An Introduction to ANOVA in R

rm()

• What about getting rid of everything?

– rm(list=ls())

• BE CAREFUL USING THIS!

Page 102: An Introduction to ANOVA in R

Help!

• ?

• ??

Page 103: An Introduction to ANOVA in R

Help!

• ? – a.k.a Help

• ??

Page 104: An Introduction to ANOVA in R

Help!

• ? – a.k.a Help

• ?? – a.k.a Helpless

Page 105: An Introduction to ANOVA in R

Kidding!

• ? – if you know the name

• ?? – if you don’t!

Page 106: An Introduction to ANOVA in R

Quick example

• ?getwd

Page 107: An Introduction to ANOVA in R
Page 108: An Introduction to ANOVA in R

Quick example

• ??anova

Page 109: An Introduction to ANOVA in R
Page 110: An Introduction to ANOVA in R

Really stuck?

Page 111: An Introduction to ANOVA in R

Some basic reminders

• ANOVA aims to detect differences

between means

• Null hypothesis is when there is no

difference between means

Page 112: An Introduction to ANOVA in R

Dan’s turn!

• With code walk throughs

Page 113: An Introduction to ANOVA in R

Let’s begin!

• ?aov

Page 114: An Introduction to ANOVA in R

See code for S(A)

• Return here for plotting and post-hoc

with S(AxB)

Page 115: An Introduction to ANOVA in R

We’re back up here!

Page 116: An Introduction to ANOVA in R

We’re back up here!

?interaction.plot

Page 117: An Introduction to ANOVA in R

?interaction.plot

• What does it all mean?

Page 118: An Introduction to ANOVA in R

• ?interaction.plot

Page 119: An Introduction to ANOVA in R
Page 120: An Introduction to ANOVA in R
Page 121: An Introduction to ANOVA in R
Page 122: An Introduction to ANOVA in R
Page 123: An Introduction to ANOVA in R
Page 124: An Introduction to ANOVA in R

But what about the rest?

Page 125: An Introduction to ANOVA in R
Page 126: An Introduction to ANOVA in R
Page 127: An Introduction to ANOVA in R
Page 128: An Introduction to ANOVA in R
Page 129: An Introduction to ANOVA in R
Page 130: An Introduction to ANOVA in R
Page 131: An Introduction to ANOVA in R
Page 132: An Introduction to ANOVA in R
Page 133: An Introduction to ANOVA in R
Page 134: An Introduction to ANOVA in R
Page 135: An Introduction to ANOVA in R
Page 136: An Introduction to ANOVA in R
Page 137: An Introduction to ANOVA in R
Page 138: An Introduction to ANOVA in R
Page 139: An Introduction to ANOVA in R
Page 140: An Introduction to ANOVA in R

R vs. SPSS

Page 141: An Introduction to ANOVA in R

R vs. SPSS

Page 142: An Introduction to ANOVA in R

R vs. SPSS

Page 143: An Introduction to ANOVA in R

R vs. SPSS

Page 144: An Introduction to ANOVA in R
Page 145: An Introduction to ANOVA in R
Page 146: An Introduction to ANOVA in R
Page 147: An Introduction to ANOVA in R
Page 148: An Introduction to ANOVA in R
Page 149: An Introduction to ANOVA in R
Page 150: An Introduction to ANOVA in R

Derek’s turn!

Page 151: An Introduction to ANOVA in R

S(A)xB

• Partially repeated

Page 152: An Introduction to ANOVA in R

S(A)xB

• Partially repeated

– A is between

– B is within/repeated

Page 153: An Introduction to ANOVA in R
Page 154: An Introduction to ANOVA in R
Page 155: An Introduction to ANOVA in R
Page 156: An Introduction to ANOVA in R
Page 157: An Introduction to ANOVA in R

reshape()

Page 158: An Introduction to ANOVA in R

The data

Page 159: An Introduction to ANOVA in R

IDs

Page 160: An Introduction to ANOVA in R

Repeated Factor

Page 161: An Introduction to ANOVA in R

New DV name

Page 162: An Introduction to ANOVA in R

New Repeated IV level names

Page 163: An Introduction to ANOVA in R

New Repeated Factor Name

Page 164: An Introduction to ANOVA in R

Data shape

Page 165: An Introduction to ANOVA in R

Phew

• Again.

Page 166: An Introduction to ANOVA in R

We know this

Page 167: An Introduction to ANOVA in R

But not that!

Page 168: An Introduction to ANOVA in R

Together

Page 169: An Introduction to ANOVA in R

Output

Page 170: An Introduction to ANOVA in R

Output

Page 171: An Introduction to ANOVA in R

Output

Page 172: An Introduction to ANOVA in R

Output

Page 173: An Introduction to ANOVA in R

Output

Page 174: An Introduction to ANOVA in R

Output

Page 175: An Introduction to ANOVA in R

Output

Page 176: An Introduction to ANOVA in R

Same ol’ same ol’

Page 177: An Introduction to ANOVA in R
Page 178: An Introduction to ANOVA in R

Difficult ANOVAs

• What about when things get weird?

Page 179: An Introduction to ANOVA in R

So many options

• car

• lme

• ez

• lm + aov + drop1

Page 180: An Introduction to ANOVA in R

So many options

• car

• lme

• ez

• lm + aov + drop1

Page 181: An Introduction to ANOVA in R

Easier than

• The “easy” pipeline!

Page 182: An Introduction to ANOVA in R

Code!

• See the Complex_Pipeline

Page 183: An Introduction to ANOVA in R

What didn’t we cover?

• Really complex ANOVAs

– Had between and within, but what

about…

Page 184: An Introduction to ANOVA in R

Fixed and random?

• lm can help.

• lme (lme4) is better

• You’ll need a book or two!

– And something scary…

Page 185: An Introduction to ANOVA in R

The Score Model.

• Dun dun dun

• Yabs = u… + αa + βb + ss(a) + αβab +

βsbs(a) + ebs(a)

Page 186: An Introduction to ANOVA in R

Sums of Squares?

• We talked about 1 and 3

– There are others!

• Some packages allow this

– ez, car

Page 187: An Introduction to ANOVA in R

Contrasts!

• They’re easy

– But not really in R…

Page 188: An Introduction to ANOVA in R

Packages

• car

• ez

• multcomp

• contrasts

• So many…

Page 189: An Introduction to ANOVA in R

Chicken!

• Just do a regression with lm:

– res <- lm(y ~ my.contrast)

• You’ll have to do your own

corrections, though…

Page 190: An Introduction to ANOVA in R

Fancy testing methods?

• What if my data are weird?

– Non normal?

– Small sample size?

–HUGE sample size?

Page 191: An Introduction to ANOVA in R

Resampling

• If we’re ready…

– we can transition directly

• Else,

– The answer: bootstrap and permutation

Page 192: An Introduction to ANOVA in R

Before we do

• Thanks!

• Questions?

– For now

• Comments, complaints, and suggestions

– But not until after the next workshop!

– Or around at the conference!