Skip to content

Commit

Permalink
Merge pull request #86 from DylanCarbone/pull_requests
Browse files Browse the repository at this point in the history
Pull requests
  • Loading branch information
DylanCarbone authored May 13, 2024
2 parents f93308e + b29cd7e commit 87955a3
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 59 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export(CompositeTrend)
export(GAM_smoothing)
export(bma)
export(bootstrap_indicator)
export(detect_jags)
export(get_bmaBUGScode)
export(lambda_indicator)
export(msi)
Expand Down
8 changes: 1 addition & 7 deletions R/bayesian_meta_analysis.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
#' @examples
#'
#' # Only run if there is a JAGS installation
#' if(detect_jags()){
#' if(suppressWarnings(runjags::testjags(silent = TRUE)$JAGS.found)){
#'
#' # Create some example data in the format required
#' data <- data.frame(species = rep(letters, each = 50),
Expand Down Expand Up @@ -91,12 +91,6 @@ stop("No installation of JAGS has been detected. You can install JAGS from https
call. = FALSE)
}

# Check if jagsUI is installed
if (!requireNamespace("jagsUI", quietly = TRUE)) {
stop("Package 'jagsUI' is needed for the 'bma' function to work. Please install this from CRAN.",
call. = FALSE)
}

# Check to see that column names are present
if (!all(c("species", "year", "index") %in% colnames(data))){

Expand Down
9 changes: 1 addition & 8 deletions R/detect_jags.r
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
#' Detect JAGS installation
#'
#' @description small function that detects whether JAGS is installed.
#' @return TRUE or FALSE, indicating whether the JAGS installation has been detected
# Internal function to detect JAGS installation.
#' @importFrom runjags testjags
#' @export
#' @examples
#'
#' detect_jags()

detect_jags <- function(){
return(suppressWarnings(runjags::testjags(silent = TRUE)$JAGS.found))
}
31 changes: 31 additions & 0 deletions R/rescale_species.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,38 @@
#' @return A matrix. In each row we have the year, the species and the
#' scaled value. There is also an additional column, 'geomean' giving
#' the geometric mean for each year.
#'
#' @export
#'
#' @examples
#'
#' # We will create fake data which will mimic a summarised output from a sparta package occupancy detection model. Please see the vignette for more details.
#'
#' # Create a matrix with random data using set.seed for reproducibility
#' set.seed(123)
#' data <- matrix(rnorm(50 * 27), nrow = 50, ncol = 27)
#'
#' # Assign column names
#' col_names <- c("year", letters[1:26])
#' dimnames(data) <- list(NULL, col_names)
#'
#' # Create the 'trends_summary' object
#' trends_summary <- data
#'
#' # Re-scale the data so that the value for all species in the first year is the same (all defaults used).
#' # This function accounts for species that have no data at the beginning of the dataset by entering them at the geometric mean for that year, this stops them dramatically changing the indicator value in the year they join the dataset.
#' # It also accounts for species that leave the dataset before the end by holding them at their last value.
#' # Finally it limits the species values that can be given, preventing extremely high or low values biasing the indicator.
#' rescale_species(Data = trends_summary)
#'
#' # Now, I have 'messed up' the data a bit by including two species with data missing at the beginning and one species with data missing at the end. We also have one species with some very high values.
#' trends_summary[1:3, 'a'] <- NA
#' trends_summary[1:5, 'b'] <- NA
#' trends_summary[2:4, 'c'] <- 1000
#' trends_summary[45:50, 'd'] <- NA
#'
#' # Let's run this data again through our scaling function (all defaults used)
#' rescale_species(Data = trends_summary)

rescale_species <- function(Data, index = 100, max = 10000,
min = 1){
Expand Down
2 changes: 1 addition & 1 deletion man/bma.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 0 additions & 18 deletions man/detect_jags.Rd

This file was deleted.

30 changes: 30 additions & 0 deletions man/rescale_species.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions tests/testthat/helper-bayesian_meta_analysis.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ stop("No installation of JAGS has been detected. You can install JAGS from https
call. = FALSE)
}

# Check if jagsUI is installed
if (!requireNamespace("jagsUI", quietly = TRUE)) {
stop("Package 'jagsUI' is needed for the 'bma' function to work. Please install this from CRAN.",
call. = FALSE)
}

# Check to see that column names are present
if (!all(c("species", "year", "index") %in% colnames(data))){

Expand Down
20 changes: 6 additions & 14 deletions tests/testthat/test-bayesian_meta_analysis.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
test_that("Does the function stop when there is no jags installation?", {
# Mock `detect_jags` to return FALSE
mockery::stub(bma, "detect_jags", FALSE)

expect_error(bma(bayesian_meta_analysis_fake_data), regexp = "No installation of JAGS has been detected")
})

# Test that function responds correctly when 'jagsUI' is not available
test_that("Does the function stop when 'jagsUI' is not available?", {
# Mock `detect_jags` to return TRUE
mockery::stub(bma, "detect_jags", TRUE)

# Mock `requireNamespace` to return FALSE
mockery::stub(bma, "requireNamespace", FALSE)

expect_error(bma(bayesian_meta_analysis_fake_data), regexp = "Package 'jagsUI' is needed")
with_mocked_bindings(
"detect_jags" = function() FALSE,
{
expect_error(bma(bayesian_meta_analysis_fake_data), regexp = "No installation of JAGS has been detected")
}
)
})

test_that("Does it correctly detect missing columns in the input data?", {
Expand Down
6 changes: 2 additions & 4 deletions vignettes/BRCindicators.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ knitr::opts_chunk$set(echo = TRUE,
tidy.opts = list(width.cutoff = 60),
tidy = FALSE)
```

```{r libraries}
require(snowfall)
to produce an error
```

# Introduction
Expand All @@ -42,7 +40,7 @@ If you already have yearly estimates of abundance or occurrence for your species

```{r set_up, eval=FALSE}
library(devtools)
#install_github('biologicalrecordscentre/sparta')
install_github('biologicalrecordscentre/sparta')
```

Let's assume you have some raw data already, we can under take occupancy modelling like this
Expand Down

0 comments on commit 87955a3

Please sign in to comment.