Skip to content

Commit

Permalink
correcting all errors and approved test run
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuan-Liu committed Aug 31, 2024
2 parents 5cf6117 + c02a61c commit b0f4f15
Show file tree
Hide file tree
Showing 14 changed files with 351 additions and 10,953 deletions.
17 changes: 17 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Title: Bayesian Marginal Structural Models
Version: 0.1.0
Author: Kuan Liu, Xiao Yan
Maintainer: Xiao Yan <[email protected]>
<<<<<<< HEAD
Depends:
R (>= 4.2.0)
Imports:
Expand All @@ -18,7 +19,23 @@ Encoding: UTF-8
LazyData: true
RoxygenNote: 7.3.2
Suggests:
=======
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.3.1
Imports:
MCMCpack,
doParallel,
foreach,
parallel,
R2jags,
knitr,
coda (>= 0.19-4),
>>>>>>> c02a61c92d3be0eeca4a22bb0196a74db590a6ec
testthat (>= 3.0.0)
NeedsCompilation: no
Config/testthat/edition: 3
Description: This package provides tools for estimating causal effects using Bayesian Marginal Structural Models. It includes functions for estimating Bayesian weights using JAGS and for Bayesian non-parametric bootstrap to calculate causal effects.
URL: https://kuan-liu-lab.github.io/bayesmsm/
BugReports: https://github.com/Kuan-Liu-Lab/bayesmsm/issues
7 changes: 5 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ export(bayesweight)
export(plot_APO)
export(plot_ATE)
export(plot_est_box)
<<<<<<< HEAD
export(summary_bayesmsm)
import(MCMCpack)
=======
>>>>>>> c02a61c92d3be0eeca4a22bb0196a74db590a6ec
import(doParallel)
import(foreach)
import(parallel)
importFrom(MCMCpack,rdirichlet)
importFrom(R2jags,jags)
importFrom(coda,mcmc)
importFrom(foreach,"%dopar%")
27 changes: 25 additions & 2 deletions R/bayesmsm.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@
#' * `reference`, the reference intervention level
#' * `comparator`, the camparison intervention level
#'
#' @import foreach
#' @importFrom foreach "%dopar%"
#' @import doParallel
<<<<<<< HEAD
#' @import parallel
#' @import MCMCpack
=======
#' @importFrom MCMCpack rdirichlet
>>>>>>> c02a61c92d3be0eeca4a22bb0196a74db590a6ec
#'
#' @export
#'
Expand Down Expand Up @@ -67,10 +71,29 @@ bayesmsm <- function(ymodel,
ncore = 6){

# load all the required R packages;
<<<<<<< HEAD
# require(foreach)
# require(doParallel)
# require(MCMCpack)
# require(parallel)
=======
# if (!require(foreach)){
# install.packages("foreach",repos="http://cran.r-project.org")
# library(foreach)
# }
# if (!require(doParallel)){
# install.packages("doParallel",repos="http://cran.r-project.org")
# library(doParallel)
# }
# if (!require(MCMCpack)){
# install.packages("MCMCpack",repos="http://cran.r-project.org")
# library(MCMCpack)
# }

require(foreach)
require(doParallel)
require(MCMCpack)
>>>>>>> c02a61c92d3be0eeca4a22bb0196a74db590a6ec

# return error message if the input weight vector has different length comparing to the outcome Y;
if (length(wmean) != nrow(data)) {
Expand Down Expand Up @@ -256,7 +279,7 @@ bayesmsm <- function(ymodel,
reference = reference,
comparator = comparator
))
} else {
} else if (family == "gaussian"){
return(list(
RD_mean = mean(results[,2]),
RD_sd = sqrt(var(results[,2])),
Expand Down
32 changes: 32 additions & 0 deletions R/bayesweight.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@
#'
#' @importFrom R2jags jags
#' @importFrom coda mcmc
<<<<<<< HEAD
#' @import parallel
#' @import doParallel
#' @import foreach
=======
#' @import doParallel
#' @importFrom foreach "%dopar%"
#'
>>>>>>> c02a61c92d3be0eeca4a22bb0196a74db590a6ec
#' @export
#'
#' @examples
Expand Down Expand Up @@ -47,12 +53,32 @@ bayesweight <- function(trtmodel.list,
parallel = TRUE){

# Load all the required R packages;
<<<<<<< HEAD
# require(foreach)
# require(doParallel)
# require(MCMCpack)
# require(parallel)
# require(R2jags)
# require(coda)
=======
# if (!require(R2jags)){
# install.packages("R2jags",repos="http://cran.r-project.org")
# library(R2jags)
# }
# if (!require(coda)){
# install.packages("coda",repos="http://cran.r-project.org")
# library(coda)
# }
# if (!require(parallel)){
# install.packages("parallel",repos="http://cran.r-project.org")
# library(parallel)
# }

require(R2jags)
require(coda)
require(doParallel)
require(foreach)
>>>>>>> c02a61c92d3be0eeca4a22bb0196a74db590a6ec

create_marginal_treatment_models <- function(trtmodel.list) {
# Initialize the list for the marginal treatment models
Expand Down Expand Up @@ -266,9 +292,15 @@ bayesweight <- function(trtmodel.list,
if (n.chains >= available_cores) {
stop(paste("Parallel MCMC requires 1 core per chain. You have", available_cores, "cores. We recommend using", available_cores - 2, "cores."))
}
<<<<<<< HEAD
# Run JAGS model in parallel
cl <- parallel::makeCluster(n.chains)
doParallel::registerDoParallel(cl)
=======
# Run JAGS model in parallel;
cl <- makeCluster(n.chains)
registerDoParallel(cl)
>>>>>>> c02a61c92d3be0eeca4a22bb0196a74db590a6ec
jags.model.wd <- paste(getwd(), '/treatment_model.txt',sep='')

posterior <- foreach::foreach(i=1:n.chains, .packages=c('R2jags'),
Expand Down
4 changes: 0 additions & 4 deletions docs/deps/data-deps.txt

This file was deleted.

Loading

0 comments on commit b0f4f15

Please sign in to comment.