# Drill 8 (April 4, 2024) # new libraries for most of you # install.packages("ez") # install.packages("lme4") # Libraries --------------------------------------------------------------- library(tidyverse) library(ez) library(lme4) # options(digits = 2) # use only if you want to keep R from going wild with decimal places # The data ---------------------------------------------------------------- df <- read_csv("http://whlevine.hosted.uark.edu/psyc5143/iguanas.csv") head(df, 15) # Iguanas are thought to use their tongues to sample the environment to detect # friend or foe. To experimentally test this, 10 iguanas are each exposed to # five different kinds of sand: two control conditions: home and clean; three # treatment conditions: iguana (fellow species members), lizard (a competitor # species), and kangaroo rats (another competitor species). The number of times # they extrude their tongue is counted to see if it varies across environments. # The ez package ---------------------------------------------------------- # Descriptive stats df %>% group_by(sand) %>% summarise(n = n(), M = mean(extrusions), SD = sd(extrusions)) ezStats(data = df, # the data frame wid = ID, # the ID variable for subjects within = sand, # the within-subjects factor dv = extrusions) # the DV # Inferential stats (i.e. RM ANOVA) ezANOVA(data = df, # the data wid = ID, # an identifier for the unit in which repeated measures occur within = sand, # the within-subjects factor dv = extrusions, # the DV detailed = T # include if you want to get SSs ) # Note the result for the sand factor: F(4, 36) = 8.8 # (we'll compare this with another model shortly) ezPlot(data = df, # a plot of results dv = extrusions, wid = ID, within = sand, x = sand) # RM as a mixed-effects model --------------------------------------------- # lmer(outcome ~ fixed_effect_1 * fixed_effect_2 + (random_effects | within_ID_variable)) df$id.f <- as.factor(df$ID) df$sand <- as.factor(df$sand) # not necessary, strictly speaking; lmer treats # it as a factor by default, but this is a good habit modelLMM <- lmer(extrusions ~ 1 + factor(sand) + (1|id.f), df) anova(modelLMM) ezANOVA(data = df, wid = ID, within = sand, dv = extrusions, detailed = T )$ANOVA$F[2] # paired-sample t-tests are good, too! (just keep FWER/FDR in mind) df %>% filter(sand == "clean" | sand == "iguana") %>% t.test(extrusions ~ sand, paired = T, data = .) df %>% filter(sand == "clean" | sand == "lizard") %>% t.test(extrusions ~ sand, paired = T, data = .)