Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Mixed Effects Models - Autocorrelation

Lecture 19 from my mixed-effects modeling course: Autocorrelation in longitudinal and time-series data

  • Be the first to comment

  • Be the first to like this

Mixed Effects Models - Autocorrelation

  1. 1. Course Business ! Lab materials on Canvas ! Package to install for today’s analyses: languageR
  2. 2. Week 11.2: Autocorrelation ! Autocorrelation ! Introduction ! Testing for Autocorrelation ! Visualizing Autocorrelation ! Direction & Lag ! Why Does Autocorrelation Matter? ! Lab
  3. 3. Longitudinal Designs • Two big questions mixed-effects models can help us answer about longitudinal data: 1) What is the overall trajectory of change across time? " Monday: Growth curve analysis 2) How does an observation at one time point relate to the next time point? " Today: Autocorrelation
  4. 4. Autocorrelation • Previously, we looked at general trajectory across time • e.g., kids who are 28 months old have larger vocabs than kids who are 20 months old • But, there may be relations among specific observations • Does reading to your kids a lot one month increase their vocab growth the next month? • Does being in a positive mood one day carry over to the next day? • Does purchasing a particular brand once make you more likely to purchase it again?
  5. 5. relationship.csv • One member of a dating couple rates their warmth towards the partner • For each of 10 consecutive days • First, let’s see if WarmthToday consistently increases or decreases across the 10 Days of the study • model.time <- lmer(WarmthToday ~ 1 + Day + (1 + Day|Couple), data=relationship)
  6. 6. relationship.csv • No linear increase or decrease in warmth over the 10 days • Makes sense … this is a relatively short timescale
  7. 7. Autocorrelation • We found no overall increase/decrease • Nevertheless, succeeding days might be more similar, even if there is no overall trend 1 2 3 4 5 L I N E A R T R E N D
  8. 8. Autocorrelation • We found no overall increase/decrease • Nevertheless, succeeding days might be more similar, even if there is no overall trend • If you have warm feelings towards your partner one day, maybe warmer the next, too + 2 3 4 5 L I N E A R T R E N D
  9. 9. Autocorrelation • We found no overall increase/decrease • Nevertheless, succeeding days might be more similar, even if there is no overall trend • If you have warm feelings towards your partner one day, maybe warmer the next, too + + 3 4 5 L I N E A R T R E N D
  10. 10. Autocorrelation • We found no overall increase/decrease • Nevertheless, succeeding days might be more similar, even if there is no overall trend • If you have warm feelings towards your partner one day, maybe warmer the next, too • If you have negative feelings, maybe less warm the next day + + 3 - 5 L I N E A R T R E N D
  11. 11. Autocorrelation • We found no overall increase/decrease • Nevertheless, succeeding days might be more similar, even if there is no overall trend • If you have warm feelings towards your partner one day, maybe warmer the next, too • If you have negative feelings, maybe less warm the next day + + 3 - - L I N E A R T R E N D
  12. 12. Autocorrelation • We found no overall increase/decrease • Nevertheless, succeeding days might be more similar, even if there is no overall trend • If you have warm feelings towards your partner one day, maybe warmer the next, too • If you have negative feelings, maybe less warm the next day + + 3 - -
  13. 13. Autocorrelation • relationship %>% head(n=10) Couple 01
  14. 14. Autocorrelation • These are examples of autocorrelation Couple 01 Couple 02
  15. 15. Autocorrelation "Maybe in order to understand mankind, we have to look at that word itself: MANKIND. Basically, it's made up of two separate words, 'mank' and 'ind.' What do these words mean? It's a mystery, and that's why so is mankind.” -- Jack Handey
  16. 16. Autocorrelation To understand autocorrelation, we have to look at the two separate words that make it up auto (self) + correlation • Autocorrelation refers to a variable correlating with itself, over time • Examples: • Positive mood in the morning # Positive mood in the afternoon • RT on the previous trial # RT on this trial due to waxing & waning of attention
  17. 17. Autocorrelation • These are examples of autocorrelation Couple 01 Couple 02
  18. 18. Week 11.2: Autocorrelation ! Autocorrelation ! Introduction ! Testing for Autocorrelation ! Visualizing Autocorrelation ! Direction & Lag ! Why Does Autocorrelation Matter? ! Lab
  19. 19. Testing for Autocorrelation • Is there autocorrelation in relationship warmth? • We want to test whether yesterday’s warmth predicts today’s warmth • So, we need to add WarmthYesterday as a variable • With package languageR… • relationship %>% mutate(WarmthYesterday = lags.fnc(relationship, time='Day', depvar='WarmthToday', group='Couple’, lag=1)) -> relationship • For each observation, get the value of WarmthToday from 1 day previous and store in WarmthYesterday Variable that identifies the time point Dependent variable Variable with the level-2 grouping factor (e.g., subjects or schools) Dataframe name Baayen & Milin, 2010
  20. 20. Testing for Autocorrelation • Is there autocorrelation in relationship warmth? • We want to test whether yesterday’s warmth predicts today’s warmth • So, we need to add WarmthYesterday as a variable • relationship %>% head(n=13) Day 1 for each couple gets the couple’s mean Baayen & Milin, 2010
  21. 21. Testing for Autocorrelation • Is there autocorrelation in relationship warmth? • Now, include WarmthYesterday in our model • model.auto <- lmer(WarmthToday ~ 1 + Day + WarmthYesterday + (1 + Day + WarmthYesterday|Couple), data=relationship)
  22. 22. Testing for Autocorrelation • Non-significant linear effect: No consistent increase/decrease in warmth over these 10 days • But, significant autocorrelation: Adjacent days are more similar in warmth towards partner • Random walk 1 2 3 4 5 L I N E A R T R E N D X
  23. 23. Autocorrelation • These are examples of autocorrelation Couple 01 Couple 02
  24. 24. Week 11.2: Autocorrelation ! Autocorrelation ! Introduction ! Testing for Autocorrelation ! Visualizing Autocorrelation ! Direction & Lag ! Why Does Autocorrelation Matter? ! Lab
  25. 25. Visualization of Autocorrelation • Visualization of the autocorrelation • With package languageR: • acf.fnc(xxx , time='xxx ', x=' ', group=' ') Variable that identifies the time point Dependent variable Variable with the level-2 grouping factor (e.g., subjects or schools). Needs to be a FACTOR variable Dataframe name Baayen & Milin, 2010
  26. 26. Visualization of Autocorrelation • Visualization of the autocorrelation • With package languageR: • acf.fnc(relationship, time='Day', x='WarmthToday', group='Couple') Variable that identifies the time point Dependent variable Variable with the level-2 grouping factor (e.g., subjects or schools). Needs to be a FACTOR variable Dataframe name Baayen & Milin, 2010
  27. 27. Lag Acf -0.5 0.0 0.5 1.0 0 2 4 6 8 Couple01 Couple02 0 2 4 6 8 Couple03 Couple04 0 2 4 6 8 Couple05 Couple06 0 2 4 6 8 Couple07 Couple08 Couple09 Couple10 Couple11 Couple12 Couple13 -0.5 0.0 0.5 1.0 Couple14 -0.5 0.0 0.5 1.0 Couple15 Couple16 Couple17 Couple18 Couple19 Couple20 Couple21 Couple22 Couple23 Couple24 Couple25 Couple26 Couple27 -0.5 0.0 0.5 1.0 Couple28 -0.5 0.0 0.5 1.0 Couple29 Couple30 Couple31 Couple32 Couple33 Couple34 Couple35 Couple36 0 2 4 6 8 Couple37 Couple38 0 2 4 6 8 Couple39 -0.5 0.0 0.5 1.0 Couple40 One box per couple Height of the second line indicates strength of autocorrelation for that couple
  28. 28. Week 11.2: Autocorrelation ! Autocorrelation ! Introduction ! Testing for Autocorrelation ! Visualizing Autocorrelation ! Direction & Lag ! Why Does Autocorrelation Matter? ! Lab
  29. 29. Direction of Autocorrelation • Positive autocorrelation—Having more of something at time t means you have more of it at time t+1 • More warmth on day 3 # More warmth on day 4 • Longer RT on one trial # Longer RT on the next trial • Negative autocorrelation—Having more at time t means you have less at t+1 • e.g., homeostatic processes • Very hungry at time t # Get something to eat # Not hungry at time t+1 • Stock prices: Price increases # People sell the stock # Price decreases • Both analyzed the same way—just look at the sign to understand the effect
  30. 30. Positive autocorrelation. This bar would be < 0 if it were a negative autocorrelation. Lag Acf -0.5 0.0 0.5 1.0 0 2 4 6 8 Couple01 Couple02 0 2 4 6 8 Couple03 Couple04 0 2 4 6 8 Couple05 Couple06 0 2 4 6 8 Couple07 Couple08 Couple09 Couple10 Couple11 Couple12 Couple13 -0.5 0.0 0.5 1.0 Couple14 -0.5 0.0 0.5 1.0 Couple15 Couple16 Couple17 Couple18 Couple19 Couple20 Couple21 Couple22 Couple23 Couple24 Couple25 Couple26 Couple27 -0.5 0.0 0.5 1.0 Couple28 -0.5 0.0 0.5 1.0 Couple29 Couple30 Couple31 Couple32 Couple33 Couple34 Couple35 Couple36 0 2 4 6 8 Couple37 Couple38 0 2 4 6 8 Couple39 -0.5 0.0 0.5 1.0 Couple40
  31. 31. Autocorrelation: Lag • Autocorrelation can happen at different time scales or lags • Most common is lag 1: an observation correlates with the next one 1 2 3 4 5 Autocorrelation: Lag • Autocorrelation can happen at different time scales or lags • Most common is lag 1: an observation correlates with the next one 1 2 3 4 5
  32. 32. Autocorrelation: Lag • Autocorrelation can happen at different time scales or lags • Most common is lag 1: an observation correlates with the next one • Lag 2: an observation correlates not with the next observation, but the one two time points later • Like the (false) idea that twins “skip a generation” • Effects that recur, but not immediately (e.g., earthquakes) 1 2 3 4 5
  33. 33. Autocorrelation: Lag • Autocorrelation can happen at different time scales or lags • Most common is lag 1: an observation correlates with the next one • Lag 2: an observation correlates not with the next observation, but the one two time points later • Like the (false) idea that twins “skip a generation” • Lag 3, Lag 4, etc… • Mood might have a lag 7 autocorrelation – weekly change (sad Monday, happy Friday) • But, be careful with autocorrelations >1—is there a theoretically plausible reason to expect them?
  34. 34. Lag 1 autocorrelation Lag 2 autocorrelation Lag 3 autocorrelation Leftmost line is lag 0—i.e., the correlation of an observation with itself This will always be 1 It’s shown for purposes of comparison
  35. 35. Lag Acf -0.5 0.0 0.5 1.0 0 2 4 6 8 Couple01 Couple02 0 2 4 6 8 Couple03 Couple04 0 2 4 6 8 Couple05 Couple06 0 2 4 6 8 Couple07 Couple08 Couple09 Couple10 Couple11 Couple12 Couple13 -0.5 0.0 0.5 1.0 Couple14 -0.5 0.0 0.5 1.0 Couple15 Couple16 Couple17 Couple18 Couple19 Couple20 Couple21 Couple22 Couple23 Couple24 Couple25 Couple26 Couple27 -0.5 0.0 0.5 1.0 Couple28 -0.5 0.0 0.5 1.0 Couple29 Couple30 Couple31 Couple32 Couple33 Couple34 Couple35 Couple36 0 2 4 6 8 Couple37 Couple38 0 2 4 6 8 Couple39 -0.5 0.0 0.5 1.0 Couple40
  36. 36. Testing Autocorrelation, continued • Does our lag 1 autocorrelation account for the sequential dependency? • Examine the autocorrelation of the model residuals • relationship %>% mutate(resids = resid(model.auto)) -> relationship • Now run acf.fnc() on resids rather than WarmthToday Baayen & Milin, 2010
  37. 37. Lag Acf -0.5 0.0 0.5 1.0 0 2 4 6 8 Couple01 Couple02 0 2 4 6 8 Couple03 Couple04 0 2 4 6 8 Couple05 Couple06 0 2 4 6 8 Couple07 Couple08 Couple09 Couple10 Couple11 Couple12 Couple13 -0.5 0.0 0.5 1.0 Couple14 -0.5 0.0 0.5 1.0 Couple15 Couple16 Couple17 Couple18 Couple19 Couple20 Couple21 Couple22 Couple23 Couple24 Couple25 Couple26 Couple27 -0.5 0.0 0.5 1.0 Couple28 -0.5 0.0 0.5 1.0 Couple29 Couple30 Couple31 Couple32 Couple33 Couple34 Couple35 Couple36 0 2 4 6 8 Couple37 Couple38 0 2 4 6 8 Couple39 -0.5 0.0 0.5 1.0 Couple40 No consistent autocorrelation remaining in the data
  38. 38. Week 11.2: Autocorrelation ! Autocorrelation ! Introduction ! Testing for Autocorrelation ! Visualizing Autocorrelation ! Direction & Lag ! Why Does Autocorrelation Matter? ! Lab
  39. 39. Why Does Autocorrelation Matter? • Autocorrelation can be an interesting research question in its own right • Important for model assumptions • A simple growth-curve model: • Assumes that time matters only insofar as there’s a general increase/decrease with time • Otherwise, all observations from a couple equally similar or dissimilar … error terms independent = Warmth Yi(j) γ00 Overall baseline Time γ10x1(j) + U0j Random intercept for couple + Error term— independent, identically distributed Ei(j) +
  40. 40. Why Does Autocorrelation Matter? • Autocorrelation can be an interesting research question in its own right • Important for model assumptions • Assumption all observations from a couple equally similar or dissimilar … error terms independent • Not true if there’s autocorrelation … observations similar in time dependent on one another • Underestimates standard error # inflates Type I error 1 2 3 4 5 L I N E A R T R E N D
  41. 41. Week 11.2: Autocorrelation ! Autocorrelation ! Introduction ! Testing for Autocorrelation ! Visualizing Autocorrelation ! Direction & Lag ! Why Does Autocorrelation Matter? ! Lab

×