# Analysis for Stothart, Simons, Boot, and Kramer (2014) # Author: Cary Stothart # Date: 12/05/2013 library(ggplot2) library(reshape) library(gmodels) library(scales) library(extrafont) my.desc <- function(x) { return(c(Mean=mean(x), SD=sd(x), Median=median(x), IQR=IQR(x))) } # Open and create datasets. init.data <- read.delim("Data_S1_Dataset.txt", header=TRUE, sep="\t") init.data$count <- 1 ##### FILTERS ##### # Only good participants without knowledge about exercise literature. w.data <- init.data[init.data$exclude==0, ] # Only good sedentary participants. #w.data <- init.data[init.data$exclude==0 & init.data$does_part_exer=="No", ] ################### w.data$part_num <- factor(w.data$part_num) w.data$exer_group_plot <- NA w.data$exer_group_plot[w.data$exer_group=="Walking"] <- "Aerobic" w.data$exer_group_plot[w.data$exer_group=="Toning"] <- "Nonaerobic" w.data$exer_group_plot <- factor(w.data$exer_group_plot) w.data$exer_group_plot <- relevel(w.data$exer_group_plot, ref="Nonaerobic") improve.l.data <- melt(w.data, variable_name="task", measure.vars=c("exer_improve_rt", "exer_improve_switch", "exer_improve_memory")) improve.l.data$part_num <- factor(improve.l.data$part_num) amount.l.data <- melt(w.data, variable_name="task", measure.vars=c("exer_improve_rt_amount", "exer_improve_switch_amount", "exer_improve_memory_amount")) amount.l.data$part_num <- factor(amount.l.data$part_num) l.data <- data.frame(improve.l.data$part_num, improve.l.data$exer_group, improve.l.data$age, improve.l.data$task, improve.l.data$value, amount.l.data$value, improve.l.data$does_part_exer, improve.l.data$exclude_knowledge) colnames(l.data) <- c("part_num", "exer_group", "age", "task", "improve", "improve_amount", "does_part_exer", "exclude_knowledge") l.data$count <- 1 l.data$task <- revalue(l.data$task, c("exer_improve_rt"="Reaction Time", "exer_improve_switch"="Task Switching", "exer_improve_memory"="Relational Memory")) l.data$cells <- paste(l.data$exer_group, l.data$task, l.data$improve) l.data$exclude_knowledge <- factor(l.data$exclude_knowledge) # Descriptives aggregate(count~exer_group, FUN=sum, data=w.data) aggregate(count~improve:task:exer_group, FUN=sum, data=l.data) aggregate(improve_amount~exer_group:task, FUN=my.desc, data=l.data[l.data$improve=="Yes", ]) aggregate(count~exer_group:gender, FUN=sum, data=w.data) aggregate(age~exer_group, FUN=function(x) c(mean=mean(x), sd=sd(x)), data=w.data) # Theme for plots font_import(pattern="[A/a]rial") y loadfonts(device="win") font <- "Arial" plot.theme <- theme(panel.background=element_rect(fill="#ffffff", color="#ffffff"), axis.title.x=element_text(face="plain", size=14, color="#000000", vjust=0, family=font), axis.title.y=element_text(face="plain", size=14, color="#000000", vjust=.25, family=font), axis.text.x=element_text(face="plain", size=14, color="#000000", family=font), axis.text.y=element_text(face="plain", size=14, color="#000000", family=font), legend.title=element_text(face="plain", size=14, color="#000000", family=font), legend.text=element_text(face="plain", size=14, color="#000000", family=font), axis.ticks=element_blank(), panel.grid.minor=element_blank(), panel.grid.major=element_blank()) # Improvement rate plot l.data$exer_group_plot <- NA l.data$exer_group_plot[l.data$exer_group=="Walking"] <- "Aerobic" l.data$exer_group_plot[l.data$exer_group=="Toning"] <- "Nonaerobic" l.data$exer_group_plot <- factor(l.data$exer_group_plot) l.data$exer_group_plot <- relevel(l.data$exer_group_plot, ref="Nonaerobic") bar.data <- aggregate(count~task:exer_group_plot, FUN=sum, data=l.data[l.data$improve=="Yes", ]) bar.data$total <- nrow(w.data[w.data$exer_group_plot=="Nonaerobic", ]) bar.data$total[bar.data$exer_group_plot=="Aerobic"] <- nrow(w.data[w.data$exer_group_plot=="Aerobic", ]) bar.data$percentage <- bar.data$count / bar.data$total bar.plot1 <- ggplot(data=bar.data, aes(x=task, y=percentage, ymax=1)) + geom_bar(aes(fill=exer_group_plot), position=position_dodge(0.9)) + #geom_text(aes(y=percentage+.025, x=task, group=exer_group_plot, # label=paste(sprintf("%.2f", percentage*100), "%", sep="")), # position=position_dodge(0.9), size=4.94) + xlab("") + ylab("Percentage of Participants who Expected Improvement") + scale_y_continuous(labels=percent, expand=c(0, 0), breaks=seq(from=0, to=1, by=.2)) + scale_x_discrete(expand=c(0, 0)) + scale_fill_manual("Intervention\nPresented", values=c("#12006f", "#8f9a9d")) + plot.theme bar.plot1 ggsave(filename="FigureX_ImproveRate.tiff", dpi=300) # Improvement amount plot bar.data2 <- aggregate(improve_amount~task:exer_group_plot, data=l.data[l.data$improve=="Yes", ], FUN=function(x) c(mean=mean(x), ci.error=qt(.975, df=length(x)-1)*sd(x)/sqrt(length(x)))) bar.plot2 <- ggplot(data=bar.data2, aes(x=task, y=bar.data2$improve_amount[, 1])) + geom_bar(aes(fill=exer_group_plot), position=position_dodge(0.9)) + geom_errorbar(aes(x=task, group=exer_group_plot, ymax=bar.data2$improve_amount[, 1]+ bar.data2$improve_amount[, 2], ymin=bar.data2$improve_amount[, 1]- bar.data2$improve_amount[, 2], width=.5), size=.5, position=position_dodge(0.9)) + xlab("") + ylab("Amount of Expected Improvement") + scale_y_continuous(expand=c(0, 0)) + scale_x_discrete(expand=c(0, 0)) + scale_fill_manual("Intervention\nPresented", values=c("#12006f", "#8f9a9d")) + plot.theme bar.plot2 ggsave(filename="FigureX_ImproveAmount.tiff", dpi=300) # Chi-squares for improvement data. rt <- l.data[l.data$task=="Reaction Time", ] # Complex RT CrossTable(rt$exer_group, rt$improve, fisher=TRUE, chisq=TRUE, expected=TRUE) switching <- l.data[l.data$task=="Task Switching", ] # Task Switching CrossTable(switching$exer_group, switching$improve, fisher=TRUE, chisq=TRUE, expected=TRUE) memory <- l.data[l.data$task=="Relational Memory", ] # Relational Memory CrossTable(memory$exer_group, memory$improve, fisher=TRUE, chisq=TRUE, expected=TRUE) # Logistic regressions for improvement data. rt_fit <- glm(improve~exer_group, data=l.data, family=binomial, subset=task=="Reaction Time") summary(rt_fit) ts_fit <- glm(improve~exer_group, data=l.data, family=binomial, subset=task=="Task Switching") summary(ts_fit) rm_fit <- glm(improve~exer_group, data=l.data, family=binomial, subset=task=="Relational Memory") summary(rm_fit) # T-tests for improvement amount data. t.test(improve_amount~exer_group, data=rt, subset=improve=="Yes", var.equal=TRUE) # Complex RT t.test(improve_amount~exer_group, data=switching, subset=improve=="Yes", var.equal=TRUE) # Task Switching t.test(improve_amount~exer_group, data=memory, subset=improve=="Yes", var.equal=TRUE) # Relational Memory