Pedro Alcocer

CV

Personal

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.


blog comments powered by Disqus