Skip to content

Commit d131dc9

Browse files
committed
write constants added to Stan's log-posterior in separate lines
1 parent c66f0b1 commit d131dc9

File tree

5 files changed

+16
-12
lines changed

5 files changed

+16
-12
lines changed

R/make_stancode.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,9 @@ make_stancode <- function(formula, data, family = gaussian(),
251251
text_disp$modelC1,
252252
text_model_loop,
253253
text_families$modelC,
254-
" // prior specifications \n",
254+
" // priors including all constants \n",
255255
text_prior,
256-
" // likelihood contribution \n",
256+
" // likelihood including all constants \n",
257257
" if (!prior_only) { \n ",
258258
text_llh,
259259
text_lp_pre$modelC,

R/misc.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -614,17 +614,18 @@ softmax <- function(x) {
614614
x / rowSums(x)
615615
}
616616

617-
wsp <- function(x, nsp = 1) {
617+
wsp <- function(x = "", nsp = 1) {
618618
# add leading and trailing whitespaces
619619
# Args:
620620
# x: object accepted by paste
621621
# nsp: number of whitespaces to add
622622
sp <- collapse(rep(" ", nsp))
623623
if (length(x)) {
624-
paste0(sp, x, sp)
624+
out <- ifelse(nzchar(x), paste0(sp, x, sp), sp)
625625
} else {
626-
NULL
626+
out <- NULL
627627
}
628+
out
628629
}
629630

630631
limit_chars <- function(x, chars = NULL, lsuffix = 4) {

R/stan-helpers.R

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ stan_prior <- function(prior, class, coef = "", group = "",
814814
# for a given class of parameters. If a parameter has has
815815
# no corresponding prior in prior, an empty string is returned.
816816
tp <- tp(wsp)
817-
wsp <- collapse(rep(" ", wsp))
817+
wsp <- wsp(nsp = wsp)
818818
prior_only <- identical(attr(prior, "sample_prior"), "only")
819819
keep <- prior$class == class &
820820
prior$coef %in% c(coef, "") & prior$group %in% c(group, "")
@@ -957,19 +957,20 @@ stan_target_prior <- function(prior, par, ncoef = 1, bound = "") {
957957
trunc_lb <- is.character(par_bound$lb) || par_bound$lb > prior_bound$lb
958958
trunc_ub <- is.character(par_bound$ub) || par_bound$ub < prior_bound$ub
959959
if (trunc_lb || trunc_ub) {
960+
wsp <- wsp(nsp = 4)
960961
if (trunc_lb && !trunc_ub) {
961962
str_add(out) <- paste0(
962-
" - ", ncoef, " * ", prior_name, "_lccdf(",
963+
"\n", wsp, "- ", ncoef, " * ", prior_name, "_lccdf(",
963964
par_bound$lb, " | ", prior_args
964965
)
965966
} else if (!trunc_lb && trunc_ub) {
966967
str_add(out) <- paste0(
967-
" - ", ncoef, " * ", prior_name, "_lcdf(",
968+
"\n", wsp, "- ", ncoef, " * ", prior_name, "_lcdf(",
968969
par_bound$ub, " | ", prior_args
969970
)
970971
} else if (trunc_lb && trunc_ub) {
971972
str_add(out) <- paste0(
972-
" - \n", collapse(rep(" ", 8)), ncoef, " * log_diff_exp(",
973+
"\n", wsp, "- ", ncoef, " * log_diff_exp(",
973974
prior_name, "_lcdf(", par_bound$ub, " | ", prior_args, ", ",
974975
prior_name, "_lcdf(", par_bound$lb, " | ", prior_args, ")"
975976
)
@@ -995,11 +996,14 @@ stan_special_prior <- function(class, prior, ncoef, nlpar = "") {
995996
global_args <- sargs(global_args, global_args)
996997
c2_args <- paste0("0.5 * hs_df_slab", p)
997998
c2_args <- sargs(c2_args, c2_args)
999+
wsp <- wsp(nsp = 4)
9981000
str_add(out) <- paste0(
9991001
tp, "normal_lpdf(zb", p, " | 0, 1); \n",
1000-
tp, "normal_lpdf(hs_local", p, "[1] | 0, 1) - ", ncoef, " * log_half; \n",
1002+
tp, "normal_lpdf(hs_local", p, "[1] | 0, 1)\n",
1003+
wsp, "- ", ncoef, " * log(0.5); \n",
10011004
tp, "inv_gamma_lpdf(hs_local", p, "[2] | ", local_args, "); \n",
1002-
tp, "normal_lpdf(hs_global", p, "[1] | 0, 1) - log_half; \n",
1005+
tp, "normal_lpdf(hs_global", p, "[1] | 0, 1)\n",
1006+
wsp, "- 1 * log(0.5); \n",
10031007
tp, "inv_gamma_lpdf(hs_global", p, "[2] | ", global_args, "); \n",
10041008
tp, "inv_gamma_lpdf(hs_c2", p, " | ", c2_args, "); \n"
10051009
)

R/stan-predictor.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,6 @@ stan_fe <- function(fixef, prior, family = gaussian(),
285285
" real<lower=0> hs_scale_global", p, "; \n",
286286
" real<lower=0> hs_scale_slab", p, "; \n"
287287
)
288-
str_add(out$tdataD) <- "real log_half = log(0.5); \n"
289288
str_add(out$par) <- paste0(
290289
" // horseshoe shrinkage parameters \n",
291290
" vector[K", ct, p, "] zb", p, "; \n",

R/sysdata.rda

-216 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)