bayes: Bayesian Inference Cognitive Model

Description Usage Arguments Details Value Author(s) References See Also Examples

View source: R/model-bayes.R

Description

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

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
bayes_beta_c(
  formula,
  data,
  fix = NULL,
  format = c("raw", "count", "cumulative"),
  priorsconstrained = FALSE,
  ...
)

bayes_beta_d(
  formula,
  data,
  fix = NULL,
  format = NULL,
  priorsconstrained = FALSE,
  ...
)

bayes_dirichlet_d(
  formula,
  data,
  fix = NULL,
  format = NULL,
  priorsconstrained = FALSE,
  ...
)

bayes_dirichlet_c(
  formula,
  data,
  fix = NULL,
  format = NULL,
  priorsconstrained = FALSE,
  ...
)

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

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 or the string "start", the fixed model parameters, if missing all parameters are estimated. 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.

priorsconstrained

(optional, default FALSE) Logical, TRUE constrains the prior parameter to sum to the number of options.

...

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:

Model Parameters

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

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(), shortfall, utility

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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 Sept. 8, 2020, 7:28 p.m.