Brms initialization changing found vs declared dimension

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 
2 Likes

@rok_cesnovar @jonah

1 Like

Thanks for sharing the reproducible example.

I ran it quickly and these were all (or mostly all) during warmup, so nothing to worry about for that one.

But for the ones that error:

When I see errors like this

variable name=b_A; dims declared=(1); dims found=()

that usually suggests to me that we need to wrap the R variable in array().

Can you try wrapping each of these list elements in array()? So the init argument would be:

init = function() {
  list(
    b_A = array(rgamma(1, 1)),
    b_B = array(rgamma(1, 1)),
    b_C = array(rgamma(1, 1))
  )
}

I think that should get it to run, but what I don’t understand is why it would have worked with previous versions and not after updating (unless I’m misunderstanding the situation). As far as I know we haven’t changed anything in CmdStanR between versions that would affect this.

1 Like

Thank you for looking into this and providing your expertise (and on the weekend)!

I was not sure if the messages were a problem during warmup or not. I am curious if you could provide some more context on those errors when you have time. Since I am using a lognormal prior I would have thought sampling would be constrained to positive values even without the lower bound being set. Normally I can’t set the lower bound since I have multiple groups and often set priors for each with coef=group_x in set_prior which does not let me set lower bounds without an awkward workaround.

Wrapping the inits in array did let the model run without the dimensionality errors though! Thank you! You are understanding the situation correctly, I’ve been using functions to initialize one or many groups separately for the last few years and this week is the first time I’ve seen these errors.

To be complete, this is the model that worked with initialization for data with one group using the data and prior from above.

fit_solution <- 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=array(rgamma(1,1)),
                                          b_B=array(rgamma(1,1)), 
                                          b_C=array(rgamma(1,1)))})

As a follow up for anyone else who hits a similar error, the models I normally use have multiple groups and this solution seems to scale for that (if you are trying to initialize groups separately, not sure what the consensus is on if that matters much). Forgive the long dput output.

library(brms)
ex_2 <- 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", 
"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", "b", "b", "b", "b", "b", 
"b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", 
"b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", 
"b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", 
"b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", 
"b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", 
"b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", 
"b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", 
"b", "b", "b", "b"), 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, 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(3.11442481888677, 
14.0995503917349, 38.6481075669006, 71.1379684871818, 104.290395707447, 
132.951472149493, 172.054638191727, 184.206821872037, 198.906679738003, 
193.716815496233, 1.41090421065147, 8.83668498406842, 27.9925372437419, 
54.2431531645146, 92.4516558501267, 125.89753934614, 149.38907143962, 
175.229316000998, 182.098399310683, 194.305753490648, 2.61489064486719, 
10.2142827997908, 25.2196713597032, 45.5130519720647, 70.1819732941379, 
105.537201755011, 120.842418924153, 149.385978743858, 159.351968738984, 
176.863417489084, 3.62672956251941, 21.3420022240203, 51.9119574756247, 
92.8630152531836, 132.826718627666, 153.893537442984, 177.86710481811, 
196.642096907194, 194.642167882959, 190.582972523945, 1.07722906793731, 
5.22631991698963, 15.1883943209587, 36.8997529241541, 64.0480292812994, 
85.6400882597351, 115.001411789019, 138.008260917827, 157.018568970844, 
167.302221847344, 1.85247388713963, 6.75177269665878, 18.2280358295362, 
38.3381214874392, 58.2110181521781, 78.0386381358326, 98.6133027094764, 
118.929596235969, 134.27466911665, 143.381734108869, 2.39039594328302, 
9.16929807534424, 24.804159007528, 44.9749435058794, 72.4567590539816, 
96.7649050264963, 115.213506196994, 143.185470454365, 154.359261616724, 
156.713578597361, 3.47219091930131, 17.7728784791006, 40.6625852894442, 
76.6389256045066, 114.027813305785, 141.910747744289, 166.842487590021, 
179.883395726928, 178.577497451073, 199.552626092841, 1.69724597375495, 
8.99680739253087, 24.5562545398499, 52.1103805016397, 81.9658590841796, 
112.398265345853, 142.272232114653, 174.527571121877, 183.12694460838, 
179.715024970623, 3.27933148986331, 12.8125702726844, 33.0393527861621, 
61.5251090631947, 96.3095026179728, 122.353592620702, 131.510133336338, 
151.502891254759, 159.932381704919, 169.286604987344, 1.35569830479481, 
8.66529065187858, 24.6639578638829, 47.2147502772664, 67.8641169923869, 
86.6312751791353, 97.6210506262355, 105.395104656579, 118.252214811544, 
122.477346688747, 1.13943613458418, 7.76182359447744, 24.2018195958388, 
48.9153292726207, 74.5559167659809, 91.4443055460628, 108.0215307242, 
113.823191573391, 116.624404368906, 128.411113102853, 1.08858792241917, 
8.37393329135247, 25.224013230462, 49.9945061635967, 69.9445096323909, 
92.9791581336544, 100.861357218047, 104.887383055483, 123.679894746481, 
116.498492536226, 0.915905593132933, 6.96396614338756, 23.052105889345, 
47.2079160070944, 76.1362725234375, 103.451462689029, 119.97201490311, 
126.245309790446, 136.826597036252, 133.969777727363, 1.29174589971268, 
9.06509473963486, 27.1478805001027, 56.5869357255352, 77.2384797578148, 
100.571484478301, 116.767349663265, 114.115344001333, 127.672981226834, 
127.311280818076, 1.4152627148585, 7.89654610984164, 24.6779574013769, 
48.8775713485162, 73.6182398616591, 92.7054591516627, 106.175809179861, 
119.870082236359, 129.487029269822, 137.994350619467, 1.21206767719028, 
7.17302776449981, 20.8268125706605, 40.850608457694, 62.5465558368006, 
85.6165242834533, 107.15865122054, 111.010282207505, 117.951898624685, 
133.786488925398, 2.4226476336, 13.5452997374865, 38.9220009930595, 
61.9154609492355, 86.6354223964047, 108.218988812113, 112.364287660542, 
119.91470730843, 127.776325016454, 130.035512688561, 1.28334860016648, 
7.50828438957748, 24.4120050349915, 46.6968849163987, 74.3494219010681, 
91.7563089698331, 105.301996321227, 112.682968851952, 128.15864327205, 
126.729235300562, 2.41211234811757, 14.4739012323429, 37.8679555188774, 
70.9853842553796, 104.050174250595, 124.0941628506, 133.157054820585, 
141.781542480285, 155.615961871451, 148.050372690905)), row.names = c(NA, 
-200L), class = "data.frame")

prior2 <- prior(student_t(3,0,5), dpar="sigma") +
  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_test_2_groups <- brm(bf(y ~ A*exp(-B*exp(-C*time)),
                   sigma ~ 0+group,
                   A+B+C ~ 0+group, 
                   autocor = ~arma(~time|sample:group,1,1),nl = TRUE),
                family = student, prior = prior2, data = ex_2, iter = 600, 
                cores = 4, chains = 4, backend = "cmdstanr", silent = 0,
                control = list(adapt_delta = 0.999,max_treedepth = 20),
                init = function(){list(b_A=array(rgamma(2,1)),
                                       b_B=array(rgamma(2,1)), 
                                       b_C=array(rgamma(2,1)))})

The constraints in the parameters block also specify the valid values for initialization so it could potentially randomly initialize to a negative value if you don’t have a lower bound of 0. Maybe that’s what’s going on here. I think that’s why brms has this warning:

It appears as if you have specified a lower bounded prior on a parameter that has no natural lower bound.
If this is really what you want, please specify argument 'lb' of 'set_prior' appropriately.
Warning occurred for prior 

Yeah that’s really strange. Maybe @rok_cesnovar or @paul.buerkner will have an idea about why we’re suddenly seeing this now (nothing occurs to me at the moment). But glad we were able to get it working using array in R.

1 Like