Skip to content

Commit

Permalink
correcting all warning and notes
Browse files Browse the repository at this point in the history
Changes: i) include a globals R script to host foreach loop index for the bayesweight and bayesweight_cen

Only one note left
checking for non-standard things in the check directory ... NOTE
  Found the following files/directories:
    'censoring_model.txt' 'treatment_model.txt'

0 errors ✔ | 0 warnings ✔ | 1 note ✖
  • Loading branch information
Kuan-Liu committed Oct 8, 2024
1 parent 49d7f93 commit 88ee1e5
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 3,567 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
^Meta$
^docs$
^README\.Rmd$
^doc$
9 changes: 5 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ Depends:
R (>= 4.2.0)
Suggests:
rmarkdown,
testthat (>= 3.0.0)
testthat (>= 3.0.0),
knitr,
DiagrammeR,
DT
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
Expand All @@ -29,12 +32,10 @@ Imports:
foreach,
parallel,
R2jags,
knitr,
coda (>= 0.19-4),
stats,
grDevices,
graphics,
knitr
graphics
NeedsCompilation: no
Config/testthat/edition: 3
URL: https://kuan-liu-lab.github.io/bayesmsm/
Expand Down
2 changes: 0 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ export(plot_APO)
export(plot_ATE)
export(plot_est_box)
export(summary_bayesmsm)
import(MCMCpack)
import(doParallel)
import(foreach)
import(parallel)
importFrom(MCMCpack,rdirichlet)
importFrom(R2jags,jags)
Expand Down
1 change: 0 additions & 1 deletion R/bayesmsm.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#' @importFrom foreach "%dopar%"
#' @import doParallel
#' @import parallel
#' @import MCMCpack
#' @importFrom MCMCpack rdirichlet
#' @importFrom stats as.formula density optim quantile sd terms var
#'
Expand Down
91 changes: 43 additions & 48 deletions R/bayesweight.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#' @importFrom coda mcmc as.mcmc geweke.diag
#' @import parallel
#' @import doParallel
#' @import foreach
#' @importFrom foreach "%dopar%"
#' @importFrom stats as.formula terms var
#'
Expand Down Expand Up @@ -52,51 +51,52 @@ bayesweight <- function(trtmodel.list,
seed = 890123,
parallel = TRUE){

# Load all the required R packages;
# 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)
# }


create_marginal_treatment_models <- function(trtmodel.list) {
# Initialize the list for the marginal treatment models
trtmodel.list_s <- list()

# Loop through each model in the original list
for (i in seq_along(trtmodel.list)) {
# Use lapply to iterate over indices
trtmodel.list_s <- lapply(seq_along(trtmodel.list), function(index) {
# Extract the response variable (treatment variable) from each model
response_var <- all.vars(trtmodel.list[[i]])[1] # assuming the response is the first variable on the LHS
response_var <- all.vars(trtmodel.list[[index]])[1] # assuming the response is the first variable on the LHS

# Create the marginal model formula
if (i == 1) {
if (index == 1) {
# The first treatment model does not depend on any previous treatments
formula_s <- as.formula(paste(response_var, "~ 1"))
} else {
# Subsequent treatment models depend on all previous treatments
previous_treatments <- sapply(seq_len(i-1), function(j) {
previous_treatments <- sapply(seq_len(index - 1), function(j) {
all.vars(trtmodel.list[[j]])[1]
})
formula_s <- as.formula(paste(response_var, "~", paste(previous_treatments, collapse = " + ")))
}

# Append the new formula to the list
trtmodel.list_s[[i]] <- formula_s
}
return(formula_s)
})

# # Initialize the list for the marginal treatment models
# trtmodel.list_s <- list()
#
# # Loop through each model in the original list
# for (i in seq_along(trtmodel.list)) {
# # Extract the response variable (treatment variable) from each model
# response_var <- all.vars(trtmodel.list[[i]])[1] # assuming the response is the first variable on the LHS
#
# # Create the marginal model formula
# if (i == 1) {
# # The first treatment model does not depend on any previous treatments
# formula_s <- as.formula(paste(response_var, "~ 1"))
# } else {
# # Subsequent treatment models depend on all previous treatments
# previous_treatments <- sapply(seq_len(length(trtmodel.list) - 1), function(j) {
# all.vars(trtmodel.list[[j]])[1]
# })
# formula_s <- as.formula(paste(response_var, "~", paste(previous_treatments, collapse = " + ")))
# }
#
# # Append the new formula to the list
# trtmodel.list_s[[i]] <- formula_s
# }

return(trtmodel.list_s)
}
Expand Down Expand Up @@ -237,9 +237,6 @@ bayesweight <- function(trtmodel.list,
# This is where you'd actually generate the JAGS model code
}

# Example writing the model to a file
cat(model_string, file = "treatment_model.txt")

return(unique(all_parameters))
}

Expand Down Expand Up @@ -288,38 +285,36 @@ bayesweight <- function(trtmodel.list,
# Run JAGS model in parallel
cl <- parallel::makeCluster(n.chains)
doParallel::registerDoParallel(cl)
# Run JAGS model in parallel;
# cl <- makeCluster(n.chains)
# registerDoParallel(cl)

# Ensure the cluster is stopped when the function exits, even in case of error
on.exit({
if (!is.null(cl)) {
parallel::stopCluster(cl)
foreach::registerDoSEQ() # Reset to sequential processing correctly
}
}, add = TRUE)
# on.exit({
# if (!is.null(cl)) {
# parallel::stopCluster(cl)
# foreach::registerDoSEQ() # Reset to sequential processing correctly
# }
# }, add = TRUE)

jags.model.wd <- paste(getwd(), '/treatment_model.txt',sep='')

posterior <- foreach::foreach(i=1:n.chains, .packages=c('R2jags'),
posterior <- foreach::foreach(chain_idx=1:n.chains, .packages=c('R2jags'),
.combine='rbind') %dopar%{

set.seed(seed+chain_idx) #define seed;
jagsfit <- R2jags::jags(data = jags.data,
parameters.to.save = jags.params,
model.file = jags.model.wd,
n.chains = 1,
n.iter = n.iter,
n.burnin = n.burnin,
n.thin = n.thin,
jags.seed = seed+i)
n.thin = n.thin)
# Combine MCMC output from multiple chains
out.mcmc <- as.mcmc(jagsfit)
return(do.call(rbind, lapply(out.mcmc, as.matrix)))

}
# parallel::stopCluster(cl)
# doParallel::registerDoSEQ()

parallel::stopCluster(cl)


} else if (parallel == FALSE) {

Expand Down
59 changes: 40 additions & 19 deletions R/bayesweight_cen.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#' @importFrom coda mcmc as.mcmc geweke.diag
#' @import parallel
#' @import doParallel
#' @import foreach
#' @importFrom foreach "%dopar%"
#' @importFrom stats as.formula terms var
#'
Expand Down Expand Up @@ -64,30 +63,51 @@ bayesweight_cen <- function(trtmodel.list = list(A1 ~ L11 + L21,


create_marginal_treatment_models <- function(trtmodel.list) {
# Initialize the list for the marginal treatment models
trtmodel.list_s <- list()

# Loop through each model in the original list
for (i in seq_along(trtmodel.list)) {
# Use lapply to iterate over indices
trtmodel.list_s <- lapply(seq_along(trtmodel.list), function(index) {
# Extract the response variable (treatment variable) from each model
response_var <- all.vars(trtmodel.list[[i]])[1] # assuming the response is the first variable on the LHS
response_var <- all.vars(trtmodel.list[[index]])[1] # assuming the response is the first variable on the LHS

# Create the marginal model formula
if (i == 1) {
if (index == 1) {
# The first treatment model does not depend on any previous treatments
formula_s <- as.formula(paste(response_var, "~ 1"))
} else {
# Subsequent treatment models depend on all previous treatments
previous_treatments <- sapply(seq_len(i-1), function(j) {
previous_treatments <- sapply(seq_len(index - 1), function(j) {
all.vars(trtmodel.list[[j]])[1]
})
formula_s <- as.formula(paste(response_var, "~", paste(previous_treatments, collapse = " + ")))
}

# Append the new formula to the list
trtmodel.list_s[[i]] <- formula_s
}

return(formula_s)
})

# # Initialize the list for the marginal treatment models
# trtmodel.list_s <- list()
#
# # Loop through each model in the original list
# for (i in seq_along(trtmodel.list)) {
# # Extract the response variable (treatment variable) from each model
# response_var <- all.vars(trtmodel.list[[i]])[1] # assuming the response is the first variable on the LHS
#
# # Create the marginal model formula
# if (i == 1) {
# # The first treatment model does not depend on any previous treatments
# formula_s <- as.formula(paste(response_var, "~ 1"))
# } else {
# # Subsequent treatment models depend on all previous treatments
# previous_treatments <- sapply(seq_len(length(trtmodel.list) - 1), function(j) {
# all.vars(trtmodel.list[[j]])[1]
# })
# formula_s <- as.formula(paste(response_var, "~", paste(previous_treatments, collapse = " + ")))
# }
#
# # Append the new formula to the list
# trtmodel.list_s[[i]] <- formula_s
# }
#
return(trtmodel.list_s)
}

Expand Down Expand Up @@ -283,28 +303,29 @@ bayesweight_cen <- function(trtmodel.list = list(A1 ~ L11 + L21,
stop(paste("Parallel MCMC requires 1 core per chain. You have", available_cores, "cores. We recommend using", available_cores - 2, "cores."))
}
# Run JAGS model in parallel
library(doParallel)
cl <- makeCluster(n.chains)
registerDoParallel(cl)
cl <- parallel::makeCluster(n.chains)
doParallel::registerDoParallel(cl)

jags.model.wd <- paste(getwd(), '/censoring_model.txt',sep='')

posterior <- foreach(i=1:n.chains, .packages=c('R2jags'),
posterior <- foreach::foreach(chain_idx=1:n.chains, .packages=c('R2jags'),
.combine='rbind') %dopar%{

set.seed(seed+chain_idx) #define seed;
jagsfit <- jags(data = jags.data,
parameters.to.save = jags.params,
model.file = jags.model.wd,
n.chains = 1,
n.iter = n.iter,
n.burnin = n.burnin,
n.thin = n.thin,
jags.seed = seed+i)
n.thin = n.thin)
# Combine MCMC output from multiple chains
out.mcmc <- as.mcmc(jagsfit)
return(do.call(rbind, lapply(out.mcmc, as.matrix)))
}

stopCluster(cl)
parallel::stopCluster(cl)

} else if (parallel == FALSE) {
if (n.chains != 1) {
stop("Non-parallel MCMC requires exactly 1 chain.")
Expand Down
3 changes: 3 additions & 0 deletions R/globals.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if (getRversion() >= "2.15.1") {
utils::globalVariables(c("chain_idx"))
}
Loading

0 comments on commit 88ee1e5

Please sign in to comment.