#' An A prime function
#'
#' This function allows you to calculate A prime from a vector hits
#' and a vector a false alarms.
#'
#' @param data A data frame.
#' @param h A vector of hits (0 = miss, 1 = hit).
#' @param f A vector of false alarms (0 = correct rejection, 1 = false alarm).
#' @keywords A prime
#' @export
#' @examples
#' # Create some data
#' set.seed(1); library(dplyr)
#' axb <- data.frame(subj = sort(rep(1:10, each = 20, times = 10)),
#' group = gl(2, 1000, labels = c("g1", "g2")),
#' hit = c(rbinom(1000, size = c(0, 1), prob = .8),
#' rbinom(1000, size = c(0, 1), prob = .6)),
#' fa = c(rbinom(1000, size = c(0, 1), prob = .3),
#' rbinom(1000, size = c(0, 1), prob = .4))
#' )
#'
#' # Calculate A' on entire data frame
#' aPrime(axb, hit, fa)
#'
#' # Calculate A' for each subject
#' # by group, plot it, and run a
#' # linear model
#' axb %>%
#' group_by(subj, group) %>%
#' summarize(ap = aPrime(., hit, fa)) %T>%
#' {
#' plot(ap ~ as.numeric(group), data = .,
#' main = "A' as a function of group", xaxt = "n",
#' xlab = "Group", ylab = "A'")
#' axis(1, at = 1:2, labels = c("g1", "g2"))
#' abline(lm(ap ~ as.numeric(group), data = .), col = "red")
#' } %>%
#' lm(ap ~ group, data = .) %>%
#' summary()
aPrime <-function(data, h, f){
if(!is.data.frame(data)) {
stop('I am so sorry, but this function requires a dataframe\n',
'You have provided an object of class: ', class(data)[1])
}
# Make columns of dataframe available w/o quotes
arguments <- as.list(match.call())
hits = eval(arguments$h, data)
fas = eval(arguments$f, data)
hRate = mean(hits)
faRate = mean(fas)
if(hRate >= faRate)
{
ap <- .5 + (((hRate - faRate) * (1 + hRate - faRate)) / ((4 * hRate) * (1 - faRate)))
} else if(hRate < faRate)
{
ap <- .5 - (((faRate - hRate) * (1 + faRate - hRate)) / ((4 * faRate) * (1 - hRate)))
}
return(ap)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.