bayes: Bayesian Inference Cognitive Model

View source: R/model-bayes.R

bayesR Documentation

Bayesian Inference Cognitive Model

Description

bayes() fits a Bayesian cognitive model, updating beliefs about the probability of discrete event outcomes based on the frequencies of outcomes.

  • bayes_beta_c() fits a model for 2 outcomes (beta-binomial) for continuous responses

  • bayes_beta_d() fits a model for 2 outcomes (beta-binomial) for discrete responses

  • bayes_dirichlet_c() fits a model for n > 2 outcomes (dirichlet-categorical/multinomial) for continuous responses

  • bayes_dirichlet_d() fits a model for n > 2 outcomes (dirichlet-categorical/multinomial) for discrete responses

Usage

bayes_beta_c(
  formula,
  data,
  fix = NULL,
  format = c("raw", "count", "cumulative"),
  prior_sum = NULL,
  ...
)

bayes_beta_d(formula, data, fix = NULL, format = NULL, prior_sum = NULL, ...)

bayes_dirichlet_d(
  formula,
  data,
  fix = NULL,
  format = NULL,
  prior_sum = NULL,
  ...
)

bayes_dirichlet_c(
  formula,
  data,
  fix = NULL,
  format = NULL,
  prior_sum = NULL,
  ...
)

bayes(
  formula,
  data = data.frame(),
  fix = list(),
  format = c("raw", "count", "cumulative"),
  type = NULL,
  discount = 0L,
  options = list(),
  prior_sum = NULL,
  ...
)

Arguments

formula

A formula, the variables in data to be modeled. For example, y ~ x1 + x2 models response y as function of one stimulus with features x1, x2.

data

A data frame, the data to be modeled.

fix

(optional) A list with parameter-value pairs of fixed parameters. If missing all free parameters are estimated. If set to "start" all parameters are fixed to their start values. Model parameter names depend on formula and can be delta, x1, x2 (see details - model parameters).

  • list(x1 = 3.09) sets parameter x1 equal to 3.09.

  • list(x1 = "x2") sets parameter x1 equal to parameter x2 (estimates x2).

  • list(x2 = "x1", x1 = 3.09) sets parameter x2 equal to parameter x1 and sets x1 equal to 3.09 (estimates none of the two).

  • list(x1 = NA) omits the parameter x1, if possible.

  • "start" sets all parameters equal to their initial values (estimates none). Useful for building a first test model.

format

(optional) A string, the format the data to be modeled, can be abbreviated, default is "raw"; allowed values:

  • "raw" means that the data are trial-by-trial binary occurrence indicators: 1, 0, 1, ... means the event happened in trial with a value of 1.

  • "cumulative" means the data are trial-by-trial cumulative counts of events: 0, 1, 1, 2, ... counts how often the event happened up to the trial.

  • "count" means the data are total events counts, ignoring the trial-by-trial order of events: 2, 10, ... means the event happened 2 times, then (starting from zero!) it happened 10 times.

prior_sum

(optional) A number; the prior hyperparameter will be constrained to sum to this number; defaults to the number of prior parameters; if prior_sum = NA no sum constraint is placed.

...

other arguments, ignored.

type

(optional) A string, the type of inference, "beta-binomial" or "dirichlet-multinomial". Can be abbreviated. Will be inferred, if missing.

discount

A number, how many initial trials to not use during parameter fitting.

options

(optional) A list, list entries change the modeling procedure. For example, list(lb = c(k=0)) changes the lower bound of parameter k to 0, or list(fit_measure = "mse") changes the goodness of fit measure in parameter estimation to mean-squared error, for all options, see cm_options.

Details

The model models – as response – the belief about the occurrence of the first event in the formula as follows:

  • y ~ x1 models the beliefe about event x1 occurring versus it not occurring.

  • y ~ x1 + x2 models beliefs about x1 versus x2 occurring.

  • y ~ x1 + x2 + x3 models beliefs about x1, x2, and x3 occurring.

Model Parameters

The model has n + 1 (n = number of events) free parameters, which are:

  • delta is the learning rate, it weights the observation during learning, value < 1 causes conservatism, > 1 causes liberal learning, and 1 is optimal Bayesian.

  • x1, x2 (dynamic names) are the prior parameter, their names correspond to the right side of formula. Also known as the hyperparameter of the prior belief distribution before trial 1. If they are constrainted to sum to n and n - 1 parameter are estimated.

  • In bayes_beta_d() or bayes_dirichlet_d(): If choicerule = "softmax": tau is the temperature or choice softness, higher values cause more equiprobable choices. If choicerule = "epsilon": eps is the error proportion, higher values cause more errors from maximizing.

Value

Returns a cognitive model object, which is an object of class cm. A model, that has been assigned to m, can be summarized with summary(m) or anova(m). The parameter space can be viewed using pa. rspace(m), constraints can be viewed using constraints(m).

Author(s)

Markus Steiner

Jana B. Jarecki, jj@janajarecki.com

References

Griffiths, T. L., & Yuille, A. (2008). Technical Introduction: A primer on probabilistic inference. In N. Chater & M. Oaksford (Eds.), The Probabilistic Mind: Prospects for Bayesian Cognitive Science (pp. 1 - 2). Oxford University Press. https://doi.org/10.1093/acprof:oso/9780199216093.003.0002

Tauber, S., Navarro, D. J., Perfors, A., & Steyvers, M. (2017). Bayesian models of cognition revisited: Setting optimality aside and letting data drive psychological theory. Psychological Review, 124(4), 410 - 441. http://dx.doi.org/10.1037/rev0000052

See Also

Other cognitive models: baseline_const_c(), choicerules, cpt, ebm(), hm1988(), shift(), shortfall, threshold(), utility

Examples

D <- data.frame(
  a = c(0,0,1,1,1),              # event A, e.g. coin toss "heads"
  b = c(1,1,0,0,0),              # event B, complement of A
  y = c(0.5,0.3,0.2,0.3,0.5))    # participants' beliefs about A

M <- bayes_beta_c(
     formula = y ~ a + b,
     data = D)   # fit all parameters
predict(M)                        # predict posterior means
summary(M)                        # summarize model
parspace(M)                       # view parameter space
anova(M)                          # anova-like table
logLik(M)                         # loglikelihood
MSE(M)                            # mean-squared error   


# Predictions ----------------------------------------------
predict(M, type = "mean")                  # posterior mean
predict(M, type = "max")                   # maximum posterior
predict(M, type = "sd")                    # posterior SD
predict(M, type = "posteriorpar")          # posterior hyper-par.
predict(M, type = "draws", ndraws = 3)     #  --"--  3 draws


# Fix parameter ---------------------------------------------
bayes_beta_c(~a+b, D, list(delta=1, priorpar=c(1, 1)))  # delta=1, uniform prior
bayes_beta_c(~a+b, D, list(delta=1, a=1, b=1))          # -- (same) --
bayes_beta_c(~a+b, D, fix = "start")                    # fix to start values


# Parameter fitting ----------------------------------------
# Use a response variable, y, to which we fit parameter
bayes(y ~ a + b, D, fix = "start")              # "start" fixes all par., fit none 
bayes(y ~ a + b, D, fix = list(delta=1))         # fix delta, fit priors 
bayes(y ~ a + b, D, fix = list(a=1, b=1))        # fix priors, fit delta 
bayes(y ~ a + b, D, fix = list(delta=1, a=1))    # fix delta & prior on "a"
bayes(y ~ a + b, D, list(delta=1, b=1))          # fix delta & prior on "b"


### Parameter meanings
# ---------------------------------------
# delta parameter: the learning rate or evidence weight
bayes(y ~ a + b, D, c(delta = 0))             # 0   -> no learning
bayes(y ~ a + b, D, c(delta = 0.1))           # 0.1 -> slow learning
bayes(y ~ a + b, D, c(delta = 9))             # 9   -> fast learning
bayes(y ~ a + b, D, c(a=1.5, b=0.5))                # prior: a more likely
bayes(y ~ a + b, D, list(priorpar=c(1.5, 0.5)))     # -- (same) --
bayes(y ~ a + b, D, c(a = 0.1, b=1.9))              # prior: b more likely
bayes(y ~ a + b, D, list(priorpar = c(0.1, 1.9)))   # -- (same) --

JanaJarecki/cogscimodels documentation built on Nov. 4, 2022, 5:33 p.m.