Skip to content

Commit

Permalink
Fixed styler and prepare for CRAN
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Parnell committed Jan 20, 2021
1 parent bb6fac9 commit 935f390
Show file tree
Hide file tree
Showing 104 changed files with 3,790 additions and 3,173 deletions.
7 changes: 4 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
Package: simmr
Type: Package
Title: A Stable Isotope Mixing Model
Version: 0.4.3.9000
Date: 2020-11-10
Version: 0.4.4
Date: 2021-01-21
Author: Andrew Parnell
URL: https://github.com/andrewcparnell/simmr
URL: https://github.com/andrewcparnell/simmr, http://andrewcparnell.github.io/simmr
BugReports: https://github.com/andrewcparnell/simmr/issues
Language: en-US
Maintainer: Andrew Parnell <[email protected]>
Description: Fits Stable Isotope Mixing Models (SIMMs) and is meant as a longer term replacement to the previous widely-used package SIAR. SIMMs are used to infer dietary proportions of organisms consuming various food sources from observations on the stable isotope values taken from the organisms' tissue samples. However SIMMs can also be used in other scenarios, such as in sediment mixing or the composition of fatty acids. The main functions are simmr_load and simmr_mcmc. The two vignettes contain a quick start and a full listing of all the features. The methods used are detailed in the papers Parnell et al 2010 <doi:10.1371/journal.pone.0009672>, and Parnell et al 2013 <doi:10.1002/env.2221>.
Expand Down
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# simmr 0.4.3.9000
# simmr 0.4.4

- Updated the simmr_elicit function to provide a more explicit warning for bad input objects
- Updated compare_sources and compare_groups to allow exporting of the plot object for editing purposes
- Used styler to correct code style

# simmr 0.4.3

Expand Down
180 changes: 94 additions & 86 deletions R/combine_sources.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,18 @@
#' data(geese_data)
#'
#' # Load into simmr
#' simmr_1 = with(geese_data_day1,
#' simmr_load(mixtures=mixtures,
#' source_names=source_names,
#' source_means=source_means,
#' source_sds=source_sds,
#' correction_means=correction_means,
#' correction_sds=correction_sds,
#' concentration_means = concentration_means))
#' simmr_1 <- with(
#' geese_data_day1,
#' simmr_load(
#' mixtures = mixtures,
#' source_names = source_names,
#' source_means = source_means,
#' source_sds = source_sds,
#' correction_means = correction_means,
#' correction_sds = correction_sds,
#' concentration_means = concentration_means
#' )
#' )
#'
#' # Plot
#' plot(simmr_1)
Expand All @@ -47,117 +51,121 @@
#' simmr_1
#'
#' # MCMC run
#' simmr_1_out = simmr_mcmc(simmr_1)
#' simmr_1_out <- simmr_mcmc(simmr_1)
#'
#' # Print it
#' print(simmr_1_out)
#'
#' # Summary
#' summary(simmr_1_out)
#' summary(simmr_1_out,type='diagnostics')
#' summary(simmr_1_out,type='correlations')
#' summary(simmr_1_out,type='statistics')
#' ans = summary(simmr_1_out,type=c('quantiles','statistics'))
#' summary(simmr_1_out, type = "diagnostics")
#' summary(simmr_1_out, type = "correlations")
#' summary(simmr_1_out, type = "statistics")
#' ans <- summary(simmr_1_out, type = c("quantiles", "statistics"))
#'
#' # Plot
#' plot(simmr_1_out)
#' plot(simmr_1_out,type='boxplot')
#' plot(simmr_1_out,type='histogram')
#' plot(simmr_1_out,type='density')
#' plot(simmr_1_out,type='matrix')
#' plot(simmr_1_out, type = "boxplot")
#' plot(simmr_1_out, type = "histogram")
#' plot(simmr_1_out, type = "density")
#' plot(simmr_1_out, type = "matrix")
#'
#' simmr_out_combine = combine_sources(simmr_1_out,
#' to_combine=c('Source A','Source D'),
#' new_source_name='Source A+D')
#' simmr_out_combine <- combine_sources(simmr_1_out,
#' to_combine = c("Source A", "Source D"),
#' new_source_name = "Source A+D"
#' )
#' plot(simmr_out_combine$input)
#' plot(simmr_out_combine,type='boxplot',title='simmr output: combined sources')
#'
#' plot(simmr_out_combine, type = "boxplot", title = "simmr output: combined sources")
#' }
#'
#' @export
combine_sources = function(simmr_out,
to_combine = simmr_out$input$source_names[1:2],
new_source_name = 'combined_source') {
UseMethod('combine_sources')
#' @export
combine_sources <- function(simmr_out,
to_combine = simmr_out$input$source_names[1:2],
new_source_name = "combined_source") {
UseMethod("combine_sources")
}
#' @export
combine_sources.simmr_output = function(simmr_out,
to_combine = simmr_out$input$source_names[1:2],
new_source_name = 'combined_source') {
#' @export
combine_sources.simmr_output <- function(simmr_out,
to_combine = simmr_out$input$source_names[1:2],
new_source_name = "combined_source") {
# A posteriori combining of sources

# Check only two sources to be combined
if (length(to_combine) != 2)
if (length(to_combine) != 2) {
stop("Currently only two sources can be combined")

}

# # Check class
# if (class(simmr_out) != 'simmr_output')
# stop("Only objects of class simmr_output can be run through this function")

# Find which columns to combine by number
to_combine_cols = sort(match(to_combine, simmr_out$input$source_names))
if (any(is.na(to_combine_cols)))
stop('1 or more source names not found')

simmr_new_out = simmr_out

to_combine_cols <- sort(match(to_combine, simmr_out$input$source_names))
if (any(is.na(to_combine_cols))) {
stop("1 or more source names not found")
}

simmr_new_out <- simmr_out

# 1 combine the chosen source means
old_source_means = simmr_out$input$source_means
simmr_new_out$input$source_means = old_source_means[-to_combine_cols[2], ,
drop = FALSE]
simmr_new_out$input$source_means[to_combine_cols[1], ] = apply(old_source_means[to_combine_cols, ,drop = FALSE], 2, 'mean')

old_source_means <- simmr_out$input$source_means
simmr_new_out$input$source_means <- old_source_means[-to_combine_cols[2], ,
drop = FALSE
]
simmr_new_out$input$source_means[to_combine_cols[1], ] <- apply(old_source_means[to_combine_cols, , drop = FALSE], 2, "mean")

# 2 combine the source sds
old_source_sds = simmr_out$input$source_sds
simmr_new_out$input$source_sds = old_source_sds[-to_combine_cols[2], ,drop= FALSE]
simmr_new_out$input$source_sds[to_combine_cols[1], ] = apply(old_source_sds[to_combine_cols, , drop = FALSE], 2, function(x)
sqrt(sum(x ^ 2)))

old_source_sds <- simmr_out$input$source_sds
simmr_new_out$input$source_sds <- old_source_sds[-to_combine_cols[2], , drop = FALSE]
simmr_new_out$input$source_sds[to_combine_cols[1], ] <- apply(old_source_sds[to_combine_cols, , drop = FALSE], 2, function(x) {
sqrt(sum(x^2))
})

# 3 combine the correction means
old_correction_means = simmr_out$input$correction_means
simmr_new_out$input$correction_means = old_correction_means[-to_combine_cols[2], ,drop = FALSE]
simmr_new_out$input$correction_means[to_combine_cols[1], ] = apply(old_correction_means[to_combine_cols, ,drop = FALSE], 2, 'mean')
old_correction_means <- simmr_out$input$correction_means
simmr_new_out$input$correction_means <- old_correction_means[-to_combine_cols[2], , drop = FALSE]
simmr_new_out$input$correction_means[to_combine_cols[1], ] <- apply(old_correction_means[to_combine_cols, , drop = FALSE], 2, "mean")

# 4 combine the correction sds
old_correction_sds = simmr_out$input$correction_sds
simmr_new_out$input$correction_sds = old_correction_sds[-to_combine_cols[2], ,drop = FALSE]
simmr_new_out$input$correction_sds[to_combine_cols[1], ] = apply(old_correction_sds[to_combine_cols, , drop = FALSE], 2, function(x)
sqrt(sum(x ^ 2)))

old_correction_sds <- simmr_out$input$correction_sds
simmr_new_out$input$correction_sds <- old_correction_sds[-to_combine_cols[2], , drop = FALSE]
simmr_new_out$input$correction_sds[to_combine_cols[1], ] <- apply(old_correction_sds[to_combine_cols, , drop = FALSE], 2, function(x) {
sqrt(sum(x^2))
})

# 5 combine the concentraion means
old_concentration_means = simmr_out$input$concentration_means
simmr_new_out$input$concentration_means = old_concentration_means[-to_combine_cols[2], ,drop = FALSE]
simmr_new_out$input$concentration_means[to_combine_cols[1], ] = apply(old_concentration_means[to_combine_cols, ,drop = FALSE], 2, 'mean')
old_concentration_means <- simmr_out$input$concentration_means
simmr_new_out$input$concentration_means <- old_concentration_means[-to_combine_cols[2], , drop = FALSE]
simmr_new_out$input$concentration_means[to_combine_cols[1], ] <- apply(old_concentration_means[to_combine_cols, , drop = FALSE], 2, "mean")

# 6 change the source names
old_source_names = simmr_out$input$source_names
simmr_new_out$input$source_names = old_source_names[-to_combine_cols[2]]
simmr_new_out$input$source_names[to_combine_cols[1]] = new_source_name
old_source_names <- simmr_out$input$source_names
simmr_new_out$input$source_names <- old_source_names[-to_combine_cols[2]]
simmr_new_out$input$source_names[to_combine_cols[1]] <- new_source_name

# 7 Change n_sources
simmr_new_out$input$n_sources = simmr_new_out$input$n_sources - 1
simmr_new_out$input$n_sources <- simmr_new_out$input$n_sources - 1

# 8 Sum across all the output values
n_groups = simmr_out$input$n_groups
n_groups <- simmr_out$input$n_groups
for (j in 1:n_groups) {
simmr_new_out$output[[j]] = simmr_out$output[[j]]
simmr_new_out$output[[j]] <- simmr_out$output[[j]]
# Change sims.list and sims.matrix
# First sims.matrix
sims.matrix = simmr_out$output[[j]]$BUGSoutput$sims.matrix
new_sims.matrix = sims.matrix[,-(to_combine_cols[2]+1)]
new_sims.matrix[,to_combine_cols[1]+1] = sims.matrix[,to_combine_cols[1]+1] + sims.matrix[,to_combine_cols[2]+1]
colnames(new_sims.matrix)[to_combine_cols[1]+1] = new_source_name
simmr_new_out$output[[j]]$BUGSoutput$sims.matrix = new_sims.matrix
sims.matrix <- simmr_out$output[[j]]$BUGSoutput$sims.matrix
new_sims.matrix <- sims.matrix[, -(to_combine_cols[2] + 1)]
new_sims.matrix[, to_combine_cols[1] + 1] <- sims.matrix[, to_combine_cols[1] + 1] + sims.matrix[, to_combine_cols[2] + 1]
colnames(new_sims.matrix)[to_combine_cols[1] + 1] <- new_source_name
simmr_new_out$output[[j]]$BUGSoutput$sims.matrix <- new_sims.matrix
# Now sims.list
sims.list = simmr_out$output[[j]]$BUGSoutput$sims.list
new_sims.list = sims.list
new_sims.list$p = sims.list$p[,-to_combine_cols[2]]
new_sims.list$p[,to_combine_cols[1]] = sims.list$p[,to_combine_cols[2]] + sims.list$p[,to_combine_cols[1]]
colnames(new_sims.list$p)[to_combine_cols[1]] = new_source_name
simmr_new_out$output[[j]]$BUGSoutput$sims.list = new_sims.list
sims.list <- simmr_out$output[[j]]$BUGSoutput$sims.list
new_sims.list <- sims.list
new_sims.list$p <- sims.list$p[, -to_combine_cols[2]]
new_sims.list$p[, to_combine_cols[1]] <- sims.list$p[, to_combine_cols[2]] + sims.list$p[, to_combine_cols[1]]
colnames(new_sims.list$p)[to_combine_cols[1]] <- new_source_name
simmr_new_out$output[[j]]$BUGSoutput$sims.list <- new_sims.list
}
class(simmr_new_out) = 'simmr_output'

class(simmr_new_out) <- "simmr_output"
return(simmr_new_out)

}
Loading

0 comments on commit 935f390

Please sign in to comment.