Pedro Alcocer

CV

Personal

Code


Code


Calculating d-prime in R

This is how to calculate d-prime in R. This function takes a data frame called data and returns a d-prime score for each unique entry in the subject column of the data frame. Note that the function will return Inf or -Inf if any value in Hrate or Frate is 1 or 0. This happens whenever someone responds correctly or incorrectly on all the trials.

dprime <- function(data) {
    yes <- subset(data, resp=="Y")
    no <- subset(data, resp=="N")
    hit <- subset(data, resp=="Y" & acc == 1)
    falsealarm <- subset(data, resp=="N" & acc == 0)
    
    Hrate <- xtabs(~subject, data=hit)/xtabs(~subject, data=yes)
    Frate <- xtabs(~subject, data=falsealarm)/xtabs(~subject, data=no)
    dprime_score <- qnorm(Hrate) - qnorm(Frate)
    
    return(dprime_score)
}

A simple usage example:

> data
   subject resp acc
1        1    Y   0
2        1    Y   1
3        1    Y   1
4        1    N   0
5        1    N   1
6        2    Y   1
7        2    N   1
8        2    Y   0
9        2    Y   1
10       2    N   0

> dprime(data)
subject
        1         2 
0.4307273 0.4307273

View Comments


This page gets a lot of search hits, so I'd like to make it as useful as possible. If you find this post useful or confusing, or have questions about it, or have suggestions on how to improve it, please make use of the comments.


Repeated measures ANOVAs in R

Repeated measures ANOVAs in R are performed with the aov() function. The aov() function requires fully balanced data to work correctly, i.e., there can be no missing values in the data. In order to use aov() on data with missing values (most experimental data has excluded values), one needs to average over subjects or items.

Below, we assume a data frame data with at least the columns subject, factor1, factor2, and logRT. The code below averages over subjects, returning the data frame data.subject.

data.subject <- aggregate(data$logRT, list(data$subject, data$factor1, data$factor2), mean)
colnames(data.subject) <- c("subject", "factor1", "factor2", "logRT")

An alternative is to use the excellent reshape package.

library(reshape)
data.subject <- recast(data, SUBJ + factor1 + factor2 ~ variable, mean, measure.var="logRT")

Once you've created the data.subject data frame using one of the methods above, you can then perform the repeated measures ANOVA on data.subject.

summary(aov(logRT ~ factor1 * factor2 + Error(subject/(factor1 * factor2)), data=data.subject))

A by-items analysis is performed in the same way. You'd simply need to replace subject with item where appropriate.


This page gets a lot of search hits, so I'd like to make it as useful as possible. If you find this post useful or confusing, or have questions about it, or have suggestions on how to improve it, please make use of the comments.

View Comments