Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM)...

50
Lesson 6, Part 1: Linear Mixed Effects Models

Transcript of Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM)...

Page 1: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

Lesson 6, Part 1: Linear Mixed Effects Models

Page 2: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

Learn about linear mixed effects models (LMEM)

This Lesson’s Goals

Summarise results in an R Markdown document

Run some preliminary LMEMs in R

Make figures for data for LMEMs

Page 3: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

Learn about linear mixed effects models (LMEM)

This Lesson’s Goals

Summarise results in an R Markdown document

Run some preliminary LMEMs in R

Make figures for data for LMEMs

Page 4: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

But aren’t percentages reallyjust summarized count data?

But we had to drop a bunch of Union states, isn’t that a problem?

But Alabama was missing one Democrat data point, isn’t it not balanced?

linear mixed effects modelsgeneralized

End of Lesson 5 Questions

But what about the variance for ‘year’, shouldn’t we try and account for that too?

Page 5: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

Math (Part 1)

Page 6: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

yi = a + b1x1i + b2x2i + b3x1ix2i + ei

How do I add factorsfor random variance?

(i.e. things we’re not directly testing)

Page 7: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

yi = a + as + b1x1i + b2x2i + b3x1ix2i + ei

random effect

s = state

yi = a + b1x1i + b2x2i + b3x1ix2i + ei

Page 8: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

yi = a + as + b1x1i + b2x2i + b3x1ix2i + ei

random intercept

s = state

yi = a + b1x1i + b2x2i + b3x1ix2i + ei

Page 9: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

yi = a + as + ay + b1x1i + b2x2i + b3x1ix2i + ei

random intercept #1

s = state

random intercept #2

y = year

yi = a + b1x1i + b2x2i + b3x1ix2i + ei

Page 10: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

ANOVA language LMEM language

independent variable

error variable

In this paper we tested the effect of time on weight. A total of 50 baby chicks were included in the study.

dependent variable

fixed effect

random effect

dependent variable

time

weight

baby chick

mixed effects

Page 11: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

yi = a + as + ay + b1x1i + b2x2i +b3x1ix2i + ei

yi = specific y value

a = intercept

as = random intercept #1 for specific level

ay = random intercept #2 for specific level

b1 = slope of first variable

x1i = specific x value for first variable

b2 = slope of second variable

x2i = specific x value for second variable

b3 = slope of third variable (interaction)

ei = random variance or the residual

Page 12: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

yi = a + as + ay + b1x1i + b2x2i + b3x1ix2i + ei

0

25

50

75

100

Union ConfederacyCivil War country

Perc

enta

ge o

f vot

e fo

r inc

umbe

ntDemocrat Republican

Percentage of Votes for Incumbentby Country in Civil War and Party of Incumbent

Page 13: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●● ●●●

●●

●●

●●●●●

●●

●●●

●●

●●

●●

● ●●

●●

●●

●●

●●

●●

●●

0

25

50

75

100

Union ConfederacyCivil War country

Perc

enta

ge o

f vot

e fo

r inc

umbe

nt● ●Democrat Republican

Percentage of Votes for Incumbentby Country in Civil War and Party of Incumbent

yi = a + as + ay + b1x1i + b2x2i + b3x1ix2i + ei

Page 14: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●● ●●●

●●

●●

●●●●●

●●

●●●

●●

●●

●●

● ●●

●●

●●

●●

●●

●●

●●

0

25

50

75

100

Union ConfederacyCivil War country

Perc

enta

ge o

f vot

e fo

r inc

umbe

nt● ●Democrat Republican

Percentage of Votes for Incumbentby Country in Civil War and Party of Incumbent

yi = a + as + ay + b1x1i + b2x2i + b3x1ix2i + ei

Page 15: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●● ●●●

●●

●●

●●●●●

●●

●●●

●●

●●

●●

● ●●

●●

●●

●●

●●

●●

●●

0

25

50

75

100

Union ConfederacyCivil War country

Perc

enta

ge o

f vot

e fo

r inc

umbe

nt● ●Democrat Republican

Percentage of Votes for Incumbentby Country in Civil War and Party of Incumbent

yi = a + as + ay + b1x1i + b2x2i + b3x1ix2i + ei

Page 16: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●● ●●●

●●

●●

●●●●●

●●

●●●

●●

●●

●●

● ●●

●●

●●

●●

●●

●●

●●

0

25

50

75

100

Union ConfederacyCivil War country

Perc

enta

ge o

f vot

e fo

r inc

umbe

nt● ●Democrat Republican

Percentage of Votes for Incumbentby Country in Civil War and Party of Incumbent

Rhode Island

Rhode Island

Rhode Island

Rhode Island

yi = a + as + ay + b1x1i + b2x2i + b3x1ix2i + ei

Page 17: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●● ●●●

●●

●●

●●●●●

●●

●●●

●●

●●

●●

● ●●

●●

●●

●●

●●

●●

●●

0

25

50

75

100

Union ConfederacyCivil War country

Perc

enta

ge o

f vot

e fo

r inc

umbe

nt● ●Democrat Republican

Percentage of Votes for Incumbentby Country in Civil War and Party of Incumbent

Kentucky

KentuckyKentucky

Kentucky

yi = a + as + ay + b1x1i + b2x2i + b3x1ix2i + ei

Page 18: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●● ●●●

●●

●●

●●●●●

●●

●●●

●●

●●

●●

● ●●

●●

●●

●●

●●

●●

●●

0

25

50

75

100

Union ConfederacyCivil War country

Perc

enta

ge o

f vot

e fo

r inc

umbe

nt● ●Democrat Republican

Percentage of Votes for Incumbentby Country in Civil War and Party of Incumbent

1964

1964

1964

1964

yi = a + as + ay + b1x1i + b2x2i + b3x1ix2i + ei

Page 19: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●● ●●●

●●

●●

●●●●●

●●

●●●

●●

●●

●●

● ●●

●●

●●

●●

●●

●●

●●

0

25

50

75

100

Union ConfederacyCivil War country

Perc

enta

ge o

f vot

e fo

r inc

umbe

nt● ●Democrat Republican

Percentage of Votes for Incumbentby Country in Civil War and Party of Incumbent

2012

2012

2012

2012

yi = a + as + ay + b1x1i + b2x2i + b3x1ix2i + ei

Page 20: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

But, at the end of the last lesson we said this was bad, because it was a

percentage of a count?

Page 21: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

log[p/(1-p)]i = a + as + ay + b1x1i + b2x2i + b3x1ix2i + ei

logistic regression —> generalized linear mixed effects model

yi = a + as + ay + b1x1i + b2x2i +b3x1ix2i + ei

Page 22: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

R Code (Part 1)

Page 23: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

lmer(perc_votes_incumbent ~incumbent_party * civil_war

+ (1|state)+ (1|year))

lme4

yi = a + as + ay + b1x1i + b2x2i + b3x1ix2i + ei

Page 24: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

lmer(perc_votes_incumbent ~incumbent_party * civil_war

+ (1|state)+ (1|year))

lme4

yi = a + as + ay + b1x1i + b2x2i + b3x1ix2i + ei

Page 25: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

But, in the ANOVA we accounted for the fact that a variable could be

within- or between-subject?

Page 26: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

Math (Part 2)

Page 27: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

yi = a + as + ay + (bs1+b1)x1i + (by1+b2)x2i +

b3x1ix2i + eirandom slope

s = state

random slope

y = year

yi = a + as + ay + b1x1i + b2x2i +b3x1ix2i + ei

Page 28: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

yi = a + as + ay + (bs1+b1)x1i + (by1+b2)x2i +

b3x1ix2i + eiyi = specific y value

a = intercept

as = random intercept #1

ay = random intercept #2

bs1 = slope of r.e. #1

b1 = slope of variable #1

x1i = x value for variable #1

by1 = slope of r.e. #2

b2 = slope of variable #2

x2i = x value for variable #2

b3 = slope of variable #3

ei = random variance

Page 29: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●● ●●●

●●

●●

●●●●●

●●

●●●

●●

●●

●●

● ●●

●●

●●

●●

●●

●●

●●

0

25

50

75

100

Union ConfederacyCivil War country

Perc

enta

ge o

f vot

e fo

r inc

umbe

nt● ●Democrat Republican

Percentage of Votes for Incumbentby Country in Civil War and Party of Incumbent

Rhode Island

Rhode Island

yi = a + as + ay + (bs1+b1)x1i + (by1+b2)x2i + b3x1ix2i + ei

Rhode Island

Rhode Island

Page 30: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●● ●●●

●●

●●

●●●●●

●●

●●●

●●

●●

●●

● ●●

●●

●●

●●

●●

●●

●●

0

25

50

75

100

Union ConfederacyCivil War country

Perc

enta

ge o

f vot

e fo

r inc

umbe

nt● ●Democrat Republican

Percentage of Votes for Incumbentby Country in Civil War and Party of Incumbent

Rhode Island

Rhode Island

Rhode Island

Rhode Island

Rhode Island

Rhode Island

Rhode Island

Rhode Island

yi = a + as + ay + (bs1+b1)x1i + (by1+b2)x2i + b3x1ix2i + ei

Page 31: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

R Code (Part 2)

Page 32: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

lmer(perc_votes_incumbent ~incumbent_party * civil_war + (1+incumbent_party|state)

+ (1+civil_war|year))

lme4

yi = a + as + ay + (bs1+b1)x1i + (by1+b2)x2i + b3x1ix2i + ei

Page 33: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

lmer(perc_votes_incumbent ~incumbent_party * civil_war + (1+incumbent_party|state)

+ (1+civil_war|year))

lme4

yi = a + as + ay + (bs1+b1)x1i + (by1+b2)x2i + b3x1ix2i + ei

Page 34: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

LMEM with only random intercepts

LMEM with only random intercepts and slopes

Page 35: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

Lab

Page 36: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

Data set: Stroop Task

source: real students!

Page 37: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

Say the color of the ink not the written word.

blue

Page 38: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

Say the color of the ink not the written word.

blue

Page 39: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

Say the color of the ink not the written word.

blueword = ink color congruent trial

blueword ≠ ink color incongruent trial

Page 40: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

Data set: Stroop Task

Congruency: Are responses to incongruent trials less accurate and slower than to congruent trials?

Experiment half: Are responses more accurate and faster in the second half of the experiment than the first half of the experiment?

Congruency x Experiment half: Is there an interaction between these variables?

accuracy

subject

source: real students!

logit pi x1 x2 r1 r2

= = = = =

congruencyexperiment half

item

reaction times

subject

yi x1 x2 r1 r2

= = = = =

congruencyexperiment half

item

accuracy (logistic) reaction times (linear)

Page 41: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

dplyr

data_clean = data_results

Page 42: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

dplyr

data_clean = data_results %>% rename(trial_number = SimpleRTBLock.TrialNr.)

change variable name

Page 43: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

dplyr

data_clean = data_results %>% rename(trial_number = SimpleRTBLock.TrialNr.) %>% rename(congruency = Congruency) %>% rename(correct_response = StroopItem.CRESP.) %>% rename(given_response = StroopItem.RESP.) %>% rename(accuracy = StroopItem.ACC.) %>% rename(rt = StroopItem.RT.) %>%

change variable name

Page 44: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

dplyr

data_clean = data_results %>% rename(trial_number = SimpleRTBLock.TrialNr.) %>% rename(congruency = Congruency) %>% rename(correct_response = StroopItem.CRESP.) %>% rename(given_response = StroopItem.RESP.) %>% rename(accuracy = StroopItem.ACC.) %>% rename(rt = StroopItem.RT.) %>% select(subject_id, block, item, trial_number, congruency, correct_response, given_response, accuracy, rt)

change variable name

choose subset of variables

Page 45: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

RColorBrewer

cols = brewer.pal(5, “PuOr")

call to make palette

Page 46: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

RColorBrewer

cols = brewer.pal(5, “PuOr")

call to make palette

number of colors

Page 47: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

RColorBrewer

cols = brewer.pal(5, “PuOr")

call to make palette

number of colors

palette name

Page 48: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

RColorBrewer

cols = brewer.pal(5, “PuOr")

call to make palette

number of colors

palette name

> cols[1] "#E66101" "#FDB863" "#F7F7F7" "#B2ABD2" "#5E3C99"

0.0

2.5

5.0

7.5

60 90 120 1500.0

2.5

5.0

7.5

60 90 120 1500.0

2.5

5.0

7.5

60 90 120 1500.0

2.5

5.0

7.5

60 90 120 1500.0

2.5

5.0

7.5

60 90 120 150

Page 49: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

RColorBrewer

cols = brewer.pal(5, “PuOr")col_con = cols[1] call to

make palette

number of colors

palette name

> cols[1] "#E66101" "#FDB863" "#F7F7F7" "#B2ABD2" "#5E3C99"

0.0

2.5

5.0

7.5

60 90 120 1500.0

2.5

5.0

7.5

60 90 120 1500.0

2.5

5.0

7.5

60 90 120 1500.0

2.5

5.0

7.5

60 90 120 1500.0

2.5

5.0

7.5

60 90 120 150

Page 50: Lesson 6, Part 1: Linear Mixed Effects Models · Learn about linear mixed effects models (LMEM) This Lesson’s Goals Summarise results in an R Markdown document Run some preliminary

RColorBrewer

cols = brewer.pal(5, “PuOr")col_con = cols[1]col_incon = cols[5]

call to make palette

number of colors

palette name

> cols[1] "#E66101" "#FDB863" "#F7F7F7" "#B2ABD2" "#5E3C99"

0.0

2.5

5.0

7.5

60 90 120 1500.0

2.5

5.0

7.5

60 90 120 1500.0

2.5

5.0

7.5

60 90 120 1500.0

2.5

5.0

7.5

60 90 120 1500.0

2.5

5.0

7.5

60 90 120 150