Skip to content

Commit

Permalink
shiny integration in qraLm
Browse files Browse the repository at this point in the history
  • Loading branch information
vcadavez committed Feb 17, 2024
1 parent b5f370b commit cadd0b2
Show file tree
Hide file tree
Showing 118 changed files with 6,966 additions and 205 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Description: qraLm: A R package for quantitative risk assessment for Listeria mo
URL: https://github.com/vcadavez/qraLm
BugReports: https://github.com/vcadavez/qraLm/issues
Depends: R (>= 3.5.0)
Imports: Rdpack, Hmisc, extraDistr, mc2d, matrixStats, DT, dplyr,
Imports: Rdpack, Hmisc, extraDistr, mc2d, matrixStats, DT, dplyr, shiny,
ggplot2, plotly, Rcpp, odeintr, doseresponsemodels
LinkingTo: Rcpp, odeintr, BH
RdMacros: Rdpack
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export(fvTesting)
export(plotLotsECDF.qraLm)
export(plotRisk.qraLm)
export(printMC.qraLm)
export(runExample)
export(sfBrineORsaltCC)
export(sfBriningCC)
export(sfCharacteristics)
Expand All @@ -48,6 +49,7 @@ export(sfSmearingCC)
export(sfSmoking)
export(sfTesting)
export(summaryLot.qraLm)
export(summaryRisk.qraLm)
export(summaryServings.qraLm)
export(summaryUnits.qraLm)
importFrom(DT,datatable)
Expand All @@ -62,7 +64,9 @@ importFrom(dplyr,"%>%")
importFrom(extraDistr,dbbinom)
importFrom(extraDistr,rtbinom)
importFrom(extraDistr,rtnorm)
importFrom(matrixStats,rowMeans2)
importFrom(matrixStats,rowSums2)
importFrom(matrixStats,rowWeightedMeans)
importFrom(mc2d,rdirichlet)
importFrom(mc2d,rmultinomial)
importFrom(mc2d,rpert)
Expand Down
1 change: 1 addition & 0 deletions R/Lot2LotGen.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#' @param propVarInter proportion of the total variance (\eqn{C0\_sdLog10^2}) attributed
#' to between-lot variance. The remaining (`1-propVarInter`) is the proportion of within-lot variance.
#' @param Poisson need to be defined
#' @param ... Other options used to control [LotGen()]
#'
#' @return A list of four elements:
#' \describe{
Expand Down
16 changes: 11 additions & 5 deletions R/LotGen.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
#' @title Generation of contaminated lots
#'
#' @description
#' The function [LotGen()] generates a matrix of contaminated lots from information on the parameters of a normal distribution (mean, sd)
#' The function [LotGen()] generates a matrix of contaminated lots from information on the
#' parameters of a normal distribution (mean, sd)
#' representing the microbial concentration in the contaminated units (log10 CFU/g).
#' Every row of the matrix corresponds to a production lot, broken down in units or portions, which are the columns of the matrix.
#' There is no assumption that lots can have different prevalence values, and therefore the microbial numbers of all units or portions produced in a lot
#' Every row of the matrix corresponds to a production lot, broken down in units or portions,
#' which are the columns of the matrix.
#' There is no assumption that lots can have different prevalence values, and therefore the
#' microbial numbers of all units or portions produced in a lot
#' are sampled from a microbial concentration distribution.
#' @param nLots number of lots sampled or size of the Monte Carlo simulation (scalar).
#' @param sizeLot number of units or portions produced in a lot (scalar).
#' @param unitSize (`g`) weight of single unit or portion from a lot (scalar).
#' @param P prevalence of contaminated lots (scalar)
#' @param C0MeanLog (log10 CFU/g) mean parameter of the normal distribution representing the variability in the microbial concentration of contaminated units (scalar or vector).
#' @param C0SdLog (log10 CFU/g) standard deviation parameter of the normal distribution representing the variability in the microbial concentration of contaminated units (scalar or vector).
#' @param C0MeanLog (log10 CFU/g) mean parameter of the normal distribution representing
#' the variability in the microbial concentration of contaminated units (scalar or vector).
#' @param C0SdLog (log10 CFU/g) standard deviation parameter of the normal distribution
#' representing the variability in the microbial concentration of contaminated units (scalar or vector).
#' @param ... Other options used to control [LotGen()]
#' @return A list of two elements:
#' \describe{
#' \item{N}{(`CFU`) A matrix of size `nLots` lots by `sizeLot` units representing the microbial numbers in the units or portions from contaminated lots;}
Expand Down
37 changes: 24 additions & 13 deletions R/caPartitioningCC.R
Original file line number Diff line number Diff line change
@@ -1,41 +1,51 @@
#' @title Cross-contamination of cantaloupe during dicing and partitioning into packed units
#'
#' @description
#' The [caPartitioningCC()] function simulates the potential cross-contamination of cantaloupes, when in direct contact with the dicing machine
#' The [caPartitioningCC()] function simulates the potential cross-contamination of cantaloupes,
#' when in direct contact with the dicing machine
#' or knives, followed by the partitioning of all dices produced in one processing lot (sublot) into packed units.
#' The cross-contamination algorithm accounts for four possible scenarios:
#' \enumerate{
#' \item cross-contamination occurring in sublots already contaminated;
#' \item contamination occurring in sublots that were not contaminated;
#' \item no cross-contamination occurring in sublots already contaminated; and
#' \item no cross-contamination occurring in sublots that were not contaminated. Probabilities of occurrence of every event are computed.
#' \item no cross-contamination occurring in sublots that were not contaminated. Probabilities
#' of occurrence of every event are computed.
#' }
#' The partitioning algorithm randomly distributes the total numbers of cells from a contaminated sublot of dices into packed units. The dispersion factor `b`,
#' which is a parameter of the beta distribution, indicates the extent of cell clustering in the bulk of dices in the sublot, and ultimately
#' The partitioning algorithm randomly distributes the total numbers of cells from a contaminated sublot
#' of dices into packed units. The dispersion factor `b`,
#' which is a parameter of the beta distribution, indicates the extent of cell clustering in the bulk
#' of dices in the sublot, and ultimately
#' the heterogeneity in the number of cells distributed among pack units.
#'
#' @param data a list of:
#' \describe{
#' \item{`N`}{(`CFU`) A matrix of size `newMC` sublots by `sizeSublot` portions of dices from one cantaloupe representing the numbers of \emph{L. monocytogenes}
#' in dices;}
#' \item{`N`}{(`CFU`) A matrix of size `newMC` sublots by `sizeSublot` portions of dices from one
#' cantaloupe representing the numbers of \emph{L. monocytogenes} in dices;}
#' \item{`P`}{Prevalence of contaminated sublots (scalar).}
#' }
#' @param probCCDice Probability of cross-contamination from the dicing machine (scalar).
#' @param trDicerMean Mean of the transfer coefficient of \emph{L. monocytogenes} from dicing machine to cantaloupe flesh (scalar or vector).
#' @param trDicerSd Standard deviation of the transfer coefficient of \emph{L. monocytogenes} from dicing machine to cantaloupe
#' @param trDicerMean Mean of the transfer coefficient of \emph{L. monocytogenes} from dicing
#' machine to cantaloupe flesh (scalar or vector).
#' @param trDicerSd Standard deviation of the transfer coefficient of \emph{L. monocytogenes}
#' from dicing machine to cantaloupe
#' flesh (scalar or vector).
#' @param nDicer (`CFU`) Numbers of \emph{L. monocytogenes} on the surface of the dicing machine ready to be transferred (scalar or vector).
#' @param nDicer (`CFU`) Numbers of \emph{L. monocytogenes} on the surface of the dicing machine
#' ready to be transferred (scalar or vector).
# #' @param newMC Number of sublots (scalar).
#' @param nLots See [Lot2LotGen()].
#' @param sizeLot Number of cantaloupes from a harvested lot (scalar).
#' @param sizeSublot Number of cantaloupes processed in a sublot. It should be a multiple of `sizeLot` (scalar).
#' @param cantaWeight (`g`) weight of a cantaloupe
#' @param pulpYield (`%`) Cantaloupe pulp yield (scalar or vector).
#' @param unitSize (`g`) Weight of a pack of cantaloupe dices (scalar).
#' @param b Dispersion factor of the beta distribution representing the degree of heterogeneity in the number of cells between pack units (scalar).
#' @param b Dispersion factor of the beta distribution representing the degree of heterogeneity in the number
#' of cells between pack units (scalar).
#'
#' @return A list with four elements:
#' \describe{
#' \item{`N`}{(`CFU`) A matrix of size `newMC` sublots by Number_packs number of packs containing the numbers of \emph{L. monocytogenes} in a pack,
#' \item{`N`}{(`CFU`) A matrix of size `newMC` sublots by Number_packs number of packs containing
#' the numbers of \emph{L. monocytogenes} in a pack,
#' from contaminated lots;}
#' \item{`P`}{Prevalence of contaminated sublots (scalar);}
#' \item{`sizeSublot`}{Number of cantaloupes processed in a sublot (scalar);}
Expand Down Expand Up @@ -63,8 +73,9 @@
#' @export
#'
#' @note The value of \eqn{beta = 1} represents moderate clustering of cells in the bulk of diced cantaloupe from a
#' sublot \insertCite{Nauta2005;textual}{qraLm}. \insertCite{Hoelzer2012;textual}{qraLm} established the log 10 of the transfer coefficient
#' of \emph{L. monocytogenes} from knives to vegetables as a normal distribution with \eqn{TRdicer\_mean = -1.42} and \eqn{TRdicer\_sd = 0.52}.
#' sublot \insertCite{Nauta2005;textual}{qraLm}. \insertCite{Hoelzer2012;textual}{qraLm} established the log 10 of
#' the transfer coefficient of \emph{L. monocytogenes} from knives to vegetables as a normal distribution with
#' \eqn{TRdicer\_mean = -1.42} and \eqn{TRdicer\_sd = 0.52}.
#'
#' @examples
#' library(extraDistr)
Expand Down
38 changes: 19 additions & 19 deletions R/plotLotsECDF.qraLm.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#'
#' @title plotLotsECDF Generic plot function to plot the ECDF
#' @param x qraLm object see [Lot2LotGen()]
#' @param ... optional plot parameters passed to the plot function
#' @param ... Optional plot parameters passed to the [plotLotsECDF()] function
#' @author Vasco Cadavez
#'
#' @importFrom stats weighted.mean
Expand All @@ -12,25 +12,25 @@
#' @examples
#'
#' prod <- Lot2LotGen(
#' nLots = 1000,
#' sizeLot = 1000,
#' unitSize = 500,
#' betaAlpha = 0.5112,
#' betaBeta = 9.959,
#' C0MeanLog = 1.023,
#' C0SdLog = 0.3267,
#' propVarInter = 0.7
#' )
#' nLots = 1000,
#' sizeLot = 1000,
#' unitSize = 500,
#' betaAlpha = 0.5112,
#' betaBeta = 9.959,
#' C0MeanLog = 1.023,
#' C0SdLog = 0.3267,
#' propVarInter = 0.7
#' )
#' prod1 <- Lot2LotGen(
#' nLots = 1000,
#' sizeLot = 1000,
#' unitSize = 500,
#' betaAlpha = 0.9112,
#' betaBeta = 2.959,
#' C0MeanLog = 2.023,
#' C0SdLog = 0.5267,
#' propVarInter = 0.7
#' )
#' nLots = 1000,
#' sizeLot = 1000,
#' unitSize = 500,
#' betaAlpha = 0.9112,
#' betaBeta = 2.959,
#' C0MeanLog = 2.023,
#' C0SdLog = 0.5267,
#' propVarInter = 0.7
#' )
#'
#' plotLotsECDF.qraLm(prod1)
#'
Expand Down
22 changes: 22 additions & 0 deletions R/runExample.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#' @title Shiny application for `qraLm` package
#'
#' @description
#' Shiny application to simulate quantitative risk assessment for *L. monocytogenes*
#' in Frozen Vegetables, Diced RTE Cantaloupe and Cold-smoked RTE Fish.
#'
#' @author Vasco Cadavez \email{[email protected]}
#'
#' @references
#' \insertRef{stats}{qraLm}
#'
#' @importFrom Rdpack reprompt
#'
#' @export
runExample <- function() {
appDir <- system.file("shiny-examples", "qraLmShiny", package = "qraLm")
if (appDir == "") {
stop("Could not find example directory. Try re-installing `qraLm`.", call. = FALSE)
}

shiny::runApp(appDir, display.mode = "normal")
}
92 changes: 47 additions & 45 deletions R/summaryLot.qraLm.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Print summary MC results
#' Print summary MC results at Lot level
#'
#' @title summaryLot Generic print function to print the between lots MC summary statistics
#' @param x qraLm object. See [Lot2LotGen()]
Expand All @@ -12,15 +12,15 @@
#'
#' @examples
#' prod <- Lot2LotGen(
#' nLots = 1000,
#' sizeLot = 1000,
#' unitSize = 500,
#' betaAlpha = 0.5112,
#' betaBeta = 9.959,
#' C0MeanLog = 1.023,
#' C0SdLog = 0.3267,
#' propVarInter = 0.7,
#' Poisson = FALSE
#' nLots = 1000,
#' sizeLot = 1000,
#' unitSize = 500,
#' betaAlpha = 0.5112,
#' betaBeta = 9.959,
#' C0MeanLog = 1.023,
#' C0SdLog = 0.3267,
#' propVarInter = 0.7,
#' Poisson = FALSE
#' )
#'
#' summaryLot.qraLm(prod)
Expand All @@ -33,62 +33,64 @@ summaryLot.qraLm <- function(x, ...) {
if (exists("unitSize", x) == TRUE) {
lotN <- rowMeans(x$N / x$unitSize, na.rm = TRUE)
} else {
lotN <- rowMeans(x$N, na.rm = TRUE)
lotN <- rowMeans(x$N, na.rm = TRUE)
}

if (exists("ProbUnitPos", x) == TRUE) {
NStats <- Hmisc::wtd.quantile(lotN,
weights = x$ProbUnitPos,
probs = c(0.0, 0.5, 0.025, 0.975, 1.0),
normwt = TRUE, na.rm = TRUE
)
NStatsMean <- stats::weighted.mean(lotN, w = x$ProbUnitPos, na.rm = TRUE)
NStats <- Hmisc::wtd.quantile(lotN,
weights = x$ProbUnitPos,
probs = c(0.0, 0.5, 0.025, 0.975, 1.0),
normwt = TRUE, na.rm = TRUE)
NStatsMean <- stats::weighted.mean(lotN, w = x$ProbUnitPos, na.rm = TRUE)
} else {
NStats <- Hmisc::wtd.quantile(lotN,
weights = rep(1, nrow(x$N)),
probs = c(0.00, 0.0, 0.025, 0.975, 1.00),
normwt = TRUE, na.rm = TRUE
)
weights = rep(1, nrow(x$N)),
probs = c(0.00, 0.0, 0.025, 0.975, 1.00),
normwt = TRUE, na.rm = TRUE)

NStatsMean <- mean(lotN, na.rm = TRUE)
}

NStatsMin <- NStats[1]
NStatsMin <- NStats[1]
NStatsMedian <- NStats[2]
Q2.5 <- NStats[3]
Q97.5 <- NStats[4]
NStatsMax <- NStats[5]
Q2.5 <- NStats[3]
Q97.5 <- NStats[4]
NStatsMax <- NStats[5]

Counts <- rbind(
unname(NStatsMin),
unname(Q2.5),
unname(NStatsMean),
unname(NStatsMedian),
unname(Q97.5),
unname(NStatsMax)
)
# log10Counts <- round(log10(ceiling(Counts)), digits=4)
log10Counts <- round(log10(Counts), digits = 4)
unname(NStatsMin),
unname(Q2.5),
unname(NStatsMean),
unname(NStatsMedian),
unname(Q97.5),
unname(NStatsMax)
)
# logs function
log_var <- function(x) {
ifelse(x != 0, log10(x), 0)
}

log10Counts <- round(log_var(Counts), digits = 6)
Statistics <- c("Minimum", "pct 2.5th", "Mean", "Median", "pct 97.5th", "Maximum")
MCstats <- data.frame(Statistics, Counts, log10Counts)


if (exists("ProbUnitPos", x) == TRUE) {
names(MCstats) <- c("Statistics", "CFU/g", "log10 CFU/g")
DT::datatable(MCstats,
caption = "Summary statistics: between lots mean counts",
class = "display",
fillContainer = FALSE,
options = list(dom = "t")
) |>
caption = "Summary statistics: between lots mean counts",
class = "display",
fillContainer = FALSE,
options = list(dom = "t")) |>
DT::formatSignif(columns = c("CFU/g", "log10 CFU/g"), digits = 4)
} else {
names(MCstats) <- c("Statistics", "CFU/Melon", "log10 CFU/Melon")

DT::datatable(MCstats,
caption = "Summary statistics: between lots mean counts",
class = "display",
fillContainer = FALSE,
options = list(dom = "t")
) |>
caption = "Summary statistics: between lots mean counts",
class = "display",
fillContainer = FALSE,
options = list(dom = "t")) |>
DT::formatSignif(columns = c("CFU/Melon", "log10 CFU/Melon"), digits = 4)
}
}
Loading

0 comments on commit cadd0b2

Please sign in to comment.