Skip to content

Commit

Permalink
adding spike and slab prior distributions (#17)
Browse files Browse the repository at this point in the history
* adding bernoulli prior distribution

* adding spike and slab prior (part I)

* adding JAGS code for bernoulli

* adding spike and slab prior (part II)

* fixing spike and slab

* spike and slab JAGS implementation (J

* 1st part of the vignette

* JAFS_inference_summary and adding posterior summary for spike and slab priors

* update the vignette

* Update BayesTools_model_tables.Rd

* JAGS_diagnostics functions

* adding marginal likelihood computation and allowing other types of prior distributions for the indicator

* update vignette

* removing spike and slab prior computation - as it's impossible with the current bridge sampling right now

* changing the default prior for inclusion

* changing the spike and slab prior specification again

* allowing factor prior distributions in spike and slab priors

* JAGS summary functions for factors

* news and check

* Update REFERENCES.bib

* Update R-CMD-check.yaml

based on: https://github.com/ropensci/jagstargets
  • Loading branch information
FBartos authored Jun 16, 2022
1 parent 9e66c03 commit d6788d5
Show file tree
Hide file tree
Showing 64 changed files with 5,276 additions and 946 deletions.
87 changes: 51 additions & 36 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# For help debugging build failures open an issue on the RStudio community with the 'github-actions' tag.
# https://community.rstudio.com/new-topic?category=Package%20development&tags=github-actions
on:
push:
branches:
Expand All @@ -24,64 +22,84 @@ jobs:
fail-fast: false
matrix:
config:
# - {os: macOS-latest, r: 'release'}
- {os: windows-latest, r: 'release'}
- {os: ubuntu-20.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"}
- {os: ubuntu-20.04, r: 'devel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest", http-user-agent: "R/4.1.0 (ubuntu-20.04) R (4.1.0 x86_64-pc-linux-gnu x86_64 linux-gnu) on GitHub Actions" }
- {os: macOS-latest, r: 'release'}
- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
- {os: ubuntu-latest, r: 'release'}
# - {os: ubuntu-latest, r: 'oldrel-1'}

env:
R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
RSPM: ${{ matrix.config.rspm }}
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
R_REMOTES_NO_ERRORS_FROM_WARNINGS: false
NOT_CRAN: true

steps:
- uses: actions/checkout@v2

- uses: r-lib/actions/setup-r@v1
- uses: r-lib/actions/setup-r@master
with:
r-version: ${{ matrix.config.r }}

- uses: r-lib/actions/setup-pandoc@v1
- uses: r-lib/actions/setup-pandoc@master

- name: Query dependencies
run: |
install.packages('remotes')
saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2)
writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version")
shell: Rscript {0}

- name: Restore R package cache
uses: actions/cache@v2
- name: Cache R packages
if: runner.os != 'Windows'
uses: actions/cache@v1
with:
path: ${{ env.R_LIBS_USER }}
key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }}
restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-

- name: Install system dependencies
if: runner.os == 'Linux'
run: |
while read -r cmd
do
eval sudo $cmd
done < <(Rscript -e 'writeLines(remotes::system_requirements("ubuntu", "20.04"))')
key: ${{ runner.os }}-r-${{ matrix.config.r }}-3-${{ hashFiles('.github/depends.Rds') }}
restore-keys: ${{ runner.os }}-r-${{ matrix.config.r }}-3-

- name: Install JAGS (windows-latest)
- name: Set path for RTools 4.0
if: runner.os == 'Windows'
run: echo "C:/rtools40/usr/bin;C:/rtools40/mingw64/bin" | Out-File -Append -FilePath $env:GITHUB_PATH -Encoding utf8

- name: Install most Windows system dependencies
if: runner.os == 'Windows'
run: |
curl.exe -o wjags.exe --url https://deac-fra.dl.sourceforge.net/project/mcmc-jags/JAGS/4.x/Windows/JAGS-4.2.0-Rtools33.exe
wjags.exe /S
del wjags.exe
pacman -Syu mingw-w64-x86_64-make --noconfirm
g++ --version
Get-Command g++ | Select-Object -ExpandProperty Definition
mingw32-make --version
Get-Command mingw32-make | Select-Object -ExpandProperty Definition
(New-Object System.Net.WebClient).DownloadFile('https://sourceforge.net/projects/mcmc-jags/files/JAGS/4.x/Windows/JAGS-4.3.1.exe', 'C:\JAGS-4.3.1.exe')
shell: powershell

- name: Install JAGS on Windows
if: runner.os == 'Windows'
run: C:\JAGS-4.3.1.exe /S
shell: cmd

- name: Install JAGS (macOS-latest)
- name: Install Linux system dependencies
if: runner.os == 'Linux'
env:
RHUB_PLATFORM: linux-x86_64-ubuntu-gcc
run: |
sudo apt-get install -y libglpk-dev
Rscript -e "remotes::install_github('r-hub/sysreqs')"
sysreqs=$(Rscript -e "cat(sysreqs::sysreq_commands('DESCRIPTION'))")
sudo -s eval "$sysreqs"
sudo -s apt-get install jags
sudo -s apt-get install libv8-dev
sudo -s apt-get install libnode-dev
sudo -s apt-get install libcurl4-openssl-dev
- name: Install Mac system dependencies
if: runner.os == 'macOS'
run : |
rm '/usr/local/bin/gfortran'
brew install automake jags
run: |
brew install jags
brew install libgit2
- name: Install dependencies
run: |
remotes::install_deps(dependencies = TRUE)
remotes::install_deps(dependencies = TRUE, type = ifelse(tolower(Sys.info()["sysname"]) == "linux", "source", "binary"))
remotes::install_cran("rcmdcheck")
shell: Rscript {0}

Expand All @@ -90,7 +108,6 @@ jobs:
env:
_R_CHECK_CRAN_INCOMING_REMOTE_: false
run: |
options(crayon.enabled = TRUE)
rcmdcheck::rcmdcheck(
args = c("--no-manual", "--as-cran", "--ignore-vignettes"),
build_args = c("--no-build-vignettes"),
Expand All @@ -102,17 +119,15 @@ jobs:
if: runner.os == 'Windows'
env:
_R_CHECK_CRAN_INCOMING_REMOTE_: false
JAGS_ROOT: "/c/progra~1/JAGS/JAGS-4.2.0"
JAGS_ROOT: "/c/progra~1/JAGS/JAGS-4.3.1"
JAGS_MAJOR_VERSION: 4
run: |
options(crayon.enabled = TRUE)
rcmdcheck::rcmdcheck(
args = c("--no-manual", "--as-cran", "--ignore-vignettes"),
build_args = c("--no-build-vignettes"),
error_on = "warning",
check_dir = "check")
shell: Rscript {0}

shell: Rscript {0}"

- name: Upload check results
if: failure()
Expand Down
82 changes: 59 additions & 23 deletions .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,49 +20,85 @@ jobs:
fail-fast: false
matrix:
config:
# - {os: macOS-latest, r: 'release'}
- {os: windows-latest, r: 'release'}
# - {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
# - {os: ubuntu-latest, r: 'release'}
# - {os: ubuntu-latest, r: 'oldrel-1'}

env:
RSPM: ${{ matrix.config.rspm }}
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
R_REMOTES_NO_ERRORS_FROM_WARNINGS: false
NOT_CRAN: true

steps:
- uses: actions/checkout@v2

- uses: r-lib/actions/setup-r@v1

- uses: r-lib/actions/setup-pandoc@v1

- name: Install JAGS (windows-latest)
if: runner.os == 'Windows'
run: |
curl.exe -o wjags.exe --url https://deac-fra.dl.sourceforge.net/project/mcmc-jags/JAGS/4.x/Windows/JAGS-4.2.0-Rtools33.exe
wjags.exe /S
del wjags.exe
shell: cmd
- uses: r-lib/actions/setup-r@master
with:
r-version: ${{ matrix.config.r }}

- name: Install JAGS (macOS-latest)
if: runner.os == 'macOS'
run : |
rm '/usr/local/bin/gfortran'
brew install jags
- uses: r-lib/actions/setup-pandoc@master

- name: Query dependencies
run: |
install.packages('remotes')
saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2)
writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version")
shell: Rscript {0}

- name: Restore R package cache
uses: actions/cache@v2
- name: Cache R packages
if: runner.os != 'Windows'
uses: actions/cache@v1
with:
path: ${{ env.R_LIBS_USER }}
key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }}
restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-
key: ${{ runner.os }}-r-${{ matrix.config.r }}-3-${{ hashFiles('.github/depends.Rds') }}
restore-keys: ${{ runner.os }}-r-${{ matrix.config.r }}-3-

- name: Set path for RTools 4.0
if: runner.os == 'Windows'
run: echo "C:/rtools40/usr/bin;C:/rtools40/mingw64/bin" | Out-File -Append -FilePath $env:GITHUB_PATH -Encoding utf8

- name: Install most Windows system dependencies
if: runner.os == 'Windows'
run: |
pacman -Syu mingw-w64-x86_64-make --noconfirm
g++ --version
Get-Command g++ | Select-Object -ExpandProperty Definition
mingw32-make --version
Get-Command mingw32-make | Select-Object -ExpandProperty Definition
(New-Object System.Net.WebClient).DownloadFile('https://sourceforge.net/projects/mcmc-jags/files/JAGS/4.x/Windows/JAGS-4.3.1.exe', 'C:\JAGS-4.3.1.exe')
shell: powershell

- name: Install JAGS on Windows
if: runner.os == 'Windows'
run: C:\JAGS-4.3.1.exe /S
shell: cmd

- name: Install Linux system dependencies
if: runner.os == 'Linux'
env:
RHUB_PLATFORM: linux-x86_64-ubuntu-gcc
run: |
sudo apt-get install -y libglpk-dev
Rscript -e "remotes::install_github('r-hub/sysreqs')"
sysreqs=$(Rscript -e "cat(sysreqs::sysreq_commands('DESCRIPTION'))")
sudo -s eval "$sysreqs"
sudo -s apt-get install jags
sudo -s apt-get install libv8-dev
sudo -s apt-get install libnode-dev
sudo -s apt-get install libcurl4-openssl-dev
- name: Install Mac system dependencies
if: runner.os == 'macOS'
run: |
brew install jags
brew install libgit2
- name: Install dependencies
run: |
install.packages(c("remotes"))
remotes::install_deps(dependencies = TRUE)
remotes::install_deps(dependencies = TRUE, type = ifelse(tolower(Sys.info()["sysname"]) == "linux", "source", "binary"))
remotes::install_cran("rcmdcheck")
remotes::install_cran("covr")
shell: Rscript {0}

Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: BayesTools
Title: Tools for Bayesian Analyses
Version: 0.2.10
Version: 0.2.11
Description: Provides tools for conducting Bayesian analyses. The package contains
functions for creating a wide range of prior distribution objects, mixing posterior
samples from 'JAGS' and 'Stan' models, plotting posterior distributions, and etc...
Expand Down
8 changes: 8 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,18 @@ export(JAGS_diagnostics)
export(JAGS_diagnostics_autocorrelation)
export(JAGS_diagnostics_density)
export(JAGS_diagnostics_trace)
export(JAGS_estimates_table)
export(JAGS_evaluate_formula)
export(JAGS_fit)
export(JAGS_formula)
export(JAGS_get_inits)
export(JAGS_inference_table)
export(JAGS_marglik_parameters)
export(JAGS_marglik_parameters_formula)
export(JAGS_marglik_priors)
export(JAGS_marglik_priors_formula)
export(JAGS_parameter_names)
export(JAGS_summary_table)
export(JAGS_to_monitor)
export(add_column)
export(ccdf)
Expand All @@ -68,12 +71,14 @@ export(interpret)
export(is.prior)
export(is.prior.PEESE)
export(is.prior.PET)
export(is.prior.discrete)
export(is.prior.dummy)
export(is.prior.factor)
export(is.prior.none)
export(is.prior.orthonormal)
export(is.prior.point)
export(is.prior.simple)
export(is.prior.spike_and_slab)
export(is.prior.vector)
export(is.prior.weightfunction)
export(lines_prior_list)
Expand Down Expand Up @@ -110,6 +115,7 @@ export(prior_factor)
export(prior_informed)
export(prior_informed_medicine_names)
export(prior_none)
export(prior_spike_and_slab)
export(prior_weightfunction)
export(qpoint)
export(quant)
Expand All @@ -121,6 +127,8 @@ export(rtwo.sided)
export(rtwo.sided_fixed)
export(runjags_estimates_empty_table)
export(runjags_estimates_table)
export(runjags_inference_empty_table)
export(runjags_inference_table)
export(sd)
export(transform_orthonormal_samples)
export(var)
Expand Down
12 changes: 12 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
## version 0.2.11
### Features
- adding Bernoulli prior distribution
- adding spike and slab type of prior distributions (without marginal likelihood computations/model-averaging capabilities)
- new vignette comparing Bayes factor computation via marginal likelihood and spike and slab priors

### Fixes
- when a transformation is applied, JAGS summary tables now produce the mean of the transformed variable (previous versions incorrectly returned transformation of the mean)

### Changes
- runjags_XXX_table functions are now also exported as JAGS_XXX_functions for consistency with the rest of the code

## version 0.2.10
### Features
- trace, density, and autocorrelation diagnostic plots for JAGS models
Expand Down
22 changes: 18 additions & 4 deletions R/JAGS-diagnostics.R
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,20 @@ JAGS_diagnostics_autocorrelation <- function(fit, parameter, plot_type = "base",


# extract the relevant parameters
if(is.prior.spike_and_slab(prior_list[[parameter]])){

if(sum(model_samples[,colnames(model_samples) == paste0(parameter, "_indicator")] != 0) < 10)
stop("The parameter with a spike and slab prior did not result in enough samples under the slab for producing a diagnostic figure.")

# change the samples between conditional
model_samples[
model_samples[,colnames(model_samples) == paste0(parameter, "_indicator")] == 0,
colnames(model_samples) == parameter] <- NA

# modify the parameter list
prior_list[[parameter]] <- prior_list[[parameter]]$variable
}

if(is.prior.factor(prior_list[[parameter]])){
if(attr(prior_list[[parameter]], "levels") > 2){
model_samples <- model_samples[,paste0(parameter, "[", 1:(attr(prior_list[[parameter]], "levels")-1), "]"),drop = FALSE]
Expand Down Expand Up @@ -339,14 +353,14 @@ JAGS_diagnostics_autocorrelation <- function(fit, parameter, plot_type = "base",
for(i in 1:ncol(plot_data)){

if(is.null(xlim)){
x_range <- range(plot_data[,i])
x_range <- range(plot_data[,i], na.rm = TRUE)
}else{
x_range <- xlim
}

for(j in seq_along(unique(chain))){

temp_args <- list(x = plot_data[chain == j,i], n = n_points, from = x_range[1], to = x_range[2])
temp_args <- list(x = plot_data[chain == j,i], n = n_points, from = x_range[1], to = x_range[2], na.rm = TRUE)
temp_density <- do.call(stats::density, temp_args)

x_den <- temp_density$x
Expand Down Expand Up @@ -404,7 +418,7 @@ JAGS_diagnostics_autocorrelation <- function(fit, parameter, plot_type = "base",
for(i in 1:ncol(plot_data)){

if(is.null(ylim)){
y_range <- range(plot_data[,i])
y_range <- range(plot_data[,i], na.rm = TRUE)
}else{
y_range <- ylim
}
Expand Down Expand Up @@ -458,7 +472,7 @@ JAGS_diagnostics_autocorrelation <- function(fit, parameter, plot_type = "base",
for(j in seq_along(unique(chain))){

temp_x <- 0:lags
temp_y <- stats::acf(plot_data[chain == j,i], lag.max = lags, plot = FALSE)$acf[, , 1L]
temp_y <- stats::acf(plot_data[chain == j,i], lag.max = lags, plot = FALSE, na.action = stats::na.pass)$acf[, , 1L]


temp_autocor <- list(
Expand Down
Loading

0 comments on commit d6788d5

Please sign in to comment.