library(knitr)
library(cowplot)
library(tidyverse)
library(ggthemes)
theme_set(theme_few())
sem <- function(x) {sd(x, na.rm=TRUE) / sqrt(sum(!is.na((x))))}
ci <- function(x) {sem(x) * 1.96} # reasonable approximation
This is problem set #4, in which we hope you will practice the visualization package ggplot2, as well as hone your knowledge of the packages tidyr and dplyr. You’ll look at two different datasets here.
First, data on children’s looking at social targets from Frank, Vul, Saxe (2011, Infancy).
Second, data from Sklar et al. (2012) on the unconscious processing of arithmetic stimuli.
In both of these cases, the goal is to poke around the data and make some plots to reveal the structure of the dataset.
This part is a warmup, it should be relatively straightforward ggplot2 practice.
Load data from Frank, Vul, Saxe (2011, Infancy), a study in which we measured infants’ looking to hands in moving scenes. There were infants from 3 months all the way to about two years, and there were two movie conditions (Faces_Medium, in which kids played on a white background, and Faces_Plus, in which the backgrounds were more complex and the people in the videos were both kids and adults). An eye-tracker measured children’s attention to faces. This version of the dataset only gives two conditions and only shows the amount of looking at hands (other variables were measured as well).
fvs <- read_csv("data/FVS2011-hands.csv")
fvs$condition = as.factor(fvs$condition)
First, use ggplot to plot a histogram of the ages of children in the study. NOTE: this is a repeated measures design, so you can’t just take a histogram of every measurement.
fvs %>%
group_by(subid) %>%
# average across measurements for each subject
summarise(avg_age = mean(age)) %>%
ggplot(aes(x=avg_age)) + geom_histogram(bins=35) +
ggtitle("Average age per subject")
Second, make a scatter plot showing hand looking as a function of age and condition. Add appropriate smoothing lines. Take the time to fix the axis labels and make the plot look nice.
ggplot(fvs, aes(x=age, y=hand.look, color=condition)) +
geom_point() +
geom_smooth(method=lm, alpha=.2) +
xlab("Age") + ylab("Looking time") +
guides(fill=guide_legend(title="New Legend Title")) +
ggtitle("Relationship between hand-looking and age by condition")
What do you conclude from this pattern of data?
There seems to be a main effect of age on looking time, and an interaction between age and looking time; the slope in the
Faces_Pluscondition seems to be steeper.
What statistical analyses would you perform here to quantify these differences?
interaction_analysis = summary(lm("hand.look ~ age * condition", fvs))
In the model above, there is a significant relationship between age and looking time (p < 0.01), and an interaction between age and condition (p < 0.02)
Sklar et al. (2012) claim evidence for unconscious arithmetic processing - they prime participants with arithmetic problems and claim that the authors are faster to repeat the answers. We’re going to do a reanalysis of their Experiment 6, which is the primary piece of evidence for that claim. The data are generously shared by Asael Sklar. (You may recall these data from the tidyverse tutorial earlier in the quarter).
First read in two data files and subject info. A and B refer to different trial order counterbalances.
subinfo <- read_csv("data/sklar_expt6_subinfo_corrected.csv")
d_a <- read_csv("data/sklar_expt6a_corrected.csv")
d_b <- read_csv("data/sklar_expt6b_corrected.csv")
gather the d_a and d_b datasets into long (“tidy data”) form. If you need to review tidying, here’s the link to R4DS (bookmark it!). Remember that you can use select_helpers to help in your gathering.
Once you’ve tidied, bind all the data together into one dataset. Check out bind_rows.
The resulting tidy dataset should look like this:
prime prime.result target congruent operand distance counterbalance subid rt
<chr> <int> <int> <chr> <chr> <int> <int> <dbl> <int>
1 =1+2+5 8 9 no A -1 1 1 597
2 =1+3+5 9 11 no A -2 1 1 699
3 =1+4+3 8 12 no A -4 1 1 700
4 =1+6+3 10 12 no A -2 1 1 628
5 =1+9+2 12 11 no A 1 1 1 768
6 =1+9+3 13 12 no A 1 1 1 595
d_a_tidy = d_a %>%
gather(num_range('', colnames(d_a)[c(8:length(colnames(d_a)))]), key='subid', value = 'rt')
d_b_tidy = d_b %>%
gather(num_range('', colnames(d_b)[c(8:length(colnames(d_b)))]), key='subid', value = 'rt')
d_ab = bind_rows(d_a_tidy, d_b_tidy)
Merge these with subject info. You will need to look into merge and its relatives, left_ and right_join. Call this dataframe d, by convention.
d = merge(d_ab, subinfo) # that simple?
Clean up the factor structure (just to make life easier). No need to, but if you want, you can make this more tidyverse-ish.
d$presentation.time <- factor(d$presentation.time)
levels(d$operand) <- c("addition","subtraction")
d$operand = as.factor(d$operand)
Examine the basic properties of the dataset. First, show a histogram of reaction times.
ggplot(d, aes(x=rt)) +
geom_histogram(bins=30, na.rm = TRUE) +
ggtitle("Distribution of reaction times")
Sklar et al. did two manipulation checks. Subjective - asking participants whether they saw the primes - and objective - asking them to report the parity of the primes (even or odd) to find out if they could actually read the primes when they tried. Examine both the unconscious and conscious manipulation checks. What do you see? Are they related to one another?
manipulation_check = d %>%
group_by(subid) %>%
summarise(objective_test = mean(objective.test),
subjective_test = as.factor(mean(subjective.test)))
check_relationship = summary(lm('objective_test ~ subjective_test', data=manipulation_check))$coefficients[2,4]
ggplot(manipulation_check, aes(y=objective_test, x=subjective_test)) + geom_point() +
ggtitle(sprintf("Relating subjective and objective tests for each subject (p<%.04f)",check_relationship))
It appears that the conscious and unconscious manipulation checks are related; subjects who report that they could see the primes score higher on objective_test (p < 0.0001)
In Experiments 6, 7, and 9, we used the binomial distribution to determine whether each participant performed better than chance on the objective block and excluded from analyses all those participants who did (21, 30, and 7 participants in Experiments 6, 7, and 9, respectively). Note that, although the number of excluded participants may seem high, they fall within the normal range of long-duration CFS priming, in which successful suppression is strongly affected by individual differences (38). We additionally excluded participants who reported any subjective awareness of the primes (four, five, and three participants in Experiments 6, 7, and 9, respectively).
OK, let’s turn back to the measure and implement Sklar et al.’s exclusion criterion. You need to have said you couldn’t see (subjective test) and also be not significantly above chance on the objective test (< .6 correct). Call your new data frame ds.
ds = d %>%
filter(subjective.test==0 & objective.test<.6)
sklar_et_al_2012_facilitation_effect
Sklar et al. show a plot of a “facilitation effect” - the amount faster you are for prime-congruent naming compared with prime-incongruent naming. They then show plot this difference score for the subtraction condition and for the two prime times they tested. Try to reproduce this analysis.
HINT: first take averages within subjects, then compute your error bars across participants, using the ci function (defined above). Sklar et al. use SEM (and do it incorectly, actually), but CI is more useful for “inference by eye” as discussed in class.
HINT 2: remember that in class, we reviewed the common need to group_by and summarise twice, the first time to get means for each subject, the second time to compute statistics across subjects.
HINT 3: The final summary dataset should have 4 rows and 5 columns (2 columns for the two conditions and 3 columns for the outcome: reaction time, ci, and n).
ds_summary = ds %>%
group_by(subid, presentation.time, congruent, operand) %>%
summarise(subject_mean = mean(rt, na.rm = TRUE)) %>%
spread(key=congruent, value = subject_mean) %>%
mutate(facilitation=no-yes) %>%
group_by(presentation.time, operand) %>%
summarise(mean=mean(facilitation),
ci=ci(facilitation),
n=n())
kable(ds_summary)
| presentation.time | operand | mean | ci | n |
|---|---|---|---|---|
| 1700 | A | -13.928899 | 17.447646 | 8 |
| 1700 | S | 20.999096 | 12.299577 | 8 |
| 2000 | A | 4.025963 | 9.843905 | 9 |
| 2000 | S | 9.975617 | 8.716826 | 9 |
Now plot this summary, giving more or less the bar plot that Sklar et al. gave (though I would keep operation as a variable here. Make sure you get some error bars on there (e.g. geom_errorbar or geom_linerange).
reproduce_figure = function(operand_type) {
plot = ds_summary %>%
filter(operand==operand_type) %>%
ggplot(aes(x=presentation.time, y=mean, fill=operand_type)) +
geom_bar(stat='identity', width=.4, fill="grey ", colour="black", size=1.5) +
geom_errorbar(aes(x=presentation.time, ymin=mean-ci, ymax=mean+ci), width=.05, size=1.5) +
theme(panel.grid.major.x = element_blank() ,
panel.grid.major.y = element_line( size=.6, color="grey" ),
axis.text = element_text(face="bold", color="black",size=20),
panel.border = element_blank(),
axis.ticks =element_blank(),
axis.title = element_text( face="bold", colour="black", size=20),
plot.title = element_text(size=25, face="bold")
) +
xlab("\nPresentation Duration") +
ylab("Facilitation (ms)\n") +
ggtitle(paste0("Experiment 6 (", operand_type, ')')) +
scale_y_continuous(labels=function(x) sprintf("%.3f", x))
return(plot)
}
s_plot = reproduce_figure('S')
a_plot = reproduce_figure('A')
plot_grid(s_plot, a_plot)
What do you see here? How close is it to what Sklar et al. report? How do you interpret these data?
When we restrict our analysis to the
Soperand condition, we recover the mean effects reported by the authors. Using 95% CIs, the pattern of data appears to be different; there appears to me much more variance in each of the presentation durations, so much that there no longer seems to be a significant difference between the two groups.