Loglikelihood: Log likelihood (log loss)

Description Usage Arguments Value References Examples

Description

Log likelihood (log loss)

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
loglikelihood(
  obs,
  pred,
  pdf = c("binomial", "multinomial", "normal", "truncnormal"),
  na.rm = FALSE,
  n = NULL,
  saturated = FALSE,
  eps = sqrt(.Machine$double.eps),
  binomial.coef = FALSE,
  sigma = NULL,
  a = NULL,
  b = NULL,
  ...
)

Arguments

obs

A numeric vector or matrix, the observed data. Can be continuous values or dicrete. Can be aggregated, and if so you must supply n (see below). The default assumes raw data.

pred

A numeric vector or matrix with predictions, in the same order as obs.

pdf

A string, probability density function;, allowed are "binomial", "multinomial", "normal", "truncnormal". When using "normal" then pred needs two columns: observed means and observed standard deviations.

na.rm

(optional) Logical (default FALSE). TRUE removes all NA rows in pred or obs jointly (list-wise removal).

n

(optional) Integer or integer vector (default: 1), number of observations underlying obs if obs, pred or both are aggregated: n=10 means each aggregate represents 10 data points, a vector n=c(10,20) means the first aggregate represents 10, the second 20 data points, etc.

saturated

(optional) logical, compute saturated model (observed proportions as prediction, which approximates the highest possible likelihood). If so obs must be raw data, and pred can be omitted. Equal to obs being raw and pred being proportions.

eps

(optional) small numeric to offset predictions from zero in binomial pdf by substituting log(eps) for log(0) = -Inf. Don't change.

binomial.coef

(optional) Logical (defaul is FALSE). If TRUE the binomial coefficient is used with pdf="binomial". Mimicks results from dbinom. Ignored for other pdfs.

sigma

(optional) Standard deviation of the normal distribution, required for pdf = "normal", "truncnormal").

a, b

(optional) A number, lower/upper bound of the truncated pdf, required if pdf = "truncnormal".

...

Other parameters, currently ignored.

Value

The log likelihood of obs given pred. Lokelihoods are computed without the binomial coefficient.

References

Busemeyer, J. R., & Diederich, A. (2010). Nonlinear parameter estimation. In Cognitive Modeling (pp. 43–84). Thousand Oaks, CAL: SAGE Publications.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Example from Busemeyer & Diederich (2010)
# Observed relative frequencies of choice 1 from 11 conditions
obs <- c(.9538, .9107, .9204, .9029, .8515, .9197,
          .7970, .8228, .8191, .7277, .7276)
# Predictions for each of the 11 conditions
pred <- c(.9526, .9168, .8721, .8229, .7736, .7277,
          .6871, .6523, .6232, .5993, .5798)
n <- 200 # number of observations
# lnL in paper is -969.9514, < 0.1 % deviation
ll <- Loglikelihood(obs = obs, pred = pred, n = 200)
# all.equal(ll, -969.9514, 0.001)
# lnLS in paper is -879.9013, < 1 % deviation due to rounding
llsat <- Loglikelihood(obs = obs, n = 200, saturated = TRUE)
# all.equal(llsat, -879.9013, .01)

# Using the raw data
# Recreate the raw data (observations 0 or 1)
obsraw  <- rep(rep(0:1, 11), round(c(t(cbind(1-obs, obs))) * 200))
predraw <- rep(pred, each = 200)
ll <- Loglikelihood(obs = obsraw, pred = predraw)
# all.equal(ll, -969.9514, 0.001)
llsat <- Loglikelihood(obs = obsraw, pred = predraw, saturated = TRUE)
# all.equal(llsat, -879.9013, .01)

JanaJarecki/cognitiveutils documentation built on Sept. 9, 2020, 9:11 a.m.