Chapter 15 Horoscopes Insights
Statistical inference is indeed critically important. But only as much as every other part of research. Scientific discovery is not an additive process, in which sin in one part can be atoned by virtue in another. Everything interacts. So equally when science works as intended as when it does not, every part of the process deserves attention. (p. 441)
In this final chapter, there are no models for us to fit and no figures for use to reimagine. McElreath took the opportunity to comment more broadly on the scientific process. He made a handful of great points, some of which I’ll quote in a bit. But for the bulk of this chapter, I’d like to take the opportunity to pass on a few of my own insights about workflow. I hope they’re of use.
15.1 Use R Notebooks
OMG
I first started using R in the winter of 2015/2016. Right from the start, I learned how to code from within the R Studio environment. But within R Studio I was using simple scripts. No longer. I now use R Notebooks for just about everything, including my scientific projects, this bookdown project, and even my academic webpage and blog. Nathan Stephens wrote a nice blog on Why I love R Notebooks. I agree. This has fundamentally changed my workflow as a scientist. I only wish I’d learned about this before starting my dissertation project. So it goes…
Do yourself a favor, adopt R Notebooks into your workflow. Do it today. If you prefer to learn with videos, here’s a nice intro by Kristine Yu and another one by JJ Allaire. Try it out for like one afternoon and you’ll be hooked.
15.2 Save your model fits
It’s embarrassing how long it took for this to dawn on me.
Unlike classical statistics, Bayesian models using MCMC take a while to compute. Most of the simple models in McElreath’s text take 30 seconds up to a couple minutes. If your data are small, well-behaved and of a simple structure, you might have a lot of wait times in that range in your future.
It hasn’t been that way, for me.
Most of my data have a complicated multilevel structure and often aren’t very well behaved. It’s normal for my models to take an hour or several to fit. Once you start measuring your model fit times in hours, you do not want to fit these things more than once. So, it’s not enough to document my code in a nice R Notebook file. I need to save my brm()
fit objects in external files.
Consider this model. It’s taken from Bürkner’s vignette, Estimating Multivariate Models with brms. It took about five minutes for my several-year-old laptop to fit.
library(brms)
data("BTdata", package = "MCMCglmm")
fit1 <-
brm(data = BTdata,
family = gaussian,
mvbind(tarsus, back) ~ sex + hatchdate + (1|p|fosternest) + (1|q|dam),
chains = 2, cores = 2,
seed = 15)
Five minutes isn’t terribly long to wait, but still. I’d prefer to never have to wait for another five minutes, again. Sure, if I save my code in a document like this, I will always be able to fit the model again. But I can work smarter. Here I’ll save my fit1
object outside of R with the save()
function.
save(fit1, file = "fit1.rda")
Hopefully y’all are savvy Bayesian R users and find this insultingly remedial. But if it’s new to you like it was me, you can learn more about .rda
files here.
Now fit1
is saved outside of R, I can safely remove it and then reload it.
rm(fit1)
load("fit1.rda")
The file took a fraction of a second to reload. Once reloaded, I can perform typical operations, like examine summaries of the model parameters or refreshing my memory on what data I used.
print(fit1)
## Family: MV(gaussian, gaussian)
## Links: mu = identity; sigma = identity
## mu = identity; sigma = identity
## Formula: tarsus ~ sex + hatchdate + (1 | p | fosternest) + (1 | q | dam)
## back ~ sex + hatchdate + (1 | p | fosternest) + (1 | q | dam)
## Data: BTdata (Number of observations: 828)
## Samples: 2 chains, each with iter = 2000; warmup = 1000; thin = 1;
## total post-warmup samples = 2000
##
## Group-Level Effects:
## ~dam (Number of levels: 106)
## Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
## sd(tarsus_Intercept) 0.48 0.05 0.40 0.59 710 1.01
## sd(back_Intercept) 0.25 0.07 0.09 0.39 267 1.00
## cor(tarsus_Intercept,back_Intercept) -0.51 0.22 -0.92 -0.07 605 1.00
##
## ~fosternest (Number of levels: 104)
## Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
## sd(tarsus_Intercept) 0.27 0.05 0.16 0.37 725 1.00
## sd(back_Intercept) 0.35 0.06 0.23 0.46 401 1.00
## cor(tarsus_Intercept,back_Intercept) 0.70 0.21 0.21 0.98 212 1.01
##
## Population-Level Effects:
## Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
## tarsus_Intercept -0.41 0.07 -0.54 -0.28 1309 1.00
## back_Intercept -0.01 0.06 -0.13 0.12 1887 1.00
## tarsus_sexMale 0.77 0.06 0.66 0.89 2713 1.00
## tarsus_sexUNK 0.23 0.13 -0.03 0.49 2574 1.00
## tarsus_hatchdate -0.04 0.06 -0.16 0.07 1038 1.00
## back_sexMale 0.01 0.07 -0.12 0.14 3385 1.00
## back_sexUNK 0.14 0.15 -0.16 0.46 2730 1.00
## back_hatchdate -0.09 0.05 -0.19 0.01 1794 1.00
##
## Family Specific Parameters:
## Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
## sigma_tarsus 0.76 0.02 0.72 0.80 2079 1.00
## sigma_back 0.90 0.02 0.86 0.95 1834 1.00
##
## Residual Correlations:
## Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
## rescor(tarsus,back) -0.05 0.04 -0.13 0.02 2349 1.00
##
## Samples were drawn using sampling(NUTS). For each parameter, Eff.Sample
## is a crude measure of effective sample size, and Rhat is the potential
## scale reduction factor on split chains (at convergence, Rhat = 1).
library(tidyverse)
fit1$data %>%
head()
## tarsus sex hatchdate fosternest dam back
## 1 -1.89229718 Fem -0.6874021 F2102 R187557 1.1464212
## 2 1.13610981 Male -0.6874021 F1902 R187559 -0.7596521
## 3 0.98468946 Male -0.4279814 A602 R187568 0.1449373
## 4 0.37900806 Male -1.4656641 A1302 R187518 0.2555847
## 5 -0.07525299 Fem -1.4656641 A2602 R187528 -0.3006992
## 6 -1.13519543 Fem 0.3502805 C2302 R187945 1.5577219
As an alternative, Bürkner recently added a file
argument in brms:brm()
that will help you do this, too. The origins of the argument live here. By default, file
is set to NULL
. To make use of the argument, specify a character string. file
will then save your fitted model object in an external .rds
file via the saveRDS()
function. Let’s give it a whirl, this time with an interaction.
fit2 <-
brm(data = BTdata,
family = gaussian,
mvbind(tarsus, back) ~ sex*hatchdate + (1|p|fosternest) + (1|q|dam),
chains = 2, cores = 2,
seed = 15,
file = "fit2")
Now fit2
is saved outside of R, I can safely remove it and then reload it.
rm(fit2)
We might load fit2
with the readRDS()
function.
fit2 <- readRDS("fit2.rds")
Now we can work with fit2
as desired.
fixef(fit2) %>%
round(digits = 3)
## Estimate Est.Error Q2.5 Q97.5
## tarsus_Intercept -0.409 0.070 -0.547 -0.268
## back_Intercept -0.011 0.067 -0.144 0.113
## tarsus_sexMale 0.771 0.057 0.658 0.884
## tarsus_sexUNK 0.194 0.147 -0.104 0.485
## tarsus_hatchdate -0.054 0.068 -0.186 0.077
## tarsus_sexMale:hatchdate 0.013 0.058 -0.101 0.125
## tarsus_sexUNK:hatchdate 0.058 0.122 -0.174 0.299
## back_sexMale 0.005 0.069 -0.128 0.138
## back_sexUNK 0.145 0.170 -0.190 0.472
## back_hatchdate -0.052 0.061 -0.173 0.061
## back_sexMale:hatchdate -0.079 0.069 -0.209 0.053
## back_sexUNK:hatchdate -0.033 0.143 -0.321 0.253
The file
method has another handy feature. Let’s remove fit2
one more time to see.
rm(fit2)
If you’ve fit a brm()
model once and saved the results with file
, executing the same brm()
code will not re-fit the model. Rather, it will just load and return the model from the .rds
file.
fit2 <-
brm(data = BTdata,
family = gaussian,
mvbind(tarsus, back) ~ sex*hatchdate + (1|p|fosternest) + (1|q|dam),
chains = 2, cores = 2,
seed = 15,
file = "fit2")
It takes just a fraction of a second. Once again, we’re ready to work with the fit.
fit2$formula
## tarsus ~ sex * hatchdate + (1 | p | fosternest) + (1 | q | dam)
## back ~ sex * hatchdate + (1 | p | fosternest) + (1 | q | dam)
And if you’d like to remind yourself what the name of that external file was, you can extract it from the brm()
fit object.
fit2$file
## [1] "fit2.rds"
Also, see Gavin Simpson’s blog post A better way of saving and loading objects in R for a discussion on the distinction between .rda
and .rds
files.
15.3 Build your models slowly
The model from Bürkner’s vignette, fit1
, was no joke. If you wanted to be verbose about it, it was a multilevel, multivariate, multivariable model. It had a cross-classified multilevel structure, two predictors (for each criterion), and two criteria. Not only is that a lot to keep track of, there’s a whole lot of places for things to go wrong.
Even if that was the final model I was interested in as a scientist, I still wouldn’t start with it. I’d build up incrementally, just to make sure nothing looked fishy. One place to start would be a simple intercepts-only model.
fit0 <-
brm(mvbind(tarsus, back) ~ 1,
data = BTdata, chains = 2, cores = 2,
file = "fit0")
plot(fit0)
print(fit0)
## Family: MV(gaussian, gaussian)
## Links: mu = identity; sigma = identity
## mu = identity; sigma = identity
## Formula: tarsus ~ 1
## back ~ 1
## Data: BTdata (Number of observations: 828)
## Samples: 2 chains, each with iter = 2000; warmup = 1000; thin = 1;
## total post-warmup samples = 2000
##
## Population-Level Effects:
## Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
## tarsus_Intercept -0.00 0.03 -0.07 0.07 2563 1.00
## back_Intercept -0.00 0.04 -0.07 0.07 2242 1.00
##
## Family Specific Parameters:
## Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
## sigma_tarsus 1.00 0.02 0.96 1.05 2566 1.00
## sigma_back 1.00 0.03 0.95 1.05 2675 1.00
##
## Residual Correlations:
## Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
## rescor(tarsus,back) -0.03 0.03 -0.10 0.04 2453 1.00
##
## Samples were drawn using sampling(NUTS). For each parameter, Eff.Sample
## is a crude measure of effective sample size, and Rhat is the potential
## scale reduction factor on split chains (at convergence, Rhat = 1).
If the chains look good and the summary statistics look like what I’d expect, I’m on good footing to keep building up to the model I really care about. The results from this model, for example, suggest that both criteria were standardized (i.e., intercepts at 0 and \(\sigma\)s at 1). If that wasn’t what I intended, I’d rather catch it here than spend five minutes fitting the more complicated fit1
model, the parameters for which are sufficiently complicated that I may have had trouble telling what scale the data were on.
Note, this is not the same as \(p\)-hacking or wandering aimlessly down the garden of forking paths. We are not chasing the flashiest model to put in a paper. Rather, this is just good pragmatic data science. If you start off with a theoretically-justified but complicated model and run into computation problems or produce odd-looking estimates, it won’t be clear where things went awry. When you build up, step by step, it’s easier to catch data cleaning failures, coding goofs and the like.
So, when I’m working on a project, I fit one or a few simplified models before fitting my complicated model of theoretical interest. This is especially the case when I’m working with model types that are new to me or that I haven’t worked with in a while. I document each step in my R Notebook files and I save the fit objects for each in external files. I have caught surprises this way. Hopefully this will help you catch your mistakes, too.
15.4 Look at your data
Relatedly, and perhaps even a precursor, you should always plot your data before fitting a model. There were plenty examples of this in the text, but it’s worth of making explicit. Simple summary statistics are great, but they’re not enough. For an entetrtaining exposition, check out Same Stats, Different Graphs: Generating Datasets with Varied Appearance and Identical Statistics through Simulated Annealing. Though it might make for a great cocktail party story, I’d hate to pollute the literature with a linear model based on a set of dinosaur-shaped data.
15.5 Use the 0 + intercept
syntax
We covered this a little in the last couple chapters, but it’s easy to miss. If your real-world model has predictors (i.e., isn’t an intercept-only model), it’s important to keep track of how you have centered those predictors. When you specify a prior for a brms Intercept
(i.e., an intercept resulting from the y ~ x
or y ~ 1 + x
style of syntax), that prior is applied under the presumption all the predictors are mean centered. In the Population-level (‘fixed’) effects subsection of the set_prior
section of the brms reference manual (version 2.8.0), we read:
Note that technically, this prior is set on an intercept that results when internally centering all population-level predictors around zero to improve sampling efficiency. On this centered intercept, specifying a prior is actually much easier and intuitive than on the original intercept, since the former represents the expected response value when all predictors are at their means. To treat the intercept as an ordinary population-level effect and avoid the centering parameterization, use
0 + intercept
on the right-hand side of the model formula. (p. 153)
We get a little more information from the Parameterization of the population-level intercept subsection of the brmsformula
section:
This behavior can be avoided by using the reserved (and internally generated) variable
intercept
. Instead ofy ~ x
, you may writey ~ 0 + intercept + x
. This way, priors can be defined on the real intercept, directly. In addition, the intercept is just treated as an ordinary population-level effect and thus priors defined onb
will also apply to it. Note that this parameterization may be less efficient than the default parameterization discussed above. (p. 32)
We didn’t bother with this for most of the project because our priors on the Intercept
were often vague and the predictors were often on small enough scales (e.g., the mean of a dummy variable is close to 0) that it just didn’t matter. But this will not always be the case. Set your Intercept
priors with care.
There’s also the flip side of the issue. If there’s no strong reason not to, consider mean-centering or even standardizing your predictors. Not only will that solve the Intercept
prior issue, but it often results in more meaningful parameter estimates.
15.6 Annotate your workflow
In a typical model-fitting file, I’ll load my data, perhaps transform the data a bit, fit several models, and examine the output of each with trace plots, model summaries, information criteria, and the like. In my early days, I just figured each of these steps were self-explanatory.
Nope.
My experience was that even a couple weeks between taking a break from a project and restarting it was enough time to make my earlier files confusing. And they were my files. I now start each R Notebook document with an introductory paragraph or two explaining exactly what the purpose of the file is. I separate my major sections by headers and subheaders. My working R Notebook files are peppered with bullets, sentences, and full on paragraphs between code blocks.
15.7 Annotate your code
This idea is implicit in McElreath’s text. But it’s easy to miss the message. I know I did, at first. I find this is especially important for data wrangling. I’m a tidyverse guy and, for me, the big-money verbs like mutate()
, gather()
, select()
, filter()
, group_by()
, and summarise()
take care of the bulk of my data wrangling. But every once and a while I need to do something less common, like with str_extract()
or case_when()
. And when I end up using a new or less familiar function, I typically annotate right in the code and even sometimes leave a hyperlink to some R-bloggers post or stackoverflow question that explained how to use it.
15.8 Break up your workflow
I’ve also learned to break up my projects into multiple R Notebook files. If you have a small project for which you just want a quick and dirty plot, fine, do it all in one file. My typical project has:
- A primary data cleaning file
- A file with basic descriptive statistics and the like
- At least one primary analysis file
- Possible secondary and tertiary analysis files
- A file or two for my major figures
- A file explaining and depicting my priors, often accompanied by my posteriors, for comparison
Putting all that information in one R Notebook file would be overwhelming. Your workflow might well look different, but hopefully you get the idea. You don’t want working files with thousands of lines of code.
And mainly to keep Jenny Bryan from setting my computer on fire, I’m also getting into the habit of organizing all these interconnected files with help from R Studio Projects, which you can learn even more about from this chapter in R4DS.
15.9 Read Gelman’s blog
Yes, that Gelman.
Actually, I started reading Gelman’s blog around the same time I dove into McElreath’s text. But if this isn’t the case for you, it’s time to correct that evil. My graduate mentor often recalled how transformative his first academic conference was. He was an undergrad at the time and it was his first experience meeting and talking with the people whose names he’d seen in his text books. He learned that science was an ongoing conversation among living scientists and–at that time–the best place to take part in that conversation was at conferences. Times keep changing. Nowadays, the living conversation of science occurs online on social media and in blogs. One of the hottest places to find scientists conversing about Bayesian statistics and related methods is Gelman’s blog. The posts are great. But a lot of the action is in the comments sections, too.
15.11 Parting wisdom
Okay, that’s enough from me. Let’s start wrapping this project up with some McElreath.
There is an aspect of science that you do personally control: openness. Pre-plan your research together with the statistical analysis. Doing so will improve both the research design and the statistics. Document it in the form of a mock analysis that you would not be ashamed to share with a colleague. Register it publicly, perhaps in a simple repository, like Github or any other. But your webpage will do just fine, as well. Then collect the data. Then analyze the data as planned. If you must change the plan, that’s fine. But document the changes and justify them. Provide all of the data and scripts necessary to repeat your analysis. Do not provide scripts and data “on request,” but rather put them online so reviewers of your paper can access them without your interaction. There are of course cases in which full data cannot be released, due to privacy concerns. But the bulk of science is not of that sort.
The data and its analysis are the scientific product. The paper is just an advertisement. If you do your honest best to design, conduct, and document your research, so that others can build directly upon it, you can make a difference. (p. 443)
Toward that end, also check out the OSF and their YouTube channel, here. Katie Corker gets the last words: “Open science is stronger because we’re doing this together.”
Session info
sessionInfo()
## R version 3.6.0 (2019-04-26)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 18.04.2 LTS
##
## Matrix products: default
## BLAS: /opt/R/3.6.0/lib/R/lib/libRblas.so
## LAPACK: /opt/R/3.6.0/lib/R/lib/libRlapack.so
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8
## [4] LC_COLLATE=en_US.UTF-8 LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C LC_ADDRESS=C
## [10] LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## attached base packages:
## [1] grid parallel stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] viridis_0.5.1 viridisLite_0.3.0 ggbeeswarm_0.6.0 dutchmasters_0.1.0 loo_2.1.0
## [6] wesanderson_0.3.6 ghibli_0.2.0 extrafont_0.17 hrbrthemes_0.6.0 ggthemes_4.2.0
## [11] broom_0.5.2 rcartocolor_2.0.0 GGally_1.4.0 bayesplot_1.7.0 fiftystater_1.0.1
## [16] ggrepel_0.8.1 tidybayes_1.1.0 rethinking_1.59 rstan_2.18.2 StanHeaders_2.18.1
## [21] gridExtra_2.3 forcats_0.4.0 stringr_1.4.0 dplyr_0.8.1 purrr_0.3.2
## [26] readr_1.3.1 tidyr_0.8.3 tibble_2.1.3 ggplot2_3.1.1 tidyverse_1.2.1
## [31] brms_2.9.0 Rcpp_1.0.1
##
## loaded via a namespace (and not attached):
## [1] coda_0.19-2 knitr_1.23 dygraphs_1.1.1.6
## [4] inline_0.3.15 generics_0.0.2 callr_3.2.0
## [7] future_1.13.0 xml2_1.2.0 lubridate_1.7.4
## [10] httpuv_1.5.1 assertthat_0.2.1 xfun_0.7
## [13] hms_0.4.2 evaluate_0.14 promises_1.0.1
## [16] fansi_0.4.0 readxl_1.3.1 igraph_1.2.4.1
## [19] htmlwidgets_1.3 reshape_0.8.8 stats4_3.6.0
## [22] crosstalk_1.0.0 backports_1.1.4 bookdown_0.11
## [25] markdown_1.0 vctrs_0.1.0 abind_1.4-5
## [28] withr_2.1.2 xts_0.11-2 prettyunits_1.0.2
## [31] mnormt_1.5-5 pacman_0.5.1 lazyeval_0.2.2
## [34] crayon_1.3.4 pkgconfig_2.0.2 labeling_0.3
## [37] vipor_0.4.5 nlme_3.1-140 rlang_0.3.4
## [40] globals_0.12.4 nleqslv_3.3.2 miniUI_0.1.1.1
## [43] colourpicker_1.0 extrafontdb_1.0 modelr_0.1.4
## [46] cellranger_1.1.0 rprojroot_1.3-2 matrixStats_0.54.0
## [49] Matrix_1.2-17 zoo_1.8-6 beeswarm_0.2.3
## [52] base64enc_0.1-3 ggridges_0.5.1 processx_3.3.1
## [55] pander_0.6.3 shinystan_2.5.0 scales_1.0.0
## [58] magrittr_1.5 plyr_1.8.4 threejs_0.3.1
## [61] compiler_3.6.0 rstantools_1.5.1 RColorBrewer_1.1-2
## [64] cli_1.1.0 listenv_0.7.0 ps_1.3.0
## [67] Brobdingnag_1.2-6 MASS_7.3-51.4 tidyselect_0.2.5
## [70] stringi_1.4.3 highr_0.8 yaml_2.2.0
## [73] svUnit_0.7-12 bridgesampling_0.6-0 tools_3.6.0
## [76] rstudioapi_0.10 foreign_0.8-71 digest_0.6.19
## [79] shiny_1.3.2 later_0.8.0 httr_1.4.0
## [82] gdtools_0.1.8 rsconnect_0.8.13 psych_1.8.12
## [85] ggstance_0.3.1 colorspace_1.4-1 rvest_0.3.4
## [88] shinythemes_1.1.2 mapproj_1.2.6 xtable_1.8-4
## [91] jsonlite_1.6 zeallot_0.1.0 R6_2.4.0
## [94] pillar_1.4.1 htmltools_0.3.6 mime_0.7
## [97] glue_1.3.1 DT_0.7 codetools_0.2-16
## [100] maps_3.3.0 pkgbuild_1.0.3 mvtnorm_1.0-10
## [103] utf8_1.1.4 lattice_0.20-38 arrayhelpers_1.0-20160527
## [106] HDInterval_0.2.0 gtools_3.8.1 shinyjs_1.0
## [109] Rttf2pt1_1.3.7 rmarkdown_1.13 munsell_0.5.0
## [112] haven_2.1.0 reshape2_1.4.3 gtable_0.3.0