├── .Rbuildignore ├── .github └── workflows │ ├── R-linux.yml │ ├── R-mac.yml │ └── R-win.yml ├── .gitignore ├── CODE_OF_CONDUCT.md ├── DESCRIPTION ├── LICENSE ├── LICENSE.md ├── NAMESPACE ├── NEWS.md ├── R ├── complete.R ├── partials.R ├── rat_data-data.R ├── rats_example.R ├── stan_axe.R ├── stan_filter.R ├── stan_names.R ├── stan_retain.R ├── stan_select.R ├── stan_slice.R ├── stan_thin.R ├── stan_utils.R ├── utils.R └── zzz.R ├── README.Rmd ├── README.md ├── _pkgdown.yml ├── codecov.yml ├── data-raw ├── 3989510611_e88c931f7d_h.jpg ├── app │ ├── global.R │ ├── server.R │ └── ui.R ├── dot.png ├── figure6.png ├── hiclipart.com.png ├── logo.png ├── logo_dot.png ├── make_hex.R ├── shred_dot_ghcard.png ├── shred_ghcard.png ├── shred_hex.png ├── shred_hex_dot.png ├── shred_hex_ribbon.png ├── shred_ribbon_ghcard.png ├── stan_filter_.R ├── stan_logo.png ├── stan_sample.R └── test │ ├── samp_1.csv │ ├── samp_2.csv │ ├── samp_3.csv │ └── samp_4.csv ├── data └── rat_data.rda ├── inst └── rats.stan ├── man ├── figures │ └── logo_ribbon.png ├── peek_pars.Rd ├── pipe.Rd ├── rat_data.Rd ├── rats_example.Rd ├── stan_axe.Rd ├── stan_filter.Rd ├── stan_names.Rd ├── stan_partials.Rd ├── stan_retain.Rd ├── stan_select.Rd ├── stan_slice.Rd └── stan_thin.Rd ├── tests ├── README.md ├── files │ └── rats.Rds ├── testthat.R └── testthat │ ├── test-axe.R │ ├── test-filter.R │ ├── test-names.R │ ├── test-retain.R │ ├── test-sampling.R │ ├── test-select.R │ └── test-slice.R └── vignettes ├── .build.timestamp ├── .gitignore ├── axe.Rmd ├── axe.Rmd.orig ├── benchmark-1.png ├── benchmark.Rmd ├── benchmark.Rmd.orig ├── brms.Rmd ├── brms.Rmd.orig ├── brms_plot_1-1.png ├── brms_plot_1-2.png ├── brms_plot_2-1.png ├── brms_plot_2-2.png ├── brms_plot_3-1.png ├── brms_plot_3-2.png ├── brms_plot_4-1.png ├── brms_plot_4-2.png ├── brms_plot_5-1.png ├── brms_plot_5-2.png ├── rstan.Rmd ├── rstan.Rmd.orig ├── tests_and_coverage.Rmd ├── tidybayes.Rmd └── tidybayes.Rmd.orig /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^data-raw$ 2 | ^.*\.Rproj$ 3 | ^\.Rproj\.user$ 4 | ^LICENSE\.md$ 5 | ^_pkgdown\.yml$ 6 | ^docs$ 7 | ^pkgdown$ 8 | ^\.travis\.yml$ 9 | ^README\.Rmd$ 10 | ^codecov\.yml$ 11 | ^CODE_OF_CONDUCT\.md$ 12 | ^presentation$ 13 | ^\.github$ 14 | ^vignettes/*\.orig$ 15 | -------------------------------------------------------------------------------- /.github/workflows/R-linux.yml: -------------------------------------------------------------------------------- 1 | on: [push, pull_request] 2 | 3 | name: R-linux 4 | 5 | jobs: 6 | check: 7 | runs-on: ${{ matrix.config.os }} 8 | name: ${{ matrix.config.os }} (${{ matrix.config.r }}) 9 | if: "!contains(github.event.head_commit.message, 'skip linux')" 10 | 11 | strategy: 12 | fail-fast: false 13 | matrix: 14 | config: 15 | - {os: ubuntu-16.04, r: '4.0', cran: "https://cran.r-project.org", args: "--no-manual"} 16 | env: 17 | R_REMOTES_NO_ERRORS_FROM_WARNINGS: true 18 | CRAN: ${{ matrix.config.cran }} 19 | 20 | steps: 21 | - uses: actions/checkout@main 22 | 23 | - uses: r-lib/actions/setup-r@master 24 | with: 25 | r-version: ${{ matrix.config.r }} 26 | 27 | - name: Install system dependencies 28 | if: runner.os == 'Linux' 29 | env: 30 | RHUB_PLATFORM: linux-x86_64-ubuntu-gcc 31 | run: | 32 | Rscript -e "install.packages('remotes')" -e "remotes::install_github('r-hub/sysreqs')" 33 | sysreqs=$(Rscript -e "cat(sysreqs::sysreq_commands('DESCRIPTION'))") 34 | sudo -s eval "$sysreqs" 35 | 36 | - name: Query dependencies 37 | run: | 38 | saveRDS(remotes::dev_package_deps(dependencies = TRUE), "depends.Rds", version = 2) 39 | shell: Rscript {0} 40 | 41 | - name: Cache R packages 42 | if: runner.os != 'Windows' 43 | uses: actions/cache@v1 44 | with: 45 | path: ${{ env.R_LIBS_USER }} 46 | key: ${{ runner.os }}-r-${{ matrix.config.r }}-${{ hashFiles('depends.Rds') }}-1 47 | restore-keys: ${{ runner.os }}-r-${{ matrix.config.r }}- 48 | 49 | - name: Install dependencies 50 | run: | 51 | library(remotes) 52 | deps <- readRDS("depends.Rds") 53 | deps[["installed"]] <- vapply(deps[["package"]], remotes:::local_sha, character(1)) 54 | update(deps) 55 | remotes::install_cran("rcmdcheck") 56 | shell: Rscript {0} 57 | 58 | - name: Check 59 | run: rcmdcheck::rcmdcheck(args = "--no-manual", error_on = "warning", check_dir = "check") 60 | shell: Rscript {0} 61 | 62 | - name: Upload check results 63 | if: failure() 64 | uses: actions/upload-artifact@main 65 | with: 66 | name: ${{ runner.os }}-r${{ matrix.config.r }}-results 67 | path: check 68 | -------------------------------------------------------------------------------- /.github/workflows/R-mac.yml: -------------------------------------------------------------------------------- 1 | on: [push, pull_request] 2 | 3 | name: R-mac 4 | 5 | jobs: 6 | check: 7 | runs-on: ${{ matrix.config.os }} 8 | name: ${{ matrix.config.os }} (${{ matrix.config.r }}) 9 | if: "!contains(github.event.head_commit.message, 'skip osx')" 10 | 11 | strategy: 12 | fail-fast: false 13 | matrix: 14 | config: 15 | - { os: macOS-latest, r: '4.0', args: "--no-manual"} 16 | env: 17 | R_REMOTES_NO_ERRORS_FROM_WARNINGS: true 18 | CRAN: ${{ matrix.config.cran }} 19 | 20 | steps: 21 | - uses: actions/checkout@main 22 | 23 | - uses: r-lib/actions/setup-pandoc@master 24 | 25 | - uses: r-lib/actions/setup-r@master 26 | with: 27 | r-version: ${{ matrix.config.r }} 28 | 29 | - name: Install system dependencies 30 | if: runner.os == 'Linux' 31 | env: 32 | RHUB_PLATFORM: linux-x86_64-ubuntu-gcc 33 | run: | 34 | Rscript -e "install.packages('remotes')" -e "remotes::install_github('r-hub/sysreqs')" 35 | sysreqs=$(Rscript -e "cat(sysreqs::sysreq_commands('DESCRIPTION'))") 36 | sudo -s eval "$sysreqs" 37 | 38 | - name: Query dependencies 39 | run: | 40 | install.packages('remotes') 41 | saveRDS(remotes::dev_package_deps(dependencies = TRUE), "depends.Rds", version = 2) 42 | shell: Rscript {0} 43 | 44 | - name: Cache R packages 45 | if: runner.os != 'Windows' 46 | uses: actions/cache@v1 47 | with: 48 | path: ${{ env.R_LIBS_USER }} 49 | key: ${{ runner.os }}-r-${{ matrix.config.r }}-${{ hashFiles('depends.Rds') }}-1 50 | restore-keys: ${{ runner.os }}-r-${{ matrix.config.r }}- 51 | 52 | - name: Install dependencies 53 | run: | 54 | library(remotes) 55 | deps <- readRDS("depends.Rds") 56 | deps[["installed"]] <- vapply(deps[["package"]], remotes:::local_sha, character(1)) 57 | update(deps) 58 | remotes::install_cran("rcmdcheck") 59 | shell: Rscript {0} 60 | 61 | - name: Check 62 | run: rcmdcheck::rcmdcheck(args = "--no-manual", error_on = "warning", check_dir = "check") 63 | shell: Rscript {0} 64 | 65 | - name: Upload check results 66 | if: failure() 67 | uses: actions/upload-artifact@main 68 | with: 69 | name: ${{ runner.os }}-r${{ matrix.config.r }}-results 70 | path: check 71 | 72 | - name: covr 73 | run: | 74 | Rscript -e 'remotes::install_github("r-lib/covr@gh-actions")' \ 75 | -e 'covr::codecov(token = "${{secrets.CODECOV_TOKEN}}")' 76 | 77 | - name: Install package 78 | run: R CMD INSTALL . 79 | 80 | - name: covrpage 81 | if: github.ref == 'refs/heads/master' 82 | run: | 83 | git config --local user.email "actions@github.com" 84 | git config --local user.name "GitHub Actions" 85 | Rscript -e 'remotes::install_github("yonicd/covrpage@actions")' \ 86 | -e 'covrpage::covrpage_ci()' 87 | git commit tests/README.md -m 'Update tests/README.Rmd' || echo "No changes to commit" 88 | git push https://${{github.actor}}:${{secrets.GH_PAT}}@github.com/${{github.repository}}.git HEAD:${{ github.ref }} || echo "No changes to commit" 89 | 90 | - name: Deploy package 91 | if: github.ref == 'refs/heads/master' 92 | run: | 93 | git config --local user.email "actions@github.com" 94 | git config --local user.name "GitHub Actions" 95 | Rscript -e 'install.packages("pkgdown")' \ 96 | -e 'pkgdown::deploy_to_branch(new_process = FALSE)' 97 | -------------------------------------------------------------------------------- /.github/workflows/R-win.yml: -------------------------------------------------------------------------------- 1 | on: [push, pull_request] 2 | 3 | name: R-win 4 | 5 | jobs: 6 | check: 7 | runs-on: ${{ matrix.config.os }} 8 | name: ${{ matrix.config.os }} (${{ matrix.config.r }}) 9 | if: "!contains(github.event.head_commit.message, 'skip winos')" 10 | 11 | strategy: 12 | fail-fast: false 13 | matrix: 14 | config: 15 | - { os: windows-latest, r: '4.0', args: "--no-manual"} 16 | env: 17 | R_REMOTES_NO_ERRORS_FROM_WARNINGS: true 18 | CRAN: ${{ matrix.config.cran }} 19 | 20 | steps: 21 | - uses: actions/checkout@main 22 | 23 | - uses: r-lib/actions/setup-tinytex@master 24 | if: contains(matrix.config.args, 'no-manual') == false 25 | 26 | - uses: r-lib/actions/setup-r@master 27 | with: 28 | r-version: ${{ matrix.config.r }} 29 | 30 | - name: Install dependencies 31 | run: Rscript -e "install.packages('remotes')" -e "remotes::install_deps(dependencies = TRUE)" -e "remotes::install_cran('rcmdcheck')" 32 | 33 | - name: Check 34 | run: Rscript -e "rcmdcheck::rcmdcheck(args = '${{ matrix.config.args }}', error_on = 'warning', check_dir = 'check')" 35 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # History files 2 | .Rhistory 3 | .Rapp.history 4 | 5 | # Session Data files 6 | .RData 7 | 8 | # Example code in package build process 9 | *-Ex.R 10 | 11 | # Output files from R CMD build 12 | /*.tar.gz 13 | 14 | # Output files from R CMD check 15 | /*.Rcheck/ 16 | 17 | # RStudio files 18 | .Rproj.user/ 19 | 20 | # produced vignettes 21 | vignettes/*.html 22 | vignettes/*.pdf 23 | 24 | # OAuth2 token, see https://github.com/hadley/httr/releases/tag/v0.3 25 | .httr-oauth 26 | 27 | # knitr and R markdown default cache directories 28 | /*_cache/ 29 | /cache/ 30 | 31 | # Temporary files created by R markdown 32 | *.utf8.md 33 | *.knit.md 34 | 35 | # Shiny token, see https://shiny.rstudio.com/articles/shinyapps.html 36 | rsconnect/ 37 | 38 | *.Rproj 39 | .Rproj.user 40 | inst/doc 41 | .Rprofile 42 | *.DS_Store 43 | 44 | presentation 45 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Code of Conduct 2 | 3 | As contributors and maintainers of this project, we pledge to respect all people who 4 | contribute through reporting issues, posting feature requests, updating documentation, 5 | submitting pull requests or patches, and other activities. 6 | 7 | We are committed to making participation in this project a harassment-free experience for 8 | everyone, regardless of level of experience, gender, gender identity and expression, 9 | sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion. 10 | 11 | Examples of unacceptable behavior by participants include the use of sexual language or 12 | imagery, derogatory comments or personal attacks, trolling, public or private harassment, 13 | insults, or other unprofessional conduct. 14 | 15 | Project maintainers have the right and responsibility to remove, edit, or reject comments, 16 | commits, code, wiki edits, issues, and other contributions that are not aligned to this 17 | Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed 18 | from the project team. 19 | 20 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by 21 | opening an issue or contacting one or more of the project maintainers. 22 | 23 | This Code of Conduct is adapted from the Contributor Covenant 24 | (https://www.contributor-covenant.org), version 1.0.0, available at 25 | https://contributor-covenant.org/version/1/0/0/. 26 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Type: Package 2 | Package: shredder 3 | Title: Rstan Manipulation API 4 | Version: 0.2.2 5 | Authors@R: 6 | person(given = "Jonathan Sidi", 7 | role = c("aut", "cre"), 8 | email = "yonicd@gmail.com") 9 | Description: API that incites exploration and iteration of rstan simulation 10 | objects using tidy-like verbs. 11 | License: MIT + file LICENSE 12 | URL: https://github.com/yonicd/shredder 13 | BugReports: 14 | https://github.com/yonicd/shredder/issues 15 | Depends: 16 | R (>= 3.2.0) 17 | Imports: 18 | checkmate, 19 | parallel, 20 | purrr, 21 | rematch2, 22 | rlang, 23 | rstan, 24 | stringi 25 | Suggests: 26 | covr, 27 | brms, 28 | details, 29 | knitr, 30 | rmarkdown, 31 | testthat, 32 | tidybayes, 33 | butcher 34 | Remotes: 35 | tidymodels/butcher 36 | VignetteBuilder: 37 | knitr 38 | Encoding: UTF-8 39 | LazyData: true 40 | Roxygen: list(markdown = TRUE) 41 | RoxygenNote: 7.1.1 42 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | YEAR: 2020 2 | COPYRIGHT HOLDER: Jonathan Sidi 3 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | Copyright (c) 2020 Jonathan Sidi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | S3method(stan_filter,brmsfit) 4 | S3method(stan_filter,stanfit) 5 | S3method(stan_names,brmsfit) 6 | S3method(stan_names,stanfit) 7 | S3method(stan_retain,brmsfit) 8 | S3method(stan_retain,stanfit) 9 | S3method(stan_select,brmsfit) 10 | S3method(stan_select,stanfit) 11 | S3method(stan_slice,brmsfit) 12 | S3method(stan_slice,stanfit) 13 | S3method(stan_thin_frac,brmsfit) 14 | S3method(stan_thin_frac,stanfit) 15 | S3method(stan_thin_n,brmsfit) 16 | S3method(stan_thin_n,stanfit) 17 | export("%>%") 18 | export("%||%") 19 | export(peek_pars) 20 | export(rats_example) 21 | export(stan_axe) 22 | export(stan_contains) 23 | export(stan_ends_with) 24 | export(stan_filter) 25 | export(stan_names) 26 | export(stan_retain) 27 | export(stan_select) 28 | export(stan_slice) 29 | export(stan_starts_with) 30 | export(stan_thin_frac) 31 | export(stan_thin_n) 32 | importFrom(checkmate,assert) 33 | importFrom(checkmate,checkClass) 34 | importFrom(parallel,detectCores) 35 | importFrom(purrr,"%>%") 36 | importFrom(purrr,"%||%") 37 | importFrom(purrr,map) 38 | importFrom(purrr,map2) 39 | importFrom(purrr,map_df) 40 | importFrom(purrr,map_dfc) 41 | importFrom(purrr,modify) 42 | importFrom(purrr,rerun) 43 | importFrom(rematch2,re_match) 44 | importFrom(rlang,enquos) 45 | importFrom(rlang,eval_tidy) 46 | importFrom(rlang,is_string) 47 | importFrom(rlang,quo) 48 | importFrom(rlang,quo_squash) 49 | importFrom(rlang,quo_text) 50 | importFrom(rstan,stan) 51 | importFrom(stringi,stri_extract_all_regex) 52 | importFrom(utils,getFromNamespace) 53 | importFrom(utils,modifyList) 54 | importFrom(utils,rc.options) 55 | -------------------------------------------------------------------------------- /NEWS.md: -------------------------------------------------------------------------------- 1 | # shredder 0.2.2 2 | 3 | * Precompile Vignettes 4 | 5 | # shredder 0.2.1 6 | 7 | * Update rstan_select to work with character inputs in addition to names and quasiquotations 8 | 9 | # shredder 0.2.0 10 | 11 | * Add stan_axe 12 | 13 | # shredder 0.1.1 14 | 15 | * Added a `NEWS.md` file to track changes to the package. 16 | * Replace stan_sample_* with stan_thin_* 17 | -------------------------------------------------------------------------------- /R/complete.R: -------------------------------------------------------------------------------- 1 | #borrowed main bits from https://github.com/jimhester/completeme (when it is put on CRAN will import directly from it) 2 | 3 | the <- new.env(parent = emptyenv()) 4 | the$completions <- list() 5 | complete_token <- get(".completeToken", asNamespace("utils")) 6 | 7 | # Register completion functions 8 | # 9 | # Completion functions should take one parameter `env`, the completion 10 | # environment, see `?rc.settings` for details of this environment. They should 11 | # simply return any completions found or `return(NULL)` otherwise. 12 | # 13 | # If all registered completions do not have any completions for a given 14 | # context, than R's standard completions are used. 15 | # @param ... One or more completion functions specified as named parameters. 16 | #' @importFrom utils modifyList 17 | register_completion <- function(...) { 18 | funs <- list(...) 19 | 20 | nms <- names(funs) 21 | if (is.null(nms) || any(nms == "" | is.na(nms))) { 22 | wch <- if (is.null(nms)) 1 else which(nms == "" | is.na(nms)) 23 | stop("All arguments must be named") 24 | } 25 | 26 | old <- the$completions 27 | the$completions <- utils::modifyList(the$completions, funs) 28 | 29 | invisible(old) 30 | } 31 | 32 | #' @importFrom utils rc.options 33 | completeme <- function(env) { 34 | env$fileName <- FALSE 35 | for (fun in the$completions) { 36 | env$comps <- fun(env) 37 | if (length(env$comps) > 0) { 38 | attributes(env$comps) <- list(class = "completions", type = 15) 39 | # this_type <- attr(env$comps, "type") %||% 15 40 | return(invisible(env$comps)) 41 | } 42 | } 43 | 44 | env$comps <- character() 45 | 46 | # if in the IDE, throw an error to fallback on normal completion 47 | if (identical(.Platform$GUI, "RStudio")) { 48 | stop("No custom completions") 49 | } 50 | 51 | # If on the command line, fall back to using the default completer 52 | on.exit(rc.options(custom.completer = completeme)) 53 | rc.options(custom.completer = NULL) 54 | complete_token() 55 | 56 | invisible(env$comps) 57 | } 58 | 59 | #' @importFrom rematch2 re_match 60 | current_function <- function(env) { 61 | buffer <- env[["linebuffer"]] 62 | fun <- rematch2::re_match(buffer, "(?[^[:space:](]+)[(][^(]*$")$fun 63 | if (is.na(fun)) { 64 | return("") 65 | } 66 | fun 67 | } 68 | 69 | populate <- function(env){ 70 | 71 | fun <- current_function(env) 72 | 73 | this_ns <- find_the() 74 | 75 | comp <- NULL 76 | 77 | if('shredder'%in%this_ns){ 78 | 79 | fun1 <- strsplit(fun,'%>%')[[1]] 80 | fun_tail <- fun1[length(fun1)] 81 | fun_head <- fun1[1] 82 | comp <- names(formals(fun_tail)) 83 | no_dots <- !grepl('...',comp,fixed = TRUE) 84 | comp[no_dots] <- sprintf('%s = ',comp[no_dots]) 85 | 86 | if(grepl('stan_(select|filter)$',fun_tail)){ 87 | 88 | object <- NULL 89 | 90 | if(fun_head%in%ls(envir = globalenv())) 91 | object <- get(fun_head,envir = globalenv()) 92 | 93 | if(inherits(object,'stanfit')){ 94 | 95 | comp <- union(object@sim$pars_oi,object@sim$fnames_oi) 96 | 97 | assign('pars',comp,envir = pars_env) 98 | 99 | } 100 | 101 | if(inherits(object,'brmsfit')){ 102 | 103 | comp <- union(object$fit@sim$pars_oi,object$fit@sim$fnames_oi) 104 | 105 | assign('pars',comp,envir = pars_env) 106 | 107 | } 108 | 109 | } 110 | 111 | } 112 | 113 | return(comp) 114 | 115 | } 116 | 117 | find_the <- function(){ 118 | names(which(sapply(loadedNamespaces(),function(x) any(grepl('^the$',ls(envir = asNamespace(x))))))) 119 | } 120 | -------------------------------------------------------------------------------- /R/partials.R: -------------------------------------------------------------------------------- 1 | grep_pars <- function (this, universe, ...) { 2 | grep(this, universe, ...) 3 | } 4 | 5 | #' @title Access stored pars for stan partials 6 | #' @description Access pars currently stored that [stan_contains][shredder::stan_contains] use. 7 | #' @return character 8 | #' @rdname peek_pars 9 | #' @export 10 | peek_pars <- function(){ 11 | pars_env$pars 12 | } 13 | 14 | #' @title Partial match functions for stanfit objects 15 | #' @description Partial match functions used in [stan_select][shredder::stan_select] 16 | #' @param match character, pattern to search for 17 | #' @param ignore.case logical to ignore the case, Default: TRUE 18 | #' @param pars list of pars to search in, Default: peek_pars() 19 | #' @examples 20 | #' \donttest{ 21 | #' rats <- rats_example() 22 | #' 23 | #' rats%>% 24 | #' stan_names() 25 | #' 26 | #' rats%>% 27 | #' stan_select(stan_contains('sq')) 28 | #' 29 | #' rats%>% 30 | #' stan_select(mu_alpha, stan_contains('sq')) 31 | #' 32 | #' 33 | #' rats%>% 34 | #' stan_select(stan_starts_with('mu')) 35 | #' 36 | #' rats%>% 37 | #' stan_select(stan_starts_with('mu'),stan_contains('sq')) 38 | #' 39 | #' rats%>% 40 | #' stan_select(stan_ends_with('0')) 41 | #' 42 | #' # free form regex in contains 43 | #' rats%>% 44 | #' stan_select(stan_contains('sq|mu')) 45 | #' } 46 | #' 47 | #' @return character 48 | #' @rdname stan_partials 49 | #' @family subsetting 50 | #' @export 51 | #' @importFrom rlang is_string 52 | stan_contains <- function (match, ignore.case = TRUE,pars = peek_pars()) { 53 | 54 | stopifnot(rlang::is_string(match), nchar(match) > 0) 55 | if (ignore.case) { 56 | pars <- tolower(pars) 57 | match <- tolower(match) 58 | } 59 | 60 | grep_pars(match, pars,value=TRUE) 61 | } 62 | 63 | #' @export 64 | #' @rdname stan_partials 65 | stan_starts_with <- function (match, ignore.case = TRUE,pars = peek_pars()) { 66 | 67 | stopifnot(rlang::is_string(match), nchar(match) > 0) 68 | if (ignore.case) { 69 | pars <- tolower(pars) 70 | match <- tolower(match) 71 | } 72 | 73 | match_list <- strsplit(match,'\\|')[[1]] 74 | 75 | match_chr <- paste0(sapply(match_list,function(x) sprintf("^(%s)", x),USE.NAMES = FALSE),collapse = '|') 76 | 77 | grep_pars(match_chr, pars,value=TRUE) 78 | } 79 | 80 | #' @export 81 | #' @rdname stan_partials 82 | stan_ends_with <- function (match, ignore.case = TRUE,pars = peek_pars()) { 83 | 84 | stopifnot(rlang::is_string(match), nchar(match) > 0) 85 | if (ignore.case) { 86 | pars <- tolower(pars) 87 | match <- tolower(match) 88 | } 89 | 90 | match_list <- strsplit(match,'\\|')[[1]] 91 | 92 | match_chr <- paste0(sapply(match_list,function(x) sprintf("(%s)$", x),USE.NAMES = FALSE),collapse = '|') 93 | 94 | grep_pars(match_chr, pars,value=TRUE) 95 | 96 | } 97 | -------------------------------------------------------------------------------- /R/rat_data-data.R: -------------------------------------------------------------------------------- 1 | #' @title Rats model data 2 | #' @description Data used in rats example model 3 | #' @format An object of class \code{"list"}: 4 | #' \describe{ 5 | #' \item{\code{N}}{integer Number of subjects} 6 | #' \item{\code{T}}{integer Number of measurements} 7 | #' \item{\code{x}}{numeric Day of measurement} 8 | #' \item{\code{Y}}{tibble Values of measurements} 9 | #' \item{\code{xbar}}{numeric Mean days of measurements} 10 | #'} 11 | #' @source [Data Description](https://www.mrc-bsu.cam.ac.uk/wp-content/uploads/WinBUGS_Vol1.pdf), 12 | #' [Data and Script](https://github.com/stan-dev/example-models/tree/master/bugs_examples/vol1/rats) 13 | #' @docType data 14 | #' @usage rat_data 15 | #' @name rat_data 16 | #' @family examples 17 | 'rat_data' 18 | 19 | globalVariables('rat_data') 20 | -------------------------------------------------------------------------------- /R/rats_example.R: -------------------------------------------------------------------------------- 1 | #' @title Rats model 2 | #' @description Run Rats data model 3 | #' @param seed numeric, seed for [rstan][rstan::stan], Default: 123 4 | #' @param nCores numeric, Maximum number of cores to use, Default: pmin(parallel::detectCores(),4) 5 | #' @return stanfit object 6 | #' @examples 7 | #' \donttest{ 8 | #' require(shredder) 9 | #' rats <- rats_example(nCores = 1) 10 | #' rats 11 | #' } 12 | #' @seealso 13 | #' [rstan][rstan::stan] 14 | #' [detectCores][parallel::detectCores] 15 | #' @rdname rats_example 16 | #' @family examples 17 | #' @source [Data Description](https://www.mrc-bsu.cam.ac.uk/wp-content/uploads/WinBUGS_Vol1.pdf), 18 | #' [Data and Script](https://github.com/stan-dev/example-models/tree/master/bugs_examples/vol1/rats) 19 | #' @export 20 | #' @importFrom rstan stan 21 | #' @importFrom parallel detectCores 22 | rats_example <- function(seed=123, nCores = pmin(parallel::detectCores(),4)){ 23 | 24 | if(exists('rats',envir = example_env)){ 25 | if(exists('seed',envir = example_env)){ 26 | if(seed==get('seed',envir = example_env)){ 27 | return(get('rats',envir = example_env)) 28 | } 29 | } 30 | } 31 | 32 | if(!('package:rstan' %in% search())) 33 | attachNamespace('rstan') 34 | 35 | ret <- rstan::stan( 36 | file = system.file('rats.stan',package='shredder'), 37 | data = rat_data, 38 | cores = nCores, 39 | seed = seed 40 | ) 41 | 42 | assign('rats',ret,envir = example_env) 43 | assign('seed',seed,envir = example_env) 44 | 45 | ret 46 | 47 | } 48 | 49 | example_env <- new.env() 50 | -------------------------------------------------------------------------------- /R/stan_axe.R: -------------------------------------------------------------------------------- 1 | #' @title Remove elements from stanfit object 2 | #' @description Safely remove large elements from stanfit objects that are not needed for postprocessing. 3 | #' @param object stanfit 4 | #' @param what name of attribute to remove, Default: c("summary", "fit_instance", "stanmodel") 5 | #' @details 6 | #' 7 | #' shredder can remove three elements in the `stanfit` object 8 | #' - The cached fit summary stored in fit@`.MISC`$summary 9 | #' - The cached cpp object stored in fit@.MISC$stan_fit_instance 10 | #' - The stanmodel stored in fit@stanmodel 11 | #' 12 | #' @return stanfit 13 | #' @rdname stan_axe 14 | #' @family utility 15 | #' @export 16 | #' @importFrom utils getFromNamespace 17 | stan_axe <- function(object, what = c('summary','fit_instance','stanmodel')){ 18 | what <- match.arg(what,c('summary','fit_instance','stanmodel')) 19 | utils::getFromNamespace(sprintf('clear_%s',what),asNamespace('shredder'))(object) 20 | } 21 | 22 | clear_summary <- function(object){ 23 | check_stanfit(object) 24 | if(exists('summary',envir = object@`.MISC`)) 25 | rm('summary',envir = object@`.MISC`) 26 | 27 | object 28 | } 29 | 30 | clear_fit_instance <- function(object){ 31 | 32 | check_stanfit(object) 33 | if(exists('stan_fit_instance',envir = object@`.MISC`)) 34 | rm('stan_fit_instance',envir = object@`.MISC`) 35 | 36 | object 37 | } 38 | 39 | clear_stanmodel <- function(object){ 40 | 41 | check_stanfit(object) 42 | attr(object,'stanmodel') <- NULL 43 | object 44 | 45 | } -------------------------------------------------------------------------------- /R/stan_filter.R: -------------------------------------------------------------------------------- 1 | #' @title Return post-warmup samples with matching conditions 2 | #' @description Use stan_filter() to choose indicies of samples across parameters 3 | #' where conditions are true. 4 | #' @param object stanfit object 5 | #' @param \dots Logical predicates defined in terms of the parameters in object 6 | #' @param permuted A logical scalar indicating whether the draws after the warmup 7 | #' period in each chain should be permuted and merged, Default: TRUE 8 | #' @return stanfit object 9 | #' @details 10 | #' 11 | #' - If no elements are returned for any chain then `NULL` is returned with a warning. 12 | #' - If there is a chain that results in no samples then the chain is dropped with a warning. 13 | #' - If permuted is FALSE then uneven chains may be returned depending on the result of the filter. 14 | #' - To comply with [extract][rstan::extract] with `permuted=TRUE` chains in which chaines are 15 | #' assumed to be of equal size. 16 | #' - Chain size returned is the length of the shortest filtered chain. 17 | #' 18 | #' @examples 19 | #' \donttest{ 20 | #' rats <- rats_example(nCores = 1) 21 | #' 22 | #' rats%>% 23 | #' stan_select(mu_alpha,mu_beta) 24 | #' 25 | #' rats%>% 26 | #' stan_select(mu_alpha,mu_beta)%>% 27 | #' stan_filter(mu_beta < 6) 28 | #' 29 | #' rats%>% 30 | #' stan_select(mu_alpha,mu_beta)%>% 31 | #' stan_filter(mu_beta < 6, permuted = FALSE) 32 | #' 33 | #' rats%>% 34 | #' stan_select(`alpha[1]`,`alpha[2]`,mu_alpha,mu_beta)%>% 35 | #' stan_filter(mu_beta < 6 & `alpha[1]` > 240) 36 | #' } 37 | #' @rdname stan_filter 38 | #' @family filtering 39 | #' @export 40 | #' @importFrom rlang quo_squash quo 41 | #' @importFrom stringi stri_extract_all_regex 42 | #' @importFrom purrr map_df map_dfc modify map map2 43 | stan_filter <- function(object, ..., permuted = TRUE){ 44 | UseMethod('stan_filter',object) 45 | } 46 | 47 | #' @export 48 | stan_filter.brmsfit <- function(object, ..., permuted = TRUE){ 49 | object$fit <- stan_filter(object$fit,..., permuted = permuted) 50 | object 51 | } 52 | 53 | #' @export 54 | stan_filter.stanfit <- function(object, ..., permuted = TRUE){ 55 | 56 | on.exit({clear_summary(object)},add = TRUE) 57 | 58 | warm_x <- seq_len(object@sim$warmup) 59 | iter_x <- seq_len(object@sim$iter)[-warm_x] 60 | 61 | squish <- rlang::quo_squash(rlang::quo(...)) 62 | squish_chr <- as.character(squish) 63 | squish_chr <- gsub('[`]','',squish_chr) 64 | 65 | model_names <- paste0(pars_regex(stan_names(object,expand = TRUE)),collapse = '|') 66 | 67 | idcs <- stringi::stri_extract_all_regex(squish_chr,model_names) 68 | idcs <- unique(unlist(idcs)) 69 | idcs <- idcs[!is.na(idcs)] 70 | 71 | if(!length(idcs)){ 72 | stop('Invalid parameter names selected\nUse stan_names(object) to list parameter names',call. = FALSE) 73 | } 74 | 75 | samp_df <- purrr::map_df(object@sim$samples, 76 | .f = function(X,idc, warm_x){ 77 | ret <- purrr::map_dfc(X[gsub('`','',idc)],identity) 78 | ret$n_ <- 1:nrow(ret) 79 | ret <- ret[-warm_x,] 80 | ret <- subset(ret,{eval(squish)}) 81 | ret 82 | },idc=idcs,warm_x = warm_x,.id = 'chain') 83 | 84 | 85 | if(!length(samp_df$n_)){ 86 | warning('filter returned no samples',call. = FALSE) 87 | return(NULL) 88 | } 89 | 90 | n_chain <- split(samp_df$n_, samp_df$chain) 91 | 92 | if(!identical(length(n_chain),length(chain_ids(object)))){ 93 | 94 | miss_id <- chain_ids(object)[!chain_ids(object)%in%as.numeric(names(n_chain))] 95 | 96 | warning(sprintf('filter returned no samples for chains: %s', 97 | paste0(miss_id,collapse = ', ') 98 | ),call. = FALSE) 99 | 100 | object <- stan_retain(object,as.numeric(names(n_chain))) 101 | } 102 | 103 | inits_x <- samp_df$n_ - length(warm_x) 104 | 105 | init_x_chain <- split(inits_x, samp_df$chain) 106 | 107 | if(permuted){ 108 | 109 | init_x_chain <- purrr::modify(init_x_chain,.f=function(x,m){ 110 | x[1:m] 111 | } , m = min(sapply(n_chain,length))) 112 | 113 | n_chain <- purrr::modify(n_chain,.f=function(x,m){ 114 | x[1:m] 115 | } , m = min(sapply(n_chain,length))) 116 | 117 | } 118 | 119 | samp_chain <- purrr::map(n_chain,.f = function(x,w) c(w,x), w = warm_x) 120 | 121 | iter_chain <- purrr::map(samp_chain,length) 122 | 123 | object@sim$iter <- length(warm_x) + mean(sapply(init_x_chain,length)) 124 | 125 | object@stan_args <- purrr::map2(object@stan_args,iter_chain,.f=function(x,i){ 126 | x$iter <- i 127 | x 128 | }) 129 | 130 | object@sim$permutation <- purrr::map2(object@sim$permutation,init_x_chain, .f = function(y,idx) y[idx]) 131 | 132 | object@sim$samples <- purrr::map2(object@sim$samples,samp_chain,stan_subset) 133 | 134 | object@sim$n_save <- as.numeric(iter_chain) 135 | 136 | object 137 | 138 | } 139 | -------------------------------------------------------------------------------- /R/stan_names.R: -------------------------------------------------------------------------------- 1 | #' @title The Par Names of a Stanfit Object 2 | #' @description Functions to get the names of a stanfit object. 3 | #' @param x a stanfit object 4 | #' @param expand logical, if TRUE par names are returned including dimesnion 5 | #' indicies (fnames_oi), Default: FALSE 6 | #' @examples 7 | #' \donttest{ 8 | #' rats <- rats_example(nCores = 1) 9 | #' 10 | #' rats%>%stan_names() 11 | #' 12 | #' rats%>%stan_names(expand = TRUE) 13 | #' } 14 | #' @return character 15 | #' @rdname stan_names 16 | #' @family utility 17 | #' @export 18 | stan_names <- function(x,expand = FALSE){ 19 | UseMethod('stan_names',x) 20 | } 21 | 22 | #' @export 23 | stan_names.brmsfit <- function(x,expand = FALSE){ 24 | stan_names(x$fit,expand=expand) 25 | } 26 | 27 | #' @export 28 | stan_names.stanfit <- function(x,expand = FALSE){ 29 | 30 | if(expand){ 31 | 32 | x@sim$fnames_oi 33 | 34 | }else{ 35 | 36 | # This is a problem sometimes 37 | #x@sim$pars_oi 38 | 39 | unique(gsub('\\[(.*?)$','',x@sim$fnames_oi)) 40 | } 41 | } -------------------------------------------------------------------------------- /R/stan_retain.R: -------------------------------------------------------------------------------- 1 | #' @title Return object with specific chains 2 | #' @description Use stan_retain() to choose chains to retain. 3 | #' @param object stanfit object 4 | #' @param chains numeric, chains to retain, Default: 1 5 | #' @return stanfit object 6 | #' @examples 7 | #' \donttest{ 8 | #' rats <- rats_example(nCores = 1) 9 | #' 10 | #' #retain first chain only 11 | #' rats%>% 12 | #' stan_retain() 13 | #' 14 | #' #retain chains 1 and 3 15 | #' rats%>% 16 | #' stan_retain(c(1,3)) 17 | #' } 18 | #' @rdname stan_retain 19 | #' @family utility 20 | #' @export 21 | stan_retain <- function(object,chains = 1){ 22 | UseMethod('stan_retain',object) 23 | } 24 | 25 | #' @export 26 | stan_retain.brmsfit <- function(object, chains = 1){ 27 | object$fit <- stan_retain(object$fit, chains = chains) 28 | object 29 | } 30 | 31 | #' @export 32 | stan_retain.stanfit <- function(object, chains = 1){ 33 | 34 | on.exit({clear_summary(object)},add = TRUE) 35 | 36 | if(is.null(chains)) 37 | return(object) 38 | 39 | if(all(chain_ids(object) %in% chains)) 40 | return(object) 41 | 42 | if(any(!(chains %in% chain_ids(object)))){ 43 | stop(sprintf( 44 | 'Invalid chains "%s", expected "%s"', 45 | paste0(chains,collapse = ', '), 46 | paste0(chain_ids(object),collapse = ', ') 47 | ), 48 | call. = FALSE) 49 | } 50 | 51 | 52 | object@sim$samples <- object@sim$samples[chains] 53 | object@sim$chains <- length(chains) 54 | object@sim$n_save <- object@sim$n_save[chains] 55 | object@sim$warmup2 <- object@sim$warmup2[chains] 56 | object@sim$permutation <- object@sim$permutation[chains] 57 | object@inits <- object@inits[chains] 58 | object@stan_args <- object@stan_args[chains] 59 | 60 | object 61 | } -------------------------------------------------------------------------------- /R/stan_select.R: -------------------------------------------------------------------------------- 1 | #' @title Select parameters by name 2 | #' @description Choose variables from a stanfit object 3 | #' @param object a stanfit object 4 | #' @param ... One or more unquoted expressions separated by commas 5 | #' @return stanfit object 6 | #' @examples 7 | #' \donttest{ 8 | #' rats <- rats_example(nCores = 1) 9 | #' 10 | #' rats%>%stan_select(mu_beta) 11 | #' 12 | #' rats%>%stan_select(mu_beta,beta) 13 | #' 14 | #' rats%>%stan_select('mu_beta') 15 | #' 16 | #' tmp <- c('mu_beta','mu_alpha') 17 | #' 18 | #' rats%>%stan_select(!!!rlang::syms(tmp)) 19 | #' } 20 | #' @seealso 21 | #' [quotation][rlang::quotation], [quo_label][rlang::quo_label], [map][purrr::map] 22 | #' @rdname stan_select 23 | #' @family subsetting 24 | #' @export 25 | #' @importFrom rlang enquos quo_text eval_tidy 26 | #' @importFrom purrr map 27 | stan_select <- function(object,...){ 28 | UseMethod('stan_select',object) 29 | } 30 | 31 | #' @export 32 | stan_select.brmsfit <- function(object,...){ 33 | object$fit <- stan_select(object$fit,...) 34 | object 35 | } 36 | 37 | #' @export 38 | stan_select.stanfit <- function(object, ...){ 39 | 40 | on.exit({clear_summary(object)},add = TRUE) 41 | 42 | all_pars <- union(object@sim$pars_oi,object@sim$fnames_oi) 43 | 44 | assign('pars',all_pars,envir = pars_env) 45 | 46 | pars <- unlist(lapply(rlang::enquos(...),pars_fun)) 47 | 48 | if(!length(pars)) 49 | return(message('no pars selected')) 50 | 51 | pars <- unique(pars) 52 | 53 | fnames_object <- find_fnames(object,pars) 54 | 55 | object <- fnames_object$object 56 | pars <- fnames_object$pars 57 | 58 | object@sim$pars_oi <- intersect(object@sim$pars_oi,pars) 59 | object@sim$dims_oi <- object@sim$dims_oi[object@sim$pars_oi] 60 | object@sim$n_flatnames <- length(object@sim$fnames_oi) 61 | 62 | object@model_pars <- intersect(object@model_pars,pars) 63 | 64 | object@inits <- purrr::map(object@inits,.f=function(x,y) x[y], y = object@model_pars) 65 | 66 | pars <- intersect(pars,names(object@par_dims)) 67 | 68 | if(!length(pars)){ 69 | return(message('No pars selected')) 70 | } 71 | 72 | object@par_dims <- object@par_dims[pars] 73 | 74 | this <- grep(paste0(pars,collapse = '|'),object@sim$fnames_oi,value = TRUE) 75 | 76 | new_samples <- purrr::map(object@sim$samples,.f=function(x,y) x[y], y = this) 77 | 78 | for(i in seq_len(length(new_samples))) 79 | attr(new_samples[[i]],'sampler_params') <- attr(object@sim$samples[[i]],'sampler_params') 80 | 81 | object@sim$samples <- new_samples 82 | 83 | object 84 | 85 | } 86 | 87 | pars_fun <- function(x){ 88 | 89 | ret <- rlang::quo_text(x) 90 | 91 | ret <- gsub('(^")|("$)','',ret) 92 | 93 | if(grepl('(stan_contains|stan_starts_with|stan_ends_with)',ret)){ 94 | ret <- rlang::eval_tidy(x) 95 | } 96 | 97 | # remove back ticks from templates of "`par[index]`" to be "par[index]" 98 | # this is needed for quot_text output 99 | ret <- sapply(ret,function(x){ 100 | if(grepl('(?=.*`)(?=.*\\[(.*?)\\])',x,perl = TRUE)){ 101 | x <- gsub('`','',x) 102 | } 103 | x 104 | }) 105 | 106 | ret 107 | } 108 | 109 | 110 | -------------------------------------------------------------------------------- /R/stan_slice.R: -------------------------------------------------------------------------------- 1 | #' @title Choose post-warumps samples by position 2 | #' @description Choose post-warumps samples by their ordinal 3 | #' position from each parameter in a stanfit object 4 | #' @param object stanfit object 5 | #' @param ... Integer samples values 6 | #' @param inc_warmup logical, include warmup in output, Default: TRUE 7 | #' @return stanfit object 8 | #' @examples 9 | #' \donttest{ 10 | #' rats <- rats_example(nCores = 1) 11 | #' 12 | #' rats%>% 13 | #' stan_select(mu_alpha)%>% 14 | #' stan_slice(1:30) 15 | #' 16 | #' rats%>% 17 | #' stan_select(mu_alpha)%>% 18 | #' stan_slice(1:30,inc_warmup = FALSE) 19 | #' } 20 | #' @rdname stan_slice 21 | #' @family filtering 22 | #' @export 23 | #' @importFrom purrr map 24 | stan_slice <- function(object,..., inc_warmup = TRUE){ 25 | UseMethod('stan_slice',object) 26 | } 27 | 28 | #' @export 29 | stan_slice.brmsfit <- function(object,..., inc_warmup = TRUE){ 30 | object$fit <- stan_slice(object$fit,...,inc_warmup = inc_warmup) 31 | object 32 | } 33 | 34 | #' @export 35 | stan_slice.stanfit <- function(object,..., inc_warmup = TRUE){ 36 | 37 | object <- clear_summary(object) 38 | 39 | dots_list <- list(...) 40 | 41 | if(!length(dots_list)){ 42 | return(object) 43 | } 44 | 45 | dots <- dots_list[[1]] 46 | 47 | warm_x <- seq_len(object@sim$warmup) 48 | iter_x <- seq_len(object@sim$iter)[-warm_x] 49 | inits_x <- iter_x[dots] - length(warm_x) 50 | 51 | check_dots <- which(length(iter_x)>=dots) 52 | 53 | if(length(check_dots)!=length(dots)){ 54 | warning('some indicies not in sample indicies of object, truncating the intersection') 55 | dots <- dots[check_dots] 56 | } 57 | 58 | if(inc_warmup){ 59 | 60 | samp <- c(warm_x,iter_x[dots]) 61 | object@sim$iter <- length(samp) 62 | 63 | }else{ 64 | 65 | samp <- iter_x[dots] 66 | 67 | object@sim$iter <- length(samp) 68 | object@sim$warmup <- 0 69 | object@sim$warmup2 <- rep(object@sim$warmup,object@sim$chains) 70 | 71 | } 72 | 73 | 74 | object@stan_args <- purrr::map(object@stan_args,.f=function(x,i,inc_warmup){ 75 | x$iter <- i 76 | 77 | if(!inc_warmup) 78 | x$warmup <- 0 79 | 80 | x 81 | },i = object@sim$iter,inc_warmup = inc_warmup) 82 | 83 | object@inits <- purrr::map(object@inits,stan_trim_postwarm,idx=inits_x) 84 | 85 | object <- reset_perm(object,inits_x) 86 | 87 | object@sim$samples <- purrr::map(object@sim$samples,stan_subset,idx=samp) 88 | object@sim$n_save <- rep(object@sim$iter,length(object@sim$n_save)) 89 | 90 | object 91 | 92 | } 93 | 94 | -------------------------------------------------------------------------------- /R/stan_thin.R: -------------------------------------------------------------------------------- 1 | #' @title thin n post-warumps samples from a stanfit object 2 | #' @description This is a wrapper around stan_slice() to make it easy to 3 | #' thin samples from each parameter in a stanfit object. 4 | #' @param object stanfit object 5 | #' @param size numeric, for [stan_thin_n][shredder::stan_thin_n] size of thin, for 6 | #' [stan_thin_frac][shredder::stan_thin_frac] fraction of samples to sample. 7 | #' @param inc_warmup logical, include warmup in output, Default: TRUE 8 | #' @return stanfit 9 | #' @examples 10 | #' \donttest{ 11 | #' rats <- rats_example(nCores = 1) 12 | #' 13 | #' rats%>% 14 | #' stan_select(mu_alpha)%>% 15 | #' stan_thin_n(30) 16 | #' 17 | #' rats%>% 18 | #' stan_select(mu_alpha)%>% 19 | #' stan_thin_frac(0.5) 20 | #' 21 | #' rats%>% 22 | #' stan_select(mu_alpha)%>% 23 | #' stan_thin_n(30,inc_warmup = FALSE) 24 | #' } 25 | #' @rdname stan_thin 26 | #' @family filtering 27 | #' @export 28 | stan_thin_n <- function(object, size, inc_warmup = TRUE){ 29 | UseMethod('stan_thin_n',object) 30 | } 31 | 32 | #' @rdname stan_thin 33 | #' @export 34 | stan_thin_frac <- function(object, size, inc_warmup = TRUE){ 35 | UseMethod('stan_thin_frac',object) 36 | } 37 | 38 | #' @export 39 | stan_thin_n.brmsfit <- function(object, size, inc_warmup = TRUE){ 40 | n_iter <- object$fit@sim$iter - object$fit@sim$warmup 41 | seq_ <- seq(1,object@sim$iter,by = size) 42 | stan_slice(object, seq_, inc_warmup = inc_warmup) 43 | } 44 | 45 | #' @export 46 | stan_thin_n.stanfit <- function(object, size, inc_warmup = TRUE){ 47 | n_iter <- object@sim$iter - object@sim$warmup 48 | seq_ <- seq(1,n_iter,by = size) 49 | stan_slice(object, seq_, inc_warmup = inc_warmup) 50 | } 51 | 52 | #' @export 53 | stan_thin_frac.brmsfit <- function(object, size, inc_warmup = TRUE){ 54 | n_iter <- object$fit@sim$iter - object$fit@sim$warmup 55 | seq_ <- floor(seq(1,n_iter,length.out = floor(n_iter*size))) 56 | stan_slice(object,seq_, inc_warmup = inc_warmup) 57 | } 58 | 59 | #' @export 60 | stan_thin_frac.stanfit <- function(object, size, inc_warmup = TRUE){ 61 | n_iter <- object@sim$iter - object@sim$warmup 62 | seq_ <- floor(seq(1,n_iter,length.out = floor(n_iter*size))) 63 | stan_slice(object, seq_, inc_warmup = inc_warmup) 64 | } -------------------------------------------------------------------------------- /R/stan_utils.R: -------------------------------------------------------------------------------- 1 | #' @importFrom purrr map 2 | stan_subset <- function(x,idx){ 3 | 4 | obj <- attr(x,"sampler_params") 5 | 6 | x <- purrr::map(x,.f=function(y,idx) y[idx],idx=idx) 7 | 8 | sub_obj <- purrr::map(obj,.f=function(y,idx) y[idx],idx=idx) 9 | attr(x,"sampler_params") <- sub_obj 10 | 11 | x 12 | 13 | } 14 | 15 | #' @importFrom purrr map 16 | stan_trim_postwarm <- function(x,idx){ 17 | 18 | purrr::map(x,.f = function(y,idx) y[idx] , idx = idx) 19 | 20 | } 21 | 22 | #' @importFrom purrr rerun 23 | reset_perm <- function(object,inits_x){ 24 | check_stanfit(object) 25 | nx <- length(inits_x) 26 | nc <- length(object@sim$permutation) 27 | object@sim$permutation <- purrr::rerun(nc,sample(seq_len(nx),size = nx)) 28 | object 29 | } 30 | 31 | find_fnames <- function(object,pars){ 32 | check_stanfit(object) 33 | f_names <- intersect(object@sim$fnames_oi,grep('\\[',pars,value = TRUE)) 34 | 35 | if(length(f_names)){ 36 | 37 | oi_names <- unique(gsub('\\[(.*?)$','',f_names)) 38 | pars <- union(setdiff(pars,f_names),oi_names) 39 | 40 | f_names_keep <- grep_ext(pars,object@sim$fnames_oi,value = TRUE) 41 | f_names_l <- grepl_ext(oi_names,f_names_keep) 42 | 43 | object@sim$fnames_oi <- c(intersect(f_names_keep[f_names_l],f_names), 44 | f_names_keep[!f_names_l]) 45 | 46 | for(nm in oi_names){ 47 | object@sim$dims_oi[nm] <- length(grep(nm,f_names)) 48 | object@par_dims[nm] <- object@sim$dims_oi[nm] 49 | } 50 | 51 | 52 | }else{ 53 | 54 | object@sim$fnames_oi <- grep(sprintf('^(%s)',paste0(pars,collapse = '|')),object@sim$fnames_oi,value = TRUE) 55 | 56 | } 57 | 58 | list(object = object, pars = pars) 59 | } 60 | 61 | grep_ext <- function(pattern,x,...){ 62 | grep(sprintf('^(%s)',paste0(sprintf('\\b%s\\b',pattern),collapse = '|')),x = x,...) 63 | } 64 | 65 | grepl_ext <- function(pattern,x,...){ 66 | grepl(sprintf('^(%s)',paste0(sprintf('\\b%s\\b',pattern),collapse = '|')),x = x,...) 67 | } 68 | 69 | chain_ids <- function(object){ 70 | check_stanfit(object) 71 | sapply(object@stan_args, function(x) x$chain_id) 72 | } -------------------------------------------------------------------------------- /R/utils.R: -------------------------------------------------------------------------------- 1 | #' Pipe operator imported from purrr 2 | #' 3 | #' @name %>% 4 | #' @rdname pipe 5 | #' @keywords internal 6 | #' @export 7 | #' @importFrom purrr %>% 8 | #' @usage lhs \%>\% rhs 9 | NULL 10 | 11 | #' @importFrom purrr %||% 12 | #' @name %||% 13 | #' @rdname pipe 14 | #' @usage x \%||\% y 15 | #' @export 16 | NULL 17 | 18 | pars_env <- new.env() 19 | 20 | #' @importFrom checkmate assert checkClass 21 | check_stanfit <- function(x){ 22 | checkmate::assert( 23 | checkmate::checkClass(x, "stanfit") 24 | ) 25 | } 26 | 27 | #' @importFrom checkmate assert checkClass 28 | check_brms <- function(x){ 29 | checkmate::assert( 30 | checkmate::checkClass(x, "brmsfit") 31 | ) 32 | } 33 | 34 | pars_regex <- function(x){ 35 | x <- gsub('\\[','\\\\[',x) 36 | x <- gsub('\\]','\\\\]',x) 37 | x 38 | } 39 | -------------------------------------------------------------------------------- /R/zzz.R: -------------------------------------------------------------------------------- 1 | .onLoad <- function(lib,pkg) { 2 | rc.options(custom.completer = completeme) 3 | register_completion(thispkg = populate) 4 | } 5 | 6 | .onAttach <- function(lib,pkg) { 7 | rc.options(custom.completer = completeme) 8 | register_completion(thispkg = populate) 9 | } 10 | -------------------------------------------------------------------------------- /README.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | output: github_document 3 | --- 4 | 5 | 6 | 7 | ```{r, include = FALSE} 8 | knitr::opts_chunk$set( 9 | collapse = TRUE, 10 | comment = "#>", 11 | fig.path = "man/figures/README-", 12 | out.width = "100%" 13 | ) 14 | ``` 15 | 16 |
17 | 18 | # shredder 19 | 20 | 21 | [![R-win build status](https://github.com/yonicd/shredder/workflows/R-win/badge.svg)](https://github.com/yonicd/shredder) 22 | [![R-mac build status](https://github.com/yonicd/shredder/workflows/R-mac/badge.svg)](https://github.com/yonicd/shredder) 23 | [![R-linux build status](https://github.com/yonicd/shredder/workflows/R-linux/badge.svg)](https://github.com/yonicd/shredder) 24 | [![Codecov test coverage](https://codecov.io/gh/yonicd/shredder/branch/master/graph/badge.svg)](https://codecov.io/gh/yonicd/shredder?branch=master) 25 | [![Covrpage Summary](https://img.shields.io/badge/covrpage-Last_Build_2020_06_17-brightgreen.svg)](http://tinyurl.com/s3fr6gn) 26 | [![Lifecycle: maturing](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://www.tidyverse.org/lifecycle/#maturing) 27 | 28 | 29 | The goal of __shredder__ is to create an API that incites exploration and iteration of rstan simulation objects. 30 | 31 | - With this API users can: 32 | - Manipulate fit objects without needing to convert arrays into rectangular form. 33 | - Generate task specifc subsets of the parent fit object for fit diagnostics and post-processing 34 | - Use pipe operators to create more user-friendly workflows 35 | - Manipulate various classes that contain a `stanfit` object, such as `rstan` and `brms`. 36 | - RStudio specific feature: Tab autocomplete in `shredder::stan_select` and `shredder::stan_filter` for the parameter names stored in the fit object. 37 | 38 | ## Installation 39 | 40 | ``` r 41 | remotes::install_github('yonicd/shredder') 42 | ``` 43 | 44 | ## Current API Verbs 45 | 46 | - chains 47 | - `shredder::stan_retain` : extract specific chains 48 | - pars 49 | - `shredder::stan_select` : extract specific pars 50 | - `shredder::stan_contains`, `shredder::stan_starts_with`,`shredder::stan_ends_with`: 51 | partial par extractions (used within `shredder::stan_select`) 52 | - `shredder::stan_names` : return names within the stanfit object 53 | - post-warmup samples 54 | - `shredder::stan_slice` : extract specific samples by index 55 | - `shredder::stan_thin_n` : Thin (remove) every n samples from the total samples 56 | - `shredder::stan_thin_frac` : Thin (remove) a fraction of total samples. 57 | - `shredder::stan_filter` : extract subset of samples conditional on 58 | filter of parameter values 59 | - cached elements 60 | - `shredder::stan_axe` : remove cached elements for stanfit objects to reduce memory usage 61 | 62 | ## Code of Conduct 63 | 64 | Please note that the 'shredder' project is released with a [Contributor Code of Conduct](CODE_OF_CONDUCT.md). 65 | By contributing to this project, you agree to abide by its terms. 66 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 | 6 | # shredder 7 | 8 | 9 | 10 | [![R-win build 11 | status](https://github.com/yonicd/shredder/workflows/R-win/badge.svg)](https://github.com/yonicd/shredder) 12 | [![R-mac build 13 | status](https://github.com/yonicd/shredder/workflows/R-mac/badge.svg)](https://github.com/yonicd/shredder) 14 | [![R-linux build 15 | status](https://github.com/yonicd/shredder/workflows/R-linux/badge.svg)](https://github.com/yonicd/shredder) 16 | [![Codecov test 17 | coverage](https://codecov.io/gh/yonicd/shredder/branch/master/graph/badge.svg)](https://codecov.io/gh/yonicd/shredder?branch=master) 18 | [![Covrpage 19 | Summary](https://img.shields.io/badge/covrpage-Last_Build_2020_06_17-brightgreen.svg)](http://tinyurl.com/s3fr6gn) 20 | [![Lifecycle: 21 | maturing](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://www.tidyverse.org/lifecycle/#maturing) 22 | 23 | 24 | The goal of **shredder** is to create an API that incites exploration 25 | and iteration of rstan simulation objects. 26 | 27 | - With this API users can: 28 | - Manipulate fit objects without needing to convert arrays into 29 | rectangular form. 30 | - Generate task specifc subsets of the parent fit object for fit 31 | diagnostics and post-processing 32 | - Use pipe operators to create more user-friendly workflows 33 | - Manipulate various classes that contain a `stanfit` object, such 34 | as `rstan` and `brms`. 35 | - RStudio specific feature: Tab autocomplete in 36 | `shredder::stan_select` and `shredder::stan_filter` for the 37 | parameter names stored in the fit object. 38 | 39 | ## Installation 40 | 41 | ``` r 42 | remotes::install_github('yonicd/shredder') 43 | ``` 44 | 45 | ## Current API Verbs 46 | 47 | - chains 48 | - `shredder::stan_retain` : extract specific chains 49 | - pars 50 | - `shredder::stan_select` : extract specific pars 51 | - `shredder::stan_contains`, 52 | `shredder::stan_starts_with`,`shredder::stan_ends_with`: partial 53 | par extractions (used within `shredder::stan_select`) 54 | - `shredder::stan_names` : return names within the stanfit object 55 | - post-warmup samples 56 | - `shredder::stan_slice` : extract specific samples by index 57 | - `shredder::stan_thin_n` : Thin (remove) every n samples from the 58 | total samples 59 | - `shredder::stan_thin_frac` : Thin (remove) a fraction of total 60 | samples. 61 | - `shredder::stan_filter` : extract subset of samples conditional 62 | on filter of parameter values 63 | - cached elements 64 | - `shredder::stan_axe` : remove cached elements for stanfit 65 | objects to reduce memory usage 66 | 67 | ## Code of Conduct 68 | 69 | Please note that the ‘shredder’ project is released with a [Contributor 70 | Code of Conduct](CODE_OF_CONDUCT.md). By contributing to this project, 71 | you agree to abide by its terms. 72 | -------------------------------------------------------------------------------- /_pkgdown.yml: -------------------------------------------------------------------------------- 1 | title: shredder 2 | 3 | destination: docs 4 | 5 | development: 6 | mode: auto 7 | 8 | navbar: 9 | structure: 10 | left: [home, reference, articleindex, news] 11 | right: [github, twitter] 12 | components: 13 | articles: ~ 14 | articleindex: 15 | text: Articles 16 | href: articles/index.html 17 | twitter: 18 | icon: fa-lg fa-twitter 19 | href: http://twitter.com/yoniceedee 20 | 21 | reference: 22 | - title: "Selecting Parameters" 23 | contents: 24 | - has_concept("subsetting") 25 | - title: "Filtering Post-Warmup Samples" 26 | contents: 27 | - has_concept("filtering") 28 | - title: "Shredder Utilities" 29 | contents: 30 | - has_concept("utility") 31 | - title: "Examples" 32 | contents: 33 | - has_concept("examples") 34 | articles: 35 | - title: "Usage" 36 | contents: 37 | - rstan 38 | - brms 39 | - title: "Utility" 40 | contents: 41 | - axe 42 | - title: "Extending" 43 | contents: 44 | - tidybayes 45 | - title: "Benchmarking" 46 | contents: 47 | - benchmark 48 | - title: "Unit Tests" 49 | contents: 50 | - tests_and_coverage 51 | template: 52 | params: 53 | bootswatch: flatly 54 | hexURL: 55 | https://github.com/yonicd/shredder/blob/master/man/figures/logo_ribbon.png?raw=true 56 | -------------------------------------------------------------------------------- /codecov.yml: -------------------------------------------------------------------------------- 1 | comment: false 2 | 3 | coverage: 4 | status: 5 | project: 6 | default: 7 | target: auto 8 | threshold: 1% 9 | patch: 10 | default: 11 | target: auto 12 | threshold: 1% 13 | -------------------------------------------------------------------------------- /data-raw/3989510611_e88c931f7d_h.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yonicd/shredder/5eacd3280de8ef8c441255f42403f78d1eab8e3e/data-raw/3989510611_e88c931f7d_h.jpg -------------------------------------------------------------------------------- /data-raw/app/global.R: -------------------------------------------------------------------------------- 1 | library(magrittr) 2 | library(patchwork) 3 | library(rstan) 4 | library(shredder) 5 | -------------------------------------------------------------------------------- /data-raw/app/server.R: -------------------------------------------------------------------------------- 1 | shinyServer(function(input, output, session) { 2 | 3 | shiny::observeEvent(c(input$params),{ 4 | this <- fit() 5 | 6 | output$diagplot <- shiny::renderPlot({ 7 | rhs <- list(rstan::stan_rhat(this(),pars=input$params), 8 | rstan::stan_mcse(this(),pars=input$params), 9 | rstan::stan_ess(this(),pars=input$params))%>% 10 | purrr::reduce(`/`) 11 | 12 | lhs <- rstan::traceplot(this(),pars=input$params) 13 | 14 | lhs | rhs 15 | 16 | }) 17 | 18 | }) 19 | 20 | shiny::observeEvent(c(input$par),{ 21 | output$parplot <- shiny::renderPlot({ 22 | this <- fit() 23 | rstan::stan_par(this(),par=input$par) 24 | }) 25 | }) 26 | 27 | fit <- shiny::eventReactive(input$path,{ 28 | shiny::reactivePoll( 29 | 1000, session, 30 | # This function returns the time that log_file was last modified 31 | checkFunc = function() { 32 | f <- list.files(file.path(input$path),full.names = TRUE) 33 | if (length(f)>0) 34 | max(sapply(f,function(x) file.info(x)[['mtime']])) 35 | else 36 | "" 37 | }, 38 | # This function returns the content of log_file 39 | valueFunc = function() { 40 | fit <- rstan::read_stan_csv(list.files('../test',full.names = TRUE,pattern = 'csv$')) 41 | 42 | fit@sim$samples <- purrr::map(fit@sim$samples,function(x,y){ 43 | names(x) <- y 44 | x 45 | },y = fit@sim$fnames_oi) 46 | 47 | fit@stan_args <- purrr::map(fit@stan_args,function(x){ 48 | x$method <- 'sampling' 49 | x 50 | }) 51 | 52 | fit 53 | 54 | } 55 | ) 56 | }) 57 | 58 | observeEvent(input$path,{ 59 | this <- fit() 60 | params <- shredder::stan_names(this(),expand = TRUE) 61 | output$params <- renderUI({ 62 | shiny::selectInput( 63 | inputId = 'params', 64 | label = 'Select Parameter', 65 | choices = params, 66 | selected = 'lp__',multiple = TRUE,selectize = TRUE 67 | ) 68 | }) 69 | 70 | output$par <- renderUI({ 71 | shiny::selectInput( 72 | inputId = 'par', 73 | label = 'Select Par Parameter', 74 | choices = params[-length(params)], 75 | selected = params[1],multiple = FALSE,selectize = TRUE 76 | ) 77 | }) 78 | 79 | }) 80 | 81 | shiny::observeEvent(input$qt, { 82 | shiny::stopApp() 83 | }) 84 | 85 | }) 86 | -------------------------------------------------------------------------------- /data-raw/app/ui.R: -------------------------------------------------------------------------------- 1 | miniUI::miniPage( 2 | miniUI::gadgetTitleBar( 3 | title = 'Stan Tracker', 4 | left = miniUI::miniTitleBarButton("qt", "Quit") 5 | ), 6 | miniUI::miniContentPanel( 7 | shiny::sidebarLayout( 8 | sidebarPanel = shiny::sidebarPanel( 9 | shiny::textInput('path','Project Path',value = '../test',placeholder = 'Enter Path'), 10 | shiny::textInput('root','CSV root','samp',placeholder = 'Enter csv root'), 11 | shiny::checkboxInput('diag_check','Show Diagnostics',value = TRUE), 12 | shiny::uiOutput('params'), 13 | shiny::checkboxInput('par_check','Show Par Plot',value = FALSE), 14 | shiny::conditionalPanel('input.par_check',{ 15 | shiny::uiOutput('par') 16 | }), 17 | width = 3 18 | ), 19 | mainPanel = shiny::mainPanel( 20 | shiny::conditionalPanel('input.diag_check',{ 21 | shiny::plotOutput('diagplot',height = 400) 22 | }), 23 | shiny::conditionalPanel('input.par_check',{ 24 | shiny::plotOutput('parplot',height = 400) 25 | }), 26 | width = 9 27 | ) 28 | ) 29 | ) 30 | ) -------------------------------------------------------------------------------- /data-raw/dot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yonicd/shredder/5eacd3280de8ef8c441255f42403f78d1eab8e3e/data-raw/dot.png -------------------------------------------------------------------------------- /data-raw/figure6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yonicd/shredder/5eacd3280de8ef8c441255f42403f78d1eab8e3e/data-raw/figure6.png -------------------------------------------------------------------------------- /data-raw/hiclipart.com.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yonicd/shredder/5eacd3280de8ef8c441255f42403f78d1eab8e3e/data-raw/hiclipart.com.png -------------------------------------------------------------------------------- /data-raw/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yonicd/shredder/5eacd3280de8ef8c441255f42403f78d1eab8e3e/data-raw/logo.png -------------------------------------------------------------------------------- /data-raw/logo_dot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yonicd/shredder/5eacd3280de8ef8c441255f42403f78d1eab8e3e/data-raw/logo_dot.png -------------------------------------------------------------------------------- /data-raw/make_hex.R: -------------------------------------------------------------------------------- 1 | library(magick) 2 | library(bunny) 3 | 4 | girl <- image_read("data-raw/3989510611_e88c931f7d_h.jpg") %>% 5 | image_crop(geometry = "700x1191+0+0") %>% 6 | image_trim() %>% 7 | image_scale("50%") 8 | 9 | string <- image_read("data-raw/3989510611_e88c931f7d_h.jpg") %>% 10 | image_crop(geometry = "1220x150+0-310") %>% 11 | image_trim() %>% 12 | image_scale("50%") 13 | 14 | mcmc_plot <- image_read("data-raw/figure6.png") %>% 15 | image_crop("510x300+130+100") %>% 16 | image_scale(geometry_size_percent(100, 200)) %>% 17 | image_transparent("white", fuzz = 40) 18 | 19 | 20 | ribbon <- image_read('data-raw/hiclipart.com.png')%>% 21 | image_scale("25%") 22 | 23 | dot <- image_read("data-raw/dot.png") 24 | 25 | dot_art <- dot%>% 26 | image_flatten() %>% 27 | image_compose(image_canvas(dot, pseudo_image = "gradient:black-white"), 28 | "CopyOpacity") %>% 29 | magick::image_rotate(degrees = 40)%>% 30 | image_scale("30%") 31 | 32 | bg_color <- "#ede6f2" 33 | bg_color2 <- "#f6f3f9" 34 | fg_color <- "#042a2b" 35 | fgd_color1 <- "#772e25" 36 | fgd_color2 <- "#411815" 37 | 38 | shred_hex <- 39 | image_canvas_hex(fill_color = bg_color, border_color = fg_color, border_size = 5) %>% 40 | image_composite(image_blank(460, 660, "white"), gravity = "center",offset = "+0+100") %>% 41 | image_composite(image_blank(460, 150, bg_color), gravity = "north", offset = "+0-750") %>% 42 | image_composite(girl, gravity = "west", offset = "-700+270") %>% 43 | #image_composite(dot_art, gravity = "north", offset = "-130+900") %>% 44 | image_composite(ribbon, gravity = "north", offset = "-100+950") %>% 45 | image_composite(mcmc_plot, gravity = "center", offset = "+0+370") %>% 46 | image_composite(image_blur(image_border(image_blank(420, 620, "transparent"), 47 | color="grey50", geometry = "60x60"), 48 | radius = 60, sigma = 40), 49 | gravity = "center",offset = "-10+110") %>% 50 | image_composite(image_border(image_blank(470, 670, "transparent"), 51 | color=fgd_color2, geometry = "60x60"), 52 | gravity = "center",offset = "+0+100") %>% 53 | image_composite(image_border(image_blank(500, 700, "transparent"), 54 | color=fgd_color1, geometry = "10x10"), 55 | gravity = "center",offset = "+0+100") %>% 56 | image_annotate("shredder", gravity = "center", location = "+0-400", 57 | size=200, font="Aller", color = fg_color, weight = 400) 58 | 59 | shred_hex %>% 60 | image_scale("1200x1200") %>% 61 | image_write("data-raw/shred_hex_ribbon.png", density = 600) 62 | 63 | shred_hex %>% 64 | image_scale("200x200") %>% 65 | image_write("man/figures/logo_ribbon.png", density = 600) 66 | 67 | shred_hex_gh <- shred_hex %>% 68 | image_scale("400x400") 69 | 70 | gh_logo <- bunny::github %>% 71 | image_scale("40x40") 72 | 73 | shred_ghcard <- image_canvas_ghcard(fill_color = bg_color2) %>% 74 | image_composite(shred_hex_gh, gravity = "East", offset = "+100+0") %>% 75 | image_annotate("Stan ", gravity = "West", location = "+60-30", 76 | style = "italic", color=fg_color, size=65, font="Volkhov", weight = 700) %>% 77 | image_annotate("models. Sliced.", gravity = "West", location = "+210-30", 78 | color=fg_color, size=60, font="Volkhov", weight = 500) %>% 79 | image_compose(gh_logo, gravity="West", offset = "+60+40") %>% 80 | image_annotate("metrumresearchgroup/shredder", gravity="West", 81 | location="+110+45", size=38, font="Ubuntu Mono") %>% 82 | image_border_ghcard(bg_color2) 83 | 84 | shred_ghcard %>% 85 | image_write("data-raw/shred_ribbon_ghcard.png", density = 600) 86 | 87 | 88 | -------------------------------------------------------------------------------- /data-raw/shred_dot_ghcard.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yonicd/shredder/5eacd3280de8ef8c441255f42403f78d1eab8e3e/data-raw/shred_dot_ghcard.png -------------------------------------------------------------------------------- /data-raw/shred_ghcard.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yonicd/shredder/5eacd3280de8ef8c441255f42403f78d1eab8e3e/data-raw/shred_ghcard.png -------------------------------------------------------------------------------- /data-raw/shred_hex.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yonicd/shredder/5eacd3280de8ef8c441255f42403f78d1eab8e3e/data-raw/shred_hex.png -------------------------------------------------------------------------------- /data-raw/shred_hex_dot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yonicd/shredder/5eacd3280de8ef8c441255f42403f78d1eab8e3e/data-raw/shred_hex_dot.png -------------------------------------------------------------------------------- /data-raw/shred_hex_ribbon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yonicd/shredder/5eacd3280de8ef8c441255f42403f78d1eab8e3e/data-raw/shred_hex_ribbon.png -------------------------------------------------------------------------------- /data-raw/shred_ribbon_ghcard.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yonicd/shredder/5eacd3280de8ef8c441255f42403f78d1eab8e3e/data-raw/shred_ribbon_ghcard.png -------------------------------------------------------------------------------- /data-raw/stan_filter_.R: -------------------------------------------------------------------------------- 1 | # This breaks rstan::extract because of assumption of equal sized iterations 2 | # in chains in rstan summary method. 3 | # https://github.com/stan-dev/rstan/blob/develop/rstan/rstan/R/stanfit-class.R#L487 4 | stan_filter_ <- function(object, ..., chains = NULL){ 5 | 6 | chains_ <- seq_len(object@sim$chains) 7 | 8 | if(is.null(chains)){ 9 | 10 | chains <- chains_ 11 | 12 | }else{ 13 | 14 | length(setdiff(chains,chains_)>0) 15 | stop('Invalid Chains Specified',call. = FALSE) 16 | 17 | } 18 | 19 | warm_x <- seq_len(object@sim$warmup) 20 | iter_x <- seq_len(object@sim$iter)[-warm_x] 21 | 22 | squish <- rlang::quo_squash(rlang::quo(...)) 23 | 24 | idcs_fnames_oi <- stringi::stri_extract_all_regex(as.character(squish),pattern = '`(.*?)`')%>% 25 | purrr::discard(is.na)%>% 26 | purrr::flatten_chr() 27 | 28 | idcs_pars_oi <- intersect(as.character(squish),stan_names(object)) 29 | 30 | idcs <- c(idcs_pars_oi,idcs_fnames_oi) 31 | 32 | if(!length(idcs)){ 33 | stop('Invalid paratameter names selected\nUse stan_names(object) to list parameter names',call. = FALSE) 34 | } 35 | 36 | samp_df <- purrr::map_df(object@sim$samples[chains], 37 | .f = function(X,idc, warm_x){ 38 | ret <- purrr::map_dfc(X[gsub('`','',idc)],identity) 39 | ret$n_ <- 1:nrow(ret) 40 | ret <- ret[-warm_x,] 41 | ret <- ret%>%subset({eval(squish)}) 42 | ret 43 | },idc=idcs,warm_x = warm_x,.id = 'chain') 44 | 45 | 46 | if(!length(samp_df$n_)){ 47 | warning('filter returned no samples',call. = FALSE) 48 | return(object) 49 | } 50 | 51 | n_chain <- split(samp_df$n_, samp_df$chain) 52 | 53 | if(!identical(length(n_chain),length(shredder:::chain_ids(object)))){ 54 | miss_id <- shredder:::chain_ids(object)[!shredder:::chain_ids(object)%in%as.numeric(names(n_chain))] 55 | 56 | warning(sprintf('filter returned no samples for chains: %s', 57 | paste0(miss_id,collapse = ', ') 58 | ),call. = FALSE) 59 | 60 | object <- stan_retain(object,as.numeric(names(n_chain))) 61 | } 62 | 63 | inits_x <- samp_df$n_ - length(warm_x) 64 | 65 | init_x_chain <- split(inits_x, samp_df$chain) 66 | 67 | init_x_chain <- purrr::modify(init_x_chain,.f=function(x,m){ 68 | x[1:m] 69 | } , m = min(sapply(n_chain,length))) 70 | 71 | n_chain <- purrr::modify(n_chain,.f=function(x,m){ 72 | x[1:m] 73 | } , m = min(sapply(n_chain,length))) 74 | 75 | samp_chain <- purrr::map(n_chain,.f = function(x,w) c(w,x), w = warm_x) 76 | 77 | iter_chain <- purrr::map(samp_chain,length) 78 | 79 | object@sim$iter <- unique(sapply(samp_chain,length)) 80 | 81 | object@stan_args <- purrr::map2(object@stan_args,iter_chain,.f=function(x,i){ 82 | x$iter <- i 83 | x 84 | }) 85 | 86 | #object@inits <- purrr::map2(object@inits,init_x_chain, shredder:::stan_trim_postwarm) 87 | 88 | object@sim$permutation <- purrr::map2(object@sim$permutation,init_x_chain, .f = function(y,idx) y[idx]) 89 | 90 | object@sim$samples <- purrr::map2(object@sim$samples,samp_chain,shredder:::stan_subset) 91 | 92 | #as.numeric(length(warm_x) + purrr::map_dbl(n_chain,length)) 93 | object@sim$n_save <- as.numeric(iter_chain) 94 | 95 | object 96 | 97 | } 98 | -------------------------------------------------------------------------------- /data-raw/stan_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yonicd/shredder/5eacd3280de8ef8c441255f42403f78d1eab8e3e/data-raw/stan_logo.png -------------------------------------------------------------------------------- /data-raw/stan_sample.R: -------------------------------------------------------------------------------- 1 | # @title Sample n post-warumps samples from a stanfit object 2 | # @description This is a wrapper around sample() to make it easy to select 3 | # random samples from each parameter in a stanfit object. 4 | # @param object stanfit object 5 | # @param size numeric, for [shredder][stan_sample_n] size of sample, for 6 | # [shredder][stan_sample_frac] fraction of samples to sample. 7 | # @param weight, a vector of probability weights for obtaining the elements of 8 | # the vector being sampled. 9 | # @param inc_warmup logical, include warmup in output, Default: TRUE 10 | # @return stanfit 11 | # @examples 12 | # rats <- rats_example(nCores = 1) 13 | # 14 | # rats%>% 15 | # stan_select(mu_alpha)%>% 16 | # stan_sample_n(30) 17 | # 18 | # rats%>% 19 | # stan_select(mu_alpha)%>% 20 | # stan_sample_frac(0.5) 21 | # 22 | # rats%>% 23 | # stan_select(mu_alpha)%>% 24 | # stan_sample_n(30,inc_warmup = FALSE) 25 | # 26 | # @rdname stan_sample 27 | # @family filtering 28 | # @export 29 | # stan_sample_n <- function(object, size, weight = NULL, inc_warmup = TRUE){ 30 | # UseMethod('stan_sample_n',object) 31 | # } 32 | 33 | # @rdname stan_sample 34 | # @export 35 | # stan_sample_frac <- function(object, size, weight = NULL, inc_warmup = TRUE){ 36 | # UseMethod('stan_sample_frac',object) 37 | # } 38 | 39 | # @export 40 | # stan_sample_n.brmsfit <- function(object, size, weight = NULL, inc_warmup = TRUE){ 41 | # object$fit <- stan_sample_n(object$fit,size=size,weight=weight,inc_warmup=inc_warmup) 42 | # object 43 | # } 44 | 45 | # @export 46 | # stan_sample_n.stanfit <- function(object, size, weight = NULL, inc_warmup = TRUE){ 47 | # 48 | # object <- clear_summary(object) 49 | # 50 | # warm_x <- seq_len(object@sim$warmup) 51 | # iter_x <- seq_len(object@sim$iter)[-warm_x] 52 | # 53 | # samp <- sort(sample(iter_x,size = size, prob = weight)) 54 | # 55 | # stan_sample(object, samp, warm_x, inc_warmup) 56 | # 57 | # } 58 | 59 | # @export 60 | # stan_sample_frac.brmsfit <- function(object, size, weight = NULL, inc_warmup = TRUE){ 61 | # object$fit <- stan_sample_frac(object$fit,size=size,weight=weight,inc_warmup=inc_warmup) 62 | # object 63 | # } 64 | 65 | # @export 66 | # stan_sample_frac.stanfit <- function(object, size, weight = NULL, inc_warmup = TRUE){ 67 | # 68 | # object <- clear_summary(object) 69 | # 70 | # warm_x <- seq_len(object@sim$warmup) 71 | # iter_x <- seq_len(object@sim$iter)[-warm_x] 72 | # 73 | # samp <- sort(sample(iter_x, size = floor(size*length(iter_x)), prob = weight)) 74 | # 75 | # stan_sample(object, samp, warm_x, inc_warmup) 76 | # 77 | # } 78 | 79 | # @importFrom purrr map 80 | # stan_sample <- function(object, samp, warm_x, inc_warmup){ 81 | # 82 | # on.exit({clear_summary(object)},add = TRUE) 83 | # 84 | # inits_x <- samp - length(warm_x) 85 | # 86 | # object@sim$iter <- length(samp) 87 | # 88 | # if(inc_warmup){ 89 | # 90 | # samp <- c(warm_x,samp) 91 | # object@sim$iter <- length(samp) 92 | # 93 | # }else{ 94 | # 95 | # object@sim$iter <- length(samp) 96 | # object@sim$warmup <- 0 97 | # object@sim$warmup2 <- rep(object@sim$warmup,object@sim$chains) 98 | # 99 | # } 100 | # 101 | # object@stan_args <- purrr::map(object@stan_args, 102 | # .f=function(x,i,inc_warmup){ 103 | # x$iter <- i 104 | # 105 | # if(!inc_warmup) 106 | # x$warmup <- 0 107 | # 108 | # x 109 | # }, 110 | # i = object@sim$iter, 111 | # inc_warmup = inc_warmup) 112 | # 113 | # object@inits <- purrr::map(object@inits,stan_trim_postwarm,idx=inits_x) 114 | # 115 | # object <- reset_perm(object,inits_x) 116 | # 117 | # object@sim$samples <- purrr::map(object@sim$samples,stan_subset,idx=samp) 118 | # object@sim$n_save <- rep(object@sim$iter,length(object@sim$n_save)) 119 | # 120 | # object 121 | # 122 | # } 123 | -------------------------------------------------------------------------------- /data/rat_data.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yonicd/shredder/5eacd3280de8ef8c441255f42403f78d1eab8e3e/data/rat_data.rda -------------------------------------------------------------------------------- /inst/rats.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int N; 3 | int T; 4 | real x[T]; 5 | real y[N,T]; 6 | real xbar; 7 | } 8 | parameters { 9 | real alpha[N]; 10 | real beta[N]; 11 | 12 | real mu_alpha; 13 | real mu_beta; // beta.c in original bugs model 14 | 15 | real sigmasq_y; 16 | real sigmasq_alpha; 17 | real sigmasq_beta; 18 | } 19 | transformed parameters { 20 | real sigma_y; // sigma in original bugs model 21 | real sigma_alpha; 22 | real sigma_beta; 23 | 24 | sigma_y = sqrt(sigmasq_y); 25 | sigma_alpha = sqrt(sigmasq_alpha); 26 | sigma_beta = sqrt(sigmasq_beta); 27 | } 28 | model { 29 | mu_alpha ~ normal(0, 100); 30 | mu_beta ~ normal(0, 100); 31 | sigmasq_y ~ inv_gamma(0.001, 0.001); 32 | sigmasq_alpha ~ inv_gamma(0.001, 0.001); 33 | sigmasq_beta ~ inv_gamma(0.001, 0.001); 34 | alpha ~ normal(mu_alpha, sigma_alpha); // vectorized 35 | beta ~ normal(mu_beta, sigma_beta); // vectorized 36 | for (n in 1:N) 37 | for (t in 1:T) 38 | y[n,t] ~ normal(alpha[n] + beta[n] * (x[t] - xbar), sigma_y); 39 | 40 | } 41 | generated quantities { 42 | real alpha0; 43 | alpha0 = mu_alpha - xbar * mu_beta; 44 | } 45 | -------------------------------------------------------------------------------- /man/figures/logo_ribbon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yonicd/shredder/5eacd3280de8ef8c441255f42403f78d1eab8e3e/man/figures/logo_ribbon.png -------------------------------------------------------------------------------- /man/peek_pars.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/partials.R 3 | \name{peek_pars} 4 | \alias{peek_pars} 5 | \title{Access stored pars for stan partials} 6 | \usage{ 7 | peek_pars() 8 | } 9 | \value{ 10 | character 11 | } 12 | \description{ 13 | Access pars currently stored that \link[=stan_contains]{stan_contains} use. 14 | } 15 | -------------------------------------------------------------------------------- /man/pipe.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/utils.R 3 | \name{\%>\%} 4 | \alias{\%>\%} 5 | \alias{\%||\%} 6 | \title{Pipe operator imported from purrr} 7 | \usage{ 8 | lhs \%>\% rhs 9 | 10 | x \%||\% y 11 | } 12 | \description{ 13 | Pipe operator imported from purrr 14 | } 15 | \keyword{internal} 16 | -------------------------------------------------------------------------------- /man/rat_data.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/rat_data-data.R 3 | \docType{data} 4 | \name{rat_data} 5 | \alias{rat_data} 6 | \title{Rats model data} 7 | \format{ 8 | An object of class \code{"list"}: 9 | \describe{ 10 | \item{\code{N}}{integer Number of subjects} 11 | \item{\code{T}}{integer Number of measurements} 12 | \item{\code{x}}{numeric Day of measurement} 13 | \item{\code{Y}}{tibble Values of measurements} 14 | \item{\code{xbar}}{numeric Mean days of measurements} 15 | } 16 | } 17 | \source{ 18 | \href{https://www.mrc-bsu.cam.ac.uk/wp-content/uploads/WinBUGS_Vol1.pdf}{Data Description}, 19 | \href{https://github.com/stan-dev/example-models/tree/master/bugs_examples/vol1/rats}{Data and Script} 20 | } 21 | \usage{ 22 | rat_data 23 | } 24 | \description{ 25 | Data used in rats example model 26 | } 27 | \seealso{ 28 | Other examples: 29 | \code{\link{rats_example}()} 30 | } 31 | \concept{examples} 32 | \keyword{datasets} 33 | -------------------------------------------------------------------------------- /man/rats_example.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/rats_example.R 3 | \name{rats_example} 4 | \alias{rats_example} 5 | \title{Rats model} 6 | \source{ 7 | \href{https://www.mrc-bsu.cam.ac.uk/wp-content/uploads/WinBUGS_Vol1.pdf}{Data Description}, 8 | \href{https://github.com/stan-dev/example-models/tree/master/bugs_examples/vol1/rats}{Data and Script} 9 | } 10 | \usage{ 11 | rats_example(seed = 123, nCores = pmin(parallel::detectCores(), 4)) 12 | } 13 | \arguments{ 14 | \item{seed}{numeric, seed for \link[rstan:stan]{rstan}, Default: 123} 15 | 16 | \item{nCores}{numeric, Maximum number of cores to use, Default: pmin(parallel::detectCores(),4)} 17 | } 18 | \value{ 19 | stanfit object 20 | } 21 | \description{ 22 | Run Rats data model 23 | } 24 | \examples{ 25 | \donttest{ 26 | require(shredder) 27 | rats <- rats_example(nCores = 1) 28 | rats 29 | } 30 | } 31 | \seealso{ 32 | \link[rstan:stan]{rstan} 33 | \link[parallel:detectCores]{detectCores} 34 | 35 | Other examples: 36 | \code{\link{rat_data}} 37 | } 38 | \concept{examples} 39 | -------------------------------------------------------------------------------- /man/stan_axe.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/stan_axe.R 3 | \name{stan_axe} 4 | \alias{stan_axe} 5 | \title{Remove elements from stanfit object} 6 | \usage{ 7 | stan_axe(object, what = c("summary", "fit_instance", "stanmodel")) 8 | } 9 | \arguments{ 10 | \item{object}{stanfit} 11 | 12 | \item{what}{name of attribute to remove, Default: c("summary", "fit_instance", "stanmodel")} 13 | } 14 | \value{ 15 | stanfit 16 | } 17 | \description{ 18 | Safely remove large elements from stanfit objects that are not needed for postprocessing. 19 | } 20 | \details{ 21 | shredder can remove three elements in the \code{stanfit} object 22 | \itemize{ 23 | \item The cached fit summary stored in fit@\code{.MISC}$summary 24 | \item The cached cpp object stored in fit@.MISC$stan_fit_instance 25 | \item The stanmodel stored in fit@stanmodel 26 | } 27 | } 28 | \seealso{ 29 | Other utility: 30 | \code{\link{stan_names}()}, 31 | \code{\link{stan_retain}()} 32 | } 33 | \concept{utility} 34 | -------------------------------------------------------------------------------- /man/stan_filter.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/stan_filter.R 3 | \name{stan_filter} 4 | \alias{stan_filter} 5 | \title{Return post-warmup samples with matching conditions} 6 | \usage{ 7 | stan_filter(object, ..., permuted = TRUE) 8 | } 9 | \arguments{ 10 | \item{object}{stanfit object} 11 | 12 | \item{\dots}{Logical predicates defined in terms of the parameters in object} 13 | 14 | \item{permuted}{A logical scalar indicating whether the draws after the warmup 15 | period in each chain should be permuted and merged, Default: TRUE} 16 | } 17 | \value{ 18 | stanfit object 19 | } 20 | \description{ 21 | Use stan_filter() to choose indicies of samples across parameters 22 | where conditions are true. 23 | } 24 | \details{ 25 | \itemize{ 26 | \item If no elements are returned for any chain then \code{NULL} is returned with a warning. 27 | \item If there is a chain that results in no samples then the chain is dropped with a warning. 28 | \item If permuted is FALSE then uneven chains may be returned depending on the result of the filter. 29 | \item To comply with \link[rstan:stanfit-method-extract]{extract} with \code{permuted=TRUE} chains in which chaines are 30 | assumed to be of equal size. 31 | \itemize{ 32 | \item Chain size returned is the length of the shortest filtered chain. 33 | } 34 | } 35 | } 36 | \examples{ 37 | \donttest{ 38 | rats <- rats_example(nCores = 1) 39 | 40 | rats\%>\% 41 | stan_select(mu_alpha,mu_beta) 42 | 43 | rats\%>\% 44 | stan_select(mu_alpha,mu_beta)\%>\% 45 | stan_filter(mu_beta < 6) 46 | 47 | rats\%>\% 48 | stan_select(mu_alpha,mu_beta)\%>\% 49 | stan_filter(mu_beta < 6, permuted = FALSE) 50 | 51 | rats\%>\% 52 | stan_select(`alpha[1]`,`alpha[2]`,mu_alpha,mu_beta)\%>\% 53 | stan_filter(mu_beta < 6 & `alpha[1]` > 240) 54 | } 55 | } 56 | \seealso{ 57 | Other filtering: 58 | \code{\link{stan_slice}()}, 59 | \code{\link{stan_thin_n}()} 60 | } 61 | \concept{filtering} 62 | -------------------------------------------------------------------------------- /man/stan_names.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/stan_names.R 3 | \name{stan_names} 4 | \alias{stan_names} 5 | \title{The Par Names of a Stanfit Object} 6 | \usage{ 7 | stan_names(x, expand = FALSE) 8 | } 9 | \arguments{ 10 | \item{x}{a stanfit object} 11 | 12 | \item{expand}{logical, if TRUE par names are returned including dimesnion 13 | indicies (fnames_oi), Default: FALSE} 14 | } 15 | \value{ 16 | character 17 | } 18 | \description{ 19 | Functions to get the names of a stanfit object. 20 | } 21 | \examples{ 22 | \donttest{ 23 | rats <- rats_example(nCores = 1) 24 | 25 | rats\%>\%stan_names() 26 | 27 | rats\%>\%stan_names(expand = TRUE) 28 | } 29 | } 30 | \seealso{ 31 | Other utility: 32 | \code{\link{stan_axe}()}, 33 | \code{\link{stan_retain}()} 34 | } 35 | \concept{utility} 36 | -------------------------------------------------------------------------------- /man/stan_partials.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/partials.R 3 | \name{stan_contains} 4 | \alias{stan_contains} 5 | \alias{stan_starts_with} 6 | \alias{stan_ends_with} 7 | \title{Partial match functions for stanfit objects} 8 | \usage{ 9 | stan_contains(match, ignore.case = TRUE, pars = peek_pars()) 10 | 11 | stan_starts_with(match, ignore.case = TRUE, pars = peek_pars()) 12 | 13 | stan_ends_with(match, ignore.case = TRUE, pars = peek_pars()) 14 | } 15 | \arguments{ 16 | \item{match}{character, pattern to search for} 17 | 18 | \item{ignore.case}{logical to ignore the case, Default: TRUE} 19 | 20 | \item{pars}{list of pars to search in, Default: peek_pars()} 21 | } 22 | \value{ 23 | character 24 | } 25 | \description{ 26 | Partial match functions used in \link[=stan_select]{stan_select} 27 | } 28 | \examples{ 29 | \donttest{ 30 | rats <- rats_example() 31 | 32 | rats\%>\% 33 | stan_names() 34 | 35 | rats\%>\% 36 | stan_select(stan_contains('sq')) 37 | 38 | rats\%>\% 39 | stan_select(mu_alpha, stan_contains('sq')) 40 | 41 | 42 | rats\%>\% 43 | stan_select(stan_starts_with('mu')) 44 | 45 | rats\%>\% 46 | stan_select(stan_starts_with('mu'),stan_contains('sq')) 47 | 48 | rats\%>\% 49 | stan_select(stan_ends_with('0')) 50 | 51 | # free form regex in contains 52 | rats\%>\% 53 | stan_select(stan_contains('sq|mu')) 54 | } 55 | 56 | } 57 | \seealso{ 58 | Other subsetting: 59 | \code{\link{stan_select}()} 60 | } 61 | \concept{subsetting} 62 | -------------------------------------------------------------------------------- /man/stan_retain.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/stan_retain.R 3 | \name{stan_retain} 4 | \alias{stan_retain} 5 | \title{Return object with specific chains} 6 | \usage{ 7 | stan_retain(object, chains = 1) 8 | } 9 | \arguments{ 10 | \item{object}{stanfit object} 11 | 12 | \item{chains}{numeric, chains to retain, Default: 1} 13 | } 14 | \value{ 15 | stanfit object 16 | } 17 | \description{ 18 | Use stan_retain() to choose chains to retain. 19 | } 20 | \examples{ 21 | \donttest{ 22 | rats <- rats_example(nCores = 1) 23 | 24 | #retain first chain only 25 | rats\%>\% 26 | stan_retain() 27 | 28 | #retain chains 1 and 3 29 | rats\%>\% 30 | stan_retain(c(1,3)) 31 | } 32 | } 33 | \seealso{ 34 | Other utility: 35 | \code{\link{stan_axe}()}, 36 | \code{\link{stan_names}()} 37 | } 38 | \concept{utility} 39 | -------------------------------------------------------------------------------- /man/stan_select.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/stan_select.R 3 | \name{stan_select} 4 | \alias{stan_select} 5 | \title{Select parameters by name} 6 | \usage{ 7 | stan_select(object, ...) 8 | } 9 | \arguments{ 10 | \item{object}{a stanfit object} 11 | 12 | \item{...}{One or more unquoted expressions separated by commas} 13 | } 14 | \value{ 15 | stanfit object 16 | } 17 | \description{ 18 | Choose variables from a stanfit object 19 | } 20 | \examples{ 21 | \donttest{ 22 | rats <- rats_example(nCores = 1) 23 | 24 | rats\%>\%stan_select(mu_beta) 25 | 26 | rats\%>\%stan_select(mu_beta,beta) 27 | 28 | rats\%>\%stan_select('mu_beta') 29 | 30 | tmp <- c('mu_beta','mu_alpha') 31 | 32 | rats\%>\%stan_select(!!!rlang::syms(tmp)) 33 | } 34 | } 35 | \seealso{ 36 | \link[rlang:nse-defuse]{quotation}, \link[rlang:quo_label]{quo_label}, \link[purrr:map]{map} 37 | 38 | Other subsetting: 39 | \code{\link{stan_contains}()} 40 | } 41 | \concept{subsetting} 42 | -------------------------------------------------------------------------------- /man/stan_slice.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/stan_slice.R 3 | \name{stan_slice} 4 | \alias{stan_slice} 5 | \title{Choose post-warumps samples by position} 6 | \usage{ 7 | stan_slice(object, ..., inc_warmup = TRUE) 8 | } 9 | \arguments{ 10 | \item{object}{stanfit object} 11 | 12 | \item{...}{Integer samples values} 13 | 14 | \item{inc_warmup}{logical, include warmup in output, Default: TRUE} 15 | } 16 | \value{ 17 | stanfit object 18 | } 19 | \description{ 20 | Choose post-warumps samples by their ordinal 21 | position from each parameter in a stanfit object 22 | } 23 | \examples{ 24 | \donttest{ 25 | rats <- rats_example(nCores = 1) 26 | 27 | rats\%>\% 28 | stan_select(mu_alpha)\%>\% 29 | stan_slice(1:30) 30 | 31 | rats\%>\% 32 | stan_select(mu_alpha)\%>\% 33 | stan_slice(1:30,inc_warmup = FALSE) 34 | } 35 | } 36 | \seealso{ 37 | Other filtering: 38 | \code{\link{stan_filter}()}, 39 | \code{\link{stan_thin_n}()} 40 | } 41 | \concept{filtering} 42 | -------------------------------------------------------------------------------- /man/stan_thin.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/stan_thin.R 3 | \name{stan_thin_n} 4 | \alias{stan_thin_n} 5 | \alias{stan_thin_frac} 6 | \title{thin n post-warumps samples from a stanfit object} 7 | \usage{ 8 | stan_thin_n(object, size, inc_warmup = TRUE) 9 | 10 | stan_thin_frac(object, size, inc_warmup = TRUE) 11 | } 12 | \arguments{ 13 | \item{object}{stanfit object} 14 | 15 | \item{size}{numeric, for \link[=stan_thin_n]{stan_thin_n} size of thin, for 16 | \link[=stan_thin_frac]{stan_thin_frac} fraction of samples to sample.} 17 | 18 | \item{inc_warmup}{logical, include warmup in output, Default: TRUE} 19 | } 20 | \value{ 21 | stanfit 22 | } 23 | \description{ 24 | This is a wrapper around stan_slice() to make it easy to 25 | thin samples from each parameter in a stanfit object. 26 | } 27 | \examples{ 28 | \donttest{ 29 | rats <- rats_example(nCores = 1) 30 | 31 | rats\%>\% 32 | stan_select(mu_alpha)\%>\% 33 | stan_thin_n(30) 34 | 35 | rats\%>\% 36 | stan_select(mu_alpha)\%>\% 37 | stan_thin_frac(0.5) 38 | 39 | rats\%>\% 40 | stan_select(mu_alpha)\%>\% 41 | stan_thin_n(30,inc_warmup = FALSE) 42 | } 43 | } 44 | \seealso{ 45 | Other filtering: 46 | \code{\link{stan_filter}()}, 47 | \code{\link{stan_slice}()} 48 | } 49 | \concept{filtering} 50 | -------------------------------------------------------------------------------- /tests/README.md: -------------------------------------------------------------------------------- 1 | Tests and Coverage 2 | ================ 3 | 31 August, 2020 11:06:24 4 | 5 | - [Coverage](#coverage) 6 | - [Unit Tests](#unit-tests) 7 | 8 | This output is created by 9 | [covrpage](https://github.com/metrumresearchgroup/covrpage). 10 | 11 | ## Coverage 12 | 13 | Coverage summary is created using the 14 | [covr](https://github.com/r-lib/covr) package. 15 | 16 | | Object | Coverage (%) | 17 | | :--------------------------------------- | :----------: | 18 | | shredder | 74.51 | 19 | | [R/rats\_example.R](../R/rats_example.R) | 0.00 | 20 | | [R/complete.R](../R/complete.R) | 12.50 | 21 | | [R/zzz.R](../R/zzz.R) | 50.00 | 22 | | [R/stan\_thin.R](../R/stan_thin.R) | 57.14 | 23 | | [R/utils.R](../R/utils.R) | 66.67 | 24 | | [R/stan\_names.R](../R/stan_names.R) | 80.00 | 25 | | [R/stan\_retain.R](../R/stan_retain.R) | 91.30 | 26 | | [R/stan\_select.R](../R/stan_select.R) | 92.31 | 27 | | [R/stan\_slice.R](../R/stan_slice.R) | 93.94 | 28 | | [R/stan\_filter.R](../R/stan_filter.R) | 96.23 | 29 | | [R/partials.R](../R/partials.R) | 100.00 | 30 | | [R/stan\_axe.R](../R/stan_axe.R) | 100.00 | 31 | | [R/stan\_utils.R](../R/stan_utils.R) | 100.00 | 32 | 33 |
34 | 35 | ## Unit Tests 36 | 37 | Unit Test summary is created using the 38 | [testthat](https://github.com/r-lib/testthat) package. 39 | 40 | | file | n | time | error | failed | skipped | warning | 41 | | :------------------------------------------ | -: | ----: | ----: | -----: | ------: | ------: | 42 | | [test-axe.R](testthat/test-axe.R) | 2 | 0.016 | 0 | 0 | 0 | 0 | 43 | | [test-filter.R](testthat/test-filter.R) | 7 | 0.209 | 0 | 0 | 0 | 0 | 44 | | [test-names.R](testthat/test-names.R) | 2 | 0.003 | 0 | 0 | 0 | 0 | 45 | | [test-retain.R](testthat/test-retain.R) | 6 | 0.376 | 0 | 0 | 0 | 0 | 46 | | [test-sampling.R](testthat/test-sampling.R) | 7 | 0.257 | 0 | 0 | 0 | 0 | 47 | | [test-select.R](testthat/test-select.R) | 15 | 0.368 | 0 | 0 | 0 | 0 | 48 | | [test-slice.R](testthat/test-slice.R) | 5 | 0.328 | 0 | 0 | 0 | 0 | 49 | 50 |
51 | 52 | Show Detailed Test Results 53 | 54 | | file | context | test | status | n | time | 55 | | :---------------------------------------------- | :------- | :--------------------------------- | :----- | -: | ----: | 56 | | [test-axe.R](testthat/test-axe.R#L11) | axe | axe elements: no fit\_instance | PASS | 1 | 0.015 | 57 | | [test-axe.R](testthat/test-axe.R#L16) | axe | axe elements: no stanmodel | PASS | 1 | 0.001 | 58 | | [test-filter.R](testthat/test-filter.R#L9) | filter | filters: default | PASS | 1 | 0.021 | 59 | | [test-filter.R](testthat/test-filter.R#L14) | filter | filters: not permuted | PASS | 1 | 0.020 | 60 | | [test-filter.R](testthat/test-filter.R#L18) | filter | filters: not permuted | PASS | 1 | 0.011 | 61 | | [test-filter.R](testthat/test-filter.R#L23) | filter | filters: indexed name | PASS | 1 | 0.011 | 62 | | [test-filter.R](testthat/test-filter.R#L30) | filter | filters: compound query | PASS | 1 | 0.009 | 63 | | [test-filter.R](testthat/test-filter.R#L34) | filter | filters: no samples | PASS | 1 | 0.007 | 64 | | [test-filter.R](testthat/test-filter.R#L38) | filter | filters: invalid pars | PASS | 1 | 0.130 | 65 | | [test-names.R](testthat/test-names.R#L9) | names | names: default | PASS | 1 | 0.002 | 66 | | [test-names.R](testthat/test-names.R#L14) | names | names: expand | PASS | 1 | 0.001 | 67 | | [test-retain.R](testthat/test-retain.R#L8) | retain | retain: default | PASS | 1 | 0.003 | 68 | | [test-retain.R](testthat/test-retain.R#L13) | retain | retain: null | PASS | 1 | 0.353 | 69 | | [test-retain.R](testthat/test-retain.R#L18) | retain | retain: single | PASS | 1 | 0.002 | 70 | | [test-retain.R](testthat/test-retain.R#L23) | retain | retain: multiple | PASS | 1 | 0.001 | 71 | | [test-retain.R](testthat/test-retain.R#L28) | retain | retain: all | PASS | 1 | 0.001 | 72 | | [test-retain.R](testthat/test-retain.R#L32) | retain | retain: bad | PASS | 1 | 0.016 | 73 | | [test-sampling.R](testthat/test-sampling.R#L10) | sampling | slice: default | PASS | 1 | 0.232 | 74 | | [test-sampling.R](testthat/test-sampling.R#L15) | sampling | slice: no warmup | PASS | 1 | 0.003 | 75 | | [test-sampling.R](testthat/test-sampling.R#L21) | sampling | slice: bad indexs | PASS | 2 | 0.008 | 76 | | [test-sampling.R](testthat/test-sampling.R#L32) | sampling | thinning: thin\_n | PASS | 1 | 0.005 | 77 | | [test-sampling.R](testthat/test-sampling.R#L37) | sampling | thinning: thin\_frac | PASS | 1 | 0.005 | 78 | | [test-sampling.R](testthat/test-sampling.R#L42) | sampling | thinning: no warmup | PASS | 1 | 0.004 | 79 | | [test-select.R](testthat/test-select.R#L6) | select | names: no pars | PASS | 1 | 0.003 | 80 | | [test-select.R](testthat/test-select.R#L11) | select | names: single par | PASS | 1 | 0.002 | 81 | | [test-select.R](testthat/test-select.R#L16) | select | names: multiple pars | PASS | 1 | 0.002 | 82 | | [test-select.R](testthat/test-select.R#L21) | select | names: par index | PASS | 1 | 0.002 | 83 | | [test-select.R](testthat/test-select.R#L26) | select | names: character par | PASS | 1 | 0.002 | 84 | | [test-select.R](testthat/test-select.R#L31) | select | names: character par syms | PASS | 1 | 0.003 | 85 | | [test-select.R](testthat/test-select.R#L37) | select | names: regex character pars | PASS | 1 | 0.003 | 86 | | [test-select.R](testthat/test-select.R#L44) | select | names: remove summary | PASS | 1 | 0.336 | 87 | | [test-select.R](testthat/test-select.R#L52) | select | partials: no pars | PASS | 1 | 0.002 | 88 | | [test-select.R](testthat/test-select.R#L57) | select | partials: starts\_with | PASS | 1 | 0.002 | 89 | | [test-select.R](testthat/test-select.R#L62) | select | partials: ends\_with | PASS | 1 | 0.002 | 90 | | [test-select.R](testthat/test-select.R#L67) | select | partials: starts\_contains | PASS | 1 | 0.003 | 91 | | [test-select.R](testthat/test-select.R#L72) | select | partials: mixed | PASS | 1 | 0.002 | 92 | | [test-select.R](testthat/test-select.R#L77) | select | partials: par regex index | PASS | 1 | 0.002 | 93 | | [test-select.R](testthat/test-select.R#L82) | select | partials: par regex multiple index | PASS | 1 | 0.002 | 94 | | [test-slice.R](testthat/test-slice.R#L7) | slice | slice: empty | PASS | 1 | 0.312 | 95 | | [test-slice.R](testthat/test-slice.R#L12) | slice | slice: single | PASS | 1 | 0.004 | 96 | | [test-slice.R](testthat/test-slice.R#L17) | slice | slice: single no warmup | PASS | 1 | 0.003 | 97 | | [test-slice.R](testthat/test-slice.R#L22) | slice | slice: vector | PASS | 1 | 0.004 | 98 | | [test-slice.R](testthat/test-slice.R#L27) | slice | slice: reset permut | PASS | 1 | 0.005 | 99 | 100 |
101 | 102 |
103 | 104 | Session Info 105 | 106 | | Field | Value | | 107 | | :------- | :-------------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | 108 | | Version | R version 4.0.2 (2020-06-22) | | 109 | | Platform | x86\_64-apple-darwin17.0 (64-bit) | ![](https://github.com/metrumresearchgroup/covrpage/blob/actions/inst/logo/gh.png?raw=true) | 110 | | Running | macOS Catalina 10.15.6 | | 111 | | Language | en\_US | | 112 | | Timezone | UTC | | 113 | 114 | | Package | Version | 115 | | :------- | :------ | 116 | | testthat | 2.3.2 | 117 | | covr | 3.3.2 | 118 | | covrpage | 0.0.71 | 119 | 120 |
121 | 122 | 123 | -------------------------------------------------------------------------------- /tests/files/rats.Rds: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yonicd/shredder/5eacd3280de8ef8c441255f42403f78d1eab8e3e/tests/files/rats.Rds -------------------------------------------------------------------------------- /tests/testthat.R: -------------------------------------------------------------------------------- 1 | library(testthat) 2 | library(shredder) 3 | test_check("shredder") 4 | -------------------------------------------------------------------------------- /tests/testthat/test-axe.R: -------------------------------------------------------------------------------- 1 | testthat::context("axe") 2 | 3 | test_path <- '../files/rats.Rds' 4 | 5 | rats_test <- readRDS(test_path) 6 | 7 | testthat::describe("axe elements", { 8 | 9 | it('no fit_instance',{ 10 | axe_fit_instance <- rats_test%>%stan_axe('fit_instance') 11 | testthat::expect_length(ls(envir = axe_fit_instance@.MISC),0L) 12 | }) 13 | 14 | it('no stanmodel',{ 15 | axe_stanmodel <- rats_test%>%stan_axe('stanmodel') 16 | testthat::expect_false('stanmodel'%in%names(attributes(axe_stanmodel))) 17 | }) 18 | 19 | }) 20 | -------------------------------------------------------------------------------- /tests/testthat/test-filter.R: -------------------------------------------------------------------------------- 1 | testthat::context('filter') 2 | 3 | rats <- readRDS('../files/rats.Rds') 4 | 5 | testthat::describe('filters',{ 6 | 7 | it('default',{ 8 | x <- rats%>%stan_filter(mu_beta < 6) 9 | testthat::expect_equal(length(x@sim$samples[[1]]$mu_beta),1032) 10 | }) 11 | 12 | it('not permuted',{ 13 | x <- rats%>%stan_filter(mu_beta < 6,permuted = FALSE) 14 | testthat::expect_equal(length(x@sim$samples[[1]]$mu_beta),1047) 15 | }) 16 | 17 | it('not permuted',{ 18 | testthat::expect_warning(rats%>%stan_filter(mu_beta < 5.85),regexp = 'no samples for chains: 1, 2') 19 | }) 20 | 21 | it('indexed name',{ 22 | x <- rats%>%stan_filter(`alpha[1]` < 240) 23 | testthat::expect_equal(length(x@sim$samples[[1]]$mu_beta),1492) 24 | }) 25 | 26 | it('compound query',{ 27 | x <- rats%>% 28 | stan_select(`alpha[1]`,`alpha[2]`,mu_alpha,mu_beta)%>% 29 | stan_filter(mu_beta < 6 & `alpha[1]` > 240) 30 | testthat::expect_equal(length(x@sim$samples[[1]]$mu_beta),1017) 31 | }) 32 | 33 | it('no samples',{ 34 | testthat::expect_warning(rats%>%stan_filter(`alpha[1]` < 6),regexp = 'no samples') 35 | }) 36 | 37 | it('invalid pars',{ 38 | testthat::expect_error(rats%>%stan_filter(bad < 6),regexp = 'Invalid parameter names') 39 | }) 40 | 41 | 42 | }) 43 | -------------------------------------------------------------------------------- /tests/testthat/test-names.R: -------------------------------------------------------------------------------- 1 | testthat::context('names') 2 | 3 | rats <- readRDS('../files/rats.Rds') 4 | 5 | testthat::describe('names',{ 6 | 7 | it('default',{ 8 | x <- rats%>%stan_names() 9 | testthat::expect_equal(rats@sim$pars_oi,x) 10 | }) 11 | 12 | it('expand',{ 13 | x <- rats%>%stan_names(expand = TRUE) 14 | testthat::expect_equal(rats@sim$fnames_oi,x) 15 | }) 16 | 17 | }) 18 | -------------------------------------------------------------------------------- /tests/testthat/test-retain.R: -------------------------------------------------------------------------------- 1 | testthat::context('retain') 2 | rats <- readRDS('../files/rats.Rds') 3 | 4 | testthat::describe('retain',{ 5 | 6 | it('default',{ 7 | x <- rats%>%stan_retain() 8 | testthat::expect_equal(chain_ids(x),1) 9 | }) 10 | 11 | it('null',{ 12 | x <- rats%>%stan_retain(chains = NULL) 13 | testthat::expect_equal(x,rats) 14 | }) 15 | 16 | it('single',{ 17 | x <- rats%>%stan_retain(chains = 3) 18 | testthat::expect_equal(sapply(x@stan_args, function(x) x$chain_id),3) 19 | }) 20 | 21 | it('multiple',{ 22 | x <- rats%>%stan_retain(chains = c(1,3)) 23 | testthat::expect_equal(sapply(x@stan_args, function(x) x$chain_id),c(1,3)) 24 | }) 25 | 26 | it('all',{ 27 | x <- rats%>%stan_retain(chains = c(2,1,3,4)) 28 | testthat::expect_equal(sapply(x@stan_args, function(x) x$chain_id),c(1,2,3,4)) 29 | }) 30 | 31 | it('bad',{ 32 | testthat::expect_error(rats%>%stan_retain(chains = c(5)),regexp = "Invalid chains") 33 | }) 34 | 35 | }) 36 | -------------------------------------------------------------------------------- /tests/testthat/test-sampling.R: -------------------------------------------------------------------------------- 1 | testthat::context('sampling') 2 | 3 | rats <- readRDS('../files/rats.Rds') 4 | 5 | testthat::describe('slice',{ 6 | 7 | it('default',{ 8 | rstan::summary(rats) 9 | x <- rats%>%shredder::stan_slice(1:10) 10 | testthat::expect_equal(length(x@sim$samples[[1]][[1]]),1010) 11 | }) 12 | 13 | it('no warmup',{ 14 | x <- rats%>%shredder::stan_slice(1:10,inc_warmup = FALSE) 15 | testthat::expect_equal(length(x@sim$samples[[1]][[1]]),10) 16 | }) 17 | 18 | 19 | it('bad indexs',{ 20 | suppressWarnings(x <- rats%>%shredder::stan_slice(900:1200,inc_warmup = FALSE)) 21 | testthat::expect_equal(length(x@sim$samples[[1]][[1]]),101) 22 | testthat::expect_warning(rats%>%shredder::stan_slice(900:1200),regexp = 'truncating the intersection') 23 | }) 24 | 25 | }) 26 | 27 | 28 | testthat::describe('thinning',{ 29 | 30 | it('thin_n',{ 31 | x <- rats%>%shredder::stan_thin_n(size = 2) 32 | testthat::expect_equal(length(x@sim$samples[[1]][[1]]),1500) 33 | }) 34 | 35 | it('thin_frac',{ 36 | x <- rats%>%shredder::stan_thin_frac(size = 0.25) 37 | testthat::expect_equal(length(x@sim$samples[[1]][[1]]),1250) 38 | }) 39 | 40 | it('no warmup',{ 41 | x <- rats%>%shredder::stan_thin_n(size = 2,inc_warmup = FALSE) 42 | testthat::expect_equal(length(x@sim$samples[[1]][[1]]),500) 43 | }) 44 | 45 | }) 46 | -------------------------------------------------------------------------------- /tests/testthat/test-select.R: -------------------------------------------------------------------------------- 1 | testthat::context('select') 2 | rats <- readRDS('../files/rats.Rds') 3 | testthat::describe('names',{ 4 | 5 | it('no pars',{ 6 | testthat::expect_message(rats%>%stan_select(),regexp = 'no pars selected') 7 | }) 8 | 9 | it('single par',{ 10 | x <- rats%>%stan_select(mu_alpha) 11 | testthat::expect_equal(x@model_pars,'mu_alpha') 12 | }) 13 | 14 | it('multiple pars',{ 15 | x <- rats%>%stan_select(mu_alpha,mu_beta) 16 | testthat::expect_equal(x@model_pars,c('mu_alpha','mu_beta')) 17 | }) 18 | 19 | it('par index',{ 20 | x <- rats%>%stan_select(alpha[1]) 21 | testthat::expect_equal(x@sim$fnames_oi,c('alpha[1]')) 22 | }) 23 | 24 | it('character par',{ 25 | x <- rats%>%stan_select('mu_alpha','mu_beta') 26 | testthat::expect_equal(x@model_pars,c('mu_alpha','mu_beta')) 27 | }) 28 | 29 | it('character par syms',{ 30 | x <- rats%>%stan_select(!!! rlang::syms(c('mu_alpha','mu_beta'))) 31 | testthat::expect_equal(x@model_pars,c('mu_alpha','mu_beta')) 32 | }) 33 | 34 | it('regex character pars',{ 35 | idx <- sprintf('alpha[%s]',1:5) 36 | x <- rats%>%stan_select(!!!rlang::syms(idx)) 37 | testthat::expect_equal(x@sim$fnames_oi,idx) 38 | }) 39 | 40 | 41 | it('remove summary',{ 42 | rstan::summary(rats) 43 | x <- rats%>%stan_select(mu_alpha) 44 | testthat::expect_equal(x@model_pars,'mu_alpha') 45 | }) 46 | 47 | }) 48 | 49 | testthat::describe('partials',{ 50 | 51 | it('no pars',{ 52 | testthat::expect_message(rats%>%stan_select(stan_starts_with('mum')),regexp = 'no pars selected') 53 | }) 54 | 55 | it('starts_with',{ 56 | x <- rats%>%stan_select(stan_starts_with('mu')) 57 | testthat::expect_equal(x@model_pars,c('mu_alpha','mu_beta')) 58 | }) 59 | 60 | it('ends_with',{ 61 | x <- rats%>%stan_select(stan_ends_with('0')) 62 | testthat::expect_equal(x@model_pars,c('alpha0')) 63 | }) 64 | 65 | it('starts_contains',{ 66 | x <- rats%>%stan_select(stan_contains('alpha')) 67 | testthat::expect_equal(x@model_pars,c('alpha','mu_alpha','sigmasq_alpha','sigma_alpha','alpha0')) 68 | }) 69 | 70 | it('mixed',{ 71 | x <- rats%>%stan_select(alpha,stan_starts_with('mu')) 72 | testthat::expect_equal(x@model_pars,c('alpha','mu_alpha','mu_beta')) 73 | }) 74 | 75 | it('par regex index',{ 76 | x <- rats%>%stan_select(stan_contains('alpha\\[1\\]')) 77 | testthat::expect_equal(x@sim$fnames_oi,c('alpha[1]')) 78 | }) 79 | 80 | it('par regex multiple index',{ 81 | x <- rats%>%stan_select(stan_contains('alpha\\[[1-2]\\]')) 82 | testthat::expect_equal(x@sim$fnames_oi,c('alpha[1]','alpha[2]')) 83 | }) 84 | }) -------------------------------------------------------------------------------- /tests/testthat/test-slice.R: -------------------------------------------------------------------------------- 1 | testthat::context('slice') 2 | rats <- readRDS('../files/rats.Rds') 3 | testthat::describe('slice',{ 4 | 5 | it('empty',{ 6 | x <- rats%>%stan_slice() 7 | testthat::expect_equal(x,rats) 8 | }) 9 | 10 | it('single',{ 11 | x <- rats%>%stan_slice(1) 12 | testthat::expect_equal(x@sim$n_save,rep(1001,4)) 13 | }) 14 | 15 | it('single no warmup',{ 16 | x <- rats%>%stan_slice(1,inc_warmup = FALSE) 17 | testthat::expect_equal(x@sim$n_save,rep(1,4)) 18 | }) 19 | 20 | it('vector',{ 21 | x <- rats%>%stan_slice(1:10) 22 | testthat::expect_equal(x@sim$n_save,rep(1010,4)) 23 | }) 24 | 25 | it('reset permut',{ 26 | x <- rats%>%stan_slice(1:10) 27 | testthat::expect_equal(sapply(x@sim$permutation,length),rep(10,4)) 28 | }) 29 | 30 | }) 31 | -------------------------------------------------------------------------------- /vignettes/.build.timestamp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yonicd/shredder/5eacd3280de8ef8c441255f42403f78d1eab8e3e/vignettes/.build.timestamp -------------------------------------------------------------------------------- /vignettes/.gitignore: -------------------------------------------------------------------------------- 1 | *.html 2 | *.R 3 | -------------------------------------------------------------------------------- /vignettes/axe.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Removing Internal Elements" 3 | output: rmarkdown::html_vignette 4 | vignette: > 5 | %\VignetteIndexEntry{Removing Internal Elements} 6 | %\VignetteEngine{knitr::rmarkdown} 7 | %\VignetteEncoding{UTF-8} 8 | --- 9 | 10 | 11 | 12 | 13 | ```r 14 | library(shredder) 15 | library(butcher) 16 | library(details) 17 | ``` 18 | 19 | `stanfit` object hold a lot of information and some of the elements that take of most of the memory are not necessarily needed in post-processing. This vignette introduces verbs that can safely remove these elements without corrupting the stanfit object. 20 | 21 | 22 | ```r 23 | rats <- rats_example(nCores = 1) 24 | ``` 25 | 26 | 27 | ```details 28 | rats@stanmodel 29 | ``` 30 | 31 | 32 |
33 | The Model 34 | 35 | ```r 36 | 37 | S4 class stanmodel 'rats' coded as follows: 38 | data { 39 | int N; 40 | int T; 41 | real x[T]; 42 | real y[N,T]; 43 | real xbar; 44 | } 45 | parameters { 46 | real alpha[N]; 47 | real beta[N]; 48 | 49 | real mu_alpha; 50 | real mu_beta; // beta.c in original bugs model 51 | 52 | real sigmasq_y; 53 | real sigmasq_alpha; 54 | real sigmasq_beta; 55 | } 56 | transformed parameters { 57 | real sigma_y; // sigma in original bugs model 58 | real sigma_alpha; 59 | real sigma_beta; 60 | 61 | sigma_y = sqrt(sigmasq_y); 62 | sigma_alpha = sqrt(sigmasq_alpha); 63 | sigma_beta = sqrt(sigmasq_beta); 64 | } 65 | model { 66 | mu_alpha ~ normal(0, 100); 67 | mu_beta ~ normal(0, 100); 68 | sigmasq_y ~ inv_gamma(0.001, 0.001); 69 | sigmasq_alpha ~ inv_gamma(0.001, 0.001); 70 | sigmasq_beta ~ inv_gamma(0.001, 0.001); 71 | alpha ~ normal(mu_alpha, sigma_alpha); // vectorized 72 | beta ~ normal(mu_beta, sigma_beta); // vectorized 73 | for (n in 1:N) 74 | for (t in 1:T) 75 | y[n,t] ~ normal(alpha[n] + beta[n] * (x[t] - xbar), sigma_y); 76 | 77 | } 78 | generated quantities { 79 | real alpha0; 80 | alpha0 = mu_alpha - xbar * mu_beta; 81 | } 82 | 83 | ``` 84 | 85 |
86 |
87 | 88 | 89 | ```details 90 | rats 91 | ``` 92 | 93 | 94 |
95 | Model Summary 96 | 97 | ```r 98 | 99 | Inference for Stan model: rats. 100 | 4 chains, each with iter=2000; warmup=1000; thin=1; 101 | post-warmup draws per chain=1000, total post-warmup draws=4000. 102 | 103 | mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat 104 | alpha[1] 239.93 0.03 2.61 234.75 238.22 239.91 241.71 244.96 6102 1 105 | alpha[2] 247.80 0.04 2.70 242.54 245.97 247.79 249.62 253.07 5745 1 106 | alpha[3] 252.44 0.04 2.60 247.27 250.72 252.46 254.21 257.55 4957 1 107 | alpha[4] 232.56 0.04 2.65 227.48 230.69 232.54 234.34 237.79 5564 1 108 | alpha[5] 231.58 0.03 2.73 226.42 229.71 231.56 233.46 236.86 6405 1 109 | alpha[6] 249.76 0.04 2.71 244.61 247.94 249.74 251.60 255.03 5168 1 110 | alpha[7] 228.66 0.03 2.67 223.37 226.97 228.65 230.47 233.89 6562 1 111 | alpha[8] 248.41 0.03 2.69 243.04 246.62 248.43 250.21 253.52 5921 1 112 | alpha[9] 283.31 0.04 2.70 277.91 281.57 283.37 285.05 288.59 4862 1 113 | alpha[10] 219.31 0.03 2.66 214.14 217.52 219.26 221.14 224.55 5997 1 114 | alpha[11] 258.25 0.04 2.71 252.87 256.39 258.32 260.12 263.41 5403 1 115 | alpha[12] 228.17 0.04 2.63 223.21 226.39 228.14 229.91 233.39 5125 1 116 | alpha[13] 242.39 0.04 2.68 237.08 240.57 242.45 244.22 247.70 5586 1 117 | alpha[14] 268.25 0.04 2.66 262.97 266.43 268.29 270.08 273.34 5341 1 118 | alpha[15] 242.73 0.03 2.65 237.51 241.02 242.71 244.48 247.91 5750 1 119 | alpha[16] 245.35 0.03 2.68 239.99 243.54 245.32 247.15 250.60 6011 1 120 | alpha[17] 232.16 0.04 2.71 226.83 230.31 232.15 233.99 237.51 5920 1 121 | alpha[18] 240.42 0.03 2.64 235.33 238.69 240.43 242.18 245.52 5940 1 122 | alpha[19] 253.77 0.04 2.68 248.45 251.99 253.80 255.54 259.06 5602 1 123 | alpha[20] 241.62 0.03 2.60 236.58 239.91 241.57 243.34 246.78 6264 1 124 | alpha[21] 248.59 0.03 2.70 243.24 246.79 248.56 250.41 253.85 6674 1 125 | alpha[22] 225.31 0.04 2.77 219.92 223.48 225.28 227.18 230.91 6170 1 126 | alpha[23] 228.52 0.03 2.61 223.43 226.79 228.54 230.24 233.62 6656 1 127 | alpha[24] 245.11 0.03 2.62 239.91 243.40 245.14 246.83 250.29 6691 1 128 | alpha[25] 234.44 0.03 2.69 229.26 232.63 234.43 236.23 239.82 6256 1 129 | alpha[26] 253.92 0.04 2.61 248.70 252.16 253.92 255.65 259.09 5479 1 130 | alpha[27] 254.27 0.03 2.57 249.27 252.55 254.25 255.96 259.55 5780 1 131 | alpha[28] 243.01 0.04 2.70 237.55 241.20 243.04 244.87 248.15 5767 1 132 | alpha[29] 217.91 0.03 2.69 212.74 216.09 217.89 219.71 223.13 6317 1 133 | alpha[30] 241.42 0.03 2.61 236.37 239.64 241.41 243.24 246.48 6029 1 134 | beta[1] 6.06 0.00 0.24 5.59 5.91 6.07 6.22 6.53 5584 1 135 | beta[2] 7.05 0.00 0.26 6.55 6.88 7.05 7.22 7.55 4936 1 136 | beta[3] 6.48 0.00 0.24 6.02 6.32 6.48 6.65 6.97 4433 1 137 | beta[4] 5.34 0.00 0.26 4.82 5.17 5.34 5.52 5.84 5458 1 138 | beta[5] 6.57 0.00 0.24 6.09 6.41 6.57 6.73 7.05 5527 1 139 | beta[6] 6.17 0.00 0.24 5.70 6.00 6.17 6.34 6.64 5028 1 140 | beta[7] 5.97 0.00 0.24 5.50 5.81 5.97 6.14 6.44 5714 1 141 | beta[8] 6.42 0.00 0.24 5.95 6.25 6.41 6.59 6.90 5518 1 142 | beta[9] 7.05 0.00 0.25 6.54 6.89 7.05 7.22 7.54 5162 1 143 | beta[10] 5.84 0.00 0.24 5.36 5.68 5.84 6.00 6.31 5171 1 144 | beta[11] 6.80 0.00 0.25 6.31 6.63 6.80 6.97 7.28 5098 1 145 | beta[12] 6.12 0.00 0.24 5.65 5.96 6.11 6.28 6.58 5552 1 146 | beta[13] 6.16 0.00 0.25 5.65 6.01 6.16 6.32 6.66 5429 1 147 | beta[14] 6.69 0.00 0.24 6.22 6.52 6.69 6.85 7.17 5107 1 148 | beta[15] 5.42 0.00 0.25 4.94 5.25 5.41 5.59 5.91 4556 1 149 | beta[16] 5.93 0.00 0.24 5.45 5.77 5.93 6.09 6.39 5506 1 150 | beta[17] 6.28 0.00 0.24 5.82 6.12 6.28 6.44 6.74 5684 1 151 | beta[18] 5.84 0.00 0.24 5.36 5.68 5.83 6.00 6.30 5159 1 152 | beta[19] 6.40 0.00 0.24 5.93 6.23 6.40 6.56 6.85 5036 1 153 | beta[20] 6.05 0.00 0.25 5.56 5.89 6.05 6.22 6.54 6193 1 154 | beta[21] 6.40 0.00 0.24 5.93 6.24 6.40 6.56 6.86 6641 1 155 | beta[22] 5.86 0.00 0.24 5.40 5.69 5.86 6.02 6.31 5890 1 156 | beta[23] 5.75 0.00 0.24 5.27 5.59 5.75 5.91 6.23 6016 1 157 | beta[24] 5.89 0.00 0.24 5.41 5.73 5.89 6.05 6.37 6260 1 158 | beta[25] 6.91 0.00 0.25 6.42 6.74 6.90 7.07 7.40 4974 1 159 | beta[26] 6.54 0.00 0.24 6.06 6.39 6.55 6.70 7.01 5722 1 160 | beta[27] 5.90 0.00 0.24 5.41 5.73 5.90 6.06 6.38 5821 1 161 | beta[28] 5.85 0.00 0.23 5.40 5.69 5.84 6.01 6.31 5740 1 162 | beta[29] 5.68 0.00 0.25 5.20 5.51 5.67 5.84 6.17 5303 1 163 | beta[30] 6.13 0.00 0.23 5.68 5.97 6.12 6.28 6.59 6428 1 164 | mu_alpha 242.47 0.05 2.76 236.95 240.61 242.50 244.38 247.70 3585 1 165 | mu_beta 6.19 0.00 0.11 5.98 6.12 6.19 6.25 6.40 4462 1 166 | sigmasq_y 37.16 0.12 5.69 27.74 33.14 36.56 40.58 50.12 2366 1 167 | sigmasq_alpha 218.39 1.06 63.89 126.08 173.31 208.62 251.30 372.24 3615 1 168 | sigmasq_beta 0.27 0.00 0.10 0.13 0.21 0.26 0.32 0.52 3028 1 169 | sigma_y 6.08 0.01 0.46 5.27 5.76 6.05 6.37 7.08 2370 1 170 | sigma_alpha 14.63 0.03 2.07 11.23 13.16 14.44 15.85 19.29 3919 1 171 | sigma_beta 0.52 0.00 0.09 0.36 0.45 0.51 0.57 0.72 2897 1 172 | alpha0 106.39 0.06 3.60 99.23 104.00 106.44 108.76 113.55 4122 1 173 | lp__ -437.92 0.21 7.04 -453.36 -442.36 -437.34 -432.93 -425.72 1098 1 174 | 175 | Samples were drawn using NUTS(diag_e) at Fri Jul 31 07:47:56 2020. 176 | For each parameter, n_eff is a crude measure of effective sample size, 177 | and Rhat is the potential scale reduction factor on split chains (at 178 | convergence, Rhat=1). 179 | 180 | ``` 181 | 182 |
183 |
184 | 185 | We use the [butcher](https://tidymodels.github.io/butcher/) package to evaluate the size of each element. 186 | 187 | 188 | ```r 189 | 190 | rats%>% 191 | attributes()%>% 192 | butcher::weigh(units = 'MB') 193 | #> # A tibble: 404 x 2 194 | #> object size 195 | #> 196 | #> 1 stanmodel 5.86 197 | #> 2 .MISC 0.0449 198 | #> 3 sim.samples.alpha[1] 0.0160 199 | #> 4 sim.samples.alpha[2] 0.0160 200 | #> 5 sim.samples.alpha[3] 0.0160 201 | #> 6 sim.samples.alpha[4] 0.0160 202 | #> 7 sim.samples.alpha[5] 0.0160 203 | #> 8 sim.samples.alpha[6] 0.0160 204 | #> 9 sim.samples.alpha[7] 0.0160 205 | #> 10 sim.samples.alpha[8] 0.0160 206 | #> # … with 394 more rows 207 | ``` 208 | 209 | ## stan_axe 210 | 211 | `shredder` can remove three elements in the `stanfit` object 212 | 213 | - The cached fit summary stored in fit@`.MISC`$summary 214 | - The cached cpp object stored in fit@.MISC$stan_fit_instance 215 | - The stanmodel stored in fit@stanmodel 216 | 217 | 218 | ```r 219 | 220 | summary <- function(x){ 221 | 222 | y <- x%>% 223 | attributes()%>% 224 | butcher::weigh() 225 | 226 | s <- y$size 227 | 228 | data.frame(min = min(s), 229 | max = max(s), 230 | mean = mean(s), 231 | sd = sd(s), 232 | sum = sum(s)) 233 | 234 | } 235 | ``` 236 | 237 | 238 | ```r 239 | butcher_rats <- rats%>% 240 | summary() 241 | 242 | butcher_fit_instance <- rats%>% 243 | stan_axe(what = 'fit_instance')%>% 244 | summary() 245 | 246 | butcher_stanmodel <- rats%>% 247 | stan_axe(what = 'fit_instance')%>% 248 | stan_axe(what = 'stanmodel')%>% 249 | summary() 250 | 251 | butcher_summary <- rats%>% 252 | stan_axe(what = 'fit_instance')%>% 253 | stan_axe(what = 'stanmodel')%>% 254 | stan_axe(what = 'summary')%>% 255 | summary() 256 | 257 | butcher_params <- rats%>% 258 | stan_axe(what = 'fit_instance')%>% 259 | stan_axe(what = 'stanmodel')%>% 260 | stan_axe(what = 'summary')%>% 261 | stan_select(alpha)%>% 262 | summary() 263 | ``` 264 | 265 | ## Compare Object Sizes 266 | 267 | 268 | ```r 269 | tbl <- purrr::map_df( 270 | list('full' = butcher_rats, 271 | 'fit_instance' = butcher_fit_instance, 272 | 'stanmodel' = butcher_stanmodel, 273 | 'summary' = butcher_summary, 274 | 'param' = butcher_params), 275 | identity,.id='axe') 276 | ``` 277 | 278 | 279 | ```r 280 | knitr::kable(tbl) 281 | ``` 282 | 283 | 284 | 285 | |axe | min| max| mean| sd| sum| 286 | |:------------|-------:|--------:|---------:|---------:|---------:| 287 | |full | 4.8e-05| 5.860896| 0.0258238| 0.2911213| 10.432832| 288 | |fit_instance | 4.8e-05| 5.860896| 0.0258238| 0.2911213| 10.432832| 289 | |stanmodel | 4.8e-05| 0.044880| 0.0113448| 0.0074566| 4.571936| 290 | |summary | 4.8e-05| 0.016048| 0.0112342| 0.0072865| 4.527392| 291 | |param | 5.6e-05| 0.016048| 0.0108903| 0.0073964| 2.014712| 292 | -------------------------------------------------------------------------------- /vignettes/axe.Rmd.orig: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Removing Internal Elements" 3 | output: rmarkdown::html_vignette 4 | vignette: > 5 | %\VignetteIndexEntry{Removing Internal Elements} 6 | %\VignetteEngine{knitr::rmarkdown} 7 | %\VignetteEncoding{UTF-8} 8 | --- 9 | 10 | ```{r, include = FALSE} 11 | knitr::opts_chunk$set( 12 | collapse = TRUE, 13 | comment = "#>" 14 | ) 15 | ``` 16 | 17 | ```{r setup} 18 | library(shredder) 19 | library(butcher) 20 | library(details) 21 | ``` 22 | 23 | `stanfit` object hold a lot of information and some of the elements that take of most of the memory are not necessarily needed in post-processing. This vignette introduces verbs that can safely remove these elements without corrupting the stanfit object. 24 | 25 | ```{r,results='hide',message=FALSE} 26 | rats <- rats_example(nCores = 1) 27 | 28 | ``` 29 | 30 | ```{details, details.summary = 'The Model'} 31 | rats@stanmodel 32 | ``` 33 | 34 | ```{details, details.summary = 'Model Summary'} 35 | rats 36 | ``` 37 | 38 | We use the [butcher](https://tidymodels.github.io/butcher/) package to evaluate the size of each element. 39 | 40 | ```{r} 41 | 42 | rats%>% 43 | attributes()%>% 44 | butcher::weigh(units = 'MB') 45 | 46 | ``` 47 | 48 | ## stan_axe 49 | 50 | `shredder` can remove three elements in the `stanfit` object 51 | 52 | - The cached fit summary stored in fit@`.MISC`$summary 53 | - The cached cpp object stored in fit@.MISC$stan_fit_instance 54 | - The stanmodel stored in fit@stanmodel 55 | 56 | ```{r} 57 | 58 | summary <- function(x){ 59 | 60 | y <- x%>% 61 | attributes()%>% 62 | butcher::weigh() 63 | 64 | s <- y$size 65 | 66 | data.frame(min = min(s), 67 | max = max(s), 68 | mean = mean(s), 69 | sd = sd(s), 70 | sum = sum(s)) 71 | 72 | } 73 | ``` 74 | 75 | ```{r} 76 | butcher_rats <- rats%>% 77 | summary() 78 | 79 | butcher_fit_instance <- rats%>% 80 | stan_axe(what = 'fit_instance')%>% 81 | summary() 82 | 83 | butcher_stanmodel <- rats%>% 84 | stan_axe(what = 'fit_instance')%>% 85 | stan_axe(what = 'stanmodel')%>% 86 | summary() 87 | 88 | butcher_summary <- rats%>% 89 | stan_axe(what = 'fit_instance')%>% 90 | stan_axe(what = 'stanmodel')%>% 91 | stan_axe(what = 'summary')%>% 92 | summary() 93 | 94 | butcher_params <- rats%>% 95 | stan_axe(what = 'fit_instance')%>% 96 | stan_axe(what = 'stanmodel')%>% 97 | stan_axe(what = 'summary')%>% 98 | stan_select(alpha)%>% 99 | summary() 100 | 101 | ``` 102 | 103 | ## Compare Object Sizes 104 | 105 | ```{r} 106 | tbl <- purrr::map_df( 107 | list('full' = butcher_rats, 108 | 'fit_instance' = butcher_fit_instance, 109 | 'stanmodel' = butcher_stanmodel, 110 | 'summary' = butcher_summary, 111 | 'param' = butcher_params), 112 | identity,.id='axe') 113 | ``` 114 | 115 | ```{r} 116 | knitr::kable(tbl) 117 | ``` -------------------------------------------------------------------------------- /vignettes/benchmark-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yonicd/shredder/5eacd3280de8ef8c441255f42403f78d1eab8e3e/vignettes/benchmark-1.png -------------------------------------------------------------------------------- /vignettes/benchmark.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Benchmarking" 3 | output: rmarkdown::html_vignette 4 | vignette: > 5 | %\VignetteIndexEntry{Benchmarking} 6 | %\VignetteEngine{knitr::rmarkdown} 7 | %\VignetteEncoding{UTF-8} 8 | --- 9 | 10 | 11 | 12 | 13 | ```r 14 | library(rstan) 15 | library(shredder) 16 | library(tidybayes) 17 | library(microbenchmark) 18 | rstan_options(auto_write = TRUE) 19 | ``` 20 | 21 | Set seed 22 | 23 | ```r 24 | set.seed(1234) 25 | ``` 26 | 27 | Run Model 28 | 29 | 30 | ```r 31 | rats <- shredder::rats_example(nCores = 4) 32 | ``` 33 | 34 | ## Set up functions 35 | 36 | ### tidybayes 37 | 38 | 39 | ```r 40 | tb <- function(mod){ 41 | mod%>% 42 | tidybayes::spread_draws(mu_alpha,mu_beta) 43 | } 44 | ``` 45 | 46 | ### tidybayes with shredder select 47 | 48 | 49 | ```r 50 | sh_select <- function(mod){ 51 | mod%>% 52 | stan_select(mu_alpha,mu_beta)%>% 53 | tidybayes::spread_draws(mu_alpha,mu_beta) 54 | } 55 | ``` 56 | 57 | ### tidybayes with shredder thin (every 20th sample) 58 | 59 | ```r 60 | sh_thin <- function(mod){ 61 | mod%>% 62 | stan_thin_n(20)%>% 63 | tidybayes::spread_draws(mu_alpha,mu_beta) 64 | } 65 | ``` 66 | 67 | ## Benchmark 68 | 69 | ### Run benchmark 70 | 71 | 72 | ```r 73 | bench <- microbenchmark( 74 | "tidybayes" = {tb(rats)}, 75 | "shredder select" = {sh_select(rats)}, 76 | "shredder thin" = {sh_thin(rats)}) 77 | ``` 78 | 79 | ### Output 80 | 81 | 82 | ```r 83 | ggplot2::autoplot(bench) 84 | ``` 85 | 86 | plot of chunk benchmark 87 | -------------------------------------------------------------------------------- /vignettes/benchmark.Rmd.orig: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Benchmarking" 3 | output: rmarkdown::html_vignette 4 | vignette: > 5 | %\VignetteIndexEntry{Benchmarking} 6 | %\VignetteEngine{knitr::rmarkdown} 7 | %\VignetteEncoding{UTF-8} 8 | --- 9 | 10 | ```{r, include = FALSE} 11 | stopifnot(require(knitr)) 12 | options(width = 90) 13 | opts_chunk$set( 14 | fig.path = './', 15 | comment = NA, 16 | message = FALSE, 17 | warning = FALSE, 18 | eval = TRUE, 19 | dev = "png", 20 | dpi = 150, 21 | fig.asp = 0.8, 22 | fig.width = 5, 23 | out.width = "60%", 24 | fig.align = "center" 25 | ) 26 | ``` 27 | 28 | ```{r setup} 29 | library(rstan) 30 | library(shredder) 31 | library(tidybayes) 32 | library(microbenchmark) 33 | rstan_options(auto_write = TRUE) 34 | ``` 35 | 36 | Set seed 37 | ```{r} 38 | set.seed(1234) 39 | ``` 40 | 41 | Run Model 42 | 43 | ```{r,results='hide',warning=FALSE,message=FALSE} 44 | rats <- shredder::rats_example(nCores = 4) 45 | ``` 46 | 47 | ## Set up functions 48 | 49 | ### tidybayes 50 | 51 | ```{r} 52 | tb <- function(mod){ 53 | mod%>% 54 | tidybayes::spread_draws(mu_alpha,mu_beta) 55 | } 56 | ``` 57 | 58 | ### tidybayes with shredder select 59 | 60 | ```{r} 61 | sh_select <- function(mod){ 62 | mod%>% 63 | stan_select(mu_alpha,mu_beta)%>% 64 | tidybayes::spread_draws(mu_alpha,mu_beta) 65 | } 66 | ``` 67 | 68 | ### tidybayes with shredder thin (every 20th sample) 69 | ```{r} 70 | sh_thin <- function(mod){ 71 | mod%>% 72 | stan_thin_n(20)%>% 73 | tidybayes::spread_draws(mu_alpha,mu_beta) 74 | } 75 | ``` 76 | 77 | ## Benchmark 78 | 79 | ### Run benchmark 80 | 81 | ```{r} 82 | bench <- microbenchmark( 83 | "tidybayes" = {tb(rats)}, 84 | "shredder select" = {sh_select(rats)}, 85 | "shredder thin" = {sh_thin(rats)}) 86 | ``` 87 | 88 | ### Output 89 | 90 | ```{r benchmark} 91 | ggplot2::autoplot(bench) 92 | ``` 93 | -------------------------------------------------------------------------------- /vignettes/brms.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Working With brms" 3 | output: rmarkdown::html_vignette 4 | vignette: > 5 | %\VignetteIndexEntry{Working With brms} 6 | %\VignetteEngine{knitr::rmarkdown} 7 | %\VignetteEncoding{UTF-8} 8 | --- 9 | 10 | 11 | 12 | 13 | ```r 14 | library(shredder) 15 | library(brms) 16 | ``` 17 | 18 | ## Generate Data 19 | 20 | 21 | ```r 22 | group <- rep(c("treat", "placebo"), 23 | each = 30) 24 | 25 | symptom_post <- c( 26 | rnorm(30, mean = 1, sd = 2), 27 | rnorm(30, mean = 0, sd = 1) 28 | ) 29 | 30 | dat1 <- data.frame(group, symptom_post) 31 | ``` 32 | 33 | ## Run Model 34 | 35 | 36 | ```r 37 | fit <- brm(bf(symptom_post ~ group, sigma ~ group), 38 | data = dat1, 39 | family = gaussian(), 40 | seed = 1234) 41 | ``` 42 | 43 | 44 | ``` 45 | Family: gaussian 46 | Links: mu = identity; sigma = log 47 | Formula: symptom_post ~ group 48 | sigma ~ group 49 | Data: dat1 (Number of observations: 60) 50 | Samples: 4 chains, each with iter = 2000; warmup = 1000; thin = 1; 51 | total post-warmup samples = 4000 52 | 53 | Population-Level Effects: 54 | Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS 55 | Intercept 0.06698 0.15800 -0.24173 0.37999 1.00169 5280 2818 56 | sigma_Intercept -0.17407 0.13197 -0.42005 0.10367 1.00107 3635 2690 57 | grouptreat 0.71749 0.41627 -0.12423 1.53980 1.00066 2569 2354 58 | sigma_grouptreat 0.90143 0.18605 0.53340 1.26824 1.00158 3215 2815 59 | 60 | Samples were drawn using sampling(NUTS). For each parameter, Bulk_ESS 61 | and Tail_ESS are effective sample size measures, and Rhat is the potential 62 | scale reduction factor on split chains (at convergence, Rhat = 1). 63 | ``` 64 | 65 | 66 | ```r 67 | fit%>% 68 | plot(N = 2, ask = FALSE) 69 | ``` 70 | 71 | plot of chunk brms_plot_1plot of chunk brms_plot_1 72 | 73 | ## Select first 100 Posterior Samples from each Chain 74 | 75 | 76 | ```r 77 | fit_slice <- fit%>%stan_slice(1:100) 78 | ``` 79 | 80 | 81 | ``` 82 | Family: gaussian 83 | Links: mu = identity; sigma = log 84 | Formula: symptom_post ~ group 85 | sigma ~ group 86 | Data: dat1 (Number of observations: 60) 87 | Samples: 4 chains, each with iter = 1100; warmup = 1000; thin = 1; 88 | total post-warmup samples = 400 89 | 90 | Population-Level Effects: 91 | Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS 92 | Intercept 0.07013 0.16137 -0.23785 0.38342 1.01057 774 364 93 | sigma_Intercept -0.16386 0.14627 -0.42317 0.12234 1.00969 403 300 94 | grouptreat 0.71854 0.41869 -0.04519 1.53400 1.01188 296 313 95 | sigma_grouptreat 0.89130 0.19825 0.51367 1.30166 1.00422 344 263 96 | 97 | Samples were drawn using sampling(NUTS). For each parameter, Bulk_ESS 98 | and Tail_ESS are effective sample size measures, and Rhat is the potential 99 | scale reduction factor on split chains (at convergence, Rhat = 1). 100 | ``` 101 | 102 | 103 | ```r 104 | fit_slice%>% 105 | plot(N = 2, ask = FALSE) 106 | ``` 107 | 108 | plot of chunk brms_plot_2plot of chunk brms_plot_2 109 | 110 | ## Thin 25% of Posterior Samples from each Chain 111 | 112 | 113 | ```r 114 | fit_thin <- fit%>% 115 | stan_thin_frac(0.25) 116 | ``` 117 | 118 | 119 | ``` 120 | Family: gaussian 121 | Links: mu = identity; sigma = log 122 | Formula: symptom_post ~ group 123 | sigma ~ group 124 | Data: dat1 (Number of observations: 60) 125 | Samples: 4 chains, each with iter = 1250; warmup = 1000; thin = 1; 126 | total post-warmup samples = 1000 127 | 128 | Population-Level Effects: 129 | Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS 130 | Intercept 0.06683 0.15555 -0.24601 0.36279 1.00068 952 1033 131 | sigma_Intercept -0.17761 0.13061 -0.42181 0.08630 1.00249 1022 1012 132 | grouptreat 0.72207 0.41571 -0.07618 1.57548 1.00041 1003 894 133 | sigma_grouptreat 0.90896 0.18475 0.56465 1.28181 1.00050 1052 994 134 | 135 | Samples were drawn using sampling(NUTS). For each parameter, Bulk_ESS 136 | and Tail_ESS are effective sample size measures, and Rhat is the potential 137 | scale reduction factor on split chains (at convergence, Rhat = 1). 138 | ``` 139 | 140 | 141 | ```r 142 | fit_thin%>% 143 | plot(N = 2, ask = FALSE) 144 | ``` 145 | 146 | plot of chunk brms_plot_3plot of chunk brms_plot_3 147 | 148 | ## Filter Posterior Samples by Conditional 149 | 150 | 151 | ```r 152 | fit_filter <- fit%>% 153 | stan_filter(b_grouptreat>=1) 154 | ``` 155 | 156 | 157 | ``` 158 | Family: gaussian 159 | Links: mu = identity; sigma = log 160 | Formula: symptom_post ~ group 161 | sigma ~ group 162 | Data: dat1 (Number of observations: 60) 163 | Samples: 4 chains, each with iter = 1228; warmup = 1000; thin = 1; 164 | total post-warmup samples = 912 165 | 166 | Population-Level Effects: 167 | Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS 168 | Intercept -0.00769 0.15381 -0.32381 0.29970 1.00015 957 755 169 | sigma_Intercept -0.17751 0.13422 -0.42817 0.13153 1.00122 659 794 170 | grouptreat 1.25471 0.20926 1.01099 1.79655 1.00431 605 775 171 | sigma_grouptreat 0.92356 0.20039 0.52929 1.30766 1.00501 614 655 172 | 173 | Samples were drawn using sampling(NUTS). For each parameter, Bulk_ESS 174 | and Tail_ESS are effective sample size measures, and Rhat is the potential 175 | scale reduction factor on split chains (at convergence, Rhat = 1). 176 | ``` 177 | 178 | 179 | ```r 180 | fit_filter%>% 181 | plot(N = 2, ask = FALSE) 182 | ``` 183 | 184 | plot of chunk brms_plot_4plot of chunk brms_plot_4 185 | 186 | ## Filter Posterior Samples by Conditional Subset Chain 187 | 188 | 189 | ```r 190 | fit_filter_chain <- fit%>% 191 | stan_filter(b_grouptreat>=1)%>% 192 | stan_retain(chains = 1) 193 | ``` 194 | 195 | 196 | ``` 197 | Family: gaussian 198 | Links: mu = identity; sigma = log 199 | Formula: symptom_post ~ group 200 | sigma ~ group 201 | Data: dat1 (Number of observations: 60) 202 | Samples: 1 chains, each with iter = 1228; warmup = 1000; thin = 1; 203 | total post-warmup samples = 228 204 | 205 | Population-Level Effects: 206 | Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS 207 | Intercept -0.01025 0.16019 -0.33472 0.30657 1.00007 333 128 208 | sigma_Intercept -0.17540 0.14239 -0.42458 0.13767 0.99844 157 153 209 | grouptreat 1.21999 0.18977 1.00719 1.70702 0.99858 175 217 210 | sigma_grouptreat 0.92037 0.22289 0.51294 1.35952 1.01292 139 140 211 | 212 | Samples were drawn using sampling(NUTS). For each parameter, Bulk_ESS 213 | and Tail_ESS are effective sample size measures, and Rhat is the potential 214 | scale reduction factor on split chains (at convergence, Rhat = 1). 215 | ``` 216 | 217 | 218 | ```r 219 | fit_filter_chain%>% 220 | plot(N = 2, ask = FALSE) 221 | ``` 222 | 223 | plot of chunk brms_plot_5plot of chunk brms_plot_5 224 | -------------------------------------------------------------------------------- /vignettes/brms.Rmd.orig: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Working With brms" 3 | output: rmarkdown::html_vignette 4 | vignette: > 5 | %\VignetteIndexEntry{Working With brms} 6 | %\VignetteEngine{knitr::rmarkdown} 7 | %\VignetteEncoding{UTF-8} 8 | --- 9 | 10 | ```{r, include = FALSE} 11 | stopifnot(require(knitr)) 12 | options(width = 90) 13 | opts_chunk$set( 14 | fig.path = './', 15 | comment = NA, 16 | message = FALSE, 17 | warning = FALSE, 18 | eval = TRUE, 19 | dev = "png", 20 | dpi = 150, 21 | fig.asp = 0.8, 22 | fig.width = 5, 23 | out.width = "60%", 24 | fig.align = "center" 25 | ) 26 | ``` 27 | 28 | ```{r setup} 29 | library(shredder) 30 | library(brms) 31 | ``` 32 | 33 | ## Generate Data 34 | 35 | ```{r} 36 | group <- rep(c("treat", "placebo"), 37 | each = 30) 38 | 39 | symptom_post <- c( 40 | rnorm(30, mean = 1, sd = 2), 41 | rnorm(30, mean = 0, sd = 1) 42 | ) 43 | 44 | dat1 <- data.frame(group, symptom_post) 45 | ``` 46 | 47 | ## Run Model 48 | 49 | ```{r,results='hide'} 50 | fit <- brm(bf(symptom_post ~ group, sigma ~ group), 51 | data = dat1, 52 | family = gaussian(), 53 | seed = 1234) 54 | ``` 55 | 56 | ```{r,echo = FALSE} 57 | print(fit,digits = 5) 58 | ``` 59 | 60 | ```{r brms_plot_1} 61 | fit%>% 62 | plot(N = 2, ask = FALSE) 63 | ``` 64 | 65 | ## Select first 100 Posterior Samples from each Chain 66 | 67 | ```{r} 68 | fit_slice <- fit%>%stan_slice(1:100) 69 | ``` 70 | 71 | ```{r,echo = FALSE} 72 | print(fit_slice,digits = 5) 73 | ``` 74 | 75 | ```{r brms_plot_2} 76 | fit_slice%>% 77 | plot(N = 2, ask = FALSE) 78 | ``` 79 | 80 | ## Thin 25% of Posterior Samples from each Chain 81 | 82 | ```{r} 83 | fit_thin <- fit%>% 84 | stan_thin_frac(0.25) 85 | ``` 86 | 87 | ```{r,echo = FALSE} 88 | print(fit_thin,digits = 5) 89 | ``` 90 | 91 | ```{r brms_plot_3} 92 | fit_thin%>% 93 | plot(N = 2, ask = FALSE) 94 | ``` 95 | 96 | ## Filter Posterior Samples by Conditional 97 | 98 | ```{r} 99 | fit_filter <- fit%>% 100 | stan_filter(b_grouptreat>=1) 101 | ``` 102 | 103 | ```{r,echo = FALSE} 104 | print(fit_filter,digits = 5) 105 | ``` 106 | 107 | ```{r brms_plot_4} 108 | fit_filter%>% 109 | plot(N = 2, ask = FALSE) 110 | ``` 111 | 112 | ## Filter Posterior Samples by Conditional Subset Chain 113 | 114 | ```{r} 115 | fit_filter_chain <- fit%>% 116 | stan_filter(b_grouptreat>=1)%>% 117 | stan_retain(chains = 1) 118 | ``` 119 | 120 | ```{r,echo = FALSE} 121 | print(fit_filter_chain,digits = 5) 122 | ``` 123 | 124 | ```{r brms_plot_5} 125 | fit_filter_chain%>% 126 | plot(N = 2, ask = FALSE) 127 | ``` -------------------------------------------------------------------------------- /vignettes/brms_plot_1-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yonicd/shredder/5eacd3280de8ef8c441255f42403f78d1eab8e3e/vignettes/brms_plot_1-1.png -------------------------------------------------------------------------------- /vignettes/brms_plot_1-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yonicd/shredder/5eacd3280de8ef8c441255f42403f78d1eab8e3e/vignettes/brms_plot_1-2.png -------------------------------------------------------------------------------- /vignettes/brms_plot_2-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yonicd/shredder/5eacd3280de8ef8c441255f42403f78d1eab8e3e/vignettes/brms_plot_2-1.png -------------------------------------------------------------------------------- /vignettes/brms_plot_2-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yonicd/shredder/5eacd3280de8ef8c441255f42403f78d1eab8e3e/vignettes/brms_plot_2-2.png -------------------------------------------------------------------------------- /vignettes/brms_plot_3-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yonicd/shredder/5eacd3280de8ef8c441255f42403f78d1eab8e3e/vignettes/brms_plot_3-1.png -------------------------------------------------------------------------------- /vignettes/brms_plot_3-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yonicd/shredder/5eacd3280de8ef8c441255f42403f78d1eab8e3e/vignettes/brms_plot_3-2.png -------------------------------------------------------------------------------- /vignettes/brms_plot_4-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yonicd/shredder/5eacd3280de8ef8c441255f42403f78d1eab8e3e/vignettes/brms_plot_4-1.png -------------------------------------------------------------------------------- /vignettes/brms_plot_4-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yonicd/shredder/5eacd3280de8ef8c441255f42403f78d1eab8e3e/vignettes/brms_plot_4-2.png -------------------------------------------------------------------------------- /vignettes/brms_plot_5-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yonicd/shredder/5eacd3280de8ef8c441255f42403f78d1eab8e3e/vignettes/brms_plot_5-1.png -------------------------------------------------------------------------------- /vignettes/brms_plot_5-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yonicd/shredder/5eacd3280de8ef8c441255f42403f78d1eab8e3e/vignettes/brms_plot_5-2.png -------------------------------------------------------------------------------- /vignettes/rstan.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Working with rstan" 3 | output: rmarkdown::html_vignette 4 | vignette: > 5 | %\VignetteIndexEntry{Working with rstan} 6 | %\VignetteEngine{knitr::rmarkdown} 7 | %\VignetteEncoding{UTF-8} 8 | --- 9 | 10 | 11 | 12 | ## Example 13 | 14 | This is a basic example which shows you how to solve a common problem: 15 | 16 | 17 | ```r 18 | library(shredder) 19 | library(rstan) 20 | ``` 21 | 22 | 23 | ```r 24 | rats <- shredder::rats_example(nCores = 1) 25 | ``` 26 | 27 | 28 | ```r 29 | rats 30 | ``` 31 | 32 |
33 | Standard Output 34 | 35 | ```r 36 | 37 | Inference for Stan model: rats. 38 | 4 chains, each with iter=2000; warmup=1000; thin=1; 39 | post-warmup draws per chain=1000, total post-warmup draws=4000. 40 | 41 | mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat 42 | alpha[1] 239.93 0.03 2.61 234.75 238.22 239.91 241.71 244.96 6102 1 43 | alpha[2] 247.80 0.04 2.70 242.54 245.97 247.79 249.62 253.07 5745 1 44 | alpha[3] 252.44 0.04 2.60 247.27 250.72 252.46 254.21 257.55 4957 1 45 | alpha[4] 232.56 0.04 2.65 227.48 230.69 232.54 234.34 237.79 5564 1 46 | alpha[5] 231.58 0.03 2.73 226.42 229.71 231.56 233.46 236.86 6405 1 47 | alpha[6] 249.76 0.04 2.71 244.61 247.94 249.74 251.60 255.03 5168 1 48 | alpha[7] 228.66 0.03 2.67 223.37 226.97 228.65 230.47 233.89 6562 1 49 | alpha[8] 248.41 0.03 2.69 243.04 246.62 248.43 250.21 253.52 5921 1 50 | alpha[9] 283.31 0.04 2.70 277.91 281.57 283.37 285.05 288.59 4862 1 51 | alpha[10] 219.31 0.03 2.66 214.14 217.52 219.26 221.14 224.55 5997 1 52 | alpha[11] 258.25 0.04 2.71 252.87 256.39 258.32 260.12 263.41 5403 1 53 | alpha[12] 228.17 0.04 2.63 223.21 226.39 228.14 229.91 233.39 5125 1 54 | alpha[13] 242.39 0.04 2.68 237.08 240.57 242.45 244.22 247.70 5586 1 55 | alpha[14] 268.25 0.04 2.66 262.97 266.43 268.29 270.08 273.34 5341 1 56 | alpha[15] 242.73 0.03 2.65 237.51 241.02 242.71 244.48 247.91 5750 1 57 | alpha[16] 245.35 0.03 2.68 239.99 243.54 245.32 247.15 250.60 6011 1 58 | alpha[17] 232.16 0.04 2.71 226.83 230.31 232.15 233.99 237.51 5920 1 59 | alpha[18] 240.42 0.03 2.64 235.33 238.69 240.43 242.18 245.52 5940 1 60 | alpha[19] 253.77 0.04 2.68 248.45 251.99 253.80 255.54 259.06 5602 1 61 | alpha[20] 241.62 0.03 2.60 236.58 239.91 241.57 243.34 246.78 6264 1 62 | alpha[21] 248.59 0.03 2.70 243.24 246.79 248.56 250.41 253.85 6674 1 63 | alpha[22] 225.31 0.04 2.77 219.92 223.48 225.28 227.18 230.91 6170 1 64 | alpha[23] 228.52 0.03 2.61 223.43 226.79 228.54 230.24 233.62 6656 1 65 | alpha[24] 245.11 0.03 2.62 239.91 243.40 245.14 246.83 250.29 6691 1 66 | alpha[25] 234.44 0.03 2.69 229.26 232.63 234.43 236.23 239.82 6256 1 67 | alpha[26] 253.92 0.04 2.61 248.70 252.16 253.92 255.65 259.09 5479 1 68 | alpha[27] 254.27 0.03 2.57 249.27 252.55 254.25 255.96 259.55 5780 1 69 | alpha[28] 243.01 0.04 2.70 237.55 241.20 243.04 244.87 248.15 5767 1 70 | alpha[29] 217.91 0.03 2.69 212.74 216.09 217.89 219.71 223.13 6317 1 71 | alpha[30] 241.42 0.03 2.61 236.37 239.64 241.41 243.24 246.48 6029 1 72 | beta[1] 6.06 0.00 0.24 5.59 5.91 6.07 6.22 6.53 5584 1 73 | beta[2] 7.05 0.00 0.26 6.55 6.88 7.05 7.22 7.55 4936 1 74 | beta[3] 6.48 0.00 0.24 6.02 6.32 6.48 6.65 6.97 4433 1 75 | beta[4] 5.34 0.00 0.26 4.82 5.17 5.34 5.52 5.84 5458 1 76 | beta[5] 6.57 0.00 0.24 6.09 6.41 6.57 6.73 7.05 5527 1 77 | beta[6] 6.17 0.00 0.24 5.70 6.00 6.17 6.34 6.64 5028 1 78 | beta[7] 5.97 0.00 0.24 5.50 5.81 5.97 6.14 6.44 5714 1 79 | beta[8] 6.42 0.00 0.24 5.95 6.25 6.41 6.59 6.90 5518 1 80 | beta[9] 7.05 0.00 0.25 6.54 6.89 7.05 7.22 7.54 5162 1 81 | beta[10] 5.84 0.00 0.24 5.36 5.68 5.84 6.00 6.31 5171 1 82 | beta[11] 6.80 0.00 0.25 6.31 6.63 6.80 6.97 7.28 5098 1 83 | beta[12] 6.12 0.00 0.24 5.65 5.96 6.11 6.28 6.58 5552 1 84 | beta[13] 6.16 0.00 0.25 5.65 6.01 6.16 6.32 6.66 5429 1 85 | beta[14] 6.69 0.00 0.24 6.22 6.52 6.69 6.85 7.17 5107 1 86 | beta[15] 5.42 0.00 0.25 4.94 5.25 5.41 5.59 5.91 4556 1 87 | beta[16] 5.93 0.00 0.24 5.45 5.77 5.93 6.09 6.39 5506 1 88 | beta[17] 6.28 0.00 0.24 5.82 6.12 6.28 6.44 6.74 5684 1 89 | beta[18] 5.84 0.00 0.24 5.36 5.68 5.83 6.00 6.30 5159 1 90 | beta[19] 6.40 0.00 0.24 5.93 6.23 6.40 6.56 6.85 5036 1 91 | beta[20] 6.05 0.00 0.25 5.56 5.89 6.05 6.22 6.54 6193 1 92 | beta[21] 6.40 0.00 0.24 5.93 6.24 6.40 6.56 6.86 6641 1 93 | beta[22] 5.86 0.00 0.24 5.40 5.69 5.86 6.02 6.31 5890 1 94 | beta[23] 5.75 0.00 0.24 5.27 5.59 5.75 5.91 6.23 6016 1 95 | beta[24] 5.89 0.00 0.24 5.41 5.73 5.89 6.05 6.37 6260 1 96 | beta[25] 6.91 0.00 0.25 6.42 6.74 6.90 7.07 7.40 4974 1 97 | beta[26] 6.54 0.00 0.24 6.06 6.39 6.55 6.70 7.01 5722 1 98 | beta[27] 5.90 0.00 0.24 5.41 5.73 5.90 6.06 6.38 5821 1 99 | beta[28] 5.85 0.00 0.23 5.40 5.69 5.84 6.01 6.31 5740 1 100 | beta[29] 5.68 0.00 0.25 5.20 5.51 5.67 5.84 6.17 5303 1 101 | beta[30] 6.13 0.00 0.23 5.68 5.97 6.12 6.28 6.59 6428 1 102 | mu_alpha 242.47 0.05 2.76 236.95 240.61 242.50 244.38 247.70 3585 1 103 | mu_beta 6.19 0.00 0.11 5.98 6.12 6.19 6.25 6.40 4462 1 104 | sigmasq_y 37.16 0.12 5.69 27.74 33.14 36.56 40.58 50.12 2366 1 105 | sigmasq_alpha 218.39 1.06 63.89 126.08 173.31 208.62 251.30 372.24 3615 1 106 | sigmasq_beta 0.27 0.00 0.10 0.13 0.21 0.26 0.32 0.52 3028 1 107 | sigma_y 6.08 0.01 0.46 5.27 5.76 6.05 6.37 7.08 2370 1 108 | sigma_alpha 14.63 0.03 2.07 11.23 13.16 14.44 15.85 19.29 3919 1 109 | sigma_beta 0.52 0.00 0.09 0.36 0.45 0.51 0.57 0.72 2897 1 110 | alpha0 106.39 0.06 3.60 99.23 104.00 106.44 108.76 113.55 4122 1 111 | lp__ -437.92 0.21 7.04 -453.36 -442.36 -437.34 -432.93 -425.72 1098 1 112 | 113 | Samples were drawn using NUTS(diag_e) at Fri Jul 31 07:47:56 2020. 114 | For each parameter, n_eff is a crude measure of effective sample size, 115 | and Rhat is the potential scale reduction factor on split chains (at 116 | convergence, Rhat=1). 117 | 118 | ``` 119 | 120 |
121 |
122 | 123 |
124 | The Stan Script 125 | 126 | ```c 127 | 128 | data { 129 | int N; 130 | int T; 131 | real x[T]; 132 | real y[N,T]; 133 | real xbar; 134 | } 135 | parameters { 136 | real alpha[N]; 137 | real beta[N]; 138 | 139 | real mu_alpha; 140 | real mu_beta; // beta.c in original bugs model 141 | 142 | real sigmasq_y; 143 | real sigmasq_alpha; 144 | real sigmasq_beta; 145 | } 146 | transformed parameters { 147 | real sigma_y; // sigma in original bugs model 148 | real sigma_alpha; 149 | real sigma_beta; 150 | 151 | sigma_y = sqrt(sigmasq_y); 152 | sigma_alpha = sqrt(sigmasq_alpha); 153 | sigma_beta = sqrt(sigmasq_beta); 154 | } 155 | model { 156 | mu_alpha ~ normal(0, 100); 157 | mu_beta ~ normal(0, 100); 158 | sigmasq_y ~ inv_gamma(0.001, 0.001); 159 | sigmasq_alpha ~ inv_gamma(0.001, 0.001); 160 | sigmasq_beta ~ inv_gamma(0.001, 0.001); 161 | alpha ~ normal(mu_alpha, sigma_alpha); // vectorized 162 | beta ~ normal(mu_beta, sigma_beta); // vectorized 163 | for (n in 1:N) 164 | for (t in 1:T) 165 | y[n,t] ~ normal(alpha[n] + beta[n] * (x[t] - xbar), sigma_y); 166 | 167 | } 168 | generated quantities { 169 | real alpha0; 170 | alpha0 = mu_alpha - xbar * mu_beta; 171 | } 172 | 173 | ``` 174 | 175 |
176 |
177 | 178 | 179 | ## Pars 180 | 181 | ### Names 182 | 183 | 184 | ```r 185 | rats%>% 186 | stan_names() 187 | [1] "alpha" "beta" "mu_alpha" "mu_beta" "sigmasq_y" 188 | [6] "sigmasq_alpha" "sigmasq_beta" "sigma_y" "sigma_alpha" "sigma_beta" 189 | [11] "alpha0" "lp__" 190 | 191 | rats%>% 192 | stan_names(expand = TRUE) 193 | [1] "alpha[1]" "alpha[2]" "alpha[3]" "alpha[4]" "alpha[5]" 194 | [6] "alpha[6]" "alpha[7]" "alpha[8]" "alpha[9]" "alpha[10]" 195 | [11] "alpha[11]" "alpha[12]" "alpha[13]" "alpha[14]" "alpha[15]" 196 | [16] "alpha[16]" "alpha[17]" "alpha[18]" "alpha[19]" "alpha[20]" 197 | [21] "alpha[21]" "alpha[22]" "alpha[23]" "alpha[24]" "alpha[25]" 198 | [26] "alpha[26]" "alpha[27]" "alpha[28]" "alpha[29]" "alpha[30]" 199 | [31] "beta[1]" "beta[2]" "beta[3]" "beta[4]" "beta[5]" 200 | [36] "beta[6]" "beta[7]" "beta[8]" "beta[9]" "beta[10]" 201 | [41] "beta[11]" "beta[12]" "beta[13]" "beta[14]" "beta[15]" 202 | [46] "beta[16]" "beta[17]" "beta[18]" "beta[19]" "beta[20]" 203 | [51] "beta[21]" "beta[22]" "beta[23]" "beta[24]" "beta[25]" 204 | [56] "beta[26]" "beta[27]" "beta[28]" "beta[29]" "beta[30]" 205 | [61] "mu_alpha" "mu_beta" "sigmasq_y" "sigmasq_alpha" "sigmasq_beta" 206 | [66] "sigma_y" "sigma_alpha" "sigma_beta" "alpha0" "lp__" 207 | ``` 208 | 209 | ### Select 210 | 211 | 212 | ```r 213 | rats%>% 214 | stan_select(mu_alpha) 215 | Inference for Stan model: rats. 216 | 4 chains, each with iter=2000; warmup=1000; thin=1; 217 | post-warmup draws per chain=1000, total post-warmup draws=4000. 218 | 219 | mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat 220 | mu_alpha 242.47 0.05 2.76 236.95 240.61 242.5 244.38 247.7 3585 1 221 | 222 | Samples were drawn using at Fri Jul 31 07:47:56 2020. 223 | For each parameter, n_eff is a crude measure of effective sample size, 224 | and Rhat is the potential scale reduction factor on split chains (at 225 | convergence, Rhat=1). 226 | 227 | rats%>% 228 | stan_select(mu_alpha,mu_beta) 229 | Inference for Stan model: rats. 230 | 4 chains, each with iter=2000; warmup=1000; thin=1; 231 | post-warmup draws per chain=1000, total post-warmup draws=4000. 232 | 233 | mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat 234 | mu_alpha 242.47 0.05 2.76 236.95 240.61 242.50 244.38 247.7 3585 1 235 | mu_beta 6.19 0.00 0.11 5.98 6.12 6.19 6.25 6.4 4462 1 236 | 237 | Samples were drawn using at Fri Jul 31 07:47:56 2020. 238 | For each parameter, n_eff is a crude measure of effective sample size, 239 | and Rhat is the potential scale reduction factor on split chains (at 240 | convergence, Rhat=1). 241 | 242 | rats%>% 243 | stan_select(!!!rlang::syms(c('mu_alpha','mu_beta'))) 244 | Inference for Stan model: rats. 245 | 4 chains, each with iter=2000; warmup=1000; thin=1; 246 | post-warmup draws per chain=1000, total post-warmup draws=4000. 247 | 248 | mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat 249 | mu_alpha 242.47 0.05 2.76 236.95 240.61 242.50 244.38 247.7 3585 1 250 | mu_beta 6.19 0.00 0.11 5.98 6.12 6.19 6.25 6.4 4462 1 251 | 252 | Samples were drawn using at Fri Jul 31 07:47:56 2020. 253 | For each parameter, n_eff is a crude measure of effective sample size, 254 | and Rhat is the potential scale reduction factor on split chains (at 255 | convergence, Rhat=1). 256 | 257 | 258 | rats%>% 259 | stan_select(alpha[1],alpha[2]) 260 | Inference for Stan model: rats. 261 | 4 chains, each with iter=2000; warmup=1000; thin=1; 262 | post-warmup draws per chain=1000, total post-warmup draws=4000. 263 | 264 | mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat 265 | alpha[1] 239.93 0.03 2.61 234.75 238.22 239.91 241.71 244.96 6102 1 266 | alpha[2] 247.80 0.04 2.70 242.54 245.97 247.79 249.62 253.07 5745 1 267 | 268 | Samples were drawn using at Fri Jul 31 07:47:56 2020. 269 | For each parameter, n_eff is a crude measure of effective sample size, 270 | and Rhat is the potential scale reduction factor on split chains (at 271 | convergence, Rhat=1). 272 | 273 | 274 | rats%>% 275 | stan_select(!!!rlang::syms(sprintf('alpha[%s]',1:5))) 276 | Inference for Stan model: rats. 277 | 4 chains, each with iter=2000; warmup=1000; thin=1; 278 | post-warmup draws per chain=1000, total post-warmup draws=4000. 279 | 280 | mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat 281 | alpha[1] 239.93 0.03 2.61 234.75 238.22 239.91 241.71 244.96 6102 1 282 | alpha[2] 247.80 0.04 2.70 242.54 245.97 247.79 249.62 253.07 5745 1 283 | alpha[3] 252.44 0.04 2.60 247.27 250.72 252.46 254.21 257.55 4957 1 284 | alpha[4] 232.56 0.04 2.65 227.48 230.69 232.54 234.34 237.79 5564 1 285 | alpha[5] 231.58 0.03 2.73 226.42 229.71 231.56 233.46 236.86 6405 1 286 | 287 | Samples were drawn using at Fri Jul 31 07:47:56 2020. 288 | For each parameter, n_eff is a crude measure of effective sample size, 289 | and Rhat is the potential scale reduction factor on split chains (at 290 | convergence, Rhat=1). 291 | ``` 292 | 293 | ### Select with Partials 294 | 295 | 296 | 297 | ```r 298 | rats%>% 299 | stan_select(stan_contains('alpha')) 300 | 301 | ``` 302 | 303 |
304 | Select all Parameters that contain "alpha" 305 | 306 | ```r 307 | 308 | Inference for Stan model: rats. 309 | 4 chains, each with iter=2000; warmup=1000; thin=1; 310 | post-warmup draws per chain=1000, total post-warmup draws=4000. 311 | 312 | mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat 313 | alpha[1] 239.93 0.03 2.61 234.75 238.22 239.91 241.71 244.96 6102 1 314 | alpha[2] 247.80 0.04 2.70 242.54 245.97 247.79 249.62 253.07 5745 1 315 | alpha[3] 252.44 0.04 2.60 247.27 250.72 252.46 254.21 257.55 4957 1 316 | alpha[4] 232.56 0.04 2.65 227.48 230.69 232.54 234.34 237.79 5564 1 317 | alpha[5] 231.58 0.03 2.73 226.42 229.71 231.56 233.46 236.86 6405 1 318 | alpha[6] 249.76 0.04 2.71 244.61 247.94 249.74 251.60 255.03 5168 1 319 | alpha[7] 228.66 0.03 2.67 223.37 226.97 228.65 230.47 233.89 6562 1 320 | alpha[8] 248.41 0.03 2.69 243.04 246.62 248.43 250.21 253.52 5921 1 321 | alpha[9] 283.31 0.04 2.70 277.91 281.57 283.37 285.05 288.59 4862 1 322 | alpha[10] 219.31 0.03 2.66 214.14 217.52 219.26 221.14 224.55 5997 1 323 | alpha[11] 258.25 0.04 2.71 252.87 256.39 258.32 260.12 263.41 5403 1 324 | alpha[12] 228.17 0.04 2.63 223.21 226.39 228.14 229.91 233.39 5125 1 325 | alpha[13] 242.39 0.04 2.68 237.08 240.57 242.45 244.22 247.70 5586 1 326 | alpha[14] 268.25 0.04 2.66 262.97 266.43 268.29 270.08 273.34 5341 1 327 | alpha[15] 242.73 0.03 2.65 237.51 241.02 242.71 244.48 247.91 5750 1 328 | alpha[16] 245.35 0.03 2.68 239.99 243.54 245.32 247.15 250.60 6011 1 329 | alpha[17] 232.16 0.04 2.71 226.83 230.31 232.15 233.99 237.51 5920 1 330 | alpha[18] 240.42 0.03 2.64 235.33 238.69 240.43 242.18 245.52 5940 1 331 | alpha[19] 253.77 0.04 2.68 248.45 251.99 253.80 255.54 259.06 5602 1 332 | alpha[20] 241.62 0.03 2.60 236.58 239.91 241.57 243.34 246.78 6264 1 333 | alpha[21] 248.59 0.03 2.70 243.24 246.79 248.56 250.41 253.85 6674 1 334 | alpha[22] 225.31 0.04 2.77 219.92 223.48 225.28 227.18 230.91 6170 1 335 | alpha[23] 228.52 0.03 2.61 223.43 226.79 228.54 230.24 233.62 6656 1 336 | alpha[24] 245.11 0.03 2.62 239.91 243.40 245.14 246.83 250.29 6691 1 337 | alpha[25] 234.44 0.03 2.69 229.26 232.63 234.43 236.23 239.82 6256 1 338 | alpha[26] 253.92 0.04 2.61 248.70 252.16 253.92 255.65 259.09 5479 1 339 | alpha[27] 254.27 0.03 2.57 249.27 252.55 254.25 255.96 259.55 5780 1 340 | alpha[28] 243.01 0.04 2.70 237.55 241.20 243.04 244.87 248.15 5767 1 341 | alpha[29] 217.91 0.03 2.69 212.74 216.09 217.89 219.71 223.13 6317 1 342 | alpha[30] 241.42 0.03 2.61 236.37 239.64 241.41 243.24 246.48 6029 1 343 | mu_alpha 242.47 0.05 2.76 236.95 240.61 242.50 244.38 247.70 3585 1 344 | sigmasq_alpha 218.39 1.06 63.89 126.08 173.31 208.62 251.30 372.24 3615 1 345 | sigma_alpha 14.63 0.03 2.07 11.23 13.16 14.44 15.85 19.29 3919 1 346 | alpha0 106.39 0.06 3.60 99.23 104.00 106.44 108.76 113.55 4122 1 347 | 348 | Samples were drawn using at Fri Jul 31 07:47:56 2020. 349 | For each parameter, n_eff is a crude measure of effective sample size, 350 | and Rhat is the potential scale reduction factor on split chains (at 351 | convergence, Rhat=1). 352 | 353 | ``` 354 | 355 |
356 |
357 | 358 | 359 | ```r 360 | rats%>% 361 | stan_select(stan_contains('alpha\\[1\\]')) 362 | Inference for Stan model: rats. 363 | 4 chains, each with iter=2000; warmup=1000; thin=1; 364 | post-warmup draws per chain=1000, total post-warmup draws=4000. 365 | 366 | mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat 367 | alpha[1] 239.93 0.03 2.61 234.75 238.22 239.91 241.71 244.96 6102 1 368 | 369 | Samples were drawn using at Fri Jul 31 07:47:56 2020. 370 | For each parameter, n_eff is a crude measure of effective sample size, 371 | and Rhat is the potential scale reduction factor on split chains (at 372 | convergence, Rhat=1). 373 | 374 | rats%>% 375 | stan_select(stan_contains('alpha\\[[1-9]\\]')) 376 | Inference for Stan model: rats. 377 | 4 chains, each with iter=2000; warmup=1000; thin=1; 378 | post-warmup draws per chain=1000, total post-warmup draws=4000. 379 | 380 | mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat 381 | alpha[1] 239.93 0.03 2.61 234.75 238.22 239.91 241.71 244.96 6102 1 382 | alpha[2] 247.80 0.04 2.70 242.54 245.97 247.79 249.62 253.07 5745 1 383 | alpha[3] 252.44 0.04 2.60 247.27 250.72 252.46 254.21 257.55 4957 1 384 | alpha[4] 232.56 0.04 2.65 227.48 230.69 232.54 234.34 237.79 5564 1 385 | alpha[5] 231.58 0.03 2.73 226.42 229.71 231.56 233.46 236.86 6405 1 386 | alpha[6] 249.76 0.04 2.71 244.61 247.94 249.74 251.60 255.03 5168 1 387 | alpha[7] 228.66 0.03 2.67 223.37 226.97 228.65 230.47 233.89 6562 1 388 | alpha[8] 248.41 0.03 2.69 243.04 246.62 248.43 250.21 253.52 5921 1 389 | alpha[9] 283.31 0.04 2.70 277.91 281.57 283.37 285.05 288.59 4862 1 390 | 391 | Samples were drawn using at Fri Jul 31 07:47:56 2020. 392 | For each parameter, n_eff is a crude measure of effective sample size, 393 | and Rhat is the potential scale reduction factor on split chains (at 394 | convergence, Rhat=1). 395 | 396 | rats%>% 397 | stan_select(stan_ends_with('0')) 398 | Inference for Stan model: rats. 399 | 4 chains, each with iter=2000; warmup=1000; thin=1; 400 | post-warmup draws per chain=1000, total post-warmup draws=4000. 401 | 402 | mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat 403 | alpha0 106.39 0.06 3.6 99.23 104 106.44 108.76 113.55 4122 1 404 | 405 | Samples were drawn using at Fri Jul 31 07:47:56 2020. 406 | For each parameter, n_eff is a crude measure of effective sample size, 407 | and Rhat is the potential scale reduction factor on split chains (at 408 | convergence, Rhat=1). 409 | 410 | rats%>% 411 | stan_select(mu_alpha,stan_ends_with('0'),beta[1]) 412 | Inference for Stan model: rats. 413 | 4 chains, each with iter=2000; warmup=1000; thin=1; 414 | post-warmup draws per chain=1000, total post-warmup draws=4000. 415 | 416 | mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat 417 | beta[1] 6.06 0.00 0.24 5.59 5.91 6.07 6.22 6.53 5584 1 418 | mu_alpha 242.47 0.05 2.76 236.95 240.61 242.50 244.38 247.70 3585 1 419 | alpha0 106.39 0.06 3.60 99.23 104.00 106.44 108.76 113.55 4122 1 420 | 421 | Samples were drawn using at Fri Jul 31 07:47:56 2020. 422 | For each parameter, n_eff is a crude measure of effective sample size, 423 | and Rhat is the potential scale reduction factor on split chains (at 424 | convergence, Rhat=1). 425 | ``` 426 | 427 | ## Post-warmup samples 428 | 429 | ### Subsetting post warmup samples 430 | 431 | 432 | ```r 433 | 434 | rats%>% 435 | stan_slice(1:50,inc_warmup = TRUE) 436 | 437 | ``` 438 | 439 |
440 | First 50 with warmup samples 441 | 442 | ```r 443 | 444 | Inference for Stan model: rats. 445 | 4 chains, each with iter=1050; warmup=1000; thin=1; 446 | post-warmup draws per chain=50, total post-warmup draws=200. 447 | 448 | mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat 449 | alpha[1] 239.91 0.13 2.79 234.83 237.75 239.96 241.93 244.89 460 0.98 450 | alpha[2] 247.78 0.14 2.86 241.44 246.16 247.74 249.45 253.35 420 0.98 451 | alpha[3] 252.50 0.16 2.45 248.25 250.85 252.45 254.18 257.19 248 0.99 452 | alpha[4] 232.60 0.15 2.54 228.13 230.63 232.46 234.50 237.33 305 0.99 453 | alpha[5] 231.47 0.12 2.67 227.33 229.23 231.24 233.56 236.06 460 1.00 454 | alpha[6] 249.85 0.15 3.00 244.16 247.56 249.99 252.13 255.52 381 1.00 455 | alpha[7] 228.31 0.15 2.92 222.42 226.57 228.58 230.07 234.15 390 1.00 456 | alpha[8] 248.23 0.15 2.47 243.59 246.46 248.43 249.74 253.05 259 0.99 457 | alpha[9] 283.23 0.14 2.60 278.46 281.62 283.14 284.70 288.23 323 1.00 458 | alpha[10] 219.20 0.12 2.52 213.74 217.56 219.18 220.79 224.63 460 0.98 459 | alpha[11] 258.04 0.13 2.87 252.82 256.02 258.16 260.11 263.35 460 0.99 460 | alpha[12] 228.07 0.14 2.71 222.44 226.56 228.26 229.76 233.11 365 0.99 461 | alpha[13] 242.45 0.12 2.48 237.67 240.59 242.51 244.51 246.94 460 0.99 462 | alpha[14] 267.90 0.14 2.59 262.43 266.33 268.04 269.78 272.54 321 0.99 463 | alpha[15] 242.74 0.14 2.92 236.82 240.99 242.48 245.04 247.51 410 1.00 464 | alpha[16] 245.43 0.13 2.70 240.26 243.67 245.64 247.11 250.60 421 0.99 465 | alpha[17] 232.36 0.13 2.87 227.26 230.75 232.33 234.21 237.85 460 0.99 466 | alpha[18] 240.19 0.12 2.59 235.58 238.41 240.10 242.16 245.37 460 0.99 467 | alpha[19] 253.65 0.15 2.61 248.83 252.11 253.51 255.05 259.02 321 0.99 468 | alpha[20] 241.74 0.16 2.69 235.82 239.79 241.78 243.64 246.05 283 1.01 469 | alpha[21] 248.44 0.14 2.61 243.52 246.75 248.35 250.14 253.85 344 0.99 470 | alpha[22] 225.52 0.17 2.76 219.87 223.87 225.62 227.20 231.14 263 1.01 471 | alpha[23] 228.73 0.13 2.74 223.15 227.00 228.84 230.37 233.41 460 0.98 472 | alpha[24] 245.03 0.13 2.86 239.83 243.20 245.00 246.66 250.47 460 0.99 473 | alpha[25] 234.28 0.15 3.00 228.18 232.45 234.19 236.23 239.98 399 0.99 474 | alpha[26] 253.90 0.14 2.42 249.18 252.24 253.86 255.53 258.53 297 1.00 475 | alpha[27] 254.31 0.12 2.34 250.24 252.57 254.30 255.84 259.02 354 0.99 476 | alpha[28] 242.91 0.14 2.53 238.26 241.10 242.93 244.68 247.30 325 0.99 477 | alpha[29] 218.12 0.15 2.65 213.31 216.19 218.28 220.05 222.61 294 0.99 478 | alpha[30] 241.59 0.12 2.50 237.41 239.78 241.63 243.33 246.55 432 0.99 479 | beta[1] 6.08 0.02 0.22 5.66 5.93 6.08 6.23 6.48 187 1.02 480 | beta[2] 7.02 0.01 0.25 6.55 6.85 7.00 7.20 7.43 323 1.00 481 | beta[3] 6.46 0.02 0.23 6.00 6.30 6.46 6.60 6.88 204 1.00 482 | beta[4] 5.36 0.01 0.31 4.70 5.16 5.35 5.57 5.98 460 0.98 483 | beta[5] 6.58 0.02 0.25 6.15 6.40 6.58 6.75 7.01 141 1.00 484 | beta[6] 6.19 0.01 0.22 5.75 6.03 6.20 6.36 6.57 334 0.99 485 | beta[7] 5.98 0.01 0.25 5.53 5.81 5.97 6.14 6.49 367 0.99 486 | beta[8] 6.42 0.01 0.27 5.93 6.23 6.40 6.60 7.00 460 0.99 487 | beta[9] 7.03 0.01 0.25 6.58 6.85 7.02 7.18 7.50 353 1.00 488 | beta[10] 5.85 0.01 0.23 5.42 5.69 5.85 5.99 6.31 344 0.99 489 | beta[11] 6.80 0.01 0.26 6.24 6.62 6.82 6.97 7.22 460 0.99 490 | beta[12] 6.12 0.01 0.22 5.69 5.99 6.12 6.28 6.54 236 1.00 491 | beta[13] 6.17 0.01 0.26 5.69 5.98 6.18 6.35 6.68 339 1.00 492 | beta[14] 6.67 0.01 0.23 6.21 6.52 6.67 6.84 7.09 367 0.99 493 | beta[15] 5.44 0.01 0.30 4.79 5.24 5.43 5.64 6.02 460 0.98 494 | beta[16] 5.93 0.01 0.23 5.54 5.77 5.93 6.07 6.39 263 0.98 495 | beta[17] 6.27 0.01 0.22 5.85 6.11 6.27 6.47 6.65 460 0.99 496 | beta[18] 5.86 0.01 0.25 5.39 5.69 5.86 6.06 6.35 364 1.00 497 | beta[19] 6.43 0.01 0.24 5.98 6.25 6.42 6.62 6.85 338 1.01 498 | beta[20] 6.06 0.01 0.23 5.63 5.88 6.07 6.24 6.48 378 1.01 499 | beta[21] 6.42 0.01 0.23 5.98 6.28 6.41 6.58 6.83 460 0.98 500 | beta[22] 5.86 0.01 0.23 5.42 5.70 5.88 6.01 6.31 292 0.99 501 | beta[23] 5.76 0.01 0.27 5.19 5.59 5.76 5.92 6.30 460 0.98 502 | beta[24] 5.89 0.01 0.25 5.45 5.71 5.89 6.07 6.37 295 0.99 503 | beta[25] 6.90 0.01 0.26 6.45 6.71 6.89 7.09 7.43 342 0.99 504 | beta[26] 6.53 0.01 0.26 6.06 6.36 6.55 6.71 6.99 366 1.00 505 | beta[27] 5.89 0.01 0.23 5.44 5.74 5.88 6.05 6.30 289 0.99 506 | beta[28] 5.85 0.01 0.24 5.41 5.68 5.83 6.02 6.30 304 0.99 507 | beta[29] 5.69 0.01 0.27 5.12 5.52 5.68 5.89 6.21 386 0.99 508 | beta[30] 6.13 0.01 0.24 5.71 5.95 6.13 6.30 6.57 440 0.99 509 | mu_alpha 242.53 0.12 2.47 237.91 240.80 242.59 244.18 247.66 456 0.99 510 | mu_beta 6.19 0.01 0.11 5.99 6.11 6.18 6.27 6.43 232 0.99 511 | sigmasq_y 37.72 0.33 4.77 29.83 34.21 37.63 41.17 46.63 210 1.00 512 | sigmasq_alpha 216.38 3.77 66.47 122.44 170.32 206.60 249.85 367.04 311 1.00 513 | sigmasq_beta 0.27 0.01 0.10 0.12 0.20 0.25 0.31 0.54 190 1.01 514 | sigma_y 6.13 0.03 0.39 5.46 5.85 6.13 6.42 6.83 204 1.00 515 | sigma_alpha 14.55 0.12 2.17 11.07 13.05 14.37 15.81 19.16 321 1.00 516 | sigma_beta 0.51 0.01 0.09 0.34 0.45 0.50 0.56 0.73 175 1.01 517 | alpha0 106.45 0.19 3.55 99.33 104.08 106.14 109.01 112.98 343 0.98 518 | lp__ -438.37 1.01 6.69 -455.55 -442.40 -437.50 -433.84 -427.19 44 1.10 519 | 520 | Samples were drawn using at Fri Jul 31 07:47:56 2020. 521 | For each parameter, n_eff is a crude measure of effective sample size, 522 | and Rhat is the potential scale reduction factor on split chains (at 523 | convergence, Rhat=1). 524 | 525 | ``` 526 | 527 |
528 |
529 | 530 | 531 | ```r 532 | rats%>% 533 | stan_slice(1:50,inc_warmup = FALSE) 534 | 535 | ``` 536 | 537 | 538 |
539 | First 50 draws from each chain without warmup samples 540 | 541 | ```r 542 | 543 | Inference for Stan model: rats. 544 | 4 chains, each with iter=50; warmup=0; thin=1; 545 | post-warmup draws per chain=50, total post-warmup draws=200. 546 | 547 | mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat 548 | alpha[1] 239.91 0.13 2.79 234.83 237.75 239.96 241.93 244.89 460 0.98 549 | alpha[2] 247.78 0.14 2.86 241.44 246.16 247.74 249.45 253.35 420 0.98 550 | alpha[3] 252.50 0.16 2.45 248.25 250.85 252.45 254.18 257.19 248 0.99 551 | alpha[4] 232.60 0.15 2.54 228.13 230.63 232.46 234.50 237.33 305 0.99 552 | alpha[5] 231.47 0.12 2.67 227.33 229.23 231.24 233.56 236.06 460 1.00 553 | alpha[6] 249.85 0.15 3.00 244.16 247.56 249.99 252.13 255.52 381 1.00 554 | alpha[7] 228.31 0.15 2.92 222.42 226.57 228.58 230.07 234.15 390 1.00 555 | alpha[8] 248.23 0.15 2.47 243.59 246.46 248.43 249.74 253.05 259 0.99 556 | alpha[9] 283.23 0.14 2.60 278.46 281.62 283.14 284.70 288.23 323 1.00 557 | alpha[10] 219.20 0.12 2.52 213.74 217.56 219.18 220.79 224.63 460 0.98 558 | alpha[11] 258.04 0.13 2.87 252.82 256.02 258.16 260.11 263.35 460 0.99 559 | alpha[12] 228.07 0.14 2.71 222.44 226.56 228.26 229.76 233.11 365 0.99 560 | alpha[13] 242.45 0.12 2.48 237.67 240.59 242.51 244.51 246.94 460 0.99 561 | alpha[14] 267.90 0.14 2.59 262.43 266.33 268.04 269.78 272.54 321 0.99 562 | alpha[15] 242.74 0.14 2.92 236.82 240.99 242.48 245.04 247.51 410 1.00 563 | alpha[16] 245.43 0.13 2.70 240.26 243.67 245.64 247.11 250.60 421 0.99 564 | alpha[17] 232.36 0.13 2.87 227.26 230.75 232.33 234.21 237.85 460 0.99 565 | alpha[18] 240.19 0.12 2.59 235.58 238.41 240.10 242.16 245.37 460 0.99 566 | alpha[19] 253.65 0.15 2.61 248.83 252.11 253.51 255.05 259.02 321 0.99 567 | alpha[20] 241.74 0.16 2.69 235.82 239.79 241.78 243.64 246.05 283 1.01 568 | alpha[21] 248.44 0.14 2.61 243.52 246.75 248.35 250.14 253.85 344 0.99 569 | alpha[22] 225.52 0.17 2.76 219.87 223.87 225.62 227.20 231.14 263 1.01 570 | alpha[23] 228.73 0.13 2.74 223.15 227.00 228.84 230.37 233.41 460 0.98 571 | alpha[24] 245.03 0.13 2.86 239.83 243.20 245.00 246.66 250.47 460 0.99 572 | alpha[25] 234.28 0.15 3.00 228.18 232.45 234.19 236.23 239.98 399 0.99 573 | alpha[26] 253.90 0.14 2.42 249.18 252.24 253.86 255.53 258.53 297 1.00 574 | alpha[27] 254.31 0.12 2.34 250.24 252.57 254.30 255.84 259.02 354 0.99 575 | alpha[28] 242.91 0.14 2.53 238.26 241.10 242.93 244.68 247.30 325 0.99 576 | alpha[29] 218.12 0.15 2.65 213.31 216.19 218.28 220.05 222.61 294 0.99 577 | alpha[30] 241.59 0.12 2.50 237.41 239.78 241.63 243.33 246.55 432 0.99 578 | beta[1] 6.08 0.02 0.22 5.66 5.93 6.08 6.23 6.48 187 1.02 579 | beta[2] 7.02 0.01 0.25 6.55 6.85 7.00 7.20 7.43 323 1.00 580 | beta[3] 6.46 0.02 0.23 6.00 6.30 6.46 6.60 6.88 204 1.00 581 | beta[4] 5.36 0.01 0.31 4.70 5.16 5.35 5.57 5.98 460 0.98 582 | beta[5] 6.58 0.02 0.25 6.15 6.40 6.58 6.75 7.01 141 1.00 583 | beta[6] 6.19 0.01 0.22 5.75 6.03 6.20 6.36 6.57 334 0.99 584 | beta[7] 5.98 0.01 0.25 5.53 5.81 5.97 6.14 6.49 367 0.99 585 | beta[8] 6.42 0.01 0.27 5.93 6.23 6.40 6.60 7.00 460 0.99 586 | beta[9] 7.03 0.01 0.25 6.58 6.85 7.02 7.18 7.50 353 1.00 587 | beta[10] 5.85 0.01 0.23 5.42 5.69 5.85 5.99 6.31 344 0.99 588 | beta[11] 6.80 0.01 0.26 6.24 6.62 6.82 6.97 7.22 460 0.99 589 | beta[12] 6.12 0.01 0.22 5.69 5.99 6.12 6.28 6.54 236 1.00 590 | beta[13] 6.17 0.01 0.26 5.69 5.98 6.18 6.35 6.68 339 1.00 591 | beta[14] 6.67 0.01 0.23 6.21 6.52 6.67 6.84 7.09 367 0.99 592 | beta[15] 5.44 0.01 0.30 4.79 5.24 5.43 5.64 6.02 460 0.98 593 | beta[16] 5.93 0.01 0.23 5.54 5.77 5.93 6.07 6.39 263 0.98 594 | beta[17] 6.27 0.01 0.22 5.85 6.11 6.27 6.47 6.65 460 0.99 595 | beta[18] 5.86 0.01 0.25 5.39 5.69 5.86 6.06 6.35 364 1.00 596 | beta[19] 6.43 0.01 0.24 5.98 6.25 6.42 6.62 6.85 338 1.01 597 | beta[20] 6.06 0.01 0.23 5.63 5.88 6.07 6.24 6.48 378 1.01 598 | beta[21] 6.42 0.01 0.23 5.98 6.28 6.41 6.58 6.83 460 0.98 599 | beta[22] 5.86 0.01 0.23 5.42 5.70 5.88 6.01 6.31 292 0.99 600 | beta[23] 5.76 0.01 0.27 5.19 5.59 5.76 5.92 6.30 460 0.98 601 | beta[24] 5.89 0.01 0.25 5.45 5.71 5.89 6.07 6.37 295 0.99 602 | beta[25] 6.90 0.01 0.26 6.45 6.71 6.89 7.09 7.43 342 0.99 603 | beta[26] 6.53 0.01 0.26 6.06 6.36 6.55 6.71 6.99 366 1.00 604 | beta[27] 5.89 0.01 0.23 5.44 5.74 5.88 6.05 6.30 289 0.99 605 | beta[28] 5.85 0.01 0.24 5.41 5.68 5.83 6.02 6.30 304 0.99 606 | beta[29] 5.69 0.01 0.27 5.12 5.52 5.68 5.89 6.21 386 0.99 607 | beta[30] 6.13 0.01 0.24 5.71 5.95 6.13 6.30 6.57 440 0.99 608 | mu_alpha 242.53 0.12 2.47 237.91 240.80 242.59 244.18 247.66 456 0.99 609 | mu_beta 6.19 0.01 0.11 5.99 6.11 6.18 6.27 6.43 232 0.99 610 | sigmasq_y 37.72 0.33 4.77 29.83 34.21 37.63 41.17 46.63 210 1.00 611 | sigmasq_alpha 216.38 3.77 66.47 122.44 170.32 206.60 249.85 367.04 311 1.00 612 | sigmasq_beta 0.27 0.01 0.10 0.12 0.20 0.25 0.31 0.54 190 1.01 613 | sigma_y 6.13 0.03 0.39 5.46 5.85 6.13 6.42 6.83 204 1.00 614 | sigma_alpha 14.55 0.12 2.17 11.07 13.05 14.37 15.81 19.16 321 1.00 615 | sigma_beta 0.51 0.01 0.09 0.34 0.45 0.50 0.56 0.73 175 1.01 616 | alpha0 106.45 0.19 3.55 99.33 104.08 106.14 109.01 112.98 343 0.98 617 | lp__ -438.37 1.01 6.69 -455.55 -442.40 -437.50 -433.84 -427.19 44 1.10 618 | 619 | Samples were drawn using at Fri Jul 31 07:47:56 2020. 620 | For each parameter, n_eff is a crude measure of effective sample size, 621 | and Rhat is the potential scale reduction factor on split chains (at 622 | convergence, Rhat=1). 623 | 624 | ``` 625 | 626 |
627 |
628 | 629 | 630 | ```r 631 | rats%>% 632 | stan_thin_n(2) 633 | 634 | ``` 635 | 636 |
637 | Thin every other sample 638 | 639 | ```r 640 | 641 | Inference for Stan model: rats. 642 | 4 chains, each with iter=1500; warmup=1000; thin=1; 643 | post-warmup draws per chain=500, total post-warmup draws=2000. 644 | 645 | mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat 646 | alpha[1] 239.88 0.06 2.61 234.83 238.14 239.85 241.65 244.97 1734 1 647 | alpha[2] 247.88 0.07 2.72 242.43 246.02 247.94 249.74 253.07 1644 1 648 | alpha[3] 252.43 0.06 2.59 247.35 250.72 252.45 254.22 257.56 1626 1 649 | alpha[4] 232.57 0.06 2.65 227.57 230.76 232.56 234.31 237.96 1960 1 650 | alpha[5] 231.55 0.06 2.71 226.40 229.69 231.54 233.44 236.79 1751 1 651 | alpha[6] 249.77 0.07 2.72 244.44 247.92 249.77 251.64 254.93 1643 1 652 | alpha[7] 228.63 0.06 2.66 223.31 226.89 228.65 230.42 233.70 1774 1 653 | alpha[8] 248.42 0.07 2.70 242.94 246.65 248.48 250.25 253.45 1563 1 654 | alpha[9] 283.28 0.06 2.68 277.99 281.53 283.28 285.06 288.41 1833 1 655 | alpha[10] 219.32 0.06 2.66 214.14 217.56 219.27 221.17 224.61 1731 1 656 | alpha[11] 258.33 0.06 2.71 253.12 256.52 258.41 260.22 263.72 1819 1 657 | alpha[12] 228.26 0.06 2.61 223.28 226.43 228.23 229.99 233.41 1903 1 658 | alpha[13] 242.40 0.06 2.66 237.01 240.61 242.45 244.17 247.57 2107 1 659 | alpha[14] 268.35 0.07 2.67 263.07 266.52 268.45 270.14 273.51 1667 1 660 | alpha[15] 242.81 0.06 2.66 237.47 241.08 242.77 244.61 247.80 1911 1 661 | alpha[16] 245.38 0.06 2.69 240.07 243.54 245.30 247.15 250.79 1789 1 662 | alpha[17] 232.15 0.07 2.70 226.75 230.26 232.19 233.95 237.54 1698 1 663 | alpha[18] 240.45 0.06 2.64 235.47 238.67 240.46 242.21 245.57 1670 1 664 | alpha[19] 253.74 0.06 2.68 248.38 252.00 253.83 255.52 258.96 1927 1 665 | alpha[20] 241.67 0.06 2.60 236.52 239.98 241.59 243.32 246.78 1858 1 666 | alpha[21] 248.68 0.06 2.69 243.50 246.84 248.63 250.47 253.88 1854 1 667 | alpha[22] 225.34 0.07 2.77 219.86 223.55 225.29 227.21 231.07 1581 1 668 | alpha[23] 228.53 0.07 2.61 223.42 226.79 228.54 230.25 233.54 1564 1 669 | alpha[24] 245.08 0.06 2.64 239.80 243.39 245.08 246.82 250.09 1756 1 670 | alpha[25] 234.24 0.06 2.68 228.92 232.47 234.28 236.09 239.49 1731 1 671 | alpha[26] 253.90 0.06 2.61 248.68 252.12 253.90 255.65 258.89 1650 1 672 | alpha[27] 254.29 0.06 2.58 249.26 252.54 254.25 256.02 259.47 1767 1 673 | alpha[28] 242.97 0.07 2.69 237.65 241.10 243.06 244.80 248.13 1699 1 674 | alpha[29] 217.91 0.06 2.69 212.79 216.06 217.88 219.73 223.16 1918 1 675 | alpha[30] 241.48 0.06 2.62 236.66 239.67 241.39 243.32 246.74 1711 1 676 | beta[1] 6.07 0.01 0.24 5.60 5.92 6.07 6.24 6.54 1894 1 677 | beta[2] 7.05 0.01 0.25 6.55 6.88 7.05 7.22 7.55 1938 1 678 | beta[3] 6.48 0.01 0.24 6.03 6.32 6.48 6.64 6.97 1786 1 679 | beta[4] 5.33 0.01 0.26 4.82 5.15 5.33 5.50 5.81 1475 1 680 | beta[5] 6.57 0.01 0.24 6.09 6.41 6.57 6.73 7.05 1715 1 681 | beta[6] 6.17 0.01 0.24 5.70 6.00 6.17 6.34 6.64 1922 1 682 | beta[7] 5.97 0.01 0.25 5.50 5.80 5.97 6.14 6.44 1906 1 683 | beta[8] 6.42 0.01 0.24 5.95 6.24 6.41 6.60 6.89 1837 1 684 | beta[9] 7.04 0.01 0.25 6.53 6.88 7.04 7.21 7.53 1682 1 685 | beta[10] 5.84 0.01 0.24 5.36 5.68 5.84 6.00 6.31 1743 1 686 | beta[11] 6.80 0.01 0.25 6.30 6.64 6.80 6.97 7.28 1685 1 687 | beta[12] 6.11 0.01 0.24 5.64 5.95 6.11 6.28 6.56 1906 1 688 | beta[13] 6.16 0.01 0.25 5.65 6.00 6.16 6.32 6.66 1552 1 689 | beta[14] 6.69 0.01 0.25 6.20 6.52 6.69 6.85 7.18 1661 1 690 | beta[15] 5.41 0.01 0.25 4.94 5.24 5.41 5.58 5.90 1754 1 691 | beta[16] 5.93 0.01 0.24 5.46 5.77 5.93 6.09 6.39 1765 1 692 | beta[17] 6.28 0.01 0.24 5.82 6.12 6.29 6.46 6.74 1574 1 693 | beta[18] 5.84 0.01 0.24 5.37 5.67 5.84 6.01 6.30 1754 1 694 | beta[19] 6.41 0.01 0.24 5.95 6.24 6.41 6.57 6.86 1808 1 695 | beta[20] 6.04 0.01 0.25 5.56 5.88 6.05 6.21 6.55 1645 1 696 | beta[21] 6.40 0.01 0.24 5.93 6.24 6.39 6.55 6.86 1668 1 697 | beta[22] 5.86 0.01 0.24 5.40 5.70 5.87 6.03 6.31 1675 1 698 | beta[23] 5.75 0.01 0.25 5.27 5.58 5.74 5.91 6.24 1586 1 699 | beta[24] 5.89 0.01 0.24 5.41 5.72 5.89 6.04 6.37 1821 1 700 | beta[25] 6.92 0.01 0.25 6.44 6.75 6.92 7.08 7.42 1625 1 701 | beta[26] 6.54 0.01 0.24 6.07 6.39 6.54 6.70 7.01 1899 1 702 | beta[27] 5.90 0.01 0.24 5.40 5.73 5.90 6.07 6.36 1676 1 703 | beta[28] 5.85 0.01 0.23 5.39 5.70 5.85 6.01 6.30 1681 1 704 | beta[29] 5.67 0.01 0.25 5.20 5.51 5.67 5.84 6.17 1791 1 705 | beta[30] 6.13 0.01 0.23 5.68 5.97 6.12 6.28 6.60 1893 1 706 | mu_alpha 242.48 0.07 2.75 237.09 240.58 242.49 244.39 247.68 1609 1 707 | mu_beta 6.19 0.00 0.11 5.99 6.12 6.19 6.26 6.40 1932 1 708 | sigmasq_y 37.10 0.15 5.72 27.69 33.06 36.60 40.53 50.33 1429 1 709 | sigmasq_alpha 219.35 1.56 65.38 126.94 173.43 209.82 252.77 375.88 1761 1 710 | sigmasq_beta 0.27 0.00 0.10 0.12 0.21 0.26 0.32 0.52 1392 1 711 | sigma_y 6.07 0.01 0.46 5.26 5.75 6.05 6.37 7.09 1417 1 712 | sigma_alpha 14.66 0.05 2.11 11.27 13.17 14.49 15.90 19.39 1804 1 713 | sigma_beta 0.51 0.00 0.09 0.35 0.45 0.51 0.57 0.72 1344 1 714 | alpha0 106.32 0.09 3.61 99.20 103.94 106.31 108.74 113.29 1763 1 715 | lp__ -437.94 0.23 6.93 -452.80 -442.36 -437.43 -433.01 -425.89 881 1 716 | 717 | Samples were drawn using at Fri Jul 31 07:47:56 2020. 718 | For each parameter, n_eff is a crude measure of effective sample size, 719 | and Rhat is the potential scale reduction factor on split chains (at 720 | convergence, Rhat=1). 721 | 722 | ``` 723 | 724 |
725 |
726 | 727 | 728 | ```r 729 | 730 | rats%>% 731 | stan_thin_frac(0.5) 732 | 733 | ``` 734 | 735 |
736 | Thin 50% of the Samples From each Chain 737 | 738 | ```r 739 | 740 | Inference for Stan model: rats. 741 | 4 chains, each with iter=1500; warmup=1000; thin=1; 742 | post-warmup draws per chain=500, total post-warmup draws=2000. 743 | 744 | mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat 745 | alpha[1] 239.89 0.06 2.61 234.83 238.17 239.85 241.66 245.03 1737 1 746 | alpha[2] 247.88 0.07 2.72 242.43 246.01 247.93 249.74 253.07 1651 1 747 | alpha[3] 252.43 0.06 2.59 247.35 250.71 252.45 254.22 257.56 1627 1 748 | alpha[4] 232.57 0.06 2.65 227.57 230.76 232.57 234.31 237.96 1974 1 749 | alpha[5] 231.55 0.06 2.71 226.40 229.71 231.54 233.44 236.79 1757 1 750 | alpha[6] 249.77 0.07 2.72 244.44 247.91 249.78 251.65 254.97 1652 1 751 | alpha[7] 228.63 0.06 2.66 223.31 226.89 228.65 230.42 233.70 1773 1 752 | alpha[8] 248.42 0.07 2.70 242.94 246.65 248.47 250.25 253.45 1549 1 753 | alpha[9] 283.28 0.06 2.68 278.02 281.54 283.28 285.06 288.41 1824 1 754 | alpha[10] 219.32 0.06 2.66 214.14 217.56 219.27 221.15 224.61 1730 1 755 | alpha[11] 258.33 0.06 2.71 253.12 256.52 258.39 260.21 263.72 1824 1 756 | alpha[12] 228.27 0.06 2.61 223.28 226.44 228.23 229.99 233.41 1907 1 757 | alpha[13] 242.39 0.06 2.66 237.01 240.60 242.45 244.16 247.57 2111 1 758 | alpha[14] 268.36 0.07 2.67 263.07 266.53 268.46 270.15 273.51 1668 1 759 | alpha[15] 242.80 0.06 2.66 237.47 241.08 242.77 244.60 247.80 1915 1 760 | alpha[16] 245.39 0.06 2.69 240.07 243.54 245.31 247.16 250.79 1791 1 761 | alpha[17] 232.14 0.07 2.70 226.75 230.25 232.19 233.94 237.54 1708 1 762 | alpha[18] 240.45 0.06 2.64 235.46 238.67 240.46 242.20 245.56 1678 1 763 | alpha[19] 253.74 0.06 2.68 248.38 252.00 253.83 255.52 258.96 1930 1 764 | alpha[20] 241.66 0.06 2.61 236.51 239.96 241.58 243.32 246.78 1866 1 765 | alpha[21] 248.67 0.06 2.69 243.50 246.83 248.62 250.47 253.88 1863 1 766 | alpha[22] 225.35 0.07 2.77 219.86 223.56 225.30 227.21 231.07 1576 1 767 | alpha[23] 228.54 0.07 2.61 223.43 226.80 228.55 230.26 233.54 1600 1 768 | alpha[24] 245.08 0.06 2.64 239.80 243.38 245.08 246.81 250.09 1758 1 769 | alpha[25] 234.24 0.06 2.68 228.92 232.47 234.29 236.09 239.49 1734 1 770 | alpha[26] 253.89 0.06 2.60 248.68 252.12 253.90 255.65 258.89 1654 1 771 | alpha[27] 254.29 0.06 2.58 249.26 252.54 254.26 256.02 259.47 1769 1 772 | alpha[28] 242.96 0.07 2.69 237.65 241.10 243.04 244.80 248.13 1694 1 773 | alpha[29] 217.91 0.06 2.69 212.79 216.08 217.88 219.73 223.16 1926 1 774 | alpha[30] 241.49 0.06 2.62 236.66 239.68 241.39 243.34 246.74 1712 1 775 | beta[1] 6.07 0.01 0.24 5.60 5.92 6.07 6.23 6.54 1897 1 776 | beta[2] 7.05 0.01 0.25 6.55 6.88 7.05 7.22 7.55 1932 1 777 | beta[3] 6.48 0.01 0.24 6.03 6.32 6.48 6.64 6.97 1787 1 778 | beta[4] 5.33 0.01 0.26 4.82 5.15 5.33 5.50 5.81 1479 1 779 | beta[5] 6.57 0.01 0.24 6.09 6.41 6.57 6.73 7.04 1721 1 780 | beta[6] 6.17 0.01 0.24 5.70 6.00 6.17 6.34 6.64 1926 1 781 | beta[7] 5.97 0.01 0.25 5.50 5.80 5.97 6.15 6.44 1897 1 782 | beta[8] 6.42 0.01 0.24 5.95 6.24 6.41 6.60 6.89 1853 1 783 | beta[9] 7.04 0.01 0.25 6.53 6.88 7.04 7.21 7.53 1687 1 784 | beta[10] 5.84 0.01 0.24 5.36 5.68 5.84 6.00 6.31 1743 1 785 | beta[11] 6.80 0.01 0.25 6.30 6.64 6.80 6.97 7.28 1683 1 786 | beta[12] 6.11 0.01 0.24 5.64 5.95 6.11 6.28 6.57 1915 1 787 | beta[13] 6.16 0.01 0.25 5.65 6.00 6.16 6.32 6.66 1581 1 788 | beta[14] 6.69 0.01 0.25 6.20 6.52 6.69 6.85 7.18 1675 1 789 | beta[15] 5.41 0.01 0.25 4.94 5.24 5.41 5.58 5.90 1757 1 790 | beta[16] 5.93 0.01 0.24 5.46 5.77 5.93 6.09 6.39 1770 1 791 | beta[17] 6.28 0.01 0.24 5.82 6.12 6.29 6.45 6.74 1574 1 792 | beta[18] 5.84 0.01 0.24 5.36 5.67 5.84 6.01 6.30 1766 1 793 | beta[19] 6.41 0.01 0.24 5.95 6.24 6.41 6.57 6.86 1812 1 794 | beta[20] 6.04 0.01 0.25 5.56 5.88 6.05 6.21 6.55 1652 1 795 | beta[21] 6.40 0.01 0.24 5.93 6.24 6.39 6.55 6.86 1675 1 796 | beta[22] 5.87 0.01 0.24 5.40 5.70 5.87 6.03 6.32 1695 1 797 | beta[23] 5.75 0.01 0.25 5.27 5.58 5.74 5.91 6.24 1580 1 798 | beta[24] 5.89 0.01 0.24 5.41 5.72 5.89 6.04 6.37 1840 1 799 | beta[25] 6.92 0.01 0.25 6.44 6.75 6.92 7.08 7.42 1624 1 800 | beta[26] 6.54 0.01 0.24 6.07 6.39 6.54 6.70 7.01 1906 1 801 | beta[27] 5.90 0.01 0.24 5.40 5.73 5.90 6.07 6.36 1661 1 802 | beta[28] 5.85 0.01 0.23 5.39 5.70 5.85 6.01 6.30 1682 1 803 | beta[29] 5.67 0.01 0.25 5.20 5.51 5.67 5.84 6.17 1794 1 804 | beta[30] 6.13 0.01 0.23 5.68 5.97 6.12 6.28 6.60 1912 1 805 | mu_alpha 242.49 0.07 2.75 237.12 240.59 242.50 244.40 247.69 1635 1 806 | mu_beta 6.19 0.00 0.11 5.99 6.12 6.19 6.26 6.40 1931 1 807 | sigmasq_y 37.12 0.15 5.73 27.69 33.06 36.60 40.58 50.49 1427 1 808 | sigmasq_alpha 219.35 1.56 65.39 126.94 173.43 209.76 252.95 375.88 1761 1 809 | sigmasq_beta 0.27 0.00 0.10 0.12 0.20 0.25 0.32 0.52 1387 1 810 | sigma_y 6.07 0.01 0.46 5.26 5.75 6.05 6.37 7.11 1414 1 811 | sigma_alpha 14.66 0.05 2.11 11.27 13.17 14.48 15.90 19.39 1804 1 812 | sigma_beta 0.51 0.00 0.09 0.35 0.45 0.50 0.57 0.72 1344 1 813 | alpha0 106.33 0.09 3.61 99.20 103.94 106.31 108.74 113.34 1779 1 814 | lp__ -437.94 0.23 6.94 -452.80 -442.36 -437.44 -433.00 -425.81 892 1 815 | 816 | Samples were drawn using at Fri Jul 31 07:47:56 2020. 817 | For each parameter, n_eff is a crude measure of effective sample size, 818 | and Rhat is the potential scale reduction factor on split chains (at 819 | convergence, Rhat=1). 820 | 821 | ``` 822 | 823 |
824 |
825 | 826 | ### Select and Slice 827 | 828 | 829 | ```r 830 | rats%>% 831 | stan_select(mu_alpha)%>% 832 | stan_slice(1:50) 833 | Inference for Stan model: rats. 834 | 4 chains, each with iter=1050; warmup=1000; thin=1; 835 | post-warmup draws per chain=50, total post-warmup draws=200. 836 | 837 | mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat 838 | mu_alpha 242.53 0.12 2.47 237.91 240.8 242.59 244.18 247.66 456 0.99 839 | 840 | Samples were drawn using at Fri Jul 31 07:47:56 2020. 841 | For each parameter, n_eff is a crude measure of effective sample size, 842 | and Rhat is the potential scale reduction factor on split chains (at 843 | convergence, Rhat=1). 844 | ``` 845 | 846 | ### Retain Chains 847 | 848 | 849 | ```r 850 | rats%>% 851 | stan_retain(chains = 1) 852 | Inference for Stan model: rats. 853 | 1 chains, each with iter=2000; warmup=1000; thin=1; 854 | post-warmup draws per chain=1000, total post-warmup draws=1000. 855 | 856 | mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat 857 | alpha[1] 239.92 0.06 2.63 234.74 238.25 239.91 241.69 245.06 2170 1 858 | alpha[2] 247.87 0.05 2.61 242.98 246.08 247.82 249.60 252.97 2298 1 859 | alpha[3] 252.45 0.06 2.70 247.15 250.62 252.45 254.34 257.65 1843 1 860 | alpha[4] 232.64 0.06 2.65 227.45 230.93 232.57 234.36 237.78 1767 1 861 | alpha[5] 231.74 0.07 2.71 226.51 229.86 231.68 233.63 236.85 1724 1 862 | alpha[6] 249.76 0.06 2.75 244.61 247.86 249.82 251.67 255.04 1852 1 863 | alpha[7] 228.73 0.05 2.51 223.78 227.14 228.70 230.32 233.52 2583 1 864 | alpha[8] 248.35 0.06 2.74 242.79 246.64 248.36 250.09 253.62 2378 1 865 | alpha[9] 283.35 0.07 2.66 277.98 281.61 283.41 285.06 288.55 1319 1 866 | alpha[10] 219.37 0.05 2.55 214.44 217.57 219.29 221.12 224.37 2180 1 867 | alpha[11] 258.19 0.06 2.60 252.95 256.47 258.25 259.93 262.81 1786 1 868 | alpha[12] 228.13 0.06 2.62 223.27 226.35 228.16 229.91 233.26 1895 1 869 | alpha[13] 242.36 0.06 2.65 237.01 240.64 242.42 244.13 247.74 2173 1 870 | alpha[14] 268.15 0.05 2.69 262.97 266.30 268.21 269.91 273.31 3000 1 871 | alpha[15] 242.80 0.06 2.59 237.71 241.10 242.72 244.45 247.70 1704 1 872 | alpha[16] 245.33 0.05 2.62 240.10 243.74 245.26 247.00 250.50 2458 1 873 | alpha[17] 232.11 0.06 2.73 226.75 230.21 232.12 234.01 237.16 2196 1 874 | alpha[18] 240.44 0.05 2.68 235.40 238.70 240.47 242.24 245.50 2400 1 875 | alpha[19] 253.79 0.05 2.66 248.75 251.89 253.84 255.62 259.00 2393 1 876 | alpha[20] 241.64 0.06 2.64 236.64 239.92 241.57 243.32 246.97 2169 1 877 | alpha[21] 248.54 0.05 2.61 243.40 246.83 248.54 250.26 253.69 2821 1 878 | alpha[22] 225.28 0.06 2.76 219.74 223.53 225.22 227.08 230.80 2119 1 879 | alpha[23] 228.54 0.06 2.61 223.46 226.80 228.53 230.24 233.78 1957 1 880 | alpha[24] 245.16 0.05 2.55 240.47 243.48 245.14 246.89 250.32 2590 1 881 | alpha[25] 234.40 0.06 2.78 229.02 232.58 234.35 236.16 239.94 2320 1 882 | alpha[26] 254.00 0.06 2.63 248.79 252.32 254.02 255.75 259.10 2220 1 883 | alpha[27] 254.26 0.06 2.49 249.30 252.62 254.22 255.92 259.31 1818 1 884 | alpha[28] 242.97 0.07 2.83 237.37 241.06 243.05 244.97 248.15 1622 1 885 | alpha[29] 217.81 0.06 2.70 212.33 216.03 217.83 219.73 222.94 2049 1 886 | alpha[30] 241.44 0.06 2.63 236.48 239.68 241.41 243.26 246.47 1665 1 887 | beta[1] 6.06 0.00 0.23 5.60 5.91 6.07 6.21 6.48 2245 1 888 | beta[2] 7.06 0.01 0.24 6.57 6.89 7.06 7.21 7.56 1707 1 889 | beta[3] 6.49 0.00 0.23 6.03 6.32 6.49 6.65 6.93 2116 1 890 | beta[4] 5.34 0.01 0.27 4.79 5.15 5.33 5.53 5.87 2281 1 891 | beta[5] 6.57 0.01 0.23 6.10 6.41 6.57 6.72 6.99 1751 1 892 | beta[6] 6.17 0.01 0.25 5.67 6.00 6.17 6.34 6.63 2401 1 893 | beta[7] 5.98 0.01 0.24 5.52 5.82 5.98 6.14 6.44 2003 1 894 | beta[8] 6.41 0.01 0.24 5.95 6.24 6.40 6.58 6.89 1659 1 895 | beta[9] 7.05 0.01 0.24 6.60 6.89 7.05 7.22 7.53 1659 1 896 | beta[10] 5.84 0.01 0.23 5.37 5.68 5.84 6.00 6.29 1914 1 897 | beta[11] 6.79 0.01 0.25 6.31 6.63 6.79 6.97 7.27 1469 1 898 | beta[12] 6.12 0.01 0.25 5.62 5.95 6.11 6.30 6.61 1922 1 899 | beta[13] 6.16 0.01 0.26 5.65 6.00 6.16 6.33 6.68 2391 1 900 | beta[14] 6.68 0.01 0.25 6.18 6.52 6.69 6.84 7.18 2237 1 901 | beta[15] 5.42 0.01 0.25 4.89 5.25 5.42 5.58 5.90 1693 1 902 | beta[16] 5.92 0.01 0.24 5.45 5.74 5.92 6.09 6.40 2303 1 903 | beta[17] 6.27 0.00 0.24 5.83 6.11 6.27 6.44 6.73 2462 1 904 | beta[18] 5.84 0.01 0.24 5.37 5.68 5.85 6.01 6.31 2121 1 905 | beta[19] 6.41 0.01 0.24 5.94 6.23 6.41 6.58 6.85 2258 1 906 | beta[20] 6.05 0.01 0.25 5.55 5.89 6.05 6.21 6.53 1895 1 907 | beta[21] 6.40 0.01 0.24 5.93 6.25 6.41 6.56 6.86 1789 1 908 | beta[22] 5.85 0.01 0.24 5.41 5.68 5.85 6.02 6.28 2047 1 909 | beta[23] 5.75 0.01 0.25 5.26 5.59 5.75 5.92 6.24 1552 1 910 | beta[24] 5.89 0.01 0.24 5.40 5.73 5.88 6.05 6.35 1880 1 911 | beta[25] 6.90 0.01 0.24 6.42 6.75 6.90 7.06 7.36 2259 1 912 | beta[26] 6.55 0.01 0.25 6.05 6.39 6.54 6.70 7.06 1926 1 913 | beta[27] 5.91 0.01 0.25 5.41 5.74 5.91 6.07 6.40 2106 1 914 | beta[28] 5.84 0.01 0.24 5.40 5.68 5.84 6.01 6.31 1765 1 915 | beta[29] 5.68 0.01 0.25 5.19 5.52 5.67 5.85 6.18 2156 1 916 | beta[30] 6.12 0.00 0.23 5.68 5.96 6.13 6.28 6.59 2313 1 917 | mu_alpha 242.45 0.07 2.65 237.11 240.71 242.54 244.26 247.36 1294 1 918 | mu_beta 6.18 0.00 0.10 5.98 6.12 6.18 6.25 6.39 1667 1 919 | sigmasq_y 37.25 0.22 5.68 28.06 33.17 36.69 40.47 50.07 698 1 920 | sigmasq_alpha 215.89 1.61 59.38 128.18 174.71 206.40 249.90 350.45 1363 1 921 | sigmasq_beta 0.27 0.00 0.11 0.13 0.20 0.26 0.32 0.52 921 1 922 | sigma_y 6.09 0.02 0.46 5.30 5.76 6.06 6.36 7.08 713 1 923 | sigma_alpha 14.56 0.05 1.96 11.32 13.22 14.37 15.81 18.72 1530 1 924 | sigma_beta 0.52 0.00 0.10 0.35 0.45 0.51 0.57 0.72 906 1 925 | alpha0 106.39 0.09 3.45 99.10 104.03 106.48 108.69 113.09 1623 1 926 | lp__ -437.72 0.44 7.11 -453.66 -442.15 -437.13 -432.48 -425.68 258 1 927 | 928 | Samples were drawn using NUTS(diag_e) at Fri Jul 31 07:47:56 2020. 929 | For each parameter, n_eff is a crude measure of effective sample size, 930 | and Rhat is the potential scale reduction factor on split chains (at 931 | convergence, Rhat=1). 932 | 933 | rats%>% 934 | stan_retain(chains = c(1,3)) 935 | Inference for Stan model: rats. 936 | 2 chains, each with iter=2000; warmup=1000; thin=1; 937 | post-warmup draws per chain=1000, total post-warmup draws=2000. 938 | 939 | mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat 940 | alpha[1] 239.93 0.05 2.62 234.76 238.23 239.93 241.67 245.03 3099 1 941 | alpha[2] 247.79 0.05 2.66 242.61 246.02 247.77 249.62 252.97 2933 1 942 | alpha[3] 252.42 0.05 2.61 247.18 250.62 252.44 254.21 257.37 2701 1 943 | alpha[4] 232.59 0.05 2.62 227.48 230.80 232.53 234.29 237.85 2701 1 944 | alpha[5] 231.64 0.05 2.79 226.42 229.75 231.62 233.54 236.83 3453 1 945 | alpha[6] 249.73 0.05 2.67 244.61 247.93 249.75 251.49 254.80 3206 1 946 | alpha[7] 228.71 0.04 2.63 223.56 227.05 228.73 230.47 233.87 3833 1 947 | alpha[8] 248.39 0.04 2.66 243.01 246.67 248.41 250.12 253.47 3636 1 948 | alpha[9] 283.37 0.05 2.67 277.99 281.65 283.42 285.09 288.65 2703 1 949 | alpha[10] 219.35 0.05 2.63 214.14 217.61 219.29 221.14 224.52 3070 1 950 | alpha[11] 258.24 0.05 2.73 252.73 256.40 258.35 260.10 263.17 2745 1 951 | alpha[12] 228.13 0.05 2.61 223.12 226.41 228.11 229.81 233.39 3232 1 952 | alpha[13] 242.39 0.05 2.65 237.01 240.65 242.44 244.15 247.57 3270 1 953 | alpha[14] 268.26 0.05 2.68 262.94 266.44 268.30 270.10 273.29 3253 1 954 | alpha[15] 242.77 0.05 2.61 237.60 241.07 242.71 244.45 247.88 3251 1 955 | alpha[16] 245.33 0.04 2.61 240.00 243.68 245.30 247.04 250.36 3807 1 956 | alpha[17] 232.12 0.05 2.66 226.96 230.28 232.14 233.96 237.32 3383 1 957 | alpha[18] 240.42 0.05 2.71 235.29 238.64 240.43 242.24 245.61 3322 1 958 | alpha[19] 253.78 0.05 2.65 248.60 251.96 253.84 255.59 259.02 3211 1 959 | alpha[20] 241.58 0.05 2.59 236.55 239.91 241.54 243.28 246.81 3202 1 960 | alpha[21] 248.57 0.04 2.68 243.27 246.77 248.53 250.35 253.76 3556 1 961 | alpha[22] 225.31 0.05 2.88 219.74 223.36 225.28 227.25 231.13 3694 1 962 | alpha[23] 228.52 0.05 2.69 223.14 226.68 228.56 230.32 233.73 3269 1 963 | alpha[24] 245.14 0.04 2.56 240.26 243.42 245.15 246.85 250.31 3584 1 964 | alpha[25] 234.40 0.05 2.68 229.18 232.65 234.40 236.12 239.76 3428 1 965 | alpha[26] 253.95 0.05 2.63 248.71 252.23 253.88 255.67 259.12 3152 1 966 | alpha[27] 254.23 0.05 2.57 249.21 252.55 254.22 255.95 259.36 3214 1 967 | alpha[28] 243.04 0.05 2.68 237.60 241.24 243.10 244.90 248.14 2857 1 968 | alpha[29] 217.91 0.05 2.70 212.69 216.10 217.91 219.76 223.15 3334 1 969 | alpha[30] 241.49 0.05 2.63 236.35 239.70 241.46 243.26 246.51 3283 1 970 | beta[1] 6.06 0.00 0.23 5.60 5.91 6.06 6.21 6.52 3456 1 971 | beta[2] 7.05 0.00 0.25 6.56 6.89 7.06 7.22 7.55 2749 1 972 | beta[3] 6.48 0.00 0.23 6.02 6.32 6.49 6.65 6.94 2486 1 973 | beta[4] 5.34 0.01 0.27 4.81 5.16 5.34 5.52 5.86 2645 1 974 | beta[5] 6.57 0.00 0.24 6.09 6.40 6.57 6.73 7.02 2798 1 975 | beta[6] 6.17 0.00 0.25 5.68 6.00 6.17 6.34 6.63 2961 1 976 | beta[7] 5.97 0.00 0.24 5.51 5.80 5.97 6.14 6.43 3017 1 977 | beta[8] 6.41 0.00 0.24 5.94 6.24 6.41 6.58 6.89 2634 1 978 | beta[9] 7.05 0.01 0.25 6.57 6.89 7.05 7.22 7.55 2503 1 979 | beta[10] 5.84 0.00 0.24 5.35 5.67 5.84 6.01 6.31 2944 1 980 | beta[11] 6.80 0.00 0.25 6.31 6.63 6.80 6.97 7.27 2519 1 981 | beta[12] 6.12 0.00 0.25 5.64 5.96 6.12 6.30 6.58 3406 1 982 | beta[13] 6.16 0.00 0.25 5.66 6.00 6.16 6.32 6.66 2941 1 983 | beta[14] 6.69 0.00 0.24 6.21 6.52 6.69 6.85 7.17 2680 1 984 | beta[15] 5.41 0.01 0.25 4.92 5.24 5.41 5.58 5.90 2289 1 985 | beta[16] 5.92 0.00 0.24 5.45 5.75 5.93 6.10 6.39 3040 1 986 | beta[17] 6.28 0.00 0.24 5.83 6.12 6.28 6.44 6.74 2847 1 987 | beta[18] 5.84 0.00 0.25 5.36 5.67 5.84 6.00 6.32 2542 1 988 | beta[19] 6.40 0.00 0.24 5.93 6.23 6.41 6.57 6.85 2731 1 989 | beta[20] 6.05 0.00 0.25 5.57 5.89 6.05 6.21 6.54 3117 1 990 | beta[21] 6.40 0.00 0.25 5.92 6.24 6.41 6.57 6.87 4018 1 991 | beta[22] 5.86 0.00 0.24 5.41 5.69 5.85 6.02 6.31 2991 1 992 | beta[23] 5.75 0.00 0.25 5.26 5.58 5.75 5.91 6.23 2926 1 993 | beta[24] 5.89 0.00 0.24 5.41 5.73 5.90 6.05 6.35 3390 1 994 | beta[25] 6.91 0.00 0.25 6.41 6.74 6.90 7.07 7.39 2701 1 995 | beta[26] 6.54 0.00 0.24 6.06 6.39 6.54 6.70 7.03 3062 1 996 | beta[27] 5.90 0.00 0.24 5.41 5.73 5.91 6.06 6.38 3491 1 997 | beta[28] 5.85 0.00 0.24 5.40 5.68 5.84 6.00 6.31 2925 1 998 | beta[29] 5.68 0.00 0.25 5.21 5.51 5.67 5.85 6.17 3167 1 999 | beta[30] 6.12 0.00 0.23 5.68 5.96 6.12 6.27 6.58 3553 1 1000 | mu_alpha 242.46 0.08 2.76 236.89 240.64 242.54 244.35 247.70 1114 1 1001 | mu_beta 6.19 0.00 0.10 5.99 6.12 6.19 6.25 6.40 2279 1 1002 | sigmasq_y 37.16 0.17 5.69 28.01 33.14 36.53 40.58 50.11 1107 1 1003 | sigmasq_alpha 217.43 1.40 60.83 127.60 174.99 208.54 249.60 367.33 1885 1 1004 | sigmasq_beta 0.27 0.00 0.10 0.13 0.20 0.26 0.32 0.52 1534 1 1005 | sigma_y 6.08 0.01 0.46 5.29 5.76 6.04 6.37 7.08 1119 1 1006 | sigma_alpha 14.61 0.04 1.99 11.30 13.23 14.44 15.80 19.17 2051 1 1007 | sigma_beta 0.51 0.00 0.09 0.35 0.45 0.51 0.57 0.72 1420 1 1008 | alpha0 106.37 0.08 3.56 99.21 104.05 106.42 108.70 113.59 2066 1 1009 | lp__ -437.92 0.30 7.09 -453.67 -442.33 -437.28 -433.00 -425.43 543 1 1010 | 1011 | Samples were drawn using NUTS(diag_e) at Fri Jul 31 07:47:56 2020. 1012 | For each parameter, n_eff is a crude measure of effective sample size, 1013 | and Rhat is the potential scale reduction factor on split chains (at 1014 | convergence, Rhat=1). 1015 | ``` 1016 | 1017 | 1018 | ### Filter 1019 | 1020 | Users can filter conditionally on posterior samples. The function will locate the indicies that the logical expression returns for each chain. Due to a constraint in `rstan::extract` with `permuted=FALSE` chains are assumed to be of equal size. To keep this assumption the chain size returned is the length of the shortest conditional chain. If there is a chain that results in no samples then the chain is dropped with a warning. If no elements are returned for any chain then `NULL` is returned. 1021 | 1022 | 1023 | ```r 1024 | rats%>% 1025 | stan_select(mu_alpha,mu_beta)%>% 1026 | stan_filter(mu_beta < 6) 1027 | Inference for Stan model: rats. 1028 | 4 chains, each with iter=1028; warmup=1000; thin=1; 1029 | post-warmup draws per chain=28, total post-warmup draws=112. 1030 | 1031 | mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat 1032 | mu_alpha 242.32 0.34 2.81 236.93 240.39 242.33 244.21 247.74 68 1.05 1033 | mu_beta 5.95 0.00 0.05 5.83 5.94 5.96 5.98 6.00 120 0.99 1034 | 1035 | Samples were drawn using at Fri Jul 31 07:47:56 2020. 1036 | For each parameter, n_eff is a crude measure of effective sample size, 1037 | and Rhat is the potential scale reduction factor on split chains (at 1038 | convergence, Rhat=1). 1039 | ``` 1040 | -------------------------------------------------------------------------------- /vignettes/rstan.Rmd.orig: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Working with rstan" 3 | output: rmarkdown::html_vignette 4 | vignette: > 5 | %\VignetteIndexEntry{Working with rstan} 6 | %\VignetteEngine{knitr::rmarkdown} 7 | %\VignetteEncoding{UTF-8} 8 | --- 9 | 10 | ```{r, include = FALSE} 11 | stopifnot(require(knitr)) 12 | options(width = 90) 13 | opts_chunk$set( 14 | comment = NA, 15 | message = FALSE, 16 | warning = FALSE, 17 | eval = TRUE, 18 | dev = "png", 19 | dpi = 150, 20 | fig.asp = 0.8, 21 | fig.width = 5, 22 | out.width = "60%", 23 | fig.align = "center" 24 | ) 25 | ``` 26 | 27 | ## Example 28 | 29 | This is a basic example which shows you how to solve a common problem: 30 | 31 | ```{r example} 32 | library(shredder) 33 | library(rstan) 34 | ``` 35 | 36 | ```{r,results='hide',warning=FALSE,message=FALSE} 37 | rats <- shredder::rats_example(nCores = 1) 38 | ``` 39 | 40 | ```{r,eval = FALSE} 41 | rats 42 | ``` 43 | 44 | ```{r,echo = FALSE} 45 | rats%>% 46 | details::details(summary = 'Standard Output') 47 | ``` 48 | 49 | ```{r,echo=FALSE} 50 | details::details(system.file('rats.stan',package='shredder'), 51 | summary = 'The Stan Script',lang = 'c') 52 | ``` 53 | 54 | 55 | ## Pars 56 | 57 | ### Names 58 | 59 | ```{r} 60 | rats%>% 61 | stan_names() 62 | 63 | rats%>% 64 | stan_names(expand = TRUE) 65 | ``` 66 | 67 | ### Select 68 | 69 | ```{r} 70 | rats%>% 71 | stan_select(mu_alpha) 72 | 73 | rats%>% 74 | stan_select(mu_alpha,mu_beta) 75 | 76 | rats%>% 77 | stan_select(!!!rlang::syms(c('mu_alpha','mu_beta'))) 78 | 79 | 80 | rats%>% 81 | stan_select(alpha[1],alpha[2]) 82 | 83 | 84 | rats%>% 85 | stan_select(!!!rlang::syms(sprintf('alpha[%s]',1:5))) 86 | 87 | ``` 88 | 89 | ### Select with Partials 90 | 91 | 92 | ```{r,eval=FALSE} 93 | rats%>% 94 | stan_select(stan_contains('alpha')) 95 | 96 | ``` 97 | 98 | ```{r,echo = FALSE} 99 | rats%>% 100 | stan_select(stan_contains('alpha'))%>% 101 | details::details(summary = 'Select all Parameters that contain "alpha"') 102 | 103 | ``` 104 | 105 | ```{r} 106 | rats%>% 107 | stan_select(stan_contains('alpha\\[1\\]')) 108 | 109 | rats%>% 110 | stan_select(stan_contains('alpha\\[[1-9]\\]')) 111 | 112 | rats%>% 113 | stan_select(stan_ends_with('0')) 114 | 115 | rats%>% 116 | stan_select(mu_alpha,stan_ends_with('0'),beta[1]) 117 | ``` 118 | 119 | ## Post-warmup samples 120 | 121 | ### Subsetting post warmup samples 122 | 123 | ```{r,eval=FALSE} 124 | 125 | rats%>% 126 | stan_slice(1:50,inc_warmup = TRUE) 127 | 128 | ``` 129 | 130 | ```{r,echo=FALSE} 131 | 132 | rats%>% 133 | stan_slice(1:50,inc_warmup = TRUE)%>% 134 | details::details(summary = 'First 50 with warmup samples') 135 | 136 | ``` 137 | 138 | ```{r,eval=FALSE} 139 | rats%>% 140 | stan_slice(1:50,inc_warmup = FALSE) 141 | 142 | ``` 143 | 144 | 145 | ```{r,echo=FALSE} 146 | rats%>% 147 | stan_slice(1:50,inc_warmup = FALSE)%>% 148 | details::details(summary = 'First 50 draws from each chain without warmup samples') 149 | 150 | ``` 151 | 152 | ```{r,eval=FALSE} 153 | rats%>% 154 | stan_thin_n(2) 155 | 156 | ``` 157 | 158 | ```{r,echo=FALSE} 159 | rats%>% 160 | stan_thin_n(2)%>% 161 | details::details(summary = 'Thin every other sample') 162 | 163 | ``` 164 | 165 | ```{r,eval=FALSE} 166 | 167 | rats%>% 168 | stan_thin_frac(0.5) 169 | 170 | ``` 171 | 172 | ```{r,echo=FALSE} 173 | rats%>% 174 | stan_thin_frac(0.5)%>% 175 | details::details(summary = 'Thin 50% of the Samples From each Chain') 176 | 177 | ``` 178 | 179 | ### Select and Slice 180 | 181 | ```{r} 182 | rats%>% 183 | stan_select(mu_alpha)%>% 184 | stan_slice(1:50) 185 | 186 | ``` 187 | 188 | ### Retain Chains 189 | 190 | ```{r} 191 | rats%>% 192 | stan_retain(chains = 1) 193 | 194 | rats%>% 195 | stan_retain(chains = c(1,3)) 196 | ``` 197 | 198 | 199 | ### Filter 200 | 201 | Users can filter conditionally on posterior samples. The function will locate the indicies that the logical expression returns for each chain. Due to a constraint in `rstan::extract` with `permuted=FALSE` chains are assumed to be of equal size. To keep this assumption the chain size returned is the length of the shortest conditional chain. If there is a chain that results in no samples then the chain is dropped with a warning. If no elements are returned for any chain then `NULL` is returned. 202 | 203 | ```{r} 204 | rats%>% 205 | stan_select(mu_alpha,mu_beta)%>% 206 | stan_filter(mu_beta < 6) 207 | 208 | ``` 209 | -------------------------------------------------------------------------------- /vignettes/tests_and_coverage.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Tests and Coverage" 3 | date: "`r format(Sys.time(), '%d %B, %Y %H:%M:%S')`" 4 | output: rmarkdown::html_vignette 5 | vignette: > 6 | %\VignetteIndexEntry{Tests and Coverage} 7 | %\VignetteEngine{knitr::rmarkdown} 8 | %\VignetteEncoding{UTF-8} 9 | --- 10 | 11 | ```{r setup, include = FALSE} 12 | knitr::opts_chunk$set( 13 | collapse = TRUE, 14 | comment = "#>", 15 | eval = FALSE 16 | ) 17 | ``` 18 | 19 | 20 | - [Coverage](#coverage) 21 | - [Unit Tests](#unit-tests) 22 | 23 | This output is created by 24 | [covrpage](https://github.com/metrumresearchgroup/covrpage). 25 | 26 | ## Coverage 27 | 28 | Coverage summary is created using the 29 | [covr](https://github.com/r-lib/covr) package. 30 | 31 | | Object | Coverage (%) | 32 | | :--------------------------------------- | :----------: | 33 | | shredder | 91.21 | 34 | | [R/rats\_example.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/R/rats_example.R) | 0.00 | 35 | | [R/stan\_split.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/R/stan_split.R) | 90.91 | 36 | | [R/partials.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/R/partials.R) | 100.00 | 37 | | [R/stan\_filter.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/R/stan_filter.R) | 100.00 | 38 | | [R/stan\_sample.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/R/stan_sample.R) | 100.00 | 39 | | [R/stan\_select.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/R/stan_select.R) | 100.00 | 40 | | [R/stan\_slice.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/R/stan_slice.R) | 100.00 | 41 | | [R/stan\_utils.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/R/stan_utils.R) | 100.00 | 42 | | [R/utils.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/R/utils.R) | 100.00 | 43 | 44 |
45 | 46 | ## Unit Tests 47 | 48 | Unit Test summary is created using the 49 | [testthat](https://github.com/r-lib/testthat) 50 | package. 51 | 52 | | file | n | time | error | failed | skipped | warning | 53 | | :------------------------------------------ | -: | ----: | ----: | -----: | ------: | ------: | 54 | | [test-filter.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/tests/testthat/test-filter.R) | 5 | 0.456 | 0 | 0 | 0 | 0 | 55 | | [test-names.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/tests/testthat/test-names.R) | 2 | 0.005 | 0 | 0 | 0 | 0 | 56 | | [test-sampling.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/tests/testthat/test-sampling.R) | 7 | 0.243 | 0 | 0 | 0 | 0 | 57 | | [test-select.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/tests/testthat/test-select.R) | 9 | 0.237 | 0 | 0 | 0 | 0 | 58 | | [test-split.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/tests/testthat/test-split.R) | 4 | 0.287 | 0 | 0 | 0 | 0 | 59 | 60 |
61 | 62 | Show Detailed Test Results 63 | 64 | 65 | | file | context | test | status | n | time | 66 | | :---------------------------------------------- | :------ | :------------------------- | :----- | -: | ----: | 67 | | [test-filter.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/tests/testthat/test-filter.R#L9) | filter | filters: default | PASS | 1 | 0.065 | 68 | | [test-filter.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/tests/testthat/test-filter.R#L14) | filter | filters: different chain | PASS | 1 | 0.372 | 69 | | [test-filter.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/tests/testthat/test-filter.R#L19) | filter | filters: indexed name | PASS | 1 | 0.009 | 70 | | [test-filter.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/tests/testthat/test-filter.R#L23) | filter | filters: no samples | PASS | 1 | 0.007 | 71 | | [test-filter.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/tests/testthat/test-filter.R#L27) | filter | filters: no pars | PASS | 1 | 0.003 | 72 | | [test-names.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/tests/testthat/test-names.R#L9) | names | names: default | PASS | 1 | 0.002 | 73 | | [test-names.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/tests/testthat/test-names.R#L14) | names | names: expand | PASS | 1 | 0.003 | 74 | | [test-sampling.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/tests/testthat/test-sampling.R#L10) | slicing | slice: default | PASS | 1 | 0.213 | 75 | | [test-sampling.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/tests/testthat/test-sampling.R#L15) | slicing | slice: no warmup | PASS | 1 | 0.002 | 76 | | [test-sampling.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/tests/testthat/test-sampling.R#L21) | slicing | slice: bad indexs | PASS | 2 | 0.014 | 77 | | [test-sampling.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/tests/testthat/test-sampling.R#L32) | slicing | sample: sample\_n | PASS | 1 | 0.005 | 78 | | [test-sampling.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/tests/testthat/test-sampling.R#L37) | slicing | sample: sample\_frac | PASS | 1 | 0.005 | 79 | | [test-sampling.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/tests/testthat/test-sampling.R#L42) | slicing | sample: no warmup | PASS | 1 | 0.004 | 80 | | [test-select.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/tests/testthat/test-select.R#L7) | select | names: single name | PASS | 1 | 0.003 | 81 | | [test-select.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/tests/testthat/test-select.R#L12) | select | names: multiple names | PASS | 1 | 0.001 | 82 | | [test-select.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/tests/testthat/test-select.R#L17) | select | names: character names | PASS | 1 | 0.002 | 83 | | [test-select.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/tests/testthat/test-select.R#L23) | select | names: remove summary | PASS | 1 | 0.222 | 84 | | [test-select.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/tests/testthat/test-select.R#L31) | select | partials: no pars | PASS | 1 | 0.002 | 85 | | [test-select.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/tests/testthat/test-select.R#L36) | select | partials: starts\_with | PASS | 1 | 0.001 | 86 | | [test-select.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/tests/testthat/test-select.R#L41) | select | partials: ends\_with | PASS | 1 | 0.002 | 87 | | [test-select.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/tests/testthat/test-select.R#L46) | select | partials: starts\_contains | PASS | 1 | 0.002 | 88 | | [test-select.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/tests/testthat/test-select.R#L51) | select | partials: mixed | PASS | 1 | 0.002 | 89 | | [test-split.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/tests/testthat/test-split.R#L7) | split | names: default | PASS | 1 | 0.053 | 90 | | [test-split.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/tests/testthat/test-split.R#L12) | split | names: specify cut | PASS | 1 | 0.219 | 91 | | [test-split.R](https://ghe.metrumrg.com/yoni/shredder/tree/master/tests/testthat/test-split.R#L17) | split | names: no warmup | PASS | 2 | 0.015 | 92 | 93 |
94 | 95 |
96 | 97 | Session Info 98 | 99 | | Field | Value | 100 | | :------- | :---------------------------------- | 101 | | Version | R version 3.5.1 (2018-07-02) | 102 | | Platform | x86\_64-apple-darwin15.6.0 (64-bit) | 103 | | Running | macOS 10.14.4 | 104 | | Language | en\_US | 105 | | Timezone | America/New\_York | 106 | 107 | | Package | Version | 108 | | :------- | :------ | 109 | | testthat | 2.0.1 | 110 | | covr | 3.2.1 | 111 | | covrpage | 0.0.70 | 112 | 113 |
114 | 115 | 116 | -------------------------------------------------------------------------------- /vignettes/tidybayes.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Interacting with tidybayes" 3 | output: rmarkdown::html_vignette 4 | vignette: > 5 | %\VignetteIndexEntry{Interacting with tidybayes} 6 | %\VignetteEngine{knitr::rmarkdown} 7 | %\VignetteEncoding{UTF-8} 8 | --- 9 | 10 | 11 | 12 | 13 | ```r 14 | library(rstan) 15 | library(shredder) 16 | library(tidybayes) 17 | ``` 18 | 19 | ## tidybayes 20 | 21 | 22 | ```r 23 | rats <- shredder::rats_example(nCores = 1) 24 | ``` 25 | 26 | ### Spread 27 | 28 | 29 | ```r 30 | rats%>% 31 | tidybayes::spread_draws(mu_alpha,mu_beta) 32 | #> # A tibble: 4,000 x 5 33 | #> .chain .iteration .draw mu_alpha mu_beta 34 | #> 35 | #> 1 1 1 1 243. 5.96 36 | #> 2 1 2 2 243. 6.41 37 | #> 3 1 3 3 247. 6.16 38 | #> 4 1 4 4 244. 6.15 39 | #> 5 1 5 5 246. 6.15 40 | #> 6 1 6 6 241. 6.19 41 | #> 7 1 7 7 245. 6.20 42 | #> 8 1 8 8 239. 6.19 43 | #> 9 1 9 9 244. 6.31 44 | #> 10 1 10 10 240. 5.93 45 | #> # … with 3,990 more rows 46 | 47 | rats%>% 48 | stan_thin_n(20)%>% 49 | tidybayes::spread_draws(mu_alpha,mu_beta) 50 | #> # A tibble: 200 x 5 51 | #> .chain .iteration .draw mu_alpha mu_beta 52 | #> 53 | #> 1 1 1 1 243. 5.96 54 | #> 2 1 2 2 242. 6.07 55 | #> 3 1 3 3 241. 6.18 56 | #> 4 1 4 4 245. 6.15 57 | #> 5 1 5 5 237. 6.14 58 | #> 6 1 6 6 239. 6.09 59 | #> 7 1 7 7 243. 6.32 60 | #> 8 1 8 8 240. 6.09 61 | #> 9 1 9 9 242. 6.31 62 | #> 10 1 10 10 241. 6.03 63 | #> # … with 190 more rows 64 | 65 | rats%>% 66 | stan_thin_n(20)%>% 67 | tidybayes::spread_draws(alpha[i],beta[i]) 68 | #> # A tibble: 6,000 x 6 69 | #> # Groups: i [30] 70 | #> i alpha .chain .iteration .draw beta 71 | #> 72 | #> 1 1 240. 1 1 1 6.63 73 | #> 2 1 241. 1 2 2 5.81 74 | #> 3 1 240. 1 3 3 6.25 75 | #> 4 1 241. 1 4 4 6.14 76 | #> 5 1 240. 1 5 5 5.91 77 | #> 6 1 242. 1 6 6 5.48 78 | #> 7 1 237. 1 7 7 6.30 79 | #> 8 1 241. 1 8 8 5.91 80 | #> 9 1 239. 1 9 9 6.24 81 | #> 10 1 242. 1 10 10 6.04 82 | #> # … with 5,990 more rows 83 | ``` 84 | 85 | ### Gather 86 | 87 | 88 | ```r 89 | rats%>% 90 | tidybayes::gather_draws(mu_alpha,mu_beta) 91 | #> # A tibble: 8,000 x 5 92 | #> # Groups: .variable [2] 93 | #> .chain .iteration .draw .variable .value 94 | #> 95 | #> 1 1 1 1 mu_alpha 243. 96 | #> 2 1 2 2 mu_alpha 243. 97 | #> 3 1 3 3 mu_alpha 247. 98 | #> 4 1 4 4 mu_alpha 244. 99 | #> 5 1 5 5 mu_alpha 246. 100 | #> 6 1 6 6 mu_alpha 241. 101 | #> 7 1 7 7 mu_alpha 245. 102 | #> 8 1 8 8 mu_alpha 239. 103 | #> 9 1 9 9 mu_alpha 244. 104 | #> 10 1 10 10 mu_alpha 240. 105 | #> # … with 7,990 more rows 106 | 107 | rats%>% 108 | stan_thin_n(20)%>% 109 | tidybayes::gather_draws(mu_alpha,mu_beta) 110 | #> # A tibble: 400 x 5 111 | #> # Groups: .variable [2] 112 | #> .chain .iteration .draw .variable .value 113 | #> 114 | #> 1 1 1 1 mu_alpha 243. 115 | #> 2 1 2 2 mu_alpha 242. 116 | #> 3 1 3 3 mu_alpha 241. 117 | #> 4 1 4 4 mu_alpha 245. 118 | #> 5 1 5 5 mu_alpha 237. 119 | #> 6 1 6 6 mu_alpha 239. 120 | #> 7 1 7 7 mu_alpha 243. 121 | #> 8 1 8 8 mu_alpha 240. 122 | #> 9 1 9 9 mu_alpha 242. 123 | #> 10 1 10 10 mu_alpha 241. 124 | #> # … with 390 more rows 125 | 126 | rats%>% 127 | stan_thin_n(20)%>% 128 | tidybayes::gather_draws(alpha[i],beta[i]) 129 | #> # A tibble: 12,000 x 6 130 | #> # Groups: i, .variable [60] 131 | #> i .chain .iteration .draw .variable .value 132 | #> 133 | #> 1 1 1 1 1 alpha 240. 134 | #> 2 1 1 2 2 alpha 241. 135 | #> 3 1 1 3 3 alpha 240. 136 | #> 4 1 1 4 4 alpha 241. 137 | #> 5 1 1 5 5 alpha 240. 138 | #> 6 1 1 6 6 alpha 242. 139 | #> 7 1 1 7 7 alpha 237. 140 | #> 8 1 1 8 8 alpha 241. 141 | #> 9 1 1 9 9 alpha 239. 142 | #> 10 1 1 10 10 alpha 242. 143 | #> # … with 11,990 more rows 144 | ``` 145 | -------------------------------------------------------------------------------- /vignettes/tidybayes.Rmd.orig: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Interacting with tidybayes" 3 | output: rmarkdown::html_vignette 4 | vignette: > 5 | %\VignetteIndexEntry{Interacting with tidybayes} 6 | %\VignetteEngine{knitr::rmarkdown} 7 | %\VignetteEncoding{UTF-8} 8 | --- 9 | 10 | ```{r, include = FALSE} 11 | knitr::opts_chunk$set( 12 | collapse = TRUE, 13 | comment = "#>" 14 | ) 15 | ``` 16 | 17 | ```{r setup} 18 | library(rstan) 19 | library(shredder) 20 | library(tidybayes) 21 | ``` 22 | 23 | ## tidybayes 24 | 25 | ```{r,results='hide',warning=FALSE,message=FALSE} 26 | rats <- shredder::rats_example(nCores = 1) 27 | ``` 28 | 29 | ### Spread 30 | 31 | ```{r} 32 | rats%>% 33 | tidybayes::spread_draws(mu_alpha,mu_beta) 34 | 35 | rats%>% 36 | stan_thin_n(20)%>% 37 | tidybayes::spread_draws(mu_alpha,mu_beta) 38 | 39 | rats%>% 40 | stan_thin_n(20)%>% 41 | tidybayes::spread_draws(alpha[i],beta[i]) 42 | 43 | ``` 44 | 45 | ### Gather 46 | 47 | ```{r} 48 | rats%>% 49 | tidybayes::gather_draws(mu_alpha,mu_beta) 50 | 51 | rats%>% 52 | stan_thin_n(20)%>% 53 | tidybayes::gather_draws(mu_alpha,mu_beta) 54 | 55 | rats%>% 56 | stan_thin_n(20)%>% 57 | tidybayes::gather_draws(alpha[i],beta[i]) 58 | ``` 59 | --------------------------------------------------------------------------------