stan-dev / stanc3

The Stan transpiler (from Stan to C++ and beyond).
BSD 3-Clause "New" or "Revised" License
138 stars 44 forks source link

Stan compiler Error when using the normal_cdf function #1369

Closed DLeBlond closed 8 months ago

DLeBlond commented 8 months ago

Current Behavior: I am unable to compile my Stan Model, comparability model2.stan, using the following R statement:

StabDSO <- stan_model( 'comparability model2.stan' )

. The full compiler message is: "When you compile models, you are also contributing to development of the NEXT Stan compiler. In this version of rstan, we compile your model as usual, but also test our new compiler on your syntactically correct model. In this case, the new compiler did not work like we hoped. By filing an issue at https://github.com/stan-dev/stanc3/issues with your model or a minimal example that shows this warning you will be contributing valuable information to Stan and ensuring your models continue working. Thank you! This message can be avoided by wrapping your function call inside suppressMessages() or by first calling rstan_options(javascript = FALSE). Error in 'string', line 20, column 7: Use of normal_cdf without a vertical bar (|) between the first two arguments of a CDF was removed in Stan 2.33.0. This can be automatically changed using the canonicalize flag for stanc Error in 'string', line 20, column 55: Use of normal_cdf without a vertical bar (|) between the first two arguments of a CDF was removed in Stan 2.33.0. This can be automatically changed using the canonicalize flag for stanc"

Expected Behavior:

I'm using the same Stan model that was used successfully with no compiler error 1 year ago.

Model Code

The Stan Model (stored in the text file comparability model2.stan) I'm trying to use is: "// The input data is a vector 'y' of length 'N'. data { int nR; vector[nR] R; int nT; vector[nT] T; }

// The parameters accepted by the model. Our model // accepts two parameters 'mu' and 'sigma'. parameters { real mu_R; real log_sigma_R; real mu_T; real log_sigma_T; }

transformed parameters { real pi; // probability of similarity real sigma_R = exp(log_sigma_R);
real sigma_T = exp(log_sigma_T);
pi = normal_cdf((mu_R+3sigma_R-mu_T)/sigma_T , 0,1) - normal_cdf((mu_R-3sigma_R-mu_T)/sigma_T , 0,1) ; }

// The model to be estimated. We model the output // 'y' to be normally distributed with mean 'mu' // and standard deviation 'sigma'. model { T ~ normal(mu_T, sigma_T); R ~ normal(mu_R, sigma_R); }

"

Model which exhibits the issue ```stan please see the above R statement used in the compile attempt. ``` ### Environment: ### Anything else:
DLeBlond commented 8 months ago

Note: Somehow not all my comments were included in the error report. I have tried modifying my statement by replacing the "," with a "|" at the apparent locations but this generates an error flag in the Rstudio Stan window and does not prevent the compile failure. Any support is GREATLY appreciated!!! :-)

DLeBlond commented 8 months ago

Sorry I accidently closed and am reopening. PLEASE HELP :-)

WardBrian commented 8 months ago

The code can be updated following the suggestions given automatically using recent versions of the compiler:

// The input data is a vector 'y' of length 'N'.
data {
  int<lower=0> nR;
  vector[nR] R;
  int<lower=0> nT;
  vector[nT] T;
}
// The parameters accepted by the model. Our model
// accepts two parameters 'mu' and 'sigma'.
parameters {
  real mu_R;
  real log_sigma_R;
  real mu_T;
  real log_sigma_T;
}
transformed parameters {
  real pi; // probability of similarity
  real<lower=0> sigma_R = exp(log_sigma_R);
  real<lower=0> sigma_T = exp(log_sigma_T);
  pi = normal_cdf((mu_R + 3 * sigma_R - mu_T) / sigma_T | 0, 1)
       - normal_cdf((mu_R - 3 * sigma_R - mu_T) / sigma_T | 0, 1);
}
// The model to be estimated. We model the output
// 'y' to be normally distributed with mean 'mu'
// and standard deviation 'sigma'.
model {
  T ~ normal(mu_T, sigma_T);
  R ~ normal(mu_R, sigma_R);
}

Make sure you are using the latest version of rstan from CRAN (2.26) with this updated code, or you may run into further errors.

DLeBlond commented 8 months ago

Brian - Thank you much for pointing me toward a solution to this problem. Based on your note I apparently need to do 2 things:

  1. Update my rstan package. This can be terrifying. My hope is that this can be done simply using the RStudio GUI tools. Is that what you are suggesting? Updating Stan itself or its compiler can be terrifying experience.

  2. Modify my code as you indicate. Of course I had tried this previously to no avail. So perhaps the old rstan version is the culprit?

Again, Thank you Brian for your quick response!! :-)

DLeBlond commented 8 months ago

Brian, I tried your suggestions (items 1 and 2 as noted in my above comment). However, this did not allow me to compile the model. in the image I am including here (hope it shows) you can see that the RStudio Stan window still is detecting some error in the statement that includes the normal_cdf() function. I do not understand this error. I cut and pasted the code you sent directly into the Stan window, saved the file and tried to recompile it and the parser sent me a disappointing error message: "SYNTAX ERROR, MESSAGE(S) FROM PARSER: error in 'modela7cc53f291b_comparability_model3' at line 20, column 17

18:   real<lower=0> sigma_R = exp(log_sigma_R);
19:   real<lower=0> sigma_T = exp(log_sigma_T);
20:   pi = normal_cdf((mu_R + 3 * sigma_R - mu_T) / sigma_T | 0, 1)
                    ^
21:        - normal_cdf((mu_R - 3 * sigma_R - mu_T) / sigma_T | 0, 1);

PARSER EXPECTED: "(" Error in stanc(file = file, model_code = model_code, model_name = model_name, : failed to parse Stan model 'comparability model3' due to the above error. In addition: Warning message: In readLines(file, warn = TRUE) : incomplete final line found on 'C:!\IABS 2023 DC Nov\Rick Burding bubble plot example\comparability model3.stan'" image

Any insight greatly appreciated!

DLeBlond commented 8 months ago

Brian,

Thank you for your help.

I was not successful using the suggestions you sent. I seem to be having some difficulty updating my rstan version using RStudio GUI. When I try to update rstan the system either acts like it updated rstan but doesn’t or simply hangs unresponsive.

The RStudio (current version of RStudio I have) Stan file window detects a syntax error when I replace the “,” with a “|”. So I am also trying to update my version of RStudio thinking that it somehow also needs updating (????).

So you see I have some cockpit challenges here. When I get this sorted out I will comment on the Error report.

Thanks,

Dave

From: Brian Ward @. Sent: Monday, October 09, 2023 1:08 PM To: stan-dev/stanc3 @.> Cc: DLeBlond @.>; State change @.> Subject: Re: [stan-dev/stanc3] Stan compiler Error when using the normal_cdf function (Issue #1369)

The code can be updated following the suggestions given automatically using recent versions of the compiler:

// The input data is a vector 'y' of length 'N'. data { int nR; vector[nR] R; int nT; vector[nT] T; } // The parameters accepted by the model. Our model // accepts two parameters 'mu' and 'sigma'. parameters { real mu_R; real log_sigma_R; real mu_T; real log_sigma_T; } transformed parameters { real pi; // probability of similarity real sigma_R = exp(log_sigma_R); real sigma_T = exp(log_sigma_T); pi = normal_cdf((mu_R + 3 * sigma_R - mu_T) / sigma_T | 0, 1)

Make sure you are using the latest version of rstan from CRAN (2.26) with this updated code, or you may run into further errors.

— Reply to this email directly, view it on GitHub https://github.com/stan-dev/stanc3/issues/1369#issuecomment-1753679084 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AQB3CDJNCP26OS775XONXETX6RKR5AVCNFSM6AAAAAA5ZIOGAKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONJTGY3TSMBYGQ . You are receiving this because you modified the open/close state.Message ID: @.***>

DLeBlond commented 8 months ago

Brian, if you are still with me...

Re: "Make sure you are using the latest version of rstan from CRAN (2.26) with this updated code, or you may run into further errors."

Indeed. I am unable to update my rstan from 2.21.2 to 2.26.23 . After nearly an hour of recompiling from source (the only option apparently) the following failure message appears in the RStudio terminal (something about ggplot2?). HELP I AM LOST "Error: package or namespace load failed for 'rstan': .onLoad failed in loadNamespace() for 'rstan', details: call: ggplot2::element_line(linewidth = 1) error: unused argument (linewidth = 1) Error: loading failed Execution halted *** arch - x64 Error: package or namespace load failed for 'rstan': .onLoad failed in loadNamespace() for 'rstan', details: call: ggplot2::element_line(linewidth = 1) error: unused argument (linewidth = 1) Error: loading failed Execution halted ERROR: loading failed for 'i386', 'x64'

andrjohns commented 8 months ago

Your version of R (4.0) is also very out of date. Once you update your R version and Rtools to the current 4.3, you'll be able to install rstan without installing from source.

Alternatively, you can use cmdstanr which has the latest Stan version and is easier to get up and running: https://mc-stan.org/cmdstanr/articles/cmdstanr.html

DLeBlond commented 8 months ago

Hi Andrew,

Thanks. If I update R will it automatically update Rtools? I’m not sure how these 2 are linked (or not). If not, then I need a refresher on updating Rtools!!

Someday I will look into cmdstanr but right now I am trying desperately to get this analysis done.

Appreciate your help here.

Dave

From: Andrew Johnson @. Sent: Monday, October 09, 2023 4:18 PM To: stan-dev/stanc3 @.> Cc: DLeBlond @.>; State change @.> Subject: Re: [stan-dev/stanc3] Stan compiler Error when using the normal_cdf function (Issue #1369)

Your version of R (4.0) is also very out of date. Once you update your R version and Rtools to the current 4.3, you'll be able to install rstan without installing from source.

Alternatively, you can use cmdstanr which has the latest Stan version and is easier to get up and running: https://mc-stan.org/cmdstanr/articles/cmdstanr.html

— Reply to this email directly, view it on GitHub https://github.com/stan-dev/stanc3/issues/1369#issuecomment-1754049634 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AQB3CDPDXZISACGPQPA5CGLX6SAZRAVCNFSM6AAAAAA5ZIOGAKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONJUGA2DSNRTGQ . You are receiving this because you modified the open/close state.Message ID: @.***>

DLeBlond commented 8 months ago

Hi Andrew,

Thanks. If I update R will it automatically update Rtools? I’m not sure how these 2 are linked (or not). If not, then I need a refresher on updating Rtools!!

Someday I will look into cmdstanr but right now I am trying desperately to get this analysis done.

Appreciate your help here.

Dave

andrjohns commented 8 months ago

The R download page contains the instructions for R and Rtools: https://cran.r-project.org/bin/windows/

DLeBlond commented 8 months ago

Andrew,

Upgraded my RStudio to the most recent version Deleted all old R versions and upgraded my R to the latest version (lost ALL my packages!!!!!) Deleted old Rtools version and upgraded my Rtools to the latest version Opened my RStudio to install the latest version of rstan (which must be loaded from source). Installation starts and console reports encouraging message: “> install.packages("rstan") Installing package into ‘C:/Users/Dave/AppData/Local/R/win-library/4.3’ (as ‘lib’ is unspecified) also installing the dependencies ‘colorspace’, ‘utf8’, ‘backports’, ‘ps’, ‘farver’, ‘labeling’, ‘munsell’, ‘RColorBrewer’, ‘viridisLite’, ‘fansi’, ‘magrittr’, ‘pillar’, ‘pkgconfig’, ‘gtable’, ‘checkmate’, ‘matrixStats’, ‘callr’, ‘cli’, ‘crayon’, ‘desc’, ‘prettyunits’, ‘processx’, ‘R6’, ‘rprojroot’, ‘jsonlite’, ‘glue’, ‘isoband’, ‘lifecycle’, ‘rlang’, ‘scales’, ‘tibble’, ‘vctrs’, ‘withr’, ‘StanHeaders’, ‘inline’, ‘gridExtra’, ‘Rcpp’, ‘RcppParallel’, ‘loo’, ‘pkgbuild’, ‘QuickJSR’, ‘ggplot2’, ‘RcppEigen’, ‘BH’

There is a binary version available but the source version is later: binary source needs_compilation fansi 1.0.4 1.0.5 TRUE ” … and 30minutes later .,,,,

NOTHING!!!. DEAD AIR!!! NO ACTIVITY!!! NO BACKGROUND JOBS!!! NO PROMPT!!! RSTUDIO DEAD AS A DOORNAIL!!!

What gives? Is this to be expected? Any thoughts?

Thanks. Appreciate your being there.

Dave

From: Andrew Johnson @. Sent: Monday, October 09, 2023 4:42 PM To: stan-dev/stanc3 @.> Cc: DLeBlond @.>; State change @.> Subject: Re: [stan-dev/stanc3] Stan compiler Error when using the normal_cdf function (Issue #1369)

The R download page contains the instructions for R and Rtools: https://cran.r-project.org/bin/windows/ — Reply to this email directly, view it on GitHub https://github.com/stan-dev/stanc3/issues/1369 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AQB3CDIZOVNW7KKDNUV6AV3X6SDVNAVCNFSM6AAAAAA5ZIOGAKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONJUGA3DIOJQGY . You are receiving this because you modified the open/close state.Message ID: @. @.> >

DLeBlond commented 8 months ago

Andrew,

Added note: Had to crash-exit RStudio. Unresponsive. Restarted RStudio Attempted to install another of the many packages I had lost (r2d2) R2d2 installed just fine!!! So…

My problem is the installation of rstan (which btw requires compliation from source). I can’t install rstan Therefore I cannot do my work Therefore I am royally screwed Please help!!!

Dave


From: David LeBlond @. Sent: Monday, October 09, 2023 8:51 PM To: 'stan-dev/stanc3' @.>; 'stan-dev/stanc3' @.> Cc: 'State change' @.> Subject: RE: [stan-dev/stanc3] Stan compiler Error when using the normal_cdf function (Issue #1369)

Andrew,

Upgraded my RStudio to the most recent version Deleted all old R versions and upgraded my R to the latest version (lost ALL my packages!!!!!) Deleted old Rtools version and upgraded my Rtools to the latest version Opened my RStudio to install the latest version of rstan (which must be loaded from source). Installation starts and console reports encouraging message: “> install.packages("rstan") Installing package into ‘C:/Users/Dave/AppData/Local/R/win-library/4.3’ (as ‘lib’ is unspecified) also installing the dependencies ‘colorspace’, ‘utf8’, ‘backports’, ‘ps’, ‘farver’, ‘labeling’, ‘munsell’, ‘RColorBrewer’, ‘viridisLite’, ‘fansi’, ‘magrittr’, ‘pillar’, ‘pkgconfig’, ‘gtable’, ‘checkmate’, ‘matrixStats’, ‘callr’, ‘cli’, ‘crayon’, ‘desc’, ‘prettyunits’, ‘processx’, ‘R6’, ‘rprojroot’, ‘jsonlite’, ‘glue’, ‘isoband’, ‘lifecycle’, ‘rlang’, ‘scales’, ‘tibble’, ‘vctrs’, ‘withr’, ‘StanHeaders’, ‘inline’, ‘gridExtra’, ‘Rcpp’, ‘RcppParallel’, ‘loo’, ‘pkgbuild’, ‘QuickJSR’, ‘ggplot2’, ‘RcppEigen’, ‘BH’

There is a binary version available but the source version is later: binary source needs_compilation fansi 1.0.4 1.0.5 TRUE ” … and 30minutes later .,,,,

NOTHING!!!. DEAD AIR!!! NO ACTIVITY!!! NO BACKGROUND JOBS!!! NO PROMPT!!! RSTUDIO DEAD AS A DOORNAIL!!!

What gives? Is this to be expected? Any thoughts?

Thanks. Appreciate your being there.

Dave

From: Andrew Johnson @. Sent: Monday, October 09, 2023 4:42 PM To: stan-dev/stanc3 @. @.> > Cc: DLeBlond @. @.> >; State change @. @.***> > Subject: Re: [stan-dev/stanc3] Stan compiler Error when using the normal_cdf function (Issue #1369)

The R download page contains the instructions for R and Rtools: https://cran.r-project.org/bin/windows/ — Reply to this email directly, view it on GitHub https://github.com/stan-dev/stanc3/issues/1369 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AQB3CDIZOVNW7KKDNUV6AV3X6SDVNAVCNFSM6AAAAAA5ZIOGAKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONJUGA3DIOJQGY . You are receiving this because you modified the open/close state.Message ID: @. @.> >

DLeBlond commented 8 months ago

Andrew (or other kind person),

Oops, my bad. Sorry. I did not notice that the installation manager had been waiting for me to respond to a yes or no question in a window baried under my RStudio window. Jeeeeezz. I said “yes” and the installation (including all required packages) apparently finished.

Now I try to compile the modified code provided to me above. And… of course,,, bran NEW error message!!!: “> StabDSO <- stan_model( 'comparability model3.stan' ) Error in stanc(file = file, model_code = model_code, model_name = model_name, : 0

Semantic error in 'string', line 21, column 7 to column 64:

Only functions with names ending in _lpdf, _lupdf, _lpmf, _lupmf, _lcdf, _lccdf can make use of conditional notation.

” Notice I am trying to use the modified version of the normal_cdf() that includes the conditional notation. BUT also notice that this function does not allow the conditional notation (as indicated in the message above). The apparently offending line is given below: pi = normal_cdf( (mu_R + 3 * sigma_R - mu_T) / sigma_T | 0, 1)

This surprises me because at the very beginning of this saga (see above) the Error told me that I MUST use the conditional notation.

So what gives here? The Stan Function guide on line (https://mc-stan.org/docs/functions-reference/normal-distribution.html )shows the following syntax: real normal_cdf(reals y, reals mu, reals sigma) Which you notice does NOT use the conditional notation that I was led to believe I MUST use.

So when I go back to the original version of the Stan model that uses a “,” instead of a “|”… Walla!! Now it compiles!!

So I guess my problem is solved (at least for now). But I have no idea why I got the weird original ERROR message telling me I MUST do something that I cannot do.

I verified the installed versions of the following: rstan 2.26.23 StanHeaders 2.26.28 R version 4.3.1 (2023-06-16 ucrt) -- "Beagle Scouts" Rtools version 4.3

Sorry to drag this on but I was completely flummoxed. Well it’s Monday what should I expect :(.

Thanks,

Dave

From: David LeBlond @. Sent: Monday, October 09, 2023 8:57 PM To: 'stan-dev/stanc3' @.>; 'stan-dev/stanc3' @.> Cc: 'State change' @.> Subject: RE: [stan-dev/stanc3] Stan compiler Error when using the normal_cdf function (Issue #1369)

Andrew,

Added note: Had to crash-exit RStudio. Unresponsive. Restarted RStudio Attempted to install another of the many packages I had lost (r2d2) R2d2 installed just fine!!! So…

My problem is the installation of rstan (which btw requires compliation from source). I can’t install rstan Therefore I cannot do my work Therefore I am royally screwed Please help!!!

Dave


From: David LeBlond @. Sent: Monday, October 09, 2023 8:51 PM To: 'stan-dev/stanc3' @. @.> >; 'stan-dev/stanc3' @. @.> > Cc: 'State change' @. @.***> > Subject: RE: [stan-dev/stanc3] Stan compiler Error when using the normal_cdf function (Issue #1369)

Andrew,

Upgraded my RStudio to the most recent version Deleted all old R versions and upgraded my R to the latest version (lost ALL my packages!!!!!) Deleted old Rtools version and upgraded my Rtools to the latest version Opened my RStudio to install the latest version of rstan (which must be loaded from source). Installation starts and console reports encouraging message: “> install.packages("rstan") Installing package into ‘C:/Users/Dave/AppData/Local/R/win-library/4.3’ (as ‘lib’ is unspecified) also installing the dependencies ‘colorspace’, ‘utf8’, ‘backports’, ‘ps’, ‘farver’, ‘labeling’, ‘munsell’, ‘RColorBrewer’, ‘viridisLite’, ‘fansi’, ‘magrittr’, ‘pillar’, ‘pkgconfig’, ‘gtable’, ‘checkmate’, ‘matrixStats’, ‘callr’, ‘cli’, ‘crayon’, ‘desc’, ‘prettyunits’, ‘processx’, ‘R6’, ‘rprojroot’, ‘jsonlite’, ‘glue’, ‘isoband’, ‘lifecycle’, ‘rlang’, ‘scales’, ‘tibble’, ‘vctrs’, ‘withr’, ‘StanHeaders’, ‘inline’, ‘gridExtra’, ‘Rcpp’, ‘RcppParallel’, ‘loo’, ‘pkgbuild’, ‘QuickJSR’, ‘ggplot2’, ‘RcppEigen’, ‘BH’

There is a binary version available but the source version is later: binary source needs_compilation fansi 1.0.4 1.0.5 TRUE ” … and 30minutes later .,,,,

NOTHING!!!. DEAD AIR!!! NO ACTIVITY!!! NO BACKGROUND JOBS!!! NO PROMPT!!! RSTUDIO DEAD AS A DOORNAIL!!!

What gives? Is this to be expected? Any thoughts?

Thanks. Appreciate your being there.

Dave

From: Andrew Johnson @. Sent: Monday, October 09, 2023 4:42 PM To: stan-dev/stanc3 @. @.> > Cc: DLeBlond @. @.> >; State change @. @.***> > Subject: Re: [stan-dev/stanc3] Stan compiler Error when using the normal_cdf function (Issue #1369)

The R download page contains the instructions for R and Rtools: https://cran.r-project.org/bin/windows/ — Reply to this email directly, view it on GitHub https://github.com/stan-dev/stanc3/issues/1369 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AQB3CDIZOVNW7KKDNUV6AV3X6SDVNAVCNFSM6AAAAAA5ZIOGAKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONJUGA3DIOJQGY . You are receiving this because you modified the open/close state.Message ID: @. @.> >

bob-carpenter commented 8 months ago

I can’t install rstan. Therefore I cannot do my work

Sorry about that. RStan's much more finicky to get configured due to its use of Stan in memory. I would strongly recommend switching to CmdStanR unless you need specific functionality only supported by RStan (unless it's gradients and transforms, which you can get through BridgeStan).

I would also suggest rewriting your model as follows for clarity and efficiency:

data {
  int<lower=0> nR;
  vector[nR] R;
  int<lower=0> nT;
  vector[nT] T;
}
parameters {
  real mu_R;
  real<lower=0> sigma_R;
  real mu_T;
  real<lower=0> sigma_T;
}
model {
  T ~ normal(mu_T, sigma_T);
  R ~ normal(mu_R, sigma_R);
  // mu_R ~ ???;
  // mu_T ~ ???;
  // sigma_R ~ ???;
  // sigma_T ~ ???;
}
generated quantities {
  real<lower=0, upper=1> pi
    = normal_cdf(mu_R + 3 * sigma_R | mu_T, sigma_T)
      - normal_cdf(mu_R - 3 * sigma_R | mu_T, sigma_T);
}

I would recommend putting explicit and proper priors on the parameters. By declaring as real<lower=0> sigma_R the implicit prior is uniform over (0, infinity), whereas by declaring as real log_sigma_R and transforming yourself, the implicit prior is uniform for log_sigma over (-infinity, infinity). The mu_T and mu_R also get improper uniform priors by default.

I moved the calculation of pi down to the generated quantities block. This will avoid calculating derivatives of the normal cdf, which are very expensive and not so stable. I also took the liberty of rewriting the CDFs to make it clearer your'e trying to compute the probability of the interval mu_R +/- 3 * sigma_R in the distribution normal(mu_T, sigma_T).

DLeBlond commented 8 months ago

Bob,

Thank you so much for the valuable input!!! Always inspiring to see a master at work.

I will check into CmdStanR. I’m an old dog.

Thanks to the help from you and others at github, my problem is solved.

As usual, it was a cockpit error on my part that conversations with my rubber ducky did not unveil.

The lesson I learned is that I need to keep by RStudio, R, Rtools, and rstan up to date. My problem was almost certainly that the older versions were not working together and were generating error messages. Once I updated everything and got rid of the junk the code ran fine. I am sure that your version will run better so I need to study and learn from it.

Best regards and thank you so much!!

Dave

From: Bob Carpenter @. Sent: Wednesday, October 11, 2023 9:56 AM To: stan-dev/stanc3 @.> Cc: DLeBlond @.>; State change @.> Subject: Re: [stan-dev/stanc3] Stan compiler Error when using the normal_cdf function (Issue #1369)

I can’t install rstan. Therefore I cannot do my work

Sorry about that. RStan's much more finicky to get configured due to its use of Stan in memory. I would strongly recommend switching to CmdStanR unless you need specific functionality only supported by RStan (unless it's gradients and transforms, which you can get through BridgeStan).

I would also suggest rewriting your model as follows for clarity and efficiency:

data { int nR; vector[nR] R; int nT; vector[nT] T; } parameters { real mu_R; real sigma_R; real mu_T; real sigma_T; } model { T ~ normal(mu_T, sigma_T); R ~ normal(mu_R, sigma_R); // mu_R ~ ???; // mu_T ~ ???; // sigma_R ~ ???; // sigma_T ~ ???; } generated quantities { real pi = normal_cdf(mu_R + 3 * sigma_R | mu_T, sigma_T)

I would recommend putting explicit and proper priors on the parameters. By declaring as real sigma_R the implicit prior is uniform over (0, infinity), whereas by declaring as real log_sigma_R and transforming yourself, the implicit prior is uniform for log_sigma over (-infinity, infinity). The mu_T and mu_R also get improper uniform priors by default.

I moved the calculation of pi down to the generated quantities block. This will avoid calculating derivatives of the normal cdf, which are very expensive and not so stable. I also took the liberty of rewriting the CDFs to make it clearer your'e trying to compute the probability of the interval mu_R +/- 3 * sigma_R in the distribution normal(mu_T, sigma_T).

— Reply to this email directly, view it on GitHub https://github.com/stan-dev/stanc3/issues/1369#issuecomment-1758107990 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AQB3CDKQURMDIR4UUESIQH3X63FRBANCNFSM6AAAAAA5ZIOGAI . You are receiving this because you modified the open/close state.Message ID: @.***>