Error: dimensions specified are smaller than model specification for variable

495 views
Skip to first unread message

Travis Gallo

unread,
Jun 23, 2021, 8:47:07 PM6/23/21
to nimble-users
Hi all,

I am getting the following error and I am stumped:

"dimensions specified are smaller than model specification for variable 'a0'"

I have fit this model many times without issue, but went back and added a few data points and now I am getting this error. I have tried removing the 'a0' parameter from the initial values and the error goes away, but then I get NA's in my model results.

There are no NA's in `city_vec` nor `grade_vec` and all objects are of the correct length. Thanks in advance for the help.

norm_mod <- nimbleCode({
  
  # priors for betas
  for(grade in 1:ngrade){
    b0[grade] ~ dnorm(0, sd = b0_sd)
    b1[grade] ~ dnorm(0, sd = b1_sd)
  }
  # priors for sigma terms
  a0_sigma ~ dunif(0.001, 100)
  data_sigma ~ dunif(0.001, 100)
  
  # hyper priors for beta sigmas
  b0_sd ~ dunif(0.001, 100)
  b1_sd ~ dunif(0.001, 100)
  
  # likelihood
  for(grade in 1:ngrade){
    for(city in 1:ncity){
      a0_linpred[grade, city] <- 
        b0[grade] + b1[grade] * log_pop_dens[city]
      a0[grade,city] ~ dnorm(a0_linpred[grade,city], sd = a0_sigma)
    }
  }
  for(n in 1:ndata){
    y[n] ~ dnorm(a0[grade_vec[n],city_vec[n]], sd = data_sigma)
  }
  
})

# create model arguments
# constants list
my.constants <- list(ngrade = ngrade,
                     ncity = ncity,
                     ndata = ndata,
                     grade_vec = as.numeric(as.factor(final_dataset$holc_grade)),
                     city_vec = final_dataset$ID,
                     log_pop_dens = log_pd_all)

# initial values list
initial.values <- function () list(b0 = rnorm(ngrade, 100),
                                   b1 = rnorm(ngrade, 100),
                                   b0_sd = runif(1,0.001,100),
                                   b1_sd = runif(1,0.001,100),
                                   a0 = matrix(rnorm(ngrade*ncity),
                                               nrow = ngrade,
                                               ncol = ncity),
                                   a0_sigma = runif(1,0.001,100),
                                   data_sigma = runif(1,0.001,100))

Perry de Valpine

unread,
Jun 23, 2021, 9:11:20 PM6/23/21
to Travis Gallo, nimble-users
Hi Travis,

Thanks for the question.

I think this would occur if somehow the values of ngrade and ncity seen by the initial.values function are not the same as those used in my.constants.  Is it possible that somehow R's scoping is throwing you a curve ball?  initial.values will see ngrade and ncity from the environment where it is defined.  Is there any chance these could be different from the ngrade and ncity you need when you add the data points?

That's a guess. If that doesn't lead somewhere helpful, would you be able to send a reproducible example?

Perry


--
You received this message because you are subscribed to the Google Groups "nimble-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/67650c08-05a3-4822-8b92-881da99c3a64n%40googlegroups.com.

Travis Gallo

unread,
Jun 23, 2021, 9:24:11 PM6/23/21
to Perry de Valpine, nimble-users
Hi Perry,

Thanks so much! This does seem to be the issue although I don't understand it. The `initial.values` function was returning the correct dimensions I needed when I ran it on its own. However, based on your advice, I removed the function and just made the `initial.values` object the list (without it being a function) and that seemed to work. Additionally, I have fit this model many times before with test runs, and this just became an issue.

Thanks again for your speedy help!

Travis Gallo

"What good is a house, if you haven't got a decent planet to put it on?" -- Henry David Thoreau


Reply all
Reply to author
Forward
0 new messages