R snippets

Using prop.test()

The following is some boilerplate R code I use to test whether, in my forced-choice (binomial) data, my participants are selecting one option different from chance (50%).

# Author: Lauren M Ackerman
# Available: https://lmackerman.com/r-snippets/
# Date: July 21, 2016

data = read.delim("rsmptv-fullsent.txt",header=TRUE,as.is=TRUE) #input data file

# "answer" is the responses which consist of 1s (hits) and 0s (misses)

sum.data.1 = sum(na.omit(data$answer[data$cond=="island"])) # find the number of 1s in the "island" condition
len.data.1 = length(na.omit(data$answer[data$cond=="island"])) # find the total number of points in the condition
 prop.test(sum.data.1, len.data.1, p = 0.5) # is the number of hits (1s) different from chance?

sum.data.2 = sum(na.omit(data$answer[data$cond=="matrix"])) # same as previous, but different condition ("matrix")
len.data.2 = length(na.omit(data$answer[data$cond=="matrix"]))
 prop.test(sum.data.2, len.data.2, p = 0.5)
summary(lmer(answer~cond+(1+cond|subj)+(1|item),data)) # summary of lmer to get Estimate and Standard Error
max = lmer(answer~cond+(1+cond|subj)+(1|item),data) # store lmer for comparison
less = lmer(answer~1+(1+cond|subj)+(1|item),data) # remove relevant term to see if it's relevant*
anova(max,less) # anova test tells you the p value for "cond" (the estimate+SE)

# Report:
# prop.test...
# χ²(df), p, 95% CI (lower-upper)
# lmer...
# Estimate, SE, χ²(df), p

# * normally, you might have a 2x2 design, but for this experiment, there was a 1x2 design, which has only one independent factor ("cond")