library(brms)
library(ggplot2)
dat <- read.csv("CRdata_nooutlier.csv")Regularization with brms
1 Overview
In this example, we compare prior distributions that regularize Bayesian count models for flower_stalk_count using negative binomial regression in brms.
2 Load packages and data
2.1 Quick look at the data
str(dat)'data.frame': 561 obs. of 13 variables:
$ flower_stalk_count: int 35 44 9 30 8 22 34 165 41 46 ...
$ Area : num 0.0568 0.7987 1.0388 1.0388 -0.0773 ...
$ Perimeter : num 0.0719 0.4266 2.0759 2.0759 -0.1143 ...
$ Canopy_height_SD : num 0.794 0.279 4.013 4.013 0.565 ...
$ Canopy_height : num 0.658 0.44 2.091 2.091 0.361 ...
$ std_red : num -0.0977 -1.1617 -0.5077 -0.5077 -0.7148 ...
$ std_blue : num -0.00501 -1.12583 -0.6735 -0.6735 -0.556 ...
$ Red : num -0.665 -1.372 -1.25 -1.25 -1.239 ...
$ Blue : num -0.732 -1.088 -1.29 -1.29 -0.929 ...
$ plant_elev : num 0.525 0.515 0.52 0.52 0.513 ...
$ month : chr "22-Sep" "22-Sep" "22-Sep" "22-Sep" ...
$ site : chr "site1" "site1" "site1" "site1" ...
$ Seasonal_diff : num 0.99 -0.556 4.206 4.206 -0.453 ...
summary(dat) flower_stalk_count Area Perimeter Canopy_height_SD
Min. : 0.00 Min. :-0.731163 Min. :-1.079520 Min. :-1.026340
1st Qu.: 4.00 1st Qu.:-0.361909 1st Qu.:-0.323715 1st Qu.:-0.319006
Median : 24.00 Median :-0.120697 Median :-0.064145 Median :-0.026548
Mean : 76.47 Mean :-0.003575 Mean :-0.001884 Mean :-0.002096
3rd Qu.: 92.00 3rd Qu.: 0.286283 3rd Qu.: 0.296475 3rd Qu.: 0.282837
Max. :709.00 Max. : 1.754956 Max. : 2.075872 Max. : 4.013342
Canopy_height std_red std_blue
Min. :-0.813657 Min. :-1.8817044 Min. :-1.3410682
1st Qu.:-0.365651 1st Qu.:-0.3294252 1st Qu.:-0.3615765
Median :-0.077639 Median :-0.0467118 Median :-0.0204098
Mean :-0.002608 Mean :-0.0002037 Mean : 0.0003336
3rd Qu.: 0.296175 3rd Qu.: 0.3473337 3rd Qu.: 0.3720812
Max. : 2.091122 Max. : 1.1839528 Max. : 1.5966034
Red Blue plant_elev
Min. :-2.2096266 Min. :-1.427864 Min. :-0.797500
1st Qu.:-0.2299413 1st Qu.:-0.318854 1st Qu.:-0.026065
Median : 0.0864125 Median :-0.001843 Median : 0.150659
Mean : 0.0006889 Mean : 0.001234 Mean : 0.002797
3rd Qu.: 0.3228717 3rd Qu.: 0.305844 3rd Qu.: 0.506924
Max. : 0.9232058 Max. : 1.367469 Max. : 0.637205
month site Seasonal_diff
Length:561 Length:561 Min. :-1.041381
Class :character Class :character 1st Qu.:-0.287752
Mode :character Mode :character Median :-0.082477
Mean :-0.001365
3rd Qu.: 0.240953
Max. : 4.206331
head(dat) flower_stalk_count Area Perimeter Canopy_height_SD Canopy_height
1 35 0.05683302 0.07190528 0.7943234 0.65810944
2 44 0.79869890 0.42658973 0.2792757 0.44023654
3 9 1.03884363 2.07587241 4.0133418 2.09112179
4 30 1.03884363 2.07587241 4.0133418 2.09112179
5 8 -0.07725237 -0.11430405 0.5649110 0.36095791
6 22 0.30274782 0.64826751 0.1887101 0.04107762
std_red std_blue Red Blue plant_elev month site
1 -0.09770562 -0.005006592 -0.6649011 -0.7322918 0.5247955 22-Sep site1
2 -1.16171002 -1.125829564 -1.3718880 -1.0880790 0.5145983 22-Sep site1
3 -0.50768891 -0.673499505 -1.2504797 -1.2901425 0.5198842 22-Sep site1
4 -0.50768891 -0.673499505 -1.2504797 -1.2901425 0.5198842 22-Sep site1
5 -0.71476261 -0.555995684 -1.2386661 -0.9287089 0.5131348 22-Sep site1
6 -0.96430274 -0.921913482 -1.1538512 -0.8046447 0.5072225 22-Sep site1
Seasonal_diff
1 0.98999701
2 -0.55636995
3 4.20633139
4 4.20633139
5 -0.45291582
6 -0.08556292
3 Comparing Normal, horseshoe, Laplace, and very wide uniform priors
Here we compare four different prior families for the same full model:
- a Normal prior for mild shrinkage
- a regularized horseshoe prior for sparse effects
- a Laplace prior as a lasso-like alternative
- a very wide Uniform(-10, 10) prior as a weakly regularizing comparison
3.1 Why compare these priors?
These priors all impose different amounts and styles of regularization on coefficients:
- Normal prior: smooth, symmetric shrinkage
- Horseshoe prior: very strong shrinkage near zero, but heavy tails allow a few large effects
- Laplace prior: sharper peak at zero than a Normal, giving lasso-like shrinkage
- Wide Uniform(-10, 10): much flatter within its bounds, so it regularizes much less strongly than the other priors
The horseshoe prior is officially supported in brms.
The old lasso() helper is no longer supported, so here we use a Laplace prior directly via Stan syntax.
3.2 Full model formula
full_formula <- flower_stalk_count ~ Canopy_height + Area + Red + Blue + plant_elev+Perimeter+Canopy_height_SD+std_red+std_blue+Seasonal_diff3.3 Define priors
For the intercept, we keep the same broad Normal prior across all models.
prior_normal <- c(
prior(normal(0, 1), class = "b"),
prior(normal(0, 5), class = "Intercept")
)
prior_horseshoe <- c(
prior(horseshoe(df = 3, par_ratio = 0.2), class = "b"),
prior(normal(0, 5), class = "Intercept")
)
prior_laplace <- c(
prior("double_exponential(0, 1)", class = "b"),
prior(normal(0, 5), class = "Intercept")
)
prior_uniform_wide <- c(
prior(uniform(-10, 10), class = "b", lb = -10, ub = 10),
prior(normal(0, 5), class = "Intercept")
)3.4 Plot the priors for a generic coefficient
These plots show the prior density for a generic slope coefficient ().
prior_plot_dat <- data.frame(
beta = seq(-10, 10, length.out = 2000)
)
prior_plot_dat$normal <- dnorm(prior_plot_dat$beta, mean = 0, sd = 1)
prior_plot_dat$laplace <- 1 / 2 * exp(-abs(prior_plot_dat$beta))
prior_plot_dat$uniform_wide <- ifelse(prior_plot_dat$beta >= -10 & prior_plot_dat$beta <= 10, 1 / 20, 0)
prior_long <- rbind(
data.frame(beta = prior_plot_dat$beta, density = prior_plot_dat$normal, model = "Normal(0, 1)"),
data.frame(beta = prior_plot_dat$beta, density = prior_plot_dat$laplace, model = "Laplace(0, 1)"),
data.frame(beta = prior_plot_dat$beta, density = prior_plot_dat$uniform_wide, model = "Uniform(-10, 10)")
)
ggplot(prior_long, aes(x = beta, y = density, color = model)) +
geom_line(linewidth = 1) +
labs(
title = "Normal, Laplace, and wide Uniform priors",
x = expression(beta),
y = "Density",
color = "Prior"
)3.5 Visualize the horseshoe prior by prior simulation
Unlike the Normal and Laplace priors, the horseshoe is hierarchical, so it is easiest to visualize by sampling from it approximately.
This code gives a simple illustration of the horseshoe shape for one generic coefficient.
set.seed(123)
n_draws <- 4000
tau <- abs(rt(n_draws, df = 1)) * 0.2
lambda <- abs(rt(n_draws, df = 3))
z <- rnorm(n_draws, 0, 1)
beta_hs <- z * tau * lambda
hs_dat <- data.frame(beta = beta_hs)
ggplot(hs_dat, aes(x = beta)) +
geom_density() +
coord_cartesian(xlim = c(-10, 10)) +
labs(
title = "Illustration of a horseshoe prior",
x = expression(beta),
y = "Density"
)3.6 Fit the four models
fit_full_normal <- brm(
formula = full_formula,
data = dat,
family = negbinomial(),
prior = prior_normal,
chains = 4, cores = 4, iter = 2000, warmup = 1000,
seed = 123,
save_pars = save_pars(all = TRUE)
)Running "C:/PROGRA~1/R/R-45~1.2/bin/x64/Rcmd.exe" SHLIB foo.c
using C compiler: 'gcc.exe (GCC) 14.3.0'
gcc -I"C:/PROGRA~1/R/R-45~1.2/include" -DNDEBUG -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/Rcpp/include/" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/unsupported" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/BH/include" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/StanHeaders/include/src/" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/StanHeaders/include/" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppParallel/include/" -DRCPP_PARALLEL_USE_TBB=1 -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/rstan/include" -DEIGEN_NO_DEBUG -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -DSTAN_THREADS -DUSE_STANC3 -DSTRICT_R_HEADERS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -D_HAS_AUTO_PTR_ETC=0 -include "C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/StanHeaders/include/stan/math/prim/fun/Eigen.hpp" -std=c++1y -I"C:/rtools45/x86_64-w64-mingw32.static.posix/include" -O2 -Wall -std=gnu2x -mfpmath=sse -msse2 -mstackrealign -c foo.c -o foo.o
cc1.exe: warning: command-line option '-std=c++14' is valid for C++/ObjC++ but not for C
In file included from C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/Eigen/Core:19,
from C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/Eigen/Dense:1,
from C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/StanHeaders/include/stan/math/prim/fun/Eigen.hpp:22,
from <command-line>:
C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/Eigen/src/Core/util/Macros.h:679:10: fatal error: cmath: No such file or directory
679 | #include <cmath>
| ^~~~~~~
compilation terminated.
make: *** [C:/PROGRA~1/R/R-45~1.2/etc/x64/Makeconf:289: foo.o] Error 1
fit_full_horseshoe <- brm(
formula = full_formula,
data = dat,
family = negbinomial(),
prior = prior_horseshoe,
chains = 4, cores = 4, iter = 2000, warmup = 1000,
control = list(adapt_delta = 0.99),
seed = 123,
save_pars = save_pars(all = TRUE)
)Running "C:/PROGRA~1/R/R-45~1.2/bin/x64/Rcmd.exe" SHLIB foo.c
using C compiler: 'gcc.exe (GCC) 14.3.0'
gcc -I"C:/PROGRA~1/R/R-45~1.2/include" -DNDEBUG -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/Rcpp/include/" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/unsupported" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/BH/include" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/StanHeaders/include/src/" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/StanHeaders/include/" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppParallel/include/" -DRCPP_PARALLEL_USE_TBB=1 -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/rstan/include" -DEIGEN_NO_DEBUG -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -DSTAN_THREADS -DUSE_STANC3 -DSTRICT_R_HEADERS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -D_HAS_AUTO_PTR_ETC=0 -include "C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/StanHeaders/include/stan/math/prim/fun/Eigen.hpp" -std=c++1y -I"C:/rtools45/x86_64-w64-mingw32.static.posix/include" -O2 -Wall -std=gnu2x -mfpmath=sse -msse2 -mstackrealign -c foo.c -o foo.o
cc1.exe: warning: command-line option '-std=c++14' is valid for C++/ObjC++ but not for C
In file included from C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/Eigen/Core:19,
from C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/Eigen/Dense:1,
from C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/StanHeaders/include/stan/math/prim/fun/Eigen.hpp:22,
from <command-line>:
C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/Eigen/src/Core/util/Macros.h:679:10: fatal error: cmath: No such file or directory
679 | #include <cmath>
| ^~~~~~~
compilation terminated.
make: *** [C:/PROGRA~1/R/R-45~1.2/etc/x64/Makeconf:289: foo.o] Error 1
fit_full_laplace <- brm(
formula = full_formula,
data = dat,
family = negbinomial(),
prior = prior_laplace,
chains = 4, cores = 4, iter = 2000, warmup = 1000,
seed = 123,
save_pars = save_pars(all = TRUE)
)Running "C:/PROGRA~1/R/R-45~1.2/bin/x64/Rcmd.exe" SHLIB foo.c
using C compiler: 'gcc.exe (GCC) 14.3.0'
gcc -I"C:/PROGRA~1/R/R-45~1.2/include" -DNDEBUG -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/Rcpp/include/" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/unsupported" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/BH/include" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/StanHeaders/include/src/" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/StanHeaders/include/" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppParallel/include/" -DRCPP_PARALLEL_USE_TBB=1 -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/rstan/include" -DEIGEN_NO_DEBUG -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -DSTAN_THREADS -DUSE_STANC3 -DSTRICT_R_HEADERS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -D_HAS_AUTO_PTR_ETC=0 -include "C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/StanHeaders/include/stan/math/prim/fun/Eigen.hpp" -std=c++1y -I"C:/rtools45/x86_64-w64-mingw32.static.posix/include" -O2 -Wall -std=gnu2x -mfpmath=sse -msse2 -mstackrealign -c foo.c -o foo.o
cc1.exe: warning: command-line option '-std=c++14' is valid for C++/ObjC++ but not for C
In file included from C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/Eigen/Core:19,
from C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/Eigen/Dense:1,
from C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/StanHeaders/include/stan/math/prim/fun/Eigen.hpp:22,
from <command-line>:
C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/Eigen/src/Core/util/Macros.h:679:10: fatal error: cmath: No such file or directory
679 | #include <cmath>
| ^~~~~~~
compilation terminated.
make: *** [C:/PROGRA~1/R/R-45~1.2/etc/x64/Makeconf:289: foo.o] Error 1
fit_full_uniform_wide <- brm(
formula = full_formula,
data = dat,
family = negbinomial(),
prior = prior_uniform_wide,
chains = 4, cores = 4, iter = 2000, warmup = 1000,
seed = 123,
save_pars = save_pars(all = TRUE)
)Running "C:/PROGRA~1/R/R-45~1.2/bin/x64/Rcmd.exe" SHLIB foo.c
using C compiler: 'gcc.exe (GCC) 14.3.0'
gcc -I"C:/PROGRA~1/R/R-45~1.2/include" -DNDEBUG -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/Rcpp/include/" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/unsupported" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/BH/include" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/StanHeaders/include/src/" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/StanHeaders/include/" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppParallel/include/" -DRCPP_PARALLEL_USE_TBB=1 -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/rstan/include" -DEIGEN_NO_DEBUG -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -DSTAN_THREADS -DUSE_STANC3 -DSTRICT_R_HEADERS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -D_HAS_AUTO_PTR_ETC=0 -include "C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/StanHeaders/include/stan/math/prim/fun/Eigen.hpp" -std=c++1y -I"C:/rtools45/x86_64-w64-mingw32.static.posix/include" -O2 -Wall -std=gnu2x -mfpmath=sse -msse2 -mstackrealign -c foo.c -o foo.o
cc1.exe: warning: command-line option '-std=c++14' is valid for C++/ObjC++ but not for C
In file included from C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/Eigen/Core:19,
from C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/Eigen/Dense:1,
from C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/StanHeaders/include/stan/math/prim/fun/Eigen.hpp:22,
from <command-line>:
C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/Eigen/src/Core/util/Macros.h:679:10: fatal error: cmath: No such file or directory
679 | #include <cmath>
| ^~~~~~~
compilation terminated.
make: *** [C:/PROGRA~1/R/R-45~1.2/etc/x64/Makeconf:289: foo.o] Error 1
3.7 Compare with LOOIC
loo_full_normal <- loo(fit_full_normal)
loo_full_horseshoe <- loo(fit_full_horseshoe)
loo_full_laplace <- loo(fit_full_laplace)
loo_full_uniform_wide <- loo(fit_full_uniform_wide)
loo_compare(
loo_full_normal,
loo_full_horseshoe,
loo_full_laplace,
loo_full_uniform_wide
) elpd_diff se_diff
fit_full_normal 0.000000e+00 0.000000e+00
fit_full_laplace -2.000000e-01 3.000000e-01
fit_full_uniform_wide -7.000000e-01 5.000000e-01
fit_full_horseshoe -3.321232e+13 2.346352e+13
loo_prior_tbl <- data.frame(
model = c("Normal", "Horseshoe", "Laplace", "Uniform(-10, 10)"),
looic = c(
loo_full_normal$estimates["looic", "Estimate"],
loo_full_horseshoe$estimates["looic", "Estimate"],
loo_full_laplace$estimates["looic", "Estimate"],
loo_full_uniform_wide$estimates["looic", "Estimate"]
)
)
ggplot(loo_prior_tbl, aes(x = reorder(model, looic), y = looic)) +
geom_point(size = 3) +
coord_flip() +
labs(
title = "LOOIC under different priors",
x = "Prior",
y = "LOOIC (lower is better)"
)3.8 Plotting priors extracted from a brms fit object
The earlier figures used analytical density functions such as dnorm() and a simulation-based approximation for the horseshoe prior.
In a Bayesian workflow, it is also useful to look at draws extracted from an actual brms object. To do this in brms, we fit the model from the prior only by setting sample_prior = "only".
3.8.1 Fit prior-only versions of the models
fit_prior_normal <- brm(
formula = full_formula,
data = dat,
family = negbinomial(),
prior = prior_normal,
sample_prior = "only",
chains = 4, cores = 4, iter = 2000, warmup = 1000,
seed = 123
)Running "C:/PROGRA~1/R/R-45~1.2/bin/x64/Rcmd.exe" SHLIB foo.c
using C compiler: 'gcc.exe (GCC) 14.3.0'
gcc -I"C:/PROGRA~1/R/R-45~1.2/include" -DNDEBUG -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/Rcpp/include/" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/unsupported" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/BH/include" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/StanHeaders/include/src/" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/StanHeaders/include/" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppParallel/include/" -DRCPP_PARALLEL_USE_TBB=1 -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/rstan/include" -DEIGEN_NO_DEBUG -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -DSTAN_THREADS -DUSE_STANC3 -DSTRICT_R_HEADERS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -D_HAS_AUTO_PTR_ETC=0 -include "C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/StanHeaders/include/stan/math/prim/fun/Eigen.hpp" -std=c++1y -I"C:/rtools45/x86_64-w64-mingw32.static.posix/include" -O2 -Wall -std=gnu2x -mfpmath=sse -msse2 -mstackrealign -c foo.c -o foo.o
cc1.exe: warning: command-line option '-std=c++14' is valid for C++/ObjC++ but not for C
In file included from C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/Eigen/Core:19,
from C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/Eigen/Dense:1,
from C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/StanHeaders/include/stan/math/prim/fun/Eigen.hpp:22,
from <command-line>:
C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/Eigen/src/Core/util/Macros.h:679:10: fatal error: cmath: No such file or directory
679 | #include <cmath>
| ^~~~~~~
compilation terminated.
make: *** [C:/PROGRA~1/R/R-45~1.2/etc/x64/Makeconf:289: foo.o] Error 1
fit_prior_horseshoe <- brm(
formula = full_formula,
data = dat,
family = negbinomial(),
prior = prior_horseshoe,
sample_prior = "only",
chains = 4, cores = 4, iter = 2000, warmup = 1000,
control = list(adapt_delta = 0.99),
seed = 123
)Running "C:/PROGRA~1/R/R-45~1.2/bin/x64/Rcmd.exe" SHLIB foo.c
using C compiler: 'gcc.exe (GCC) 14.3.0'
gcc -I"C:/PROGRA~1/R/R-45~1.2/include" -DNDEBUG -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/Rcpp/include/" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/unsupported" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/BH/include" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/StanHeaders/include/src/" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/StanHeaders/include/" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppParallel/include/" -DRCPP_PARALLEL_USE_TBB=1 -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/rstan/include" -DEIGEN_NO_DEBUG -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -DSTAN_THREADS -DUSE_STANC3 -DSTRICT_R_HEADERS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -D_HAS_AUTO_PTR_ETC=0 -include "C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/StanHeaders/include/stan/math/prim/fun/Eigen.hpp" -std=c++1y -I"C:/rtools45/x86_64-w64-mingw32.static.posix/include" -O2 -Wall -std=gnu2x -mfpmath=sse -msse2 -mstackrealign -c foo.c -o foo.o
cc1.exe: warning: command-line option '-std=c++14' is valid for C++/ObjC++ but not for C
In file included from C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/Eigen/Core:19,
from C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/Eigen/Dense:1,
from C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/StanHeaders/include/stan/math/prim/fun/Eigen.hpp:22,
from <command-line>:
C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/Eigen/src/Core/util/Macros.h:679:10: fatal error: cmath: No such file or directory
679 | #include <cmath>
| ^~~~~~~
compilation terminated.
make: *** [C:/PROGRA~1/R/R-45~1.2/etc/x64/Makeconf:289: foo.o] Error 1
fit_prior_laplace <- brm(
formula = full_formula,
data = dat,
family = negbinomial(),
prior = prior_laplace,
sample_prior = "only",
chains = 4, cores = 4, iter = 2000, warmup = 1000,
seed = 123
)Running "C:/PROGRA~1/R/R-45~1.2/bin/x64/Rcmd.exe" SHLIB foo.c
using C compiler: 'gcc.exe (GCC) 14.3.0'
gcc -I"C:/PROGRA~1/R/R-45~1.2/include" -DNDEBUG -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/Rcpp/include/" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/unsupported" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/BH/include" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/StanHeaders/include/src/" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/StanHeaders/include/" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppParallel/include/" -DRCPP_PARALLEL_USE_TBB=1 -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/rstan/include" -DEIGEN_NO_DEBUG -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -DSTAN_THREADS -DUSE_STANC3 -DSTRICT_R_HEADERS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -D_HAS_AUTO_PTR_ETC=0 -include "C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/StanHeaders/include/stan/math/prim/fun/Eigen.hpp" -std=c++1y -I"C:/rtools45/x86_64-w64-mingw32.static.posix/include" -O2 -Wall -std=gnu2x -mfpmath=sse -msse2 -mstackrealign -c foo.c -o foo.o
cc1.exe: warning: command-line option '-std=c++14' is valid for C++/ObjC++ but not for C
In file included from C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/Eigen/Core:19,
from C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/Eigen/Dense:1,
from C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/StanHeaders/include/stan/math/prim/fun/Eigen.hpp:22,
from <command-line>:
C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/Eigen/src/Core/util/Macros.h:679:10: fatal error: cmath: No such file or directory
679 | #include <cmath>
| ^~~~~~~
compilation terminated.
make: *** [C:/PROGRA~1/R/R-45~1.2/etc/x64/Makeconf:289: foo.o] Error 1
fit_prior_uniform_wide <- brm(
formula = full_formula,
data = dat,
family = negbinomial(),
prior = prior_uniform_wide,
sample_prior = "only",
chains = 4, cores = 4, iter = 2000, warmup = 1000,
seed = 123
)Running "C:/PROGRA~1/R/R-45~1.2/bin/x64/Rcmd.exe" SHLIB foo.c
using C compiler: 'gcc.exe (GCC) 14.3.0'
gcc -I"C:/PROGRA~1/R/R-45~1.2/include" -DNDEBUG -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/Rcpp/include/" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/unsupported" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/BH/include" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/StanHeaders/include/src/" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/StanHeaders/include/" -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppParallel/include/" -DRCPP_PARALLEL_USE_TBB=1 -I"C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/rstan/include" -DEIGEN_NO_DEBUG -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -DSTAN_THREADS -DUSE_STANC3 -DSTRICT_R_HEADERS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -D_HAS_AUTO_PTR_ETC=0 -include "C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/StanHeaders/include/stan/math/prim/fun/Eigen.hpp" -std=c++1y -I"C:/rtools45/x86_64-w64-mingw32.static.posix/include" -O2 -Wall -std=gnu2x -mfpmath=sse -msse2 -mstackrealign -c foo.c -o foo.o
cc1.exe: warning: command-line option '-std=c++14' is valid for C++/ObjC++ but not for C
In file included from C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/Eigen/Core:19,
from C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/Eigen/Dense:1,
from C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/StanHeaders/include/stan/math/prim/fun/Eigen.hpp:22,
from <command-line>:
C:/Users/trevorcaughlin/AppData/Local/R/win-library/4.5/RcppEigen/include/Eigen/src/Core/util/Macros.h:679:10: fatal error: cmath: No such file or directory
679 | #include <cmath>
| ^~~~~~~
compilation terminated.
make: *** [C:/PROGRA~1/R/R-45~1.2/etc/x64/Makeconf:289: foo.o] Error 1
3.8.2 Extract prior and posterior draws for selected coefficients
Below we compare prior and posterior draws for two coefficients:
b_Canopy_height, which appears to have a stronger effectb_Blue, which appears to have a weaker effect
library(tibble)
library(posterior)
library(dplyr)
extract_coef_draws <- function(fit_prior, fit_post, coef_name, prior_type) {
prior_draws <- as_tibble(as_draws_df(fit_prior)) |>
transmute(
value = .data[[coef_name]],
distribution = "Prior",
prior_type = prior_type,
coefficient = coef_name
)
posterior_draws <- as_tibble(as_draws_df(fit_post)) |>
transmute(
value = .data[[coef_name]],
distribution = "Posterior",
prior_type = prior_type,
coefficient = coef_name
)
bind_rows(prior_draws, posterior_draws)
}
coef_compare <- bind_rows(
extract_coef_draws(fit_prior_normal, fit_full_normal, "b_Canopy_height", "Normal"),
extract_coef_draws(fit_prior_laplace, fit_full_laplace, "b_Canopy_height", "Laplace"),
extract_coef_draws(fit_prior_horseshoe, fit_full_horseshoe, "b_Canopy_height", "Horseshoe"),
extract_coef_draws(fit_prior_uniform_wide, fit_full_uniform_wide, "b_Canopy_height", "Uniform(-10, 10)"),
extract_coef_draws(fit_prior_normal, fit_full_normal, "b_Blue", "Normal"),
extract_coef_draws(fit_prior_laplace, fit_full_laplace, "b_Blue", "Laplace"),
extract_coef_draws(fit_prior_horseshoe, fit_full_horseshoe, "b_Blue", "Horseshoe"),
extract_coef_draws(fit_prior_uniform_wide, fit_full_uniform_wide, "b_Blue", "Uniform(-10, 10)")
)3.8.3 Prior vs posterior for b_Canopy_height
ggplot(
filter(coef_compare, coefficient == "b_Canopy_height"),
aes(x = value, color = distribution, fill = distribution)
) +
geom_density(alpha = 0.2) +
facet_wrap(~ prior_type, scales = "free") +
labs(
title = "Prior vs posterior for b_Canopy_height across prior families",
x = "Coefficient value",
y = "Density"
)3.8.4 Prior vs posterior for b_Blue
ggplot(
filter(coef_compare, coefficient == "b_Blue"),
aes(x = value, color = distribution, fill = distribution)
) +
geom_density(alpha = 0.2) +
facet_wrap(~ prior_type, scales = "free") +
labs(
title = "Prior vs posterior for b_Blue across prior families",
x = "Coefficient value",
y = "Density"
)3.9 Overall notes
- The Normal prior gives smooth shrinkage to all coefficients.
- The Laplace prior gives stronger pull toward zero near the origin and is the Bayesian analogue of lasso-style shrinkage.
- The horseshoe prior is especially useful when we think most predictors are near zero but a small number may have meaningful effects.
- The wide Uniform(-10, 10) prior is useful here mainly as a comparison point because it places much weaker regularization on slopes.
- Comparing
b_Canopy_heightandb_Bluehelps show how prior choice matters differently for stronger and weaker effects. - If predictors are on different scales, prior comparisons become harder to interpret, so standardizing predictors is usually a good idea.
- Because the horseshoe prior can lead to more divergent transitions, increasing
adapt_deltais often helpful.