Hello,
I had to reinstall ubuntu (22.04) yesterday and with it I installed R (4.1.2) , brms (2.19.0), and moved to the new cmdstanr (0.6.0).
Since those updates I have been having trouble with models that were working very predictably using cmdstanr 0.4.0 and brms 2.17.0/2.19.0 under R 4.2.0 and on macOS/Ubuntu (20.04). The problems seem to be stemming from initialization and warn about the declared vs found dimensions for the first non-linear parameter (b_A here). The message says that observed dimension is the length of the element in the initialization list, but I have not seen that happen before with brms
.
From other posts it looked like that error often had to do with data types (passing an array instead of a vector), but I don’t see how that would change between these models since the only difference is the init argument. These errors are consistent between backends cmdstanr
and rstan
and based on the cmdstanr::sample
documentation this should still work as I understand.
Any advice would be greatly appreciated.
Here is an example using a small toy dataset and few iterations. Some example messages and errors are below.
library(brms)
# output from dput on some small example data
ex<-structure(list(id = c("id_1", "id_1", "id_1", "id_1", "id_1",
"id_1", "id_1", "id_1", "id_1", "id_1", "id_2", "id_2", "id_2",
"id_2", "id_2", "id_2", "id_2", "id_2", "id_2", "id_2", "id_3",
"id_3", "id_3", "id_3", "id_3", "id_3", "id_3", "id_3", "id_3",
"id_3", "id_4", "id_4", "id_4", "id_4", "id_4", "id_4", "id_4",
"id_4", "id_4", "id_4", "id_5", "id_5", "id_5", "id_5", "id_5",
"id_5", "id_5", "id_5", "id_5", "id_5", "id_6", "id_6", "id_6",
"id_6", "id_6", "id_6", "id_6", "id_6", "id_6", "id_6", "id_7",
"id_7", "id_7", "id_7", "id_7", "id_7", "id_7", "id_7", "id_7",
"id_7", "id_8", "id_8", "id_8", "id_8", "id_8", "id_8", "id_8",
"id_8", "id_8", "id_8", "id_9", "id_9", "id_9", "id_9", "id_9",
"id_9", "id_9", "id_9", "id_9", "id_9", "id_10", "id_10", "id_10",
"id_10", "id_10", "id_10", "id_10", "id_10", "id_10", "id_10"
), group = c("a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a"),
time = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L), y = c(2.74475025044536, 10.1921565179006,
24.1072078438499, 47.1881483690918, 72.6368217580982, 97.1666532090654,
113.565128116017, 142.86253377555, 153.06114477328, 163.946058061714,
2.48514182973363, 9.88457760438115, 27.8335088411506, 51.2913412797086,
81.1744438836015, 112.581439988203, 137.567221844923, 152.763001311142,
168.32660226753, 184.01691518901, 1.31164651966428, 8.64822317578209,
26.4579412405875, 55.4071784497364, 85.265116939471, 120.205630836521,
136.327180663789, 160.130804665902, 168.559490903364, 173.946689859869,
2.15950636112573, 9.70273981461778, 25.2629719071549, 50.8538977758164,
81.4954408428258, 109.532265150856, 132.149124669598, 153.442950237014,
165.704841011627, 178.28369916261, 3.15668118034453, 12.265313370203,
29.7293632416026, 56.3596366457171, 88.2957381830673, 120.800061765422,
149.15987346655, 164.905694278059, 184.339229790729, 198.634039052937,
4.91392752230102, 24.2974153110245, 59.9240449919487, 102.599825061614,
139.34355508688, 160.947453518865, 184.735208676012, 185.499628489456,
197.081489017182, 194.624475897962, 2.43364918319672, 10.3408957111251,
24.9342333112476, 47.0704889844873, 71.5042350710066, 93.8944136928708,
112.062082351471, 127.852238543083, 143.389917423098, 148.592483470889,
1.85952119314531, 9.27151155214752, 26.8615008056052, 56.279958255506,
87.461738589312, 128.774094076567, 152.142783228647, 174.275944602787,
194.837400144913, 195.816566458147, 2.97897818390772, 11.178309482919,
25.7467544036851, 48.3560001535617, 72.4026027672258, 99.4159399123482,
115.930564976063, 129.64268858178, 148.465640341127, 150.215739653175,
2.06990388679979, 10.2107703083078, 29.9149817886782, 55.1570253525488,
80.4828272020481, 122.242650945746, 142.860109428487, 163.339035170718,
174.122333363362, 182.540472103961)), row.names = c(NA, -100L
), class = "data.frame")
prior1 <- prior(student_t(3,0,5), dpar="sigma", class="Intercept") +
prior(gamma(2,0.1), class="nu")+
prior(lognormal(log(130), .25),nlpar = "A") +
prior(lognormal(log(12), .25), nlpar = "B") +
prior(lognormal(log(1.2), .25), nlpar = "C")
fit_works <- brm(bf(y ~ A*exp(-B*exp(-C*time)), # this works, but gives lots of exceptions
sigma ~ 1,
A+B+C ~ 1,
autocor = ~arma(~time|sample:group,1,1),nl = TRUE),
family = student, prior = prior1, data = ex, iter = 600,
cores = 4, chains = 4, backend = "cmdstanr", silent = 0,
control = list(adapt_delta = 0.999,max_treedepth = 20))
fit_fails_1 <- brm(bf(y ~ A*exp(-B*exp(-C*time)),
sigma ~ 1,
A+B+C ~ 1,
autocor = ~arma(~time|sample:group,1,1),nl = TRUE),
family = student, prior = prior1, data = ex, iter = 600,
cores = 4, chains = 4, backend = "cmdstanr", silent = 0,
control = list(adapt_delta = 0.999,max_treedepth = 20),
init = function(){list(b_A=rgamma(1,1),b_B=rgamma(1,1),b_C=rgamma(1,1))})
# fails and warns about dims declared (1)
# giving extra inits to show the problem again
fit_fails_2 <- brm(bf(y ~ A*exp(-B*exp(-C*time)),
sigma ~ 1,
A+B+C ~ 1,
autocor = ~arma(~time|sample:group,1,1),nl = TRUE),
family = student, prior = prior1, data = ex, iter = 600,
cores = 4, chains = 4, backend = "cmdstanr", silent = 0,
control = list(adapt_delta = 0.999,max_treedepth = 20),
init = function(){list(b_A=rgamma(4,1),b_B=rgamma(4,1),b_C=rgamma(4,1))})
# fails and warns about dims declared (4)
fit_fails_3 <- brm(bf(y ~ A*exp(-B*exp(-C*time)),
sigma ~ 1,
A+B+C ~ 1,
autocor = ~arma(~time|sample:group,1,1),nl = TRUE),
family = student, prior = prior1, data = ex, iter = 600,
cores = 4, chains = 4, backend = "cmdstanr", silent = 0,
control = list(adapt_delta = 0.999,max_treedepth = 20),
init = 0) # fails and warns about dim declared (1)
With the models were I specify an init
argument I am getting errors like:
Chain X Unrecoverable error evaluating the log probability at the initial value.
Chain X Exception: mismatch in number dimensions declared and found in context; processing stage=parameter initialization; variable name=b_A; dims declared=(1); dims found=() (in '/tmp/RtmpYy6uvL/model-15d11c822ea8.stan', line 57, column 2 to column 18)
The model that does fit still gives several messages that I am not used to, but I am not sure if those are from the change in cmdstanr’s messaging or from not initializing chains to be strictly positive (or some other thing I am not understanding).
Chain 4 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Chain 4 Exception: student_t_lpdf: Scale parameter[1] is inf, but must be positive finite! (in '/tmp/RtmpYy6uvL/model-15d1bf8c49a.stan', line 93, column 4 to column 48)
Chain 4 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
Chain 4 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Chain 4
Chain 2 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Chain 2 Exception: lognormal_lpdf: Random variable[1] is -0.185849, but must be nonnegative! (in '/tmp/RtmpYy6uvL/model-15d1bf8c49a.stan', line 67, column 2 to column 49)
Chain 2 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
Chain 2 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Chain 2