Normal_id_glm

I am trying to use the normal_id_glm functions and cmdstanr. But in the model below, using either normal_id_glm or normal_id_glm_lpdf results in every transition being rejected:

Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Chain 1 Exception: normal_id_glm_lpdf: Matrix of independent variables is -nan, but must be finite! (in '/tmp/RtmppXmY0a/model-7ec359369638.stan', line 18, column 24 to column 80)
Chain 1 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
Chain 1 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.

What’s wrong with the model?


data{
  int N;
  int M;
  matrix[N,M] x;
  vector[N] y;
  real sigma;
  int use_glm;
}
parameters {
  real alpha;
  vector[M] beta;
}
model {
  beta ~ normal(0, 2);
  alpha ~ normal(0, 2);
  
  if(use_glm == 2) y ~ normal_id_glm(x, alpha, beta, sigma);
  else if(use_glm == 1) target += normal_id_glm_lpdf(y | x, alpha, beta, sigma);
  else y ~ normal(alpha + x * beta, sigma);
}
library(cmdstanr)
seed <- 03282020
set.seed(seed)
N <- 1e3
M <- 2
a <- 0.5
sigma <- 0.5
x <- matrix(rnorm(N*M), nrow=N)
b <- rnorm(M)
y <- rnorm(N, a + x %*% b, sd=sigma)
data_glm <-    list(N=N, sigma=sigma, y=y, M=M, x=x, use_glm = 2)
data_lpdf <-   list(N=N, sigma=sigma, y=y, M=M, x=x, use_glm = 1)
data_normal <- list(N=N, sigma=sigma, y=y, M=M, x=x, use_glm = 0)

model <- cmdstan_model("normal_id_glm.stan", quiet = FALSE)

# 100% divergence
fit_glm <- model$sample(
  data = data_glm, 
  num_warmup = 500,
  num_samples = 100, 
  num_chains = 1, 
  num_cores = 1,
  seed = seed)

# 100% divergence
fit_lpdf <- model$sample(
  data = data_lpdf, 
  num_warmup = 500,
  num_samples = 100, 
  num_chains = 1, 
  num_cores = 1,
  seed = seed)

# does just fine
fit_normal <- model$sample(
  data = data_normal, 
  num_warmup = 500,
  num_samples = 100, 
  num_chains = 1, 
  num_cores = 1,
  seed = seed)

Thanks,

Session Info
> sessioninfo::session_info()
─ Session info ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 setting  value                       
 version  R version 3.6.3 (2020-02-29)
 os       Ubuntu 18.04.4 LTS          
 system   x86_64, linux-gnu           
 ui       RStudio                     
 language (EN)                        
 collate  en_US.UTF-8                 
 ctype    en_US.UTF-8                 
 tz       America/New_York            
 date     2020-03-28                  

─ Packages ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 package     * version    date       lib source                            
 abind         1.4-5      2016-07-21 [1] CRAN (R 3.6.0)                    
 assertthat    0.2.1      2019-03-21 [1] CRAN (R 3.6.0)                    
 backports     1.1.5      2019-10-02 [1] CRAN (R 3.6.1)                    
 callr         3.4.3      2020-03-28 [1] CRAN (R 3.6.3)                    
 checkmate     2.0.0      2020-02-06 [1] CRAN (R 3.6.1)                    
 cli           2.0.2      2020-02-28 [1] CRAN (R 3.6.1)                    
 cmdstanr    * 0.0.0.9000 2020-03-28 [1] Github (stan-dev/cmdstanr@ad9c58d)
 colorspace    1.4-1      2019-03-18 [1] CRAN (R 3.6.1)                    
 crayon        1.3.4      2017-09-16 [1] CRAN (R 3.6.0)                    
 dplyr         0.8.5      2020-03-07 [1] CRAN (R 3.6.1)                    
 fansi         0.4.1      2020-01-08 [1] CRAN (R 3.6.1)                    
 ggplot2       3.3.0      2020-03-05 [1] CRAN (R 3.6.1)                    
 glue          1.3.2      2020-03-12 [1] CRAN (R 3.6.1)                    
 gridExtra     2.3        2017-09-09 [1] CRAN (R 3.6.0)                    
 gtable        0.3.0      2019-03-25 [1] CRAN (R 3.6.0)                    
 inline        0.3.15     2018-05-18 [1] CRAN (R 3.6.0)                    
 jsonlite      1.6.1      2020-02-02 [1] CRAN (R 3.6.1)                    
 lifecycle     0.2.0      2020-03-06 [1] CRAN (R 3.6.1)                    
 loo           2.2.0      2019-12-19 [1] CRAN (R 3.6.1)                    
 magrittr      1.5        2014-11-22 [1] CRAN (R 3.6.0)                    
 matrixStats   0.56.0     2020-03-13 [1] CRAN (R 3.6.1)                    
 munsell       0.5.0      2018-06-12 [1] CRAN (R 3.6.0)                    
 pillar        1.4.3      2019-12-20 [1] CRAN (R 3.6.1)                    
 pkgbuild      1.0.6      2019-10-09 [1] standard (@1.0.6)                 
 pkgconfig     2.0.3      2019-09-22 [1] standard (@2.0.3)                 
 posterior     0.0.2      2020-03-28 [1] Github (jgabry/posterior@e517f32) 
 prettyunits   1.1.1      2020-01-24 [1] CRAN (R 3.6.1)                    
 processx      3.4.2      2020-02-09 [1] CRAN (R 3.6.1)                    
 ps            1.3.2      2020-02-13 [1] CRAN (R 3.6.1)                    
 purrr         0.3.3      2019-10-18 [1] CRAN (R 3.6.1)                    
 R6            2.4.1      2019-11-12 [1] CRAN (R 3.6.1)                    
 Rcpp          1.0.4      2020-03-17 [1] CRAN (R 3.6.3)                    
 rlang         0.4.5      2020-03-01 [1] CRAN (R 3.6.1)                    
 rstan         2.19.3     2020-02-11 [1] CRAN (R 3.6.1)                    
 rstudioapi    0.11       2020-02-07 [1] CRAN (R 3.6.1)                    
 scales        1.1.0      2019-11-18 [1] standard (@1.1.0)                 
 sessioninfo   1.1.1      2018-11-05 [1] CRAN (R 3.6.0)                    
 StanHeaders   2.19.2     2020-02-11 [1] CRAN (R 3.6.1)                    
 tibble        2.1.3      2019-06-06 [1] CRAN (R 3.6.0)                    
 tidyselect    1.0.0      2020-01-27 [1] CRAN (R 3.6.1)                    
 withr         2.1.2      2018-03-15 [1] CRAN (R 3.6.0)                    

[1] /home/psadil/R/x86_64-pc-linux-gnu-library/3.6
[2] /usr/local/lib/R/site-library
[3] /usr/lib/R/site-library
[4] /usr/lib/R/library```
</details>
1 Like

@rok_cesnovar do u have an idea?

Hm, I kind of doubt this is a cmdstanr issue. More likely a cmdstan one. @tadej worked on the glms a ton. He might know something more.

I will definitely try to reproduce in cmdstan and raise issues on the appropriate repos.

1 Like

@psadil Thanks for the report!

I see what is the issue. It is simple to fix. Here is the PR with the fix: https://github.com/stan-dev/math/pull/1807.

4 Likes