metrumresearchgroup / Torsten

library of C++ functions that support applications of Stan in Pharmacometrics
BSD 3-Clause "New" or "Revised" License
52 stars 11 forks source link

[BUG] Function pmx_solve_twocpt not found. #38

Closed diegosrodrigues closed 2 years ago

diegosrodrigues commented 2 years ago

First I should mention that I really do not know if this is really a bug of the code. Probably it is not.

After installation, I am trying to run the example "https://github.com/metrumresearchgroup/torsten_stancon_2019_tutorial/blob/master/RScripts/twoCptPopModel.r" but when I do that, I am getting the following error when run the fit <- stan(file = ...):

SYNTAX ERROR, MESSAGE(S) FROM PARSER: No matches for:

pmx_solve_twocpt(real[ ], real[ ], real[ ], real[ ], int[ ], int[ ], int[ ], int[ ], real[ ], real[ ], real[ ])

Function pmx_solve_twocpt not found. error in 'model1a9761637faef_twoCptPopulationModel' at line 64, column 72

62:                                                addl[start[j]:end[j]],
63:                                                ss[start[j]:end[j]],
64:                                                theta[j, ], biovar, tlag);
                                                                           ^
65: 

I am using RStudio 2021.9.1 in Ubuntu 20.04. I have installed rstan, cmdstan and I also downloaded Torsten via git clone. I have performed the steps bellow, and in it everything looks fine.

cd Torsten/cmdstan/ make build make ../example-models/pk2cpt/pk2cpt

Many thanks for your support.

yizhang-yiz commented 2 years ago

If you use cmdstan to run that model you'll see it builds fine.

cd Torsten/cmdstan
make /PATH_XXX/torsten_stancon_2019_tutorial/RScripts/model/solutions/twoCptPopulationModel

The reason you got the error is that last year we switched from rstan to cmdstanr, and that 2019 tutorial is based on rstan, which you probably have installed in rstudio but not the correct version that Torsten used to provide.

Anyways, to run the same model using cmdstanr in that R script, replace the fit <- stan(...) lines with the following (assuming you're at path torsten_stancon_2019_tutorial/RScripts)

library("cmdstanr")
set_cmdstan_path("/PATH_TO_TORSTEN/Torsten/cmdstan")
mod  <- cmdstan_model("model/solutions/twoCptPopulationModel.stan", force_recompile=TRUE)
fit  <- mod$sample(data="data/twoCptPop.data.r", init=init)

But first you'll need to install the package from https://mc-stan.org/cmdstanr/index.html. The postprocessing would also be different, see, for example, the simple workflow https://metrumrg.com/wp-content/uploads/2021/09/7188-page_2021_poster_Final.pdf.

Let me know if that works for you.

diegosrodrigues commented 2 years ago

Dear Yi Zhang,

Thank you for your immediate reply. I already have installed cmdstanr, but indeed I was using rstan and not the cmdstanr package.

I replaced the line fit <- stan(...) for the ones you recommended and not everything seems to work well. Just to let you know, for that I have kept the command library(rstan) in the code (on the contrary, I got an error when running the (updated) line related to the fitting: Error in runif(nIIV, 0, 1) : object 'nIIV' not found

Is that OK?

As a last comment, in the beginning of the running of each chain I am receiving the following message - I am using set.seed(1954):

Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue: Chain 1 Exception: PMXTwoCptModel: CL is 0, but must be positive finite! (in '/tmp/Rtmpu3ojtR/model-1d85b79498df.stan', line 56, column 4 to line 64, column 73) 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. Chain 1 Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue: Chain 1 Exception: twoCptPopulationModel_model_namespace::log_prob: concentration[sym1__] is -nan, but must be greater than or equal to 0 (in '/tmp/Rtmpu3ojtR/model-1d85b79498df.stan', line 51, column 2 to column 46) 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.

Anyways, many thanks also for the post-processing example you recommended.

yizhang-yiz commented 2 years ago

Error in runif(nIIV, 0, 1) : object 'nIIV' not found

It's likely from the init function in the R script. You should rerun it so make sure the variables are generated correctly, like

rm(list = ls())
gc()
set.seed(1954)

library(plyr)
library(tidyr)
library(dplyr)

data <- rstan::read_rdump("data/twoCptPop.data.r")
nIIV <- data$nIIV
nSubjects <- data$nSubjects

# Specify initial conditions: check if needed.
init <- function() {
  list(CL_pop = exp(rnorm(1, log(10), 0.2)),
       Q_pop = exp(rnorm(1, log(15), 0.2)),
       VC_pop = exp(rnorm(1, log(35), 0.2)),
       VP_pop = exp(rnorm(1, log(105), 0.2)),
       ka_pop = exp(rnorm(1, log(2), 0.2)),
       sigma = abs(rcauchy(1, 0, 1)),
       omega = abs(runif(nIIV, 0, 1)),
       # for non-centered param
       alpha = matrix(rnorm(nSubjects * nIIV, 0, 1),
                      nrow = nSubjects, ncol = nIIV))
}

# Run Stan
library("cmdstanr")
set_cmdstan_path("/PATH_TO_TORSTEN/Torsten/cmdstan")
mod  <- cmdstan_model("model/solutions/twoCptPopulationModel.stan", force_recompile=TRUE)
fit  <- mod$sample(data="data/twoCptPop.data.r", init=init)

Running the above lines doesn't produce the error on my end.

It's ok to have those Chain 1 Informational Message outputs at the beginning of the sampling. For this particular model it happens only at the first ~50 iterations, so the output will be similar to

Running MCMC with 4 sequential chains...

Chain 1 Iteration:    1 / 2000 [  0%]  (Warmup) 
Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Chain 1 Exception: PMXTwoCptModel: CL is 0, but must be positive finite! (in '/var/folders/5d/_pkf036n7rzcbc2b5yl_zqsh0000gn/T/Rtmp7V7qyG/model-6d085318a309.stan', line 56, column 4 to line 64, column 73)
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.
Chain 1 
Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Chain 1 Exception: PMXTwoCptModel: CL is 0, but must be positive finite! (in '/var/folders/5d/_pkf036n7rzcbc2b5yl_zqsh0000gn/T/Rtmp7V7qyG/model-6d085318a309.stan', line 56, column 4 to line 64, column 73)
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.
Chain 1 
Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Chain 1 Exception: twoCptPopulationModel_model_namespace::log_prob: concentration[sym1__] is nan, but must be greater than or equal to 0 (in '/var/folders/5d/_pkf036n7rzcbc2b5yl_zqsh0000gn/T/Rtmp7V7qyG/model-6d085318a309.stan', line 51, column 2 to column 46)
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.
Chain 1 
Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Chain 1 Exception: twoCptPopulationModel_model_namespace::log_prob: concentration[sym1__] is nan, but must be greater than or equal to 0 (in '/var/folders/5d/_pkf036n7rzcbc2b5yl_zqsh0000gn/T/Rtmp7V7qyG/model-6d085318a309.stan', line 51, column 2 to column 46)
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.
Chain 1 
Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Chain 1 Exception: PMXTwoCptModel: CL is 0, but must be positive finite! (in '/var/folders/5d/_pkf036n7rzcbc2b5yl_zqsh0000gn/T/Rtmp7V7qyG/model-6d085318a309.stan', line 56, column 4 to line 64, column 73)
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.
Chain 1 
Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Chain 1 Exception: PMXTwoCptModel: CL is 0, but must be positive finite! (in '/var/folders/5d/_pkf036n7rzcbc2b5yl_zqsh0000gn/T/Rtmp7V7qyG/model-6d085318a309.stan', line 56, column 4 to line 64, column 73)
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.
Chain 1 
Chain 1 Iteration:  100 / 2000 [  5%]  (Warmup) 
Chain 1 Iteration:  200 / 2000 [ 10%]  (Warmup) 
Chain 1 Iteration:  300 / 2000 [ 15%]  (Warmup) 
Chain 1 Iteration:  400 / 2000 [ 20%]  (Warmup) 
Chain 1 Iteration:  500 / 2000 [ 25%]  (Warmup) 
....
yizhang-yiz commented 2 years ago

@diegosrodrigues were you able to get the model work? Let me know if you have any questions.

yizhang-yiz commented 2 years ago

I'm closing this due to lack of activity. Feel free to reopen for further discussions.