diff --git a/DESCRIPTION b/DESCRIPTION index 1c842c72..046910b3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: bmm Title: Easy and Accesible Bayesian Measurement Models using 'brms' -Version: 0.1.1.9000 +Version: 0.2.0 Authors@R: c( person("Vencislav", "Popov", , "vencislav.popov@gmail.com", role = c("aut", "cre", "cph")), person("Gidon", "Frischkorn", , "gidon.frischkorn@psychologie.uzh.ch", role = c("aut", "cph"))) diff --git a/NEWS.md b/NEWS.md index cf60e5a8..497661f3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,4 @@ -# bmm 0.1.1+ +# bmm 0.2.0 ### New features diff --git a/dev_utils/stan_data_structure.html b/dev_utils/stan_data_structure.html deleted file mode 100644 index 2b3f3b91..00000000 --- a/dev_utils/stan_data_structure.html +++ /dev/null @@ -1,4889 +0,0 @@ - - - - - - - - - - -BRMS Stan data structure - - - - - - - - - - - - - - - - - - - -
- -
- -
-
-

BRMS Stan data structure

-
- - - -
- -
-
Author
-
-

Ven Popov

-
-
- - - -
- - - -
- - -
-
-
- -
-
-Note -
-
-
-

This document is a work in progress

-
-
-
-

Introduction

-

This is a utility document to explore the stan data structure generated by brms for a variety of formulas. The goal is to understand how the data is organized so that I can write a user-friendly function for setting initial values1. Currently it’s quite opaque and cumbersome2.

-

To generate initial value, we need to know two things:

-
    -
  • How key information about the data and predictors is structured (from the stan data)
  • -
  • Which parameters are being estimated (from the stan code)
  • -
-
-
-

Example init function

-

Following the example by Henrik, here’s a function that sets initial values for a simple model (in bmm, but raw brms is similar). First, we need to extract the stan data and the stan code for the parameters block:

-
-
-Code -
pacman::p_load(here, tidyverse, brms, bmm)
-
-# setup data and model formula
-dat <- bmm::OberauerLin_2017
-dat$SetSize <- as.factor(dat$SetSize)
-formula <- bf(dev_rad ~ 1,
-              c ~ 0 + SetSize + (0 + SetSize || ID),
-              kappa ~ 1 + (1 | ID))
-
-# get standata
-sdat <- get_standata(formula = formula,
-                     data = dat,
-                     model = sdmSimple())
-
-# get stan code for parameters block
-stan_code <- get_stancode(formula = formula,
-                          data = dat,
-                          model = sdmSimple())
-par_block <- str_match(as.character(stan_code), 
-                       "(?s)parameters \\{\\n(.*?)\\}\\ntransformed")[,2]
-
-
-

The standata looks like this:

-
-
str(sdat,  give.attr=F) 
-
-
List of 28
- $ N          : int 15200
- $ Y          : num [1:15200(1d)] 0.384 -0.4538 -0.0873 0.3665 -0.0349 ...
- $ K          : int 1
- $ Kc         : num 0
- $ X          : num [1:15200, 1] 1 1 1 1 1 1 1 1 1 1 ...
- $ K_c        : int 8
- $ X_c        : num [1:15200, 1:8] 0 0 0 0 1 1 0 0 0 0 ...
- $ Z_1_c_1    : num [1:15200(1d)] 0 0 0 0 1 1 0 0 0 0 ...
- $ Z_1_c_2    : num [1:15200(1d)] 0 0 0 0 0 0 0 0 1 0 ...
- $ Z_1_c_3    : num [1:15200(1d)] 0 1 0 0 0 0 1 0 0 0 ...
- $ Z_1_c_4    : num [1:15200(1d)] 0 0 0 0 0 0 0 0 0 0 ...
- $ Z_1_c_5    : num [1:15200(1d)] 0 0 1 0 0 0 0 0 0 1 ...
- $ Z_1_c_6    : num [1:15200(1d)] 0 0 0 1 0 0 0 0 0 0 ...
- $ Z_1_c_7    : num [1:15200(1d)] 1 0 0 0 0 0 0 0 0 0 ...
- $ Z_1_c_8    : num [1:15200(1d)] 0 0 0 0 0 0 0 1 0 0 ...
- $ K_kappa    : int 1
- $ Kc_kappa   : num 0
- $ X_kappa    : num [1:15200, 1] 1 1 1 1 1 1 1 1 1 1 ...
- $ Z_2_kappa_1: num [1:15200(1d)] 1 1 1 1 1 1 1 1 1 1 ...
- $ J_1        : int [1:15200(1d)] 1 1 1 1 1 1 1 1 1 1 ...
- $ J_2        : int [1:15200(1d)] 1 1 1 1 1 1 1 1 1 1 ...
- $ N_1        : int 19
- $ M_1        : int 8
- $ NC_1       : int 28
- $ N_2        : int 19
- $ M_2        : int 1
- $ NC_2       : int 0
- $ prior_only : int 0
-
-
-

And the parameters block looks like this:

-
-
cat(par_block)
-
-
  vector[K_c] b_c;  // regression coefficients
-  real Intercept_kappa;  // temporary intercept for centered predictors
-  vector<lower=0>[M_1] sd_1;  // group-level standard deviations
-  array[M_1] vector[N_1] z_1;  // standardized group-level effects
-  vector<lower=0>[M_2] sd_2;  // group-level standard deviations
-  array[M_2] vector[N_2] z_2;  // standardized group-level effects
-
-
-

We need to define initial values for every parameter listed in the parameters block, and get the appropriate dimensions from the stan data. Here’s a function that does this:

-
-
initfun <- function(seed) {
-  set.seed(seed)
-  list(
-    b_c = as.array(runif(sdat$K_c,0,2)),
-    Intercept_kappa = runif(1,0,2),
-    sd_1 = as.array(runif(sdat$M_1,0,1)),
-    sd_2 = as.array(runif(sdat$M_2,0,1)),
-    z_1 = matrix(rnorm(sdat$M_1*sdat$N_1, 0, 0.01),
-                 sdat$M_1, sdat$N_1),
-    z_2 = matrix(rnorm(sdat$M_2*sdat$N_2, 0, 0.01),
-             sdat$M_2, sdat$N_2)
-  )
-}
-
-

This function can be used to set initial values for the model:

-
-
initlist <- list(
-  initfun(1),
-  initfun(2),
-  initfun(3),
-  initfun(4)
-)
-
-

and then we would run the model as

-
fit_model(formula, dat, sdmSimple(), 
-          parallel=T, init=initlist, backend='cmdstanr')
-

But I don’t want to code this initfun for every possible design and model.

-
-
-

Formula generation

-

Generate a bunch of different formulas:

-
-
-Code -
dat <- simData
-ff <- list()
-
-i=1
-
-ff[[i]] <- brms::bf(dev_rad ~ 1,
-                    c ~ 1,
-                    kappa ~ 1); i=i+1
-ff[[i]] <- brms::bf(dev_rad ~ 1,
-                    c ~ setsize,
-                    kappa ~ 1); i=i+1
-ff[[i]] <- brms::bf(dev_rad ~ 1,
-                    c ~ 0 + setsize,
-                    kappa ~ 1); i=i+1
-ff[[i]] <- brms::bf(dev_rad ~ 1,
-                    c ~ 0 + setsize,
-                    kappa ~ material); i=i+1
-ff[[i]] <- brms::bf(dev_rad ~ 1,
-                    c ~ 0 + setsize*material,
-                    kappa ~ material); i=i+1
-ff[[i]] <- brms::bf(dev_rad ~ 1,
-                    c ~ 0 + setsize*material,
-                    kappa ~ setsize*material); i=i+1
-ff[[i]] <- brms::bf(dev_rad ~ 1,
-                    c ~ 1 + (1|subID),
-                    kappa ~ 1); i=i+1
-ff[[i]] <- brms::bf(dev_rad ~ 1,
-                    c ~ 1,
-                    kappa ~ 1 + (1|subID)); i=i+1
-ff[[i]] <- brms::bf(dev_rad ~ 1,
-                    c ~ 1 + (1|subID),
-                    kappa ~ 1 + (1|subID)); i=i+1
-ff[[i]] <- brms::bf(dev_rad ~ 1,
-                    c ~ 1 + (1|p1|subID),
-                    kappa ~ 1 + (1|p1|subID)); i=i+1
-ff[[i]] <- brms::bf(dev_rad ~ 1,
-                    c ~ 0 + setsize + (1 | subID),
-                    kappa ~ material + (1 | subID)); i=i+1
-ff[[i]] <- brms::bf(dev_rad ~ 1,
-                    c ~ 0 + setsize + (0 + setsize | subID),
-                    kappa ~ material + (material | subID)); i=i+1
-ff[[i]] <- brms::bf(dev_rad ~ 1,
-                    c ~ 0 + setsize + (0 + setsize | subID),
-                    kappa ~ material*setsize + (material | subID)); i=i+1
-ff[[i]] <- brms::bf(dev_rad ~ 1,
-                    c ~ 0 + setsize + (0 + setsize | subID),
-                    kappa ~ material*setsize + (material*setsize | subID)); i=i+1
-ff[[i]] <- brms::bf(dev_rad ~ 1,
-                    c ~ 0 + setsize + (0 + setsize | subID),
-                    kappa ~ 0 + material*setsize + (0 + material*setsize | subID)); i=i+1
-
-
-
-
-

Stan data

-

Extract the standata for each formula:

-
-
# get standata 
-sdat <- list()
-for (i in 1:length(ff)) {
-  sdat[[i]] <- get_standata(formula = ff[[i]],
-                        data = dat,
-                        model = sdmSimple())
-}
-
-# get stan code
-stan_code <- list()
-pars_block <- list()
-for (i in 1:length(ff)) {
-  stan_code[[i]] <- get_stancode(formula = ff[[i]],
-                          data = dat,
-                          model = sdmSimple())
-  pars_block[[i]] <- str_match(as.character(stan_code[[i]]), 
-                                   "(?s)parameters \\{\\n(.*?)\\}\\ntransformed")[,2]
-}
-
-

Here’s the structure for each formula:

-
- -
-
-
-

Data pars

-
    -
  • nSub: 4
  • -
  • setsize: 3 levels (3,6,9)
  • -
  • material: 2 levels (“color”, “orientation”)
  • -
  • nTrials: 5
  • -
  • Total obs: 120
  • -
-
-
-

Formula

-
dev_rad ~ 1 
-c ~ 1
-kappa ~ 1
-
-
-

Stan code (parameters block)

-
  real Intercept_c;  // temporary intercept for centered predictors
-  real Intercept_kappa;  // temporary intercept for centered predictors
-
-
-

Stan data structure

-
List of 12
- $ N         : int 120
- $ Y         : num [1:120(1d)] -0.0958 -0.1577 0.2624 -0.0864 -2.9212 ...
- $ K         : int 1
- $ Kc        : num 0
- $ X         : num [1:120, 1] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr "Intercept"
-  ..- attr(*, "assign")= int 0
- $ K_c       : int 1
- $ Kc_c      : num 0
- $ X_c       : num [1:120, 1] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr "Intercept"
-  ..- attr(*, "assign")= int 0
- $ K_kappa   : int 1
- $ Kc_kappa  : num 0
- $ X_kappa   : num [1:120, 1] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr "Intercept"
-  ..- attr(*, "assign")= int 0
- $ prior_only: int 0
- - attr(*, "class")= chr [1:2] "standata" "list"
-
-
-
-
-

Data pars

-
    -
  • nSub: 4
  • -
  • setsize: 3 levels (3,6,9)
  • -
  • material: 2 levels (“color”, “orientation”)
  • -
  • nTrials: 5
  • -
  • Total obs: 120
  • -
-
-
-

Formula

-
dev_rad ~ 1 
-c ~ setsize
-kappa ~ 1
-
-
-

Stan code (parameters block)

-
  vector[Kc_c] b_c;  // regression coefficients
-  real Intercept_c;  // temporary intercept for centered predictors
-  real Intercept_kappa;  // temporary intercept for centered predictors
-
-
-

Stan data structure

-
List of 12
- $ N         : int 120
- $ Y         : num [1:120(1d)] -0.0958 -0.1577 0.2624 -0.0864 -2.9212 ...
- $ K         : int 1
- $ Kc        : num 0
- $ X         : num [1:120, 1] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr "Intercept"
-  ..- attr(*, "assign")= int 0
- $ K_c       : int 3
- $ Kc_c      : num 2
- $ X_c       : num [1:120, 1:3] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr [1:3] "Intercept" "setsize6" "setsize9"
-  ..- attr(*, "assign")= int [1:3] 0 1 1
-  ..- attr(*, "contrasts")=List of 1
-  .. ..$ setsize: num [1:3, 1:2] 0 1 0 0 0 1
-  .. .. ..- attr(*, "dimnames")=List of 2
- $ K_kappa   : int 1
- $ Kc_kappa  : num 0
- $ X_kappa   : num [1:120, 1] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr "Intercept"
-  ..- attr(*, "assign")= int 0
- $ prior_only: int 0
- - attr(*, "class")= chr [1:2] "standata" "list"
-
-
-
-
-

Data pars

-
    -
  • nSub: 4
  • -
  • setsize: 3 levels (3,6,9)
  • -
  • material: 2 levels (“color”, “orientation”)
  • -
  • nTrials: 5
  • -
  • Total obs: 120
  • -
-
-
-

Formula

-
dev_rad ~ 1 
-c ~ 0 + setsize
-kappa ~ 1
-
-
-

Stan code (parameters block)

-
  vector[K_c] b_c;  // regression coefficients
-  real Intercept_kappa;  // temporary intercept for centered predictors
-
-
-

Stan data structure

-
List of 11
- $ N         : int 120
- $ Y         : num [1:120(1d)] -0.0958 -0.1577 0.2624 -0.0864 -2.9212 ...
- $ K         : int 1
- $ Kc        : num 0
- $ X         : num [1:120, 1] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr "Intercept"
-  ..- attr(*, "assign")= int 0
- $ K_c       : int 3
- $ X_c       : num [1:120, 1:3] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr [1:3] "setsize3" "setsize6" "setsize9"
-  ..- attr(*, "assign")= int [1:3] 1 1 1
-  ..- attr(*, "contrasts")=List of 1
-  .. ..$ setsize: num [1:3, 1:2] 0 1 0 0 0 1
-  .. .. ..- attr(*, "dimnames")=List of 2
- $ K_kappa   : int 1
- $ Kc_kappa  : num 0
- $ X_kappa   : num [1:120, 1] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr "Intercept"
-  ..- attr(*, "assign")= int 0
- $ prior_only: int 0
- - attr(*, "class")= chr [1:2] "standata" "list"
-
-
-
-
-

Data pars

-
    -
  • nSub: 4
  • -
  • setsize: 3 levels (3,6,9)
  • -
  • material: 2 levels (“color”, “orientation”)
  • -
  • nTrials: 5
  • -
  • Total obs: 120
  • -
-
-
-

Formula

-
dev_rad ~ 1 
-c ~ 0 + setsize
-kappa ~ material
-
-
-

Stan code (parameters block)

-
  vector[K_c] b_c;  // regression coefficients
-  vector[Kc_kappa] b_kappa;  // regression coefficients
-  real Intercept_kappa;  // temporary intercept for centered predictors
-
-
-

Stan data structure

-
List of 11
- $ N         : int 120
- $ Y         : num [1:120(1d)] -0.0958 -0.1577 0.2624 -0.0864 -2.9212 ...
- $ K         : int 1
- $ Kc        : num 0
- $ X         : num [1:120, 1] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr "Intercept"
-  ..- attr(*, "assign")= int 0
- $ K_c       : int 3
- $ X_c       : num [1:120, 1:3] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr [1:3] "setsize3" "setsize6" "setsize9"
-  ..- attr(*, "assign")= int [1:3] 1 1 1
-  ..- attr(*, "contrasts")=List of 1
-  .. ..$ setsize: num [1:3, 1:2] 0 1 0 0 0 1
-  .. .. ..- attr(*, "dimnames")=List of 2
- $ K_kappa   : int 2
- $ Kc_kappa  : num 1
- $ X_kappa   : num [1:120, 1:2] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr [1:2] "Intercept" "materialorientation"
-  ..- attr(*, "assign")= int [1:2] 0 1
-  ..- attr(*, "contrasts")=List of 1
-  .. ..$ material: num [1:2, 1] 0 1
-  .. .. ..- attr(*, "dimnames")=List of 2
- $ prior_only: int 0
- - attr(*, "class")= chr [1:2] "standata" "list"
-
-
-
-
-

Data pars

-
    -
  • nSub: 4
  • -
  • setsize: 3 levels (3,6,9)
  • -
  • material: 2 levels (“color”, “orientation”)
  • -
  • nTrials: 5
  • -
  • Total obs: 120
  • -
-
-
-

Formula

-
dev_rad ~ 1 
-c ~ 0 + setsize * material
-kappa ~ material
-
-
-

Stan code (parameters block)

-
  vector[K_c] b_c;  // regression coefficients
-  vector[Kc_kappa] b_kappa;  // regression coefficients
-  real Intercept_kappa;  // temporary intercept for centered predictors
-
-
-

Stan data structure

-
List of 11
- $ N         : int 120
- $ Y         : num [1:120(1d)] -0.0958 -0.1577 0.2624 -0.0864 -2.9212 ...
- $ K         : int 1
- $ Kc        : num 0
- $ X         : num [1:120, 1] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr "Intercept"
-  ..- attr(*, "assign")= int 0
- $ K_c       : int 6
- $ X_c       : num [1:120, 1:6] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr [1:6] "setsize3" "setsize6" "setsize9" "materialorientation" ...
-  ..- attr(*, "assign")= int [1:6] 1 1 1 2 3 3
-  ..- attr(*, "contrasts")=List of 2
-  .. ..$ setsize : num [1:3, 1:2] 0 1 0 0 0 1
-  .. .. ..- attr(*, "dimnames")=List of 2
-  .. ..$ material: num [1:2, 1] 0 1
-  .. .. ..- attr(*, "dimnames")=List of 2
- $ K_kappa   : int 2
- $ Kc_kappa  : num 1
- $ X_kappa   : num [1:120, 1:2] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr [1:2] "Intercept" "materialorientation"
-  ..- attr(*, "assign")= int [1:2] 0 1
-  ..- attr(*, "contrasts")=List of 1
-  .. ..$ material: num [1:2, 1] 0 1
-  .. .. ..- attr(*, "dimnames")=List of 2
- $ prior_only: int 0
- - attr(*, "class")= chr [1:2] "standata" "list"
-
-
-
-
-

Data pars

-
    -
  • nSub: 4
  • -
  • setsize: 3 levels (3,6,9)
  • -
  • material: 2 levels (“color”, “orientation”)
  • -
  • nTrials: 5
  • -
  • Total obs: 120
  • -
-
-
-

Formula

-
dev_rad ~ 1 
-c ~ 0 + setsize * material
-kappa ~ setsize * material
-
-
-

Stan code (parameters block)

-
  vector[K_c] b_c;  // regression coefficients
-  vector[Kc_kappa] b_kappa;  // regression coefficients
-  real Intercept_kappa;  // temporary intercept for centered predictors
-
-
-

Stan data structure

-
List of 11
- $ N         : int 120
- $ Y         : num [1:120(1d)] -0.0958 -0.1577 0.2624 -0.0864 -2.9212 ...
- $ K         : int 1
- $ Kc        : num 0
- $ X         : num [1:120, 1] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr "Intercept"
-  ..- attr(*, "assign")= int 0
- $ K_c       : int 6
- $ X_c       : num [1:120, 1:6] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr [1:6] "setsize3" "setsize6" "setsize9" "materialorientation" ...
-  ..- attr(*, "assign")= int [1:6] 1 1 1 2 3 3
-  ..- attr(*, "contrasts")=List of 2
-  .. ..$ setsize : num [1:3, 1:2] 0 1 0 0 0 1
-  .. .. ..- attr(*, "dimnames")=List of 2
-  .. ..$ material: num [1:2, 1] 0 1
-  .. .. ..- attr(*, "dimnames")=List of 2
- $ K_kappa   : int 6
- $ Kc_kappa  : num 5
- $ X_kappa   : num [1:120, 1:6] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr [1:6] "Intercept" "setsize6" "setsize9" "materialorientation" ...
-  ..- attr(*, "assign")= int [1:6] 0 1 1 2 3 3
-  ..- attr(*, "contrasts")=List of 2
-  .. ..$ setsize : num [1:3, 1:2] 0 1 0 0 0 1
-  .. .. ..- attr(*, "dimnames")=List of 2
-  .. ..$ material: num [1:2, 1] 0 1
-  .. .. ..- attr(*, "dimnames")=List of 2
- $ prior_only: int 0
- - attr(*, "class")= chr [1:2] "standata" "list"
-
-
-
-
-

Data pars

-
    -
  • nSub: 4
  • -
  • setsize: 3 levels (3,6,9)
  • -
  • material: 2 levels (“color”, “orientation”)
  • -
  • nTrials: 5
  • -
  • Total obs: 120
  • -
-
-
-

Formula

-
dev_rad ~ 1 
-c ~ 1 + (1 | subID)
-kappa ~ 1
-
-
-

Stan code (parameters block)

-
  real Intercept_c;  // temporary intercept for centered predictors
-  real Intercept_kappa;  // temporary intercept for centered predictors
-  vector<lower=0>[M_1] sd_1;  // group-level standard deviations
-  array[M_1] vector[N_1] z_1;  // standardized group-level effects
-
-
-

Stan data structure

-
List of 17
- $ N         : int 120
- $ Y         : num [1:120(1d)] -0.0958 -0.1577 0.2624 -0.0864 -2.9212 ...
- $ K         : int 1
- $ Kc        : num 0
- $ X         : num [1:120, 1] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr "Intercept"
-  ..- attr(*, "assign")= int 0
- $ K_c       : int 1
- $ Kc_c      : num 0
- $ X_c       : num [1:120, 1] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr "Intercept"
-  ..- attr(*, "assign")= int 0
- $ Z_1_c_1   : num [1:120(1d)] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ K_kappa   : int 1
- $ Kc_kappa  : num 0
- $ X_kappa   : num [1:120, 1] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr "Intercept"
-  ..- attr(*, "assign")= int 0
- $ J_1       : int [1:120(1d)] 1 1 1 1 1 2 2 2 2 2 ...
- $ N_1       : int 4
- $ M_1       : int 1
- $ NC_1      : int 0
- $ prior_only: int 0
- - attr(*, "class")= chr [1:2] "standata" "list"
-
-
-
-
-

Data pars

-
    -
  • nSub: 4
  • -
  • setsize: 3 levels (3,6,9)
  • -
  • material: 2 levels (“color”, “orientation”)
  • -
  • nTrials: 5
  • -
  • Total obs: 120
  • -
-
-
-

Formula

-
dev_rad ~ 1 
-c ~ 1
-kappa ~ 1 + (1 | subID)
-
-
-

Stan code (parameters block)

-
  real Intercept_c;  // temporary intercept for centered predictors
-  real Intercept_kappa;  // temporary intercept for centered predictors
-  vector<lower=0>[M_1] sd_1;  // group-level standard deviations
-  array[M_1] vector[N_1] z_1;  // standardized group-level effects
-
-
-

Stan data structure

-
List of 17
- $ N          : int 120
- $ Y          : num [1:120(1d)] -0.0958 -0.1577 0.2624 -0.0864 -2.9212 ...
- $ K          : int 1
- $ Kc         : num 0
- $ X          : num [1:120, 1] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr "Intercept"
-  ..- attr(*, "assign")= int 0
- $ K_c        : int 1
- $ Kc_c       : num 0
- $ X_c        : num [1:120, 1] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr "Intercept"
-  ..- attr(*, "assign")= int 0
- $ K_kappa    : int 1
- $ Kc_kappa   : num 0
- $ X_kappa    : num [1:120, 1] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr "Intercept"
-  ..- attr(*, "assign")= int 0
- $ Z_1_kappa_1: num [1:120(1d)] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ J_1        : int [1:120(1d)] 1 1 1 1 1 2 2 2 2 2 ...
- $ N_1        : int 4
- $ M_1        : int 1
- $ NC_1       : int 0
- $ prior_only : int 0
- - attr(*, "class")= chr [1:2] "standata" "list"
-
-
-
-
-

Data pars

-
    -
  • nSub: 4
  • -
  • setsize: 3 levels (3,6,9)
  • -
  • material: 2 levels (“color”, “orientation”)
  • -
  • nTrials: 5
  • -
  • Total obs: 120
  • -
-
-
-

Formula

-
dev_rad ~ 1 
-c ~ 1 + (1 | subID)
-kappa ~ 1 + (1 | subID)
-
-
-

Stan code (parameters block)

-
  real Intercept_c;  // temporary intercept for centered predictors
-  real Intercept_kappa;  // temporary intercept for centered predictors
-  vector<lower=0>[M_1] sd_1;  // group-level standard deviations
-  array[M_1] vector[N_1] z_1;  // standardized group-level effects
-  vector<lower=0>[M_2] sd_2;  // group-level standard deviations
-  array[M_2] vector[N_2] z_2;  // standardized group-level effects
-
-
-

Stan data structure

-
List of 22
- $ N          : int 120
- $ Y          : num [1:120(1d)] -0.0958 -0.1577 0.2624 -0.0864 -2.9212 ...
- $ K          : int 1
- $ Kc         : num 0
- $ X          : num [1:120, 1] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr "Intercept"
-  ..- attr(*, "assign")= int 0
- $ K_c        : int 1
- $ Kc_c       : num 0
- $ X_c        : num [1:120, 1] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr "Intercept"
-  ..- attr(*, "assign")= int 0
- $ Z_1_c_1    : num [1:120(1d)] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ K_kappa    : int 1
- $ Kc_kappa   : num 0
- $ X_kappa    : num [1:120, 1] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr "Intercept"
-  ..- attr(*, "assign")= int 0
- $ Z_2_kappa_1: num [1:120(1d)] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ J_1        : int [1:120(1d)] 1 1 1 1 1 2 2 2 2 2 ...
- $ J_2        : int [1:120(1d)] 1 1 1 1 1 2 2 2 2 2 ...
- $ N_1        : int 4
- $ M_1        : int 1
- $ NC_1       : int 0
- $ N_2        : int 4
- $ M_2        : int 1
- $ NC_2       : int 0
- $ prior_only : int 0
- - attr(*, "class")= chr [1:2] "standata" "list"
-
-
-
-
-

Data pars

-
    -
  • nSub: 4
  • -
  • setsize: 3 levels (3,6,9)
  • -
  • material: 2 levels (“color”, “orientation”)
  • -
  • nTrials: 5
  • -
  • Total obs: 120
  • -
-
-
-

Formula

-
dev_rad ~ 1 
-c ~ 1 + (1 | p1 | subID)
-kappa ~ 1 + (1 | p1 | subID)
-
-
-

Stan code (parameters block)

-
  real Intercept_c;  // temporary intercept for centered predictors
-  real Intercept_kappa;  // temporary intercept for centered predictors
-  vector<lower=0>[M_1] sd_1;  // group-level standard deviations
-  matrix[M_1, N_1] z_1;  // standardized group-level effects
-  cholesky_factor_corr[M_1] L_1;  // cholesky factor of correlation matrix
-
-
-

Stan data structure

-
List of 18
- $ N          : int 120
- $ Y          : num [1:120(1d)] -0.0958 -0.1577 0.2624 -0.0864 -2.9212 ...
- $ K          : int 1
- $ Kc         : num 0
- $ X          : num [1:120, 1] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr "Intercept"
-  ..- attr(*, "assign")= int 0
- $ K_c        : int 1
- $ Kc_c       : num 0
- $ X_c        : num [1:120, 1] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr "Intercept"
-  ..- attr(*, "assign")= int 0
- $ Z_1_c_1    : num [1:120(1d)] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ K_kappa    : int 1
- $ Kc_kappa   : num 0
- $ X_kappa    : num [1:120, 1] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr "Intercept"
-  ..- attr(*, "assign")= int 0
- $ Z_1_kappa_2: num [1:120(1d)] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ J_1        : int [1:120(1d)] 1 1 1 1 1 2 2 2 2 2 ...
- $ N_1        : int 4
- $ M_1        : int 2
- $ NC_1       : int 1
- $ prior_only : int 0
- - attr(*, "class")= chr [1:2] "standata" "list"
-
-
-
-
-

Data pars

-
    -
  • nSub: 4
  • -
  • setsize: 3 levels (3,6,9)
  • -
  • material: 2 levels (“color”, “orientation”)
  • -
  • nTrials: 5
  • -
  • Total obs: 120
  • -
-
-
-

Formula

-
dev_rad ~ 1 
-c ~ 0 + setsize + (1 | subID)
-kappa ~ material + (1 | subID)
-
-
-

Stan code (parameters block)

-
  vector[K_c] b_c;  // regression coefficients
-  vector[Kc_kappa] b_kappa;  // regression coefficients
-  real Intercept_kappa;  // temporary intercept for centered predictors
-  vector<lower=0>[M_1] sd_1;  // group-level standard deviations
-  array[M_1] vector[N_1] z_1;  // standardized group-level effects
-  vector<lower=0>[M_2] sd_2;  // group-level standard deviations
-  array[M_2] vector[N_2] z_2;  // standardized group-level effects
-
-
-

Stan data structure

-
List of 21
- $ N          : int 120
- $ Y          : num [1:120(1d)] -0.0958 -0.1577 0.2624 -0.0864 -2.9212 ...
- $ K          : int 1
- $ Kc         : num 0
- $ X          : num [1:120, 1] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr "Intercept"
-  ..- attr(*, "assign")= int 0
- $ K_c        : int 3
- $ X_c        : num [1:120, 1:3] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr [1:3] "setsize3" "setsize6" "setsize9"
-  ..- attr(*, "assign")= int [1:3] 1 1 1
-  ..- attr(*, "contrasts")=List of 1
-  .. ..$ setsize: num [1:3, 1:2] 0 1 0 0 0 1
-  .. .. ..- attr(*, "dimnames")=List of 2
- $ Z_1_c_1    : num [1:120(1d)] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ K_kappa    : int 2
- $ Kc_kappa   : num 1
- $ X_kappa    : num [1:120, 1:2] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr [1:2] "Intercept" "materialorientation"
-  ..- attr(*, "assign")= int [1:2] 0 1
-  ..- attr(*, "contrasts")=List of 1
-  .. ..$ material: num [1:2, 1] 0 1
-  .. .. ..- attr(*, "dimnames")=List of 2
- $ Z_2_kappa_1: num [1:120(1d)] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ J_1        : int [1:120(1d)] 1 1 1 1 1 2 2 2 2 2 ...
- $ J_2        : int [1:120(1d)] 1 1 1 1 1 2 2 2 2 2 ...
- $ N_1        : int 4
- $ M_1        : int 1
- $ NC_1       : int 0
- $ N_2        : int 4
- $ M_2        : int 1
- $ NC_2       : int 0
- $ prior_only : int 0
- - attr(*, "class")= chr [1:2] "standata" "list"
-
-
-
-
-

Data pars

-
    -
  • nSub: 4
  • -
  • setsize: 3 levels (3,6,9)
  • -
  • material: 2 levels (“color”, “orientation”)
  • -
  • nTrials: 5
  • -
  • Total obs: 120
  • -
-
-
-

Formula

-
dev_rad ~ 1 
-c ~ 0 + setsize + (0 + setsize | subID)
-kappa ~ material + (material | subID)
-
-
-

Stan code (parameters block)

-
  vector[K_c] b_c;  // regression coefficients
-  vector[Kc_kappa] b_kappa;  // regression coefficients
-  real Intercept_kappa;  // temporary intercept for centered predictors
-  vector<lower=0>[M_1] sd_1;  // group-level standard deviations
-  matrix[M_1, N_1] z_1;  // standardized group-level effects
-  cholesky_factor_corr[M_1] L_1;  // cholesky factor of correlation matrix
-  vector<lower=0>[M_2] sd_2;  // group-level standard deviations
-  matrix[M_2, N_2] z_2;  // standardized group-level effects
-  cholesky_factor_corr[M_2] L_2;  // cholesky factor of correlation matrix
-
-
-

Stan data structure

-
List of 24
- $ N          : int 120
- $ Y          : num [1:120(1d)] -0.0958 -0.1577 0.2624 -0.0864 -2.9212 ...
- $ K          : int 1
- $ Kc         : num 0
- $ X          : num [1:120, 1] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr "Intercept"
-  ..- attr(*, "assign")= int 0
- $ K_c        : int 3
- $ X_c        : num [1:120, 1:3] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr [1:3] "setsize3" "setsize6" "setsize9"
-  ..- attr(*, "assign")= int [1:3] 1 1 1
-  ..- attr(*, "contrasts")=List of 1
-  .. ..$ setsize: num [1:3, 1:2] 0 1 0 0 0 1
-  .. .. ..- attr(*, "dimnames")=List of 2
- $ Z_1_c_1    : num [1:120(1d)] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ Z_1_c_2    : num [1:120(1d)] 0 0 0 0 0 0 0 0 0 0 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ Z_1_c_3    : num [1:120(1d)] 0 0 0 0 0 0 0 0 0 0 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ K_kappa    : int 2
- $ Kc_kappa   : num 1
- $ X_kappa    : num [1:120, 1:2] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr [1:2] "Intercept" "materialorientation"
-  ..- attr(*, "assign")= int [1:2] 0 1
-  ..- attr(*, "contrasts")=List of 1
-  .. ..$ material: num [1:2, 1] 0 1
-  .. .. ..- attr(*, "dimnames")=List of 2
- $ Z_2_kappa_1: num [1:120(1d)] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ Z_2_kappa_2: num [1:120(1d)] 0 0 0 0 0 0 0 0 0 0 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ J_1        : int [1:120(1d)] 1 1 1 1 1 2 2 2 2 2 ...
- $ J_2        : int [1:120(1d)] 1 1 1 1 1 2 2 2 2 2 ...
- $ N_1        : int 4
- $ M_1        : int 3
- $ NC_1       : int 3
- $ N_2        : int 4
- $ M_2        : int 2
- $ NC_2       : int 1
- $ prior_only : int 0
- - attr(*, "class")= chr [1:2] "standata" "list"
-
-
-
-
-

Data pars

-
    -
  • nSub: 4
  • -
  • setsize: 3 levels (3,6,9)
  • -
  • material: 2 levels (“color”, “orientation”)
  • -
  • nTrials: 5
  • -
  • Total obs: 120
  • -
-
-
-

Formula

-
dev_rad ~ 1 
-c ~ 0 + setsize + (0 + setsize | subID)
-kappa ~ material * setsize + (material | subID)
-
-
-

Stan code (parameters block)

-
  vector[K_c] b_c;  // regression coefficients
-  vector[Kc_kappa] b_kappa;  // regression coefficients
-  real Intercept_kappa;  // temporary intercept for centered predictors
-  vector<lower=0>[M_1] sd_1;  // group-level standard deviations
-  matrix[M_1, N_1] z_1;  // standardized group-level effects
-  cholesky_factor_corr[M_1] L_1;  // cholesky factor of correlation matrix
-  vector<lower=0>[M_2] sd_2;  // group-level standard deviations
-  matrix[M_2, N_2] z_2;  // standardized group-level effects
-  cholesky_factor_corr[M_2] L_2;  // cholesky factor of correlation matrix
-
-
-

Stan data structure

-
List of 24
- $ N          : int 120
- $ Y          : num [1:120(1d)] -0.0958 -0.1577 0.2624 -0.0864 -2.9212 ...
- $ K          : int 1
- $ Kc         : num 0
- $ X          : num [1:120, 1] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr "Intercept"
-  ..- attr(*, "assign")= int 0
- $ K_c        : int 3
- $ X_c        : num [1:120, 1:3] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr [1:3] "setsize3" "setsize6" "setsize9"
-  ..- attr(*, "assign")= int [1:3] 1 1 1
-  ..- attr(*, "contrasts")=List of 1
-  .. ..$ setsize: num [1:3, 1:2] 0 1 0 0 0 1
-  .. .. ..- attr(*, "dimnames")=List of 2
- $ Z_1_c_1    : num [1:120(1d)] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ Z_1_c_2    : num [1:120(1d)] 0 0 0 0 0 0 0 0 0 0 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ Z_1_c_3    : num [1:120(1d)] 0 0 0 0 0 0 0 0 0 0 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ K_kappa    : int 6
- $ Kc_kappa   : num 5
- $ X_kappa    : num [1:120, 1:6] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr [1:6] "Intercept" "materialorientation" "setsize6" "setsize9" ...
-  ..- attr(*, "assign")= int [1:6] 0 1 2 2 3 3
-  ..- attr(*, "contrasts")=List of 2
-  .. ..$ material: num [1:2, 1] 0 1
-  .. .. ..- attr(*, "dimnames")=List of 2
-  .. ..$ setsize : num [1:3, 1:2] 0 1 0 0 0 1
-  .. .. ..- attr(*, "dimnames")=List of 2
- $ Z_2_kappa_1: num [1:120(1d)] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ Z_2_kappa_2: num [1:120(1d)] 0 0 0 0 0 0 0 0 0 0 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ J_1        : int [1:120(1d)] 1 1 1 1 1 2 2 2 2 2 ...
- $ J_2        : int [1:120(1d)] 1 1 1 1 1 2 2 2 2 2 ...
- $ N_1        : int 4
- $ M_1        : int 3
- $ NC_1       : int 3
- $ N_2        : int 4
- $ M_2        : int 2
- $ NC_2       : int 1
- $ prior_only : int 0
- - attr(*, "class")= chr [1:2] "standata" "list"
-
-
-
-
-

Data pars

-
    -
  • nSub: 4
  • -
  • setsize: 3 levels (3,6,9)
  • -
  • material: 2 levels (“color”, “orientation”)
  • -
  • nTrials: 5
  • -
  • Total obs: 120
  • -
-
-
-

Formula

-
dev_rad ~ 1 
-c ~ 0 + setsize + (0 + setsize | subID)
-kappa ~ material * setsize + (material * setsize | subID)
-
-
-

Stan code (parameters block)

-
  vector[K_c] b_c;  // regression coefficients
-  vector[Kc_kappa] b_kappa;  // regression coefficients
-  real Intercept_kappa;  // temporary intercept for centered predictors
-  vector<lower=0>[M_1] sd_1;  // group-level standard deviations
-  matrix[M_1, N_1] z_1;  // standardized group-level effects
-  cholesky_factor_corr[M_1] L_1;  // cholesky factor of correlation matrix
-  vector<lower=0>[M_2] sd_2;  // group-level standard deviations
-  matrix[M_2, N_2] z_2;  // standardized group-level effects
-  cholesky_factor_corr[M_2] L_2;  // cholesky factor of correlation matrix
-
-
-

Stan data structure

-
List of 28
- $ N          : int 120
- $ Y          : num [1:120(1d)] -0.0958 -0.1577 0.2624 -0.0864 -2.9212 ...
- $ K          : int 1
- $ Kc         : num 0
- $ X          : num [1:120, 1] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr "Intercept"
-  ..- attr(*, "assign")= int 0
- $ K_c        : int 3
- $ X_c        : num [1:120, 1:3] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr [1:3] "setsize3" "setsize6" "setsize9"
-  ..- attr(*, "assign")= int [1:3] 1 1 1
-  ..- attr(*, "contrasts")=List of 1
-  .. ..$ setsize: num [1:3, 1:2] 0 1 0 0 0 1
-  .. .. ..- attr(*, "dimnames")=List of 2
- $ Z_1_c_1    : num [1:120(1d)] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ Z_1_c_2    : num [1:120(1d)] 0 0 0 0 0 0 0 0 0 0 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ Z_1_c_3    : num [1:120(1d)] 0 0 0 0 0 0 0 0 0 0 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ K_kappa    : int 6
- $ Kc_kappa   : num 5
- $ X_kappa    : num [1:120, 1:6] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr [1:6] "Intercept" "materialorientation" "setsize6" "setsize9" ...
-  ..- attr(*, "assign")= int [1:6] 0 1 2 2 3 3
-  ..- attr(*, "contrasts")=List of 2
-  .. ..$ material: num [1:2, 1] 0 1
-  .. .. ..- attr(*, "dimnames")=List of 2
-  .. ..$ setsize : num [1:3, 1:2] 0 1 0 0 0 1
-  .. .. ..- attr(*, "dimnames")=List of 2
- $ Z_2_kappa_1: num [1:120(1d)] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ Z_2_kappa_2: num [1:120(1d)] 0 0 0 0 0 0 0 0 0 0 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ Z_2_kappa_3: num [1:120(1d)] 0 0 0 0 0 0 0 0 0 0 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ Z_2_kappa_4: num [1:120(1d)] 0 0 0 0 0 0 0 0 0 0 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ Z_2_kappa_5: num [1:120(1d)] 0 0 0 0 0 0 0 0 0 0 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ Z_2_kappa_6: num [1:120(1d)] 0 0 0 0 0 0 0 0 0 0 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ J_1        : int [1:120(1d)] 1 1 1 1 1 2 2 2 2 2 ...
- $ J_2        : int [1:120(1d)] 1 1 1 1 1 2 2 2 2 2 ...
- $ N_1        : int 4
- $ M_1        : int 3
- $ NC_1       : int 3
- $ N_2        : int 4
- $ M_2        : int 6
- $ NC_2       : int 15
- $ prior_only : int 0
- - attr(*, "class")= chr [1:2] "standata" "list"
-
-
-
-
-

Data pars

-
    -
  • nSub: 4
  • -
  • setsize: 3 levels (3,6,9)
  • -
  • material: 2 levels (“color”, “orientation”)
  • -
  • nTrials: 5
  • -
  • Total obs: 120
  • -
-
-
-

Formula

-
dev_rad ~ 1 
-c ~ 0 + setsize + (0 + setsize | subID)
-kappa ~ 0 + material * setsize + (0 + material * setsize | subID)
-
-
-

Stan code (parameters block)

-
  vector[K_c] b_c;  // regression coefficients
-  vector[K_kappa] b_kappa;  // regression coefficients
-  vector<lower=0>[M_1] sd_1;  // group-level standard deviations
-  matrix[M_1, N_1] z_1;  // standardized group-level effects
-  cholesky_factor_corr[M_1] L_1;  // cholesky factor of correlation matrix
-  vector<lower=0>[M_2] sd_2;  // group-level standard deviations
-  matrix[M_2, N_2] z_2;  // standardized group-level effects
-  cholesky_factor_corr[M_2] L_2;  // cholesky factor of correlation matrix
-
-
-

Stan data structure

-
List of 27
- $ N          : int 120
- $ Y          : num [1:120(1d)] -0.0958 -0.1577 0.2624 -0.0864 -2.9212 ...
- $ K          : int 1
- $ Kc         : num 0
- $ X          : num [1:120, 1] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr "Intercept"
-  ..- attr(*, "assign")= int 0
- $ K_c        : int 3
- $ X_c        : num [1:120, 1:3] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr [1:3] "setsize3" "setsize6" "setsize9"
-  ..- attr(*, "assign")= int [1:3] 1 1 1
-  ..- attr(*, "contrasts")=List of 1
-  .. ..$ setsize: num [1:3, 1:2] 0 1 0 0 0 1
-  .. .. ..- attr(*, "dimnames")=List of 2
- $ Z_1_c_1    : num [1:120(1d)] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ Z_1_c_2    : num [1:120(1d)] 0 0 0 0 0 0 0 0 0 0 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ Z_1_c_3    : num [1:120(1d)] 0 0 0 0 0 0 0 0 0 0 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ K_kappa    : int 6
- $ X_kappa    : num [1:120, 1:6] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 2
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
-  .. ..$ : chr [1:6] "materialcolor" "materialorientation" "setsize6" "setsize9" ...
-  ..- attr(*, "assign")= int [1:6] 1 1 2 2 3 3
-  ..- attr(*, "contrasts")=List of 2
-  .. ..$ material: num [1:2, 1] 0 1
-  .. .. ..- attr(*, "dimnames")=List of 2
-  .. ..$ setsize : num [1:3, 1:2] 0 1 0 0 0 1
-  .. .. ..- attr(*, "dimnames")=List of 2
- $ Z_2_kappa_1: num [1:120(1d)] 1 1 1 1 1 1 1 1 1 1 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ Z_2_kappa_2: num [1:120(1d)] 0 0 0 0 0 0 0 0 0 0 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ Z_2_kappa_3: num [1:120(1d)] 0 0 0 0 0 0 0 0 0 0 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ Z_2_kappa_4: num [1:120(1d)] 0 0 0 0 0 0 0 0 0 0 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ Z_2_kappa_5: num [1:120(1d)] 0 0 0 0 0 0 0 0 0 0 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ Z_2_kappa_6: num [1:120(1d)] 0 0 0 0 0 0 0 0 0 0 ...
-  ..- attr(*, "dimnames")=List of 1
-  .. ..$ : chr [1:120] "1" "2" "3" "4" ...
- $ J_1        : int [1:120(1d)] 1 1 1 1 1 2 2 2 2 2 ...
- $ J_2        : int [1:120(1d)] 1 1 1 1 1 2 2 2 2 2 ...
- $ N_1        : int 4
- $ M_1        : int 3
- $ NC_1       : int 3
- $ N_2        : int 4
- $ M_2        : int 6
- $ NC_2       : int 15
- $ prior_only : int 0
- - attr(*, "class")= chr [1:2] "standata" "list"
-
-
-
-
-
-

Explanation of stan data structure

-

Always present:

-
    -
  • N: total number of observations
  • -
  • Y: vector[N] of responses
  • -
  • K, K_c, K_kappa: number of predictors for each parameter (general form: K_dpar)
  • -
  • Kc, Kc_c, Kc_kappa: number of centered predictors for each parameter. (general form: Kc_dpar) -
      -
    • argument missing if intercept is supressed
    • -
    • K_dpar-1 if intercept is included
    • -
  • -
  • X, X_c, X_kappa: population-level design matrix[N, K_dpar] for each parameter (general form: X_dpar) -
      -
    • colnames are the names of the predictors as they would appear in the model fit, e.g.: -
        -
      • Intercept, setsize6, setsize9, materialorientation, setsize6:materialorientation, setsize9:materialorientation
      • -
      • setsize3, setsize6, setsize9, materialorientation, setsize6:materialorientation, setsize9:materialorientation
      • -
    • -
  • -
-

Only present if random effects are included:

-
    -
  • Z_1_c_1,Z_1_c_2, … Z_1_kappa_1: random effects predictor values, vector[N] for each parameter (general form: Z_dpar)
  • -
  • J_1, J_2, … : vector[N] grouping indicator for the random effects (e.g. values of subID) -
      -
    • J_1 is the grouping indicator for the first random effect that appears in the formula
    • -
  • -
  • N_1, N_2,…: number of grouping levels for the random effects (e.g. number of unique subID values)
  • -
  • M_1, M_2…: numer of random effects coefficients (e.g. 1 if only random intercepts are included)
  • -
  • NC_1, NC_2,…: number of group-level correlations
  • -
-
-
-
-

Component parts of pars/inits

-

All possible parts for the sdmSimple are:

-
    -
  • if only intercept -
      -
    • real[1] Intercept_c
    • -
    • real[1] Intercept_kappa
    • -
  • -
  • if predictors but intercept is not supressed -
      -
    • real[1] Intercept_c
    • -
    • real[1] Intercept_kappa
    • -
    • vector[Kc_c] b_c
    • -
    • vector[Kc_kappa] b_kappa
    • -
  • -
  • If predictors but intercept is supressed -
      -
    • vector[K_c] b_c
    • -
    • vector[K_kappa] b_kappa
    • -
  • -
  • If random effects are included -
      -
    • [M_1] sd_1
    • -
    • [M_2] sd_2
    • -
    • matrix[M_1, N_1] z_1
    • -
    • matrix[M_2, N_2] z_2
    • -
  • -
-
-
-

Steps for constructing an init function automatically

-
    -
  1. Get the names of all distributional parameters
  2. -
  3. Get priors on the parameters
  4. -
  5. Exclude any parameters that have constant priors
  6. -
  7. For every distributional parameter, determine if they have an intercept or not
  8. -
  9. Get the names of variables in the list of stan data
  10. -
  11. Check the stan data for the presence of -
      -
    1. Intercept_*
    2. -
    3. Kc_*
    4. -
    5. K_*
    6. -
    7. M_#
    8. -
    9. N_#
    10. -
  12. -
-

alternatively maybe write a script to parse the stan code parameters block?

-
-
-

How to extract the parameters

-

I can extract the parameter names from the formula

-
-
# TODO: Need to export the family so that it is accessible for these functions
-f <- brms::bf(dev_rad ~ 1,
-              c ~ 0 + setsize + (0 + setsize | subID),
-              kappa ~ material*setsize + (material*setsize | subID))
-family <- configure_model(sdmSimple(),dat,f)$family
-f <- brms::bf(f, family=family)
-
-bterms <- brms::brmsterms(f)
-dpars <- names(bterms$dpars)[-1]
-ranef <- brms:::tidy_ranef(bterms, data=dat)
-
-data <- brms:::validate_data(dat, bterms = bterms)
-
- -
- - -

Footnotes

- -
    -
  1. Implementing such a function was discussed for brms here, but seems like it has not been done yet↩︎

  2. -
  3. for example, see how Henrik does it here - it works and I’ll use it as a starting point, but ideally I don’t want to do this manually for every model and design. Also look into the functions described here↩︎

  4. -
-
- - -
- - - - - \ No newline at end of file diff --git a/dev_utils/stan_data_structure.qmd b/dev_utils/stan_data_structure.qmd deleted file mode 100644 index c14ecd10..00000000 --- a/dev_utils/stan_data_structure.qmd +++ /dev/null @@ -1,405 +0,0 @@ ---- -title: "BRMS Stan data structure" -author: Ven Popov -format: - html: - embed-resources: true - toc: true -execute: - cache: true -editor: visual ---- - -```{r setup, include=FALSE} -pacman::p_load(here, tidyverse, devtools, bmm, brms) - -nSub <- 4 -setsize <- c(3,6,9) -material <- c("color", "orientation") -nTrials <- 5 - -simParms <- expand.grid( - subID = 1:nSub, - setsize = setsize, - material = material -) - -library(tidyverse) -simParms <- simParms %>% - mutate( - c = case_when(setsize == 3 ~ rnorm(n(), mean = 3, sd = 0.5), - TRUE ~ rnorm(n(), mean = 2, sd = 0.5)), - kappa = case_when(material == "color" ~ rnorm(n(), mean = 8, sd = 1.5), - TRUE ~ rnorm(n(), mean = 6, sd = 1.5)) - ) - -simData <- data.frame( - subID = integer(), - setsize = integer(), - material = character(), - dev_rad = numeric() -) - -for (i in 1:nrow(simParms)) { - devRad <- bmm::rsdm(n = nTrials, c = simParms$c[i], kappa = simParms$kappa[i]) - - subData <- data.frame( - subID = simParms$subID[i], - setsize = simParms$setsize[i], - material = simParms$material[i], - dev_rad = devRad - ) - - simData <- rbind(simData,subData) -} - -simData$setsize <- as.factor(simData$setsize) -``` - -::: callout-note -This document is a work in progress -::: - -## Introduction - -This is a utility document to explore the stan data structure generated by `brms` for a variety of formulas. The goal is to understand how the data is organized so that I can write a user-friendly function for setting initial values[^1]. Currently it's quite opaque and cumbersome[^2]. - -[^1]: Implementing such a function was discussed for brms [here](https://github.com/paul-buerkner/brms/issues/883#issuecomment-618948473), but seems like it has not been done yet - -[^2]: for example, see how Henrik does it [here](http://singmann.org/wiener-model-analysis-with-brms-part-i/) - it works and I'll use it as a starting point, but ideally I don't want to do this manually for every model and design. Also look into the functions described [here](https://users.aalto.fi/~ave/casestudies/Birthdays/birthdays.html#Workflow_for_quick_iterative_model_building) - -To generate initial value, we need to know two things: - -- How key information about the data and predictors is structured (from the stan data) -- Which parameters are being estimated (from the stan code) - -## Example init function - -Following the example by Henrik, here's a function that sets initial values for a simple model (in `bmm`, but raw `brms` is similar). First, we need to extract the stan data and the stan code for the parameters block: - -```{r} -#| code-fold: true -pacman::p_load(here, tidyverse, brms, bmm) - -# setup data and model formula -dat <- bmm::OberauerLin_2017 -dat$SetSize <- as.factor(dat$SetSize) -formula <- bf(dev_rad ~ 1, - c ~ 0 + SetSize + (0 + SetSize || ID), - kappa ~ 1 + (1 | ID)) - -# get standata -sdat <- get_standata(formula = formula, - data = dat, - model = sdmSimple()) - -# get stan code for parameters block -stan_code <- get_stancode(formula = formula, - data = dat, - model = sdmSimple()) -par_block <- str_match(as.character(stan_code), - "(?s)parameters \\{\\n(.*?)\\}\\ntransformed")[,2] -``` - -The standata looks like this: - -```{r} -str(sdat, give.attr=F) -``` - -And the parameters block looks like this: - -```{r} -cat(par_block) -``` - -We need to define initial values for every parameter listed in the parameters block, and get the appropriate dimensions from the stan data. Here's a function that does this: - -```{r} -initfun <- function(seed) { - set.seed(seed) - list( - b_c = as.array(runif(sdat$K_c,0,2)), - Intercept_kappa = runif(1,0,2), - sd_1 = as.array(runif(sdat$M_1,0,1)), - sd_2 = as.array(runif(sdat$M_2,0,1)), - z_1 = matrix(rnorm(sdat$M_1*sdat$N_1, 0, 0.01), - sdat$M_1, sdat$N_1), - z_2 = matrix(rnorm(sdat$M_2*sdat$N_2, 0, 0.01), - sdat$M_2, sdat$N_2) - ) -} -``` - -This function can be used to set initial values for the model: - -```{r} -initlist <- list( - initfun(1), - initfun(2), - initfun(3), - initfun(4) -) -``` - -and then we would run the model as - -``` r -fit_model(formula, dat, sdmSimple(), - parallel=T, init=initlist, backend='cmdstanr') -``` - -But I don't want to code this initfun for every possible design and model. - -## Formula generation - -Generate a bunch of different formulas: - -```{r ff-gen} -#| code-fold: true -dat <- simData -ff <- list() - -i=1 - -ff[[i]] <- brms::bf(dev_rad ~ 1, - c ~ 1, - kappa ~ 1); i=i+1 -ff[[i]] <- brms::bf(dev_rad ~ 1, - c ~ setsize, - kappa ~ 1); i=i+1 -ff[[i]] <- brms::bf(dev_rad ~ 1, - c ~ 0 + setsize, - kappa ~ 1); i=i+1 -ff[[i]] <- brms::bf(dev_rad ~ 1, - c ~ 0 + setsize, - kappa ~ material); i=i+1 -ff[[i]] <- brms::bf(dev_rad ~ 1, - c ~ 0 + setsize*material, - kappa ~ material); i=i+1 -ff[[i]] <- brms::bf(dev_rad ~ 1, - c ~ 0 + setsize*material, - kappa ~ setsize*material); i=i+1 -ff[[i]] <- brms::bf(dev_rad ~ 1, - c ~ 1 + (1|subID), - kappa ~ 1); i=i+1 -ff[[i]] <- brms::bf(dev_rad ~ 1, - c ~ 1, - kappa ~ 1 + (1|subID)); i=i+1 -ff[[i]] <- brms::bf(dev_rad ~ 1, - c ~ 1 + (1|subID), - kappa ~ 1 + (1|subID)); i=i+1 -ff[[i]] <- brms::bf(dev_rad ~ 1, - c ~ 1 + (1|p1|subID), - kappa ~ 1 + (1|p1|subID)); i=i+1 -ff[[i]] <- brms::bf(dev_rad ~ 1, - c ~ 0 + setsize + (1 | subID), - kappa ~ material + (1 | subID)); i=i+1 -ff[[i]] <- brms::bf(dev_rad ~ 1, - c ~ 0 + setsize + (0 + setsize | subID), - kappa ~ material + (material | subID)); i=i+1 -ff[[i]] <- brms::bf(dev_rad ~ 1, - c ~ 0 + setsize + (0 + setsize | subID), - kappa ~ material*setsize + (material | subID)); i=i+1 -ff[[i]] <- brms::bf(dev_rad ~ 1, - c ~ 0 + setsize + (0 + setsize | subID), - kappa ~ material*setsize + (material*setsize | subID)); i=i+1 -ff[[i]] <- brms::bf(dev_rad ~ 1, - c ~ 0 + setsize + (0 + setsize | subID), - kappa ~ 0 + material*setsize + (0 + material*setsize | subID)); i=i+1 -``` - -## Stan data - -Extract the standata for each formula: - -```{r get_standata} -# get standata -sdat <- list() -for (i in 1:length(ff)) { - sdat[[i]] <- get_standata(formula = ff[[i]], - data = dat, - model = sdmSimple()) -} - -# get stan code -stan_code <- list() -pars_block <- list() -for (i in 1:length(ff)) { - stan_code[[i]] <- get_stancode(formula = ff[[i]], - data = dat, - model = sdmSimple()) - pars_block[[i]] <- str_match(as.character(stan_code[[i]]), - "(?s)parameters \\{\\n(.*?)\\}\\ntransformed")[,2] -} -``` - -Here's the structure for each formula: - -::: panel-tabset -```{r print_standata} -#| results: asis -# print standata structure -for (i in 1:length(sdat)) { - cat("## Formula ", i, "\n") - cat("### Data pars\n\n") - - cat( - "* nSub: 4\n", - "* setsize: 3 levels (3,6,9)\n", - '* material: 2 levels ("color", "orientation")\n', - '* nTrials: 5\n', - '* Total obs: 120\n\n', - sep = "" - ) - - cat("### Formula\n\n") - cat("``` r\n") - brms:::print.brmsformula(ff[[i]]) - cat("```\n\n") - - cat("### Stan code (parameters block)\n\n") - cat("``` r\n") - cat(pars_block[[i]]) - cat("\n\n") - cat("```\n\n") - - cat("### Stan data structure\n\n") - cat("``` r\n") - str(sdat[[i]],3) - cat("\n\n") - cat("```\n\n") - -} -``` -::: - -### Explanation of stan data structure - -Always present: - -- `N`: total number of observations -- `Y`: vector\[N\] of responses -- `K`, `K_c`, `K_kappa`: number of predictors for each parameter (general form: `K_dpar`) -- `Kc`, `Kc_c`, `Kc_kappa`: number of centered predictors for each parameter. (general form: `Kc_dpar`) - - argument missing if intercept is supressed - - `K_dpar`-1 if intercept is included -- `X`, `X_c`, `X_kappa`: population-level design matrix\[N, K_dpar\] for each parameter (general form: `X_dpar`) - - colnames are the names of the predictors as they would appear in the model fit, e.g.: - - `r colnames(sdat[[6]]$X_kappa)` - - `r colnames(sdat[[6]]$X_c)` - -Only present if random effects are included: - -- `Z_1_c_1`,`Z_1_c_2`, ... `Z_1_kappa_1`: random effects predictor values, vector\[N\] for each parameter (general form: `Z_dpar`) -- `J_1`, `J_2`, ... : vector\[N\] grouping indicator for the random effects (e.g. values of subID) - - `J_1` is the grouping indicator for the first random effect that appears in the formula -- `N_1`, `N_2`,...: number of grouping levels for the random effects (e.g. number of unique subID values) -- `M_1`, `M_2`...: numer of random effects coefficients (e.g. 1 if only random intercepts are included) -- `NC_1`, `NC_2`,...: number of group-level correlations - -## Component parts of pars/inits - -All possible parts for the sdmSimple are: - -- [**if only intercept**]{.underline} - - real\[1\] Intercept_c - - real\[1\] Intercept_kappa -- [**if predictors but intercept is not supressed**]{.underline} - - real\[1\] Intercept_c - - real\[1\] Intercept_kappa - - vector\[Kc_c\] b_c - - vector\[Kc_kappa\] b_kappa -- [**If predictors but intercept is supressed**]{.underline} - - vector\[K_c\] b_c - - vector\[K_kappa\] b_kappa -- [**If random effects are included**]{.underline} - - \[M_1\] sd_1 - - \[M_2\] sd_2 - - matrix\[M_1, N_1\] z_1 - - matrix\[M_2, N_2\] z_2 - -## Steps for constructing an init function automatically - -1. Get the names of all distributional parameters -2. Get priors on the parameters -3. Exclude any parameters that have constant priors -4. For every distributional parameter, determine if they have an intercept or not -5. Get the names of variables in the list of stan data -6. Check the stan data for the presence of - 1. Intercept\_\* - 2. Kc\_\* - 3. K\_\* - 4. M\_# - 5. N\_# - -alternatively maybe write a script to parse the stan code parameters block? - -## How to extract the parameters - -I can extract the parameter names from the formula - -```{r} -# TODO: Need to export the family so that it is accessible for these functions -f <- brms::bf(dev_rad ~ 1, - c ~ 0 + setsize + (0 + setsize | subID), - kappa ~ material*setsize + (material*setsize | subID)) -family <- configure_model(sdmSimple(),dat,f)$family -f <- brms::bf(f, family=family) - -bterms <- brms::brmsterms(f) -dpars <- names(bterms$dpars)[-1] -ranef <- brms:::tidy_ranef(bterms, data=dat) - -data <- brms:::validate_data(dat, bterms = bterms) -``` - - - -```{r, include=FALSE, eval=FALSE} -c_terms <- bterms$dpars[['c']] -kappa_terms <- bterms$dpars[['kappa']] - -has_intercept <- function (formula) -{ - if (is.terms(formula)) { - out <- as.logical(attr(formula, "intercept")) - } - else { - formula <- as.formula(formula) - try_terms <- try(terms(formula), silent = TRUE) - if (is_try_error(try_terms)) { - out <- FALSE - } - else { - out <- as.logical(attr(try_terms, "intercept")) - } - } - out -} - -``` - -```{r, include=FALSE, eval=FALSE} -#brms:::stan_fe - - - -bterms <- kappa_terms - - -fixef <- colnames(brms:::data_fe(bterms, data)$X) -center_X <- brms:::stan_center_X(bterms) -ct <- brms:::str_if(center_X, "c") -if (center_X) { - fixef <- setdiff(fixef, "Intercept") -} -px <- brms:::check_prefix(bterms) -p <- brms:::usc(brms:::combine_prefix(px)) -if (length(fixef)) { - b_type <- glue::glue("vector[K{ct}{p}]") -} - - -cat(brms:::stan_fe(kappa_terms,data=dat, normalize=T, prior=get_model_prior(f, dat, sdmSimple()), primitive=T)$par) -``` diff --git a/dev_utils/stan_data_structure_cache/html/__packages b/dev_utils/stan_data_structure_cache/html/__packages deleted file mode 100644 index 106afb73..00000000 --- a/dev_utils/stan_data_structure_cache/html/__packages +++ /dev/null @@ -1,16 +0,0 @@ -here -tidyverse -ggplot2 -tibble -tidyr -readr -purrr -dplyr -stringr -forcats -lubridate -usethis -devtools -bmm -Rcpp -brms diff --git a/dev_utils/stan_data_structure_cache/html/ff-gen_d2cc4136c9554faf6ea6be868e1db5e5.RData b/dev_utils/stan_data_structure_cache/html/ff-gen_d2cc4136c9554faf6ea6be868e1db5e5.RData deleted file mode 100644 index 4d7b31fc..00000000 Binary files a/dev_utils/stan_data_structure_cache/html/ff-gen_d2cc4136c9554faf6ea6be868e1db5e5.RData and /dev/null differ diff --git a/dev_utils/stan_data_structure_cache/html/ff-gen_d2cc4136c9554faf6ea6be868e1db5e5.rdb b/dev_utils/stan_data_structure_cache/html/ff-gen_d2cc4136c9554faf6ea6be868e1db5e5.rdb deleted file mode 100644 index 07e9307f..00000000 Binary files a/dev_utils/stan_data_structure_cache/html/ff-gen_d2cc4136c9554faf6ea6be868e1db5e5.rdb and /dev/null differ diff --git a/dev_utils/stan_data_structure_cache/html/ff-gen_d2cc4136c9554faf6ea6be868e1db5e5.rdx b/dev_utils/stan_data_structure_cache/html/ff-gen_d2cc4136c9554faf6ea6be868e1db5e5.rdx deleted file mode 100644 index 0de2d3b5..00000000 Binary files a/dev_utils/stan_data_structure_cache/html/ff-gen_d2cc4136c9554faf6ea6be868e1db5e5.rdx and /dev/null differ diff --git a/dev_utils/stan_data_structure_cache/html/get_stancode_df9d5f1d975a19a4c88db34785e8f558.RData b/dev_utils/stan_data_structure_cache/html/get_stancode_df9d5f1d975a19a4c88db34785e8f558.RData deleted file mode 100644 index 41672e0e..00000000 Binary files a/dev_utils/stan_data_structure_cache/html/get_stancode_df9d5f1d975a19a4c88db34785e8f558.RData and /dev/null differ diff --git a/dev_utils/stan_data_structure_cache/html/get_stancode_df9d5f1d975a19a4c88db34785e8f558.rdb b/dev_utils/stan_data_structure_cache/html/get_stancode_df9d5f1d975a19a4c88db34785e8f558.rdb deleted file mode 100644 index 6d1e836c..00000000 Binary files a/dev_utils/stan_data_structure_cache/html/get_stancode_df9d5f1d975a19a4c88db34785e8f558.rdb and /dev/null differ diff --git a/dev_utils/stan_data_structure_cache/html/get_stancode_df9d5f1d975a19a4c88db34785e8f558.rdx b/dev_utils/stan_data_structure_cache/html/get_stancode_df9d5f1d975a19a4c88db34785e8f558.rdx deleted file mode 100644 index 1681c995..00000000 Binary files a/dev_utils/stan_data_structure_cache/html/get_stancode_df9d5f1d975a19a4c88db34785e8f558.rdx and /dev/null differ diff --git a/dev_utils/stan_data_structure_cache/html/get_standata_125661b2903f52b74202fc59f5417acb.RData b/dev_utils/stan_data_structure_cache/html/get_standata_125661b2903f52b74202fc59f5417acb.RData deleted file mode 100644 index e6cf16eb..00000000 Binary files a/dev_utils/stan_data_structure_cache/html/get_standata_125661b2903f52b74202fc59f5417acb.RData and /dev/null differ diff --git a/dev_utils/stan_data_structure_cache/html/get_standata_125661b2903f52b74202fc59f5417acb.rdb b/dev_utils/stan_data_structure_cache/html/get_standata_125661b2903f52b74202fc59f5417acb.rdb deleted file mode 100644 index 841353a9..00000000 Binary files a/dev_utils/stan_data_structure_cache/html/get_standata_125661b2903f52b74202fc59f5417acb.rdb and /dev/null differ diff --git a/dev_utils/stan_data_structure_cache/html/get_standata_125661b2903f52b74202fc59f5417acb.rdx b/dev_utils/stan_data_structure_cache/html/get_standata_125661b2903f52b74202fc59f5417acb.rdx deleted file mode 100644 index d6b20e1b..00000000 Binary files a/dev_utils/stan_data_structure_cache/html/get_standata_125661b2903f52b74202fc59f5417acb.rdx and /dev/null differ diff --git a/dev_utils/stan_data_structure_cache/html/print_standata_e56099a6b47ed3d39868bf8b513c7650.RData b/dev_utils/stan_data_structure_cache/html/print_standata_e56099a6b47ed3d39868bf8b513c7650.RData deleted file mode 100644 index 0e89ca5d..00000000 Binary files a/dev_utils/stan_data_structure_cache/html/print_standata_e56099a6b47ed3d39868bf8b513c7650.RData and /dev/null differ diff --git a/dev_utils/stan_data_structure_cache/html/print_standata_e56099a6b47ed3d39868bf8b513c7650.rdb b/dev_utils/stan_data_structure_cache/html/print_standata_e56099a6b47ed3d39868bf8b513c7650.rdb deleted file mode 100644 index 065e39df..00000000 Binary files a/dev_utils/stan_data_structure_cache/html/print_standata_e56099a6b47ed3d39868bf8b513c7650.rdb and /dev/null differ diff --git a/dev_utils/stan_data_structure_cache/html/print_standata_e56099a6b47ed3d39868bf8b513c7650.rdx b/dev_utils/stan_data_structure_cache/html/print_standata_e56099a6b47ed3d39868bf8b513c7650.rdx deleted file mode 100644 index 3192ba55..00000000 Binary files a/dev_utils/stan_data_structure_cache/html/print_standata_e56099a6b47ed3d39868bf8b513c7650.rdx and /dev/null differ diff --git a/dev_utils/stan_data_structure_cache/html/setup_a4b6263f603e5a165a627185b2ffcabe.RData b/dev_utils/stan_data_structure_cache/html/setup_a4b6263f603e5a165a627185b2ffcabe.RData deleted file mode 100644 index 03ad7dcb..00000000 Binary files a/dev_utils/stan_data_structure_cache/html/setup_a4b6263f603e5a165a627185b2ffcabe.RData and /dev/null differ diff --git a/dev_utils/stan_data_structure_cache/html/setup_a4b6263f603e5a165a627185b2ffcabe.rdb b/dev_utils/stan_data_structure_cache/html/setup_a4b6263f603e5a165a627185b2ffcabe.rdb deleted file mode 100644 index 950bd8cc..00000000 Binary files a/dev_utils/stan_data_structure_cache/html/setup_a4b6263f603e5a165a627185b2ffcabe.rdb and /dev/null differ diff --git a/dev_utils/stan_data_structure_cache/html/setup_a4b6263f603e5a165a627185b2ffcabe.rdx b/dev_utils/stan_data_structure_cache/html/setup_a4b6263f603e5a165a627185b2ffcabe.rdx deleted file mode 100644 index ae4534e9..00000000 Binary files a/dev_utils/stan_data_structure_cache/html/setup_a4b6263f603e5a165a627185b2ffcabe.rdx and /dev/null differ diff --git a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-1_14c6b4dd9368f6852d1a28c050223535.RData b/dev_utils/stan_data_structure_cache/html/unnamed-chunk-1_14c6b4dd9368f6852d1a28c050223535.RData deleted file mode 100644 index 9a473f0a..00000000 Binary files a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-1_14c6b4dd9368f6852d1a28c050223535.RData and /dev/null differ diff --git a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-1_14c6b4dd9368f6852d1a28c050223535.rdb b/dev_utils/stan_data_structure_cache/html/unnamed-chunk-1_14c6b4dd9368f6852d1a28c050223535.rdb deleted file mode 100644 index e7c85fe4..00000000 Binary files a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-1_14c6b4dd9368f6852d1a28c050223535.rdb and /dev/null differ diff --git a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-1_14c6b4dd9368f6852d1a28c050223535.rdx b/dev_utils/stan_data_structure_cache/html/unnamed-chunk-1_14c6b4dd9368f6852d1a28c050223535.rdx deleted file mode 100644 index 418109a5..00000000 Binary files a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-1_14c6b4dd9368f6852d1a28c050223535.rdx and /dev/null differ diff --git a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-2_d944081f63ad8c89c80d24c28325be6e.RData b/dev_utils/stan_data_structure_cache/html/unnamed-chunk-2_d944081f63ad8c89c80d24c28325be6e.RData deleted file mode 100644 index 7e0b46fb..00000000 Binary files a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-2_d944081f63ad8c89c80d24c28325be6e.RData and /dev/null differ diff --git a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-2_d944081f63ad8c89c80d24c28325be6e.rdb b/dev_utils/stan_data_structure_cache/html/unnamed-chunk-2_d944081f63ad8c89c80d24c28325be6e.rdb deleted file mode 100644 index e69de29b..00000000 diff --git a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-2_d944081f63ad8c89c80d24c28325be6e.rdx b/dev_utils/stan_data_structure_cache/html/unnamed-chunk-2_d944081f63ad8c89c80d24c28325be6e.rdx deleted file mode 100644 index a97c3311..00000000 Binary files a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-2_d944081f63ad8c89c80d24c28325be6e.rdx and /dev/null differ diff --git a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-3_45e2bcd3e1f2224ea912e3ddeb2b7f18.RData b/dev_utils/stan_data_structure_cache/html/unnamed-chunk-3_45e2bcd3e1f2224ea912e3ddeb2b7f18.RData deleted file mode 100644 index 85a4197a..00000000 Binary files a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-3_45e2bcd3e1f2224ea912e3ddeb2b7f18.RData and /dev/null differ diff --git a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-3_45e2bcd3e1f2224ea912e3ddeb2b7f18.rdb b/dev_utils/stan_data_structure_cache/html/unnamed-chunk-3_45e2bcd3e1f2224ea912e3ddeb2b7f18.rdb deleted file mode 100644 index e69de29b..00000000 diff --git a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-3_45e2bcd3e1f2224ea912e3ddeb2b7f18.rdx b/dev_utils/stan_data_structure_cache/html/unnamed-chunk-3_45e2bcd3e1f2224ea912e3ddeb2b7f18.rdx deleted file mode 100644 index a97c3311..00000000 Binary files a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-3_45e2bcd3e1f2224ea912e3ddeb2b7f18.rdx and /dev/null differ diff --git a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-4_ad664068ce867a166776f83d06a4ee35.RData b/dev_utils/stan_data_structure_cache/html/unnamed-chunk-4_ad664068ce867a166776f83d06a4ee35.RData deleted file mode 100644 index 47f61e84..00000000 Binary files a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-4_ad664068ce867a166776f83d06a4ee35.RData and /dev/null differ diff --git a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-4_ad664068ce867a166776f83d06a4ee35.rdb b/dev_utils/stan_data_structure_cache/html/unnamed-chunk-4_ad664068ce867a166776f83d06a4ee35.rdb deleted file mode 100644 index 67885b02..00000000 Binary files a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-4_ad664068ce867a166776f83d06a4ee35.rdb and /dev/null differ diff --git a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-4_ad664068ce867a166776f83d06a4ee35.rdx b/dev_utils/stan_data_structure_cache/html/unnamed-chunk-4_ad664068ce867a166776f83d06a4ee35.rdx deleted file mode 100644 index 41e1f28f..00000000 Binary files a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-4_ad664068ce867a166776f83d06a4ee35.rdx and /dev/null differ diff --git a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-5_f65c3c9429d1abc427e3e798a4e299b4.RData b/dev_utils/stan_data_structure_cache/html/unnamed-chunk-5_f65c3c9429d1abc427e3e798a4e299b4.RData deleted file mode 100644 index 5ca815c8..00000000 Binary files a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-5_f65c3c9429d1abc427e3e798a4e299b4.RData and /dev/null differ diff --git a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-5_f65c3c9429d1abc427e3e798a4e299b4.rdb b/dev_utils/stan_data_structure_cache/html/unnamed-chunk-5_f65c3c9429d1abc427e3e798a4e299b4.rdb deleted file mode 100644 index 4f348368..00000000 Binary files a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-5_f65c3c9429d1abc427e3e798a4e299b4.rdb and /dev/null differ diff --git a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-5_f65c3c9429d1abc427e3e798a4e299b4.rdx b/dev_utils/stan_data_structure_cache/html/unnamed-chunk-5_f65c3c9429d1abc427e3e798a4e299b4.rdx deleted file mode 100644 index 55418622..00000000 Binary files a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-5_f65c3c9429d1abc427e3e798a4e299b4.rdx and /dev/null differ diff --git a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-6_75160aca8cc5aaa094bf88c5a7e51f59.RData b/dev_utils/stan_data_structure_cache/html/unnamed-chunk-6_75160aca8cc5aaa094bf88c5a7e51f59.RData deleted file mode 100644 index 02abc0f1..00000000 Binary files a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-6_75160aca8cc5aaa094bf88c5a7e51f59.RData and /dev/null differ diff --git a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-6_75160aca8cc5aaa094bf88c5a7e51f59.rdb b/dev_utils/stan_data_structure_cache/html/unnamed-chunk-6_75160aca8cc5aaa094bf88c5a7e51f59.rdb deleted file mode 100644 index 4346a4e8..00000000 Binary files a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-6_75160aca8cc5aaa094bf88c5a7e51f59.rdb and /dev/null differ diff --git a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-6_75160aca8cc5aaa094bf88c5a7e51f59.rdx b/dev_utils/stan_data_structure_cache/html/unnamed-chunk-6_75160aca8cc5aaa094bf88c5a7e51f59.rdx deleted file mode 100644 index baeb0169..00000000 Binary files a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-6_75160aca8cc5aaa094bf88c5a7e51f59.rdx and /dev/null differ diff --git a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-7_136a92ebd8394ce84c63c98f5c7445a9.RData b/dev_utils/stan_data_structure_cache/html/unnamed-chunk-7_136a92ebd8394ce84c63c98f5c7445a9.RData deleted file mode 100644 index eb3dc1da..00000000 Binary files a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-7_136a92ebd8394ce84c63c98f5c7445a9.RData and /dev/null differ diff --git a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-7_136a92ebd8394ce84c63c98f5c7445a9.rdb b/dev_utils/stan_data_structure_cache/html/unnamed-chunk-7_136a92ebd8394ce84c63c98f5c7445a9.rdb deleted file mode 100644 index e69de29b..00000000 diff --git a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-7_136a92ebd8394ce84c63c98f5c7445a9.rdx b/dev_utils/stan_data_structure_cache/html/unnamed-chunk-7_136a92ebd8394ce84c63c98f5c7445a9.rdx deleted file mode 100644 index a97c3311..00000000 Binary files a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-7_136a92ebd8394ce84c63c98f5c7445a9.rdx and /dev/null differ diff --git a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-8_17df3a44712670e24e0697b0a1c1c9f1.RData b/dev_utils/stan_data_structure_cache/html/unnamed-chunk-8_17df3a44712670e24e0697b0a1c1c9f1.RData deleted file mode 100644 index 2e90505a..00000000 Binary files a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-8_17df3a44712670e24e0697b0a1c1c9f1.RData and /dev/null differ diff --git a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-8_17df3a44712670e24e0697b0a1c1c9f1.rdb b/dev_utils/stan_data_structure_cache/html/unnamed-chunk-8_17df3a44712670e24e0697b0a1c1c9f1.rdb deleted file mode 100644 index e69de29b..00000000 diff --git a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-8_17df3a44712670e24e0697b0a1c1c9f1.rdx b/dev_utils/stan_data_structure_cache/html/unnamed-chunk-8_17df3a44712670e24e0697b0a1c1c9f1.rdx deleted file mode 100644 index a97c3311..00000000 Binary files a/dev_utils/stan_data_structure_cache/html/unnamed-chunk-8_17df3a44712670e24e0697b0a1c1c9f1.rdx and /dev/null differ