Behavioral Analysis Script

This script takes the cleaned master file, excludes a few subjects, then gets basic descriptive stats and computes behavioral analyses

# Load Packages -----------------------------------------------------------
library(ggplot2)
library(tidyverse)
library(readr)
library("car")

# Use data from package
library(totERP)
str(totBehavMasterCleaned)

Exclude subjects with <5 usable ERP Trials (this criterion came from checking ERP data) for both TOT and no-TOT conditions

# Remove Subjects Based on <5 Usable Trials in ERP Data
totBehavMasterCleaned <- subset(totBehavMasterCleaned, id !=2)
totBehavMasterCleaned <- subset(totBehavMasterCleaned, id !=5)
totBehavMasterCleaned <- subset(totBehavMasterCleaned, id !=19)
head(totBehavMasterCleaned)

Clean master of experimenter mistakes and code for descriptives

totBehavMasterCleaned <- subset(totBehavMasterCleaned, mistake == 0)
totBehavMasterCleaned$acc[totBehavMasterCleaned$recall == "C"] <- 1 #1 for correct
totBehavMasterCleaned$acc[totBehavMasterCleaned$recall == "I"] <- 0 #2 for incorrect
totBehavMasterCleaned$tot_cond[totBehavMasterCleaned$tot == "yes"] <- 1
totBehavMasterCleaned$tot_cond[totBehavMasterCleaned$tot == "no"] <- 0
totBehavMasterCleaned$tot_cond[totBehavMasterCleaned$tot == "N/A"] <- 0
totBehavMasterCleaned$ans_cond[totBehavMasterCleaned$tot == "yes"] <- 0
totBehavMasterCleaned$ans_cond[totBehavMasterCleaned$tot == "no"] <- 0
totBehavMasterCleaned$ans_cond[totBehavMasterCleaned$tot == "N/A"] <- 1
totBehavMasterCleaned$dk_cond[totBehavMasterCleaned$tot == "yes"] <- 0
totBehavMasterCleaned$dk_cond[totBehavMasterCleaned$tot == "no"] <- 1
totBehavMasterCleaned$dk_cond[totBehavMasterCleaned$tot == "N/A"] <- 0

Get the average proportion of questions eliciting TOT, 'Don't Know', and answer responses

# Average Proportion of TOTs

totsbysubject <- totBehavMasterCleaned %>%
  group_by(id) %>% summarise(n = n(), totnum = sum(tot_cond), totprob = totnum/n,
                             ansnum = sum(ans_cond), ansprob = ansnum/n,
                             dknum = sum(dk_cond), dkprob = dknum/n)

#probability of getting tot
mean(totsbysubject$totprob)
sd(totsbysubject$totprob)

# probability of getting answer
mean(totsbysubject$ansprob)
sd(totsbysubject$ansprob)

# probability of 'don't know'
mean(totsbysubject$dkprob)
sd(totsbysubject$dkprob)



# Check to make sure the means add up to 1
mean(totsbysubject$totprob) + mean(totsbysubject$ansprob) + mean(totsbysubject$dkprob)

Now, filter out trials where subjects answered

totBehavMasterCleaned <-filter(totBehavMasterCleaned, tot != "N/A") # Take Out Trials Where Subject Answered

How Many TOTs Did Each Subject Have?

totBehavMasterCleaned$tot <- as.factor(totBehavMasterCleaned$tot)

e <- totBehavMasterCleaned %>% 
  group_by(id, tot) %>%
  summarise(n = n(), recallnum = sum(acc), prob = recallnum/n)
head(e)



group_TOT_Plot <- ggplot(e, aes(x = tot, y = prob)) + 
  stat_summary(fun.data = mean_cl_boot) + 
  geom_jitter(size = .5, width = .02, col = 'blue')
group_TOT_Plot + 
  labs(x = "TOT State - points are individual subject probabilities", 
       y = "P(Recall)", 
       title = "Recall Probability by TOT")

# T Test


t.test(e$prob ~ e$tot, paired = T)

Some Summary Stats

mean(e$prob[e$tot == "yes"]) #.751 average recall following tot states
mean(e$prob[e$tot == "no"]) #.516 average recall following don't know
mean(e$prob[e$tot == "yes"]) - mean(e$prob[e$tot == "no"]) #23.5% difference
sd(e$prob[e$tot == "yes"]) #.115 sd for tot state recall
sd(e$prob[e$tot == "no"]) #.187 sd for don't know recall


# Other Summary Statistics -------------------------------------------

# Some descriptives on how many TOTs each subject experienced

head(totsbysubject)
summary(totsbysubject$totnum)
mean(totsbysubject$totnum)/150 #on average 21% of trials elicited TOTs
sd((totsbysubject$totnum)/150) #Standard deviation for proportion of trials eliciting TOTs (.085)

#How was recall in general?
totBehavMasterCleanedrecall <- totBehavMasterCleaned %>% 
  group_by(id) %>% summarise(n = n(), recallnum = sum(acc), recallprop = recallnum/n)

head(totBehavMasterCleanedrecall)
mean(totBehavMasterCleanedrecall$recallprop) #.59 mean recall rate overall
sd(totBehavMasterCleanedrecall$recallprop) #.16 sd for recall rate

Within-Subjects Scatter Plot of Recall Probability (Conditional on TOT/no-TOT)

e1 <- select(e, id, tot, prob)
subject_means_wide <-
  spread(e1,
         key = tot,
         value = prob,
         sep = "_")
head(subject_means_wide)


withinSubScatter <- ggplot(subject_means_wide, aes(x = tot_no, y = tot_yes)) +
  geom_point() +
  xlim(0,1) +
  ylim(0,1) +
  geom_abline() + 
  theme_bw() + 
  theme(aspect.ratio=1) +
  labs(x = 'P(Recall | No-TOT)', y = 'P(Recall | TOT)')
withinSubScatter

Within Subjects-Scatter With Bootstrapped Confidence Intervals

A quick function to get the standard error of a proportion

se_proportion <- function(p, n){
  se <- sqrt(p*(1-p)/n)
  return(se)
}

Get SE for each subject/condition

e$se <- se_proportion(e$prob, e$n)
e$lower <- e$prob - 2*e$se
e$upper <- e$prob + 2*e$se

Add total n and standard error to subject_means_wide data frame

se_wide <-
  spread((select(e, id, tot, se)),
         key = tot,
         value = se,
         sep = "_se_")
head(se_wide)

n_wide <-
  spread((select(e, id, tot, n)),
         key = tot,
         value = n,
         sep = "_n_")
head(n_wide)



# Join with standard errors and n
subject_means_wide <- dplyr::left_join(subject_means_wide, se_wide)
subject_means_wide <- dplyr::left_join(subject_means_wide, n_wide)

Re-plot

withinSubScatterConf <- ggplot(subject_means_wide, aes(x = tot_no, y = tot_yes)) +
  geom_point(color = 'red') +
  xlim(0,1) +
  ylim(0,1) +
  geom_abline() + 
  theme_bw() + 
  theme(aspect.ratio=1) +
  labs(x = 'P(Recall | No-TOT)', y = 'P(Recall | TOT)') +
  geom_errorbar(aes(ymin=(tot_yes - 2*tot_se_yes), ymax = (tot_yes + 2*tot_se_yes)), alpha = .3) +
  geom_errorbarh(aes(xmin=(tot_no - 2*tot_se_no), xmax = (tot_no + 2*tot_se_no)), alpha = .3) +
  geom_errorbar(data = dplyr::filter(subject_means_wide, id ==3 | id == 8 | id == 28), 
                aes(ymin=(tot_yes - 2*tot_se_yes), ymax = 1), alpha = .3, width = 0) +
  theme(panel.grid.minor = element_blank()) +
  theme(panel.grid.major = element_blank())


withinSubScatterConf

** Note -- I did manually put in error bars for 3 subjects (3, 8, 28) for which the upper bound of their 95% CI for P(Recall|TOT) > 1. I just capped the these bars at 1 and kept the same lower bound, although the estimated upper bound based on the standard error for each were 1.07, 1.04, and 1.01. Also, one subject (16) got 7/7 TOT questions correct, resulting in a 100% correct rate in this condition. Confidence intervals were not calculated for this condition for this subject.



pab2163/TOT_ERP documentation built on May 16, 2019, 8:13 p.m.