# R/fns.R In psadil/cfsBayes: Stan Based Analysis of CFS Data

#### Defines functions SDTlikelihoodnormal_cdf

###
### Define useful functions
###
# Lookup - 01

normal_cdf = function(x,m,s) {
# Purpose:
# Calculates cdf for normal distributions, renamed for
# greater ease of transferring to Stan
# Arguments:
# x - data
# m - mean
# s - standard deviation

pnorm(x,m,s)
}

SDTlikelihood = function( resp, correct, kappa, dprime, prob = F) {
# Purpose:
# Likelihood function for SDT model reparameterized for 2AFC
# Arguments:
# resp    - A response (or vector of responses), 0 for left,
#           1 for right
# correct - The correct choice, 0 for left, 1 for right
# kappa   - A subject's criterion for choosing left or right,
#           where positive values indicate a bias towards left,
#           and negative values indicate a bias towards right
# dprime  - The degree of separation between the distributions for
#           the left and right distributions (larger values
#           indicate greater discriminability)
# prob    - If true, returns the probabilties for picking right
#           when the correct choice was either on the right or left
# Returns:
# A vector of probabilities or the likelihood for a response

# theta = P(Y|kappa,dprime)
# Subscript gives choice vs. correct
theta_RL <- 1.0 - normal_cdf( kappa, -dprime*.5, 1.0 );
theta_RR <- 1.0 - normal_cdf( kappa, dprime*.5, 1.0 );

if (prob) {
out = cbind( theta_RR, theta_RL )
} else {
out = dbinom( resp, 1, correct*theta_RR + (1-correct)*theta_RL )
}

out
}
psadil/cfsBayes documentation built on May 24, 2017, 12:17 p.m.