#### Homework 10 Key #### library(lmSupport) library(car) library(ggplot2) library(psych) #### 1.Read in, univariate and bivariate stats #### d <- dfReadDat("HW10_Data.dat") str(d) head(d) varDescribe(d) # Univariate: varPlot(d$Coordination) varPlot(d$Score) varPlot(d$Experience) # Bivariate corr.test(d) spm(d) #### 2. Make a descriptives table #### varDescribeBy(d$Score, IVList=list(Cond=d$Condition,Exper=d$Experience)) varDescribeBy(d$Coordination, IVList=list(Cond=d$Condition,Exper=d$Experience)) #### 3. Center variables #### d$ExperienceC <- varRecode(d$Experience, c(0,1), c(-0.5,0.5)) d$ConditionC <- varRecode(d$Condition, c(0,1), c(-0.5,0.5)) d$CoordinationC <- d$Coordination-mean(d$Coordination) #### 4. interaction experience and condition #### #4a. m1 <- lm(Score ~ ConditionC * ExperienceC, data=d) modelSummary(m1) modelEffectSizes(m1) confint(m1) #4b. # b0 represents the predicted score for someone who is neutral with respect to condition # and experience. # b1 represents the effect of condition for someone who is neutral with respect to experience # Those in the alcohol condition, on average, trend toward doing worse than those in the # placebo condition # b2 represents the effect of experience for someone who is neutral with respect to condition (or at average # of the conditions). There are no significant differences overall across experience groups # b3 quantifies how the (simple) effect of condition differs between those with low experience and those # with high experience. The interaction is significant. #4c. Describe the experience effect for those in alcohol group d$ConditionA <- varRecode(d$Condition, c(0,1), c(1,0)) m2 <- lm(Score ~ ConditionA * ExperienceC, data=d) modelSummary(m2) modelEffectSizes(m2) confint(m2) # There is a significant positive effect of experience for those in the Alcohol group, with # experienced players scoring 22 points higher than novices, on average. #4d. Describe the experience efffect for those in placebo group d$ConditionP <- d$Condition #since it is already coded properly m3 <- lm(Score ~ ConditionP * ExperienceC, data=d) modelSummary(m3) modelEffectSizes(m3) confint(m3) # There is a marginal effect of experience for participants in the placebo group with experienced # placebo participants scoring 18.7 points lower than novices, on average #4e. #### GRAPHING #### # Recode from numeric to actual names, make factors d$ConditionStr <- varRecode(d$Condition, c(0, 1), c("Placebo","Alcohol")) d$ExperienceStr <- varRecode(d$Experience, c(0, 1), c("Novice","Experienced")) d$ConditionStr = as.factor(d$ConditionStr) d$ExperienceStr = as.factor(d$ExperienceStr) # Re-run the model with IVs as factors and make sure that everything looks fine m4 <- lm(Score ~ ConditionStr + ExperienceStr + ConditionStr:ExperienceStr, data=d) modelSummary(m4) # Yhat predictions YhatPlacebo <- data.frame(ConditionStr = 'Placebo', ExperienceStr = c('Novice','Experienced')) YhatPlacebo <- modelPredictions(m4, YhatPlacebo) YhatAlcohol <- data.frame(ConditionStr = 'Alcohol', ExperienceStr = c('Novice','Experienced')) YhatAlcohol <- modelPredictions(m4, YhatAlcohol) Yhats <- rbind(YhatPlacebo,YhatAlcohol) # Graph plot4 <- ggplot(Yhats, aes(x = ConditionStr, y = Predicted, fill = ExperienceStr))+ geom_bar(stat='identity', position=position_dodge(.9))+ geom_errorbar(aes(ymax=CIHi, ymin=CILo), position=position_dodge(.9), width=0.25)+ theme_bw()+ theme(legend.title=element_blank(), panel.grid.major=element_blank(), panel.grid.minor=element_blank(), panel.border=element_blank(), axis.line=element_line(), text=element_text(family=''))+ ylab('Game Performance') + xlab('Intoxication Condition') plot4 #### 5a. Interaction condition and coordination #### m5 <- lm(Score ~ ConditionC * CoordinationC, data=d) modelSummary(m5) modelEffectSizes(m5) confint(m5) # 5b. # b0 represents the predicted score for someone who is average with respect to coordination # and condition. # b1 represents the condition effect for someone of average coordination. The effect is # non-significant in this sample. # b2 represents the coordination effect for someone who is neutral with respect to coordination # The effect is non-significant. # b3 indicates how much the coordination effect differs between conditions. This interaction # effect approached significance. ## 5c. #Describe the condition effect for someone with self-assessed coordination of 8 d$Coordination8 <- d$Coordination - 8 m6 <- lm(Score ~ ConditionC * Coordination8, data=d) modelSummary(m6) modelEffectSizes(m6) confint(m6) # Among those with a coordination score of 8, the effect of condition was significant, with # those in the alcohol group doing significantly worse than those in the placebo group. # 5d. #### GRAPHING #### # Already made factor variable for condition # Model with factorial dichotomous variable m7 <- lm(Score ~ ConditionStr * Coordination, data=d) modelSummary(m7) # predict data from the model separately by group YhatPlaceboB <- data.frame(Coordination=seq(min(d$Coordination),max(d$Coordination)),ConditionStr='Placebo') YhatPlaceboB <- modelPredictions(m7, YhatPlaceboB) YhatAlcoholB <- data.frame(Coordination=seq(min(d$Coordination),max(d$Coordination)),ConditionStr='Alcohol') YhatAlcoholB <- modelPredictions(m7, YhatAlcoholB) plot5 <- ggplot(data=d, aes(x = Coordination, y = Score)) + geom_point(aes(color=ConditionStr, shape=ConditionStr)) + scale_colour_brewer(palette="Set1") + geom_smooth(aes(ymin = CILo, ymax = CIHi, x = Coordination, y = Predicted), # Placebo group data = YhatPlaceboB, stat = "identity", color="blue") + geom_smooth(aes(ymin = CILo, ymax = CIHi, x = Coordination, y = Predicted), # Alcohol group data = YhatAlcoholB, stat = "identity", color="red") + theme_bw(base_size = 14) + labs(x = 'Coordination Before Dart Game', y = 'Game Performance') + coord_cartesian(ylim = c(0,150), xlim = c(0,11)) + theme(legend.position = c(.92,.92), legend.background = element_blank(), legend.title = element_blank()) plot5