Skip to content

Commit

Permalink
Fixing things up to pass devtools::check() in preparation for submiss…
Browse files Browse the repository at this point in the history
…ion to CRAN
  • Loading branch information
Jonathan Bartlett committed May 22, 2023
1 parent 8b6248e commit 1504e76
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 18 deletions.
9 changes: 5 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ Package: gFormulaMI
Title: G-formula for causal inference via multiple imputation
Version: 0.0.1
Authors@R:
person("Jonathan", "Bartlett", , "jonathan.bartlett1@lshtm.ac.uk", role = c("aut", "cre"),
comment = c(ORCID = "0000-0001-7117-0195"))
c(person("Jonathan", "Bartlett", , "jonathan.bartlett1@lshtm.ac.uk", role = c("aut", "cre"),
comment = c(ORCID = "0000-0001-7117-0195")),
person("Camila", "Olarte Parra", email = "camila.olarte-parra@lshtm.ac.uk", role = "ctb",
comment = c(ORCID = "0000-0003-0263-4392")))
Description: gFormulaMI implements the G-formula method for causal inference
using Bayesian multiple imputation methods. It creates multiple synthetic imputed
datasets under treatment regimes of interest. These can then be analysed
using rules developed for analysing multiple synthetic datasets.
License: GPL (>= 3)
Encoding: UTF-8
Imports:
mice,
mitools
mice
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
Suggests:
Expand Down
8 changes: 6 additions & 2 deletions R/gFormulaImpute.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
#' @param data The observed data frame
#' @param M The number of imputed datasets to generate
#' @param trtVars A vector of variable names indicating the time-varying treatment variables
#' @param trtRegimes A vector specifying the treatment regime of interest, or a list of
#' vectors specifying the treatment regimes of interest
#' @param nSim The number of individuals to simulate in each imputed dataset. Defaults to
#' number of individuals in observed data
#' @param micePrintFlag TRUE/FALSE specifying whether the output from the call(s) to mice
Expand Down Expand Up @@ -78,7 +80,7 @@ gFormulaImpute <- function(data, M=50, trtVars, trtRegimes,
nSim=NULL, micePrintFlag=FALSE,silent=FALSE,
method=NULL,predictorMatrix=NULL) {

if (class(data)=="mids") {
if (inherits(data, "mids")) {
missingData <- TRUE
if (silent==FALSE) {
print("Input data is a mice created multiple imputation object.")
Expand All @@ -91,7 +93,7 @@ gFormulaImpute <- function(data, M=50, trtVars, trtRegimes,
}
M <- data$m
firstImp <- mice::complete(data,1)
} else if (class(data)=="data.frame") {
} else if (inherits(data,"data.frame")) {
missingData <- FALSE
if (silent==FALSE) {
print("Input data is a regular data frame.")
Expand Down Expand Up @@ -208,6 +210,8 @@ gFormulaImpute <- function(data, M=50, trtVars, trtRegimes,
}

#remove original data from imputations
#but first declare regime=NULL to avoid CRAN note
regime <- NULL
returnImps <- mice::filter(imps, regime!=0)

} else {
Expand Down
20 changes: 10 additions & 10 deletions R/syntheticPool.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
#' @examples
#' set.seed(7626)
#' #impute synthetic datasets under two regimes of interest using gFormulaImpute
#' imps <- gFormulaImpute(data=simDataFullyObs,M=10,trtVarStem="a",
#' timePoints=3,
#' imps <- gFormulaImpute(data=simDataFullyObs,M=10,
#' trtVars=c("a0","a1","a2"),
#' trtRegimes=list(c(0,0,0),c(1,1,1)))
#' #fit linear model to final outcome with regime as covariate
#' fits <- with(imps, lm(y~factor(regime)))
Expand All @@ -45,18 +45,18 @@
syntheticPool <- function(fits) {

M <- length(fits$analyses)
p <- length(fits$analyses[[1]]$coefficients)
p <- length(stats::coefficients(fits$analyses[[1]]))
ests <- array(0, dim=c(M,p))
within_vars <- array(0, dim=c(M,p))

for (i in 1:M) {
ests[i,] <- coefficients(fits$analyses[[i]])
within_vars[i,] <- diag(vcov(fits$analyses[[i]]))
ests[i,] <- stats::coefficients(fits$analyses[[i]])
within_vars[i,] <- diag(stats::vcov(fits$analyses[[i]]))
}

overall_ests <- colMeans(ests)
v <- colMeans(within_vars)
b <- diag(var(ests))
b <- diag(stats::var(ests))
total_var <- (1+1/M)*b - v

#check that all variances are positive
Expand All @@ -74,13 +74,13 @@ syntheticPool <- function(fits) {
resTable[,4] <- total_var
resTable[,5] <- df
#95% confidence interval
resTable[,6] <- overall_ests - qt(0.975,df=df)*sqrt(total_var)
resTable[,7] <- overall_ests + qt(0.975,df=df)*sqrt(total_var)
resTable[,6] <- overall_ests - stats::qt(0.975,df=df)*sqrt(total_var)
resTable[,7] <- overall_ests + stats::qt(0.975,df=df)*sqrt(total_var)
#two sided p-value
resTable[,8] <- 2*pt(-abs(overall_ests/sqrt(total_var)),df=df)
resTable[,8] <- 2*stats::pt(-abs(overall_ests/sqrt(total_var)),df=df)

colnames(resTable) <- c("Estimate", "Within", "Between", "Total", "df",
"95% CI L", "95% CI U", "p")
rownames(resTable) <- names(coefficients(fits$analyses[[1]]))
rownames(resTable) <- names(stats::coefficients(fits$analyses[[1]]))
resTable
}
3 changes: 3 additions & 0 deletions man/gFormulaImpute.Rd

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

4 changes: 2 additions & 2 deletions man/syntheticPool.Rd

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

0 comments on commit 1504e76

Please sign in to comment.