Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set up lintr continuous integration and fix lints #58

Merged
merged 15 commits into from
Apr 28, 2023
Merged
644 changes: 333 additions & 311 deletions R/CFR_estimation.R

Large diffs are not rendered by default.

81 changes: 44 additions & 37 deletions R/S4stuff.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,21 @@
##' @rdname cd.fit
##' @aliases cd.fit-class
##' @exportClass cd.fit
setClass("cd.fit",
representation(ests = "matrix",
conv = "numeric",
MSG="character",
loglik="numeric",
samples="data.frame",
data="data.frame",
dist="character",
inv.hessian="matrix",
est.method="character",
ci.method="character"))
setClass(
"cd.fit",
representation(
ests = "matrix",
conv = "numeric",
MSG = "character",
loglik = "numeric",
samples = "data.frame",
data = "data.frame",
dist = "character",
inv.hessian = "matrix",
est.method = "character",
ci.method = "character"
)
)


##' An S4 Class that stores a MCMC fit coarse data object
Expand All @@ -41,7 +45,7 @@ setClass("cd.fit",
##' This is the output from \code{dic.fit.mcmc()}, which contains the important bits of information about the model fit and key options used.
##'
##'
##'@section Slots:
##' @section Slots:
##' \describe{
##' \item{\code{ests}:}{Matrix of class \code{"numeric"}. This matrix summarizes the results of fitting the model. Rows correspond to the first parameter , the second parameter and then percentiles specified by the ptiles argument. Columns correspond to the point estimate, the lower and upper bounds on the 95\% credible interval and the standard error of the point estimate.}
##' \item{\code{conv}:}{Object of class \code{"numeric"}. Not used in with \code{dic.fit.mcmc}}
Expand All @@ -60,30 +64,32 @@ setClass("cd.fit",
##' @aliases cd.fit.mcmc-class
##' @exportClass cd.fit.mcmc
setClass("cd.fit.mcmc",
contains="cd.fit")
contains = "cd.fit"
)

## we don't want boots and data printing out all the time
setMethod("show","cd.fit",function(object){
cat(sprintf("Coarse Data Model Parameter and Quantile Estimates: \n"))
print(object@ests)
cat(sprintf("\n-2*Log Likelihood = %.1f \n",-2*object@loglik))
if (object@conv == 0) warning("This model did not converge. Try different starting values or increase the number of iterations")
if (object@dist == "L" & nrow(object@inv.hessian) > 0) {
se.dispersion <- sqrt(object@inv.hessian[2,2] * (exp(object@ests[2,1] + log(object@ests[2,1])))^2) #using delta method
cat(sprintf("\nNote: dispersion parameter is exp(sdlog). In this case it is %.3f (95%% CI %.3f-%.3f). \n",
exp(object@ests[2,1]),
exp(object@ests[2,1])-qt(.975, nrow(object@data)-1)*se.dispersion,
exp(object@ests[2,1])+qt(.975, nrow(object@data)-1)*se.dispersion
))
}
setMethod("show", "cd.fit", function(object) {
cat(sprintf("Coarse Data Model Parameter and Quantile Estimates: \n"))
print(object@ests)
cat(sprintf("\n-2*Log Likelihood = %.1f \n", -2 * object@loglik))
if (object@conv == 0) warning("This model did not converge. Try different starting values or increase the number of iterations")
if (object@dist == "L" & nrow(object@inv.hessian) > 0) {
se.dispersion <- sqrt(object@inv.hessian[2, 2] * (exp(object@ests[2, 1] + log(object@ests[2, 1])))^2) # using delta method
cat(sprintf(
"\nNote: dispersion parameter is exp(sdlog). In this case it is %.3f (95%% CI %.3f-%.3f). \n",
exp(object@ests[2, 1]),
exp(object@ests[2, 1]) - qt(0.975, nrow(object@data) - 1) * se.dispersion,
exp(object@ests[2, 1]) + qt(0.975, nrow(object@data) - 1) * se.dispersion
))
}
})

## we don't want boots and data printing out all the time
setMethod("show","cd.fit.mcmc",function(object){
cat(sprintf("Coarse Data Model Parameter and Quantile Estimates: \n"))
print(object@ests)
if (object@dist == "L") cat(sprintf("\n Note: dispersion parameter is exp(sdlog). In this case it is %.3f. \n",exp(object@ests[2,1])))
cat(sprintf("Note: please check that the MCMC converged on the target distribution by running multiple chains. MCMC samples are available in the mcmc slot (e.g. my.fit@mcmc) \n"))
setMethod("show", "cd.fit.mcmc", function(object) {
cat(sprintf("Coarse Data Model Parameter and Quantile Estimates: \n"))
print(object@ests)
if (object@dist == "L") cat(sprintf("\n Note: dispersion parameter is exp(sdlog). In this case it is %.3f. \n", exp(object@ests[2, 1])))
cat(sprintf("Note: please check that the MCMC converged on the target distribution by running multiple chains. MCMC samples are available in the mcmc slot (e.g. my.fit@mcmc) \n"))
})

#' Get the log-likelihood value of a \code{cd.fit} or \code{cd.fit.mcmc} object
Expand All @@ -96,9 +102,10 @@ setMethod("show","cd.fit.mcmc",function(object){
#' @aliases logLik logLik,cd.fit-method
#' @export
#' @importFrom stats logLik
setMethod("logLik",
"cd.fit",
function(object){
object@loglik
})

setMethod(
"logLik",
"cd.fit",
function(object) {
object@loglik
}
)
18 changes: 9 additions & 9 deletions R/coarseDataTools-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

##' @name exp.win.lengths
##' @title Exposure window lengths from an influenza outbreak at a NYC school
##' @description A numeric vector of exposure window lengths taken from a dataset of doubly interval-censored incubation period observations. All observations came from a NYC public school. The outbreak has been described in full in Lessler et al. (see citation below).
##' @description A numeric vector of exposure window lengths taken from a dataset of doubly interval-censored incubation period observations. All observations came from a NYC public school. The outbreak has been described in full in Lessler et al. (see citation below).
##' @docType data
##' @format A numeric vector with 134 positive values. Each value represents an exposure window length from an observation of the incubation period for that individual. The exposure window length is the length of time during which exposure could have occurred. For example, if an individual could have been exposed anytime between 6am on Monday to 6am on Wednesday, her exposure window length would be 2 days.
##'
##' @format A numeric vector with 134 positive values. Each value represents an exposure window length from an observation of the incubation period for that individual. The exposure window length is the length of time during which exposure could have occurred. For example, if an individual could have been exposed anytime between 6am on Monday to 6am on Wednesday, her exposure window length would be 2 days.
##'
##' @usage data(exp.win.lengths)
##' @source Lessler J et al. New England Journal of Medicine. Outbreak of 2009 Pandemic Influenza A (H1N1) at a New York City School. 2009. 361(27):2628-2636. \url{https://www.nejm.org/doi/full/10.1056/nejmoa0906089}
##'
##'
##' @examples
##' data(exp.win.lengths)
##' summary(exp.win.lengths)
Expand All @@ -25,9 +25,9 @@ NULL
##' @name fluA.inc.per
##' @docType data
##' @title Coarse incubation period data for influenza A
##' @description These observations on the incubation period of influenza A come from a variety of sources, and were gathered for a literature review. They report doubly interval-censored, single interval-censored or exact observations for the incubation period.
##' @description These observations on the incubation period of influenza A come from a variety of sources, and were gathered for a literature review. They report doubly interval-censored, single interval-censored or exact observations for the incubation period.
##' @usage data(fluA.inc.per)
##' @format A data frame with 151 observations on the following 7 variables.
##' @format A data frame with 151 observations on the following 7 variables.
##' \describe{
##' \item{\code{author}}{the name of the primary author for the source of the observation}
##' \item{\code{year}}{the year of the study which is the source of the observation}
Expand All @@ -36,7 +36,7 @@ NULL
##' \item{\code{SL}}{the earliest possible time of symptom onset}
##' \item{\code{SR}}{the latest possible time of symptom onset}
##' \item{\code{type}}{an indicator of the type of observation: 0 for doubly interval-censored, 1 for single-interval censored, 2 for exact}}
##'@source Lessler J, Reich NG, Brookmeyer R, Perl TM, Nelson KE, Cummings DAT. (2009) A systematic review of the incubation periods of acute respiratory viral infections. Lancet Infectious Diseases. 9(5):291-300.
##' @source Lessler J, Reich NG, Brookmeyer R, Perl TM, Nelson KE, Cummings DAT. (2009) A systematic review of the incubation periods of acute respiratory viral infections. Lancet Infectious Diseases. 9(5):291-300.
##' @examples
##' data(fluA.inc.per)
##' head(fluA.inc.per)
Expand Down Expand Up @@ -66,9 +66,9 @@ NULL
#' @name nycH1N1
##' @docType data
##' @title Incubation period data from New York City Public Schools, 2009 H1N1 influenza outbreak
##' @description These observations on the incubation period of influenza A come from the investigation of the H1N1 outbreak in NYC schools in the spring of 2009. They report doubly interval-censored observations for the incubation period.
##' @description These observations on the incubation period of influenza A come from the investigation of the H1N1 outbreak in NYC schools in the spring of 2009. They report doubly interval-censored observations for the incubation period.
##' @usage data(nycH1N1)
##' @format A data frame with 134 observations on the following 5 variables.
##' @format A data frame with 134 observations on the following 5 variables.
##' \describe{
##' \item{\code{EL}}{the earliest possible time of infection}
##' \item{\code{ER}}{the latest possible time of infection}
Expand Down
Loading