Absent parameters

  • Operating System: Ubuntu 16.04.4
  • brms Version: 2.2.3

Hello!

I have trouble after fitting a distributional model with splines. Here is the code to specify the model :

formula_mu_phi_ymix <- brmsformula(h ~ s(years, by = divsp, k = 4, fx = F, bs = "ts") +
                                      block + (1|mixture) + (1|divsp),
                                    phi ~ s(years, by = divsp, k = 4, fx = F, bs = "ts") +
                                      block + (1|mixture) + (1|divsp),
                                    family = gamma2)

## Set new priors
prior_mu_phi_ymix <- c(prior(student_t(3,0,3), class = b),
                        prior(student_t(3,0,3), class = b, dpar = "phi"),
                        prior(student_t(3,0,1), class = sd),
                        prior(student_t(3,0,1), class = sd, dpar = "phi"),
                        prior(student_t(3,0,1), class = sds),
                        prior(student_t(3,0,1), class = sds, dpar = "phi"))

# Fit the model
gamm_mu_phi_ymix <- brm(formula_mu_phi_ymix, data = Dfact, prior = prior_mu_phi_ymix,
                         stan_funs = stan_funs, inits = 0,
                         iter = 2000, chains = 4, cores = 4,
                         algorithm = "sampling",
                         save_model = "Results/gamm_mu_phi_ymix.stan",
                         control = list(adapt_delta = 0.95, max_treedepth = 12))

The model has been well sampled, even if I think he might be overfitted. However, when I try to compute information criteria, or to compute any other operations using posterior distributions (such as marginal effects, fitted, etc.), I get the following error message :

Error in check_pars(allpars, pars) : 
  no parameter sds_syearsdivspHigh 1_1, sds_syearsdivspHigh 2_1, sds_syearsdivspHigh 3_1, sds_syearsdivspHigh 4_1, sds_syearsdivspHigh 5_1, sds_syearsdivspHigh 6_1, sds_syearsdivspHigh 7_1, sds_syearsdivspLow 1_1, sds_syearsdivspLow 2_1, sds_syearsdivspLow 3_1, sds_syearsdivspLow 4_1, sds_syearsdivspLow 5_1, sds_syearsdivspLow 6_1, sds_syearsdivspLow 7_1, sds_syearsdivspMed 1_1, sds_syearsdivspMed 2_1, sds_syearsdivspMed 3_1, sds_syearsdivspMed 4_1, sds_syearsdivspMed 5_1, sds_syearsdivspMed 6_1, sds_syearsdivspMed 7_1, sds_phi_syearsdivspHigh 1_1, sds_phi_syearsdivspHigh 2_1, sds_phi_syearsdivspHigh 3_1, sds_phi_syearsdivspHigh 4_1, sds_phi_syearsdivspHigh 5_1, sds_phi_syearsdivspHigh 6_1, sds_phi_syearsdivspHigh 7_1, sds_phi_syearsdivspLow 1_1, sds_phi_syearsdivspLow 2_1, sds_phi_syearsdivspLow 3_1, sds_phi_syearsdivspLow 4_1, sds_phi_syearsdivspLow 5_1, sds_phi_syearsdivspLow 6_1, sds_phi_syearsdivspLow 7_1, sds_phi_syearsdivspMed 1_1, sds_phi_syearsdivspMed 2_1, sds_phi_

It seems that the returned brmsfit object does not contain any standard-deviation of splines’ weights! However, I can see the summary of the posterior samples using summary. My guess is that they can’t be accessed…

Any help? I guess it might be a linguistic environment problem : I already had trouble using the brms output of one computer on another one with different language settings (living in a bilingual country :D )

Sys.getenv()
...
LANG                        fr_CA.UTF-8
LANGUAGE                    fr_CA:fr
...
Sys.getlocale()
"LC_CTYPE=fr_CA.UTF-8;LC_NUMERIC=C;LC_TIME=fr_CA.UTF-8;LC_COLLATE=fr_CA.UTF-8;LC_MONETARY=fr_CA.UTF-8;LC_MESSAGES=fr_CA.UTF-8;LC_PAPER=fr_CA.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=fr_CA.UTF-8;LC_IDENTIFICATION=C"

Lucas

I would try calling brm with the non-default save_all_pars = TRUE argument.

Thanks for the suggestion!

I just tried this setting with a few iterations, but it unfortunatly did not solve the problem…

My intuition is that the problem comes from how spaces are encoded in parameters’ names… But I am not a computer specialist!

If we look at the parnames of a model I sampled from last week, which dit not create any trouble.

"sds_phi_syearsspecies1_1"    "sds_phi_syearsspecies2_1"    "sds_phi_syearsspecies3_1"   
[19] "sds_phi_syearsspecies4_1"    "sds_phi_syearsspecies5_1"    "sds_phi_syearsspecies6_1"

and the model I fitted tonight :

"sds_phi_syearsdivspHigh 2_1"    "sds_phi_syearsdivspHigh 3_1"   
 [33] "sds_phi_syearsdivspHigh 4_1"    "sds_phi_syearsdivspHigh 5_1"   
 [35] "sds_phi_syearsdivspHigh 6_1"    "sds_phi_syearsdivspHigh 7_1"

I realize this is due to the way I encoded my variable, I forget sep = "" in paste().

Ok, problem solved, what a ridiculous (but informative) mistake…

can you send us a reproducible example?

Here is a very quickly made reproducible example.

## Empty the environment
rm(list = ls())

library(tidyverse)
library(brms)

# Number of individual per species
nsp = 20

for(i in 1:5){
  Xi<- data.frame(
    div = c(rep("Low",2*nsp), rep("Med",2*nsp), rep("High",2*nsp)),
    sp = c(rep(1,nsp), rep(2,nsp), rep(1,nsp), rep(2,nsp), rep(1,nsp), rep(2,nsp)),
    year = i, mu = NA, y = NA) %>% 
    mutate(divsp = as.factor(paste(div, sp))) # Create a category for each sp and div level
  if(i == 1) X <- Xi
  if(i > 1) X <- rbind(X,Xi)
}
summary(X)

udivsp <- unique(X$divsp)

## Extract mean slopes
mu_divsp <- rnorm(udivsp, 0 ,1)
mu_divsp2 <- rnorm(udivsp, 0 ,1)
mu_divsp3 <- rnorm(udivsp, 0 ,1)

## Compute predictor
for(i in 1:length(udivsp)){
  X$mu[X$divsp == udivsp[i]] <- (mu_divsp[i] * X$year[X$divsp == udivsp[i]] + 
                               mu_divsp2[i]*X$year[X$divsp == udivsp[i]]^2 + 
                                 mu_divsp3[i]*X$year[X$divsp == udivsp[i]]^3)
}

## Compute stochastic value
for(i in 1:nrow(X)) X$y[i] <- rnorm(1, X$mu[i], 10)

## Look at the result
ggplot(data = X, aes(x = year, y = y, color = div)) + 
  facet_wrap(~sp) +
  geom_point() + geom_smooth()

## Sample from the posterior
mod <- brm(y ~ s(year, by = divsp, k = 3, fx = F), data = X, chains = 1, cores = 1)
head(fitted(mod))

Error in check_pars(allpars, pars) : 
  no parameter sds_syeardivspHigh 1_1, sds_syeardivspHigh 2_1, sds_syeardivspLow 1_1, sds_syeardivspLow 2_1, sds_syeardivspMed 1_1, sds_syeardivspMed 2_1, s_syeardivspHigh 1_1[1], s_syeardivspHigh 2_1[1], s_syeardivspLow 1_1[1], s_syeardivspLow 2_1[1], s_syeardivspMed 1_1[1], s_syeardivspMed 2_1[1]

I guess this bug with space in factor level will appear everytime the parameters returned by brms will have it on its name. However, one would not normally code a composite factor, I did it only because of the nature of the “by” argument of s().

The problem is that the levels of divsp contain spaces, which causes the parameter extraction to fail. Removing the spaces will fix your problem.

Yes, I did understand that this morning after almost one night of posterior sampling :P

Thanks Paul!

Oh sorry, I did not see the last sentence of your former post. Sorry for informing you about something you already knew. ;-)

I now fixed the problem also from the brms side (dev version on github), which will now remove the whitespaces automatically in the parameter names. Unfortunately, you will still have to refit your model for this fix to work.

Thank you Paul, you’re great! It’s incredible to use a package with such a reactive and efficient maintainer!!

All the best,
Lucas