Skip to content

Commit

Permalink
Merge pull request #98 from istallworthy/feedback
Browse files Browse the repository at this point in the history
adding packages and incorporating noah's push
  • Loading branch information
istallworthy authored Sep 27, 2023
2 parents 90cdebe + 2d0e255 commit 13ed508
Show file tree
Hide file tree
Showing 13 changed files with 266 additions and 283 deletions.
Binary file modified .DS_Store
Binary file not shown.
9 changes: 5 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,24 @@ License: GPL-3
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.2
RoxygenNote: 7.2.3
Imports:
WeightIt,
marginaleffects,
tidyr,
stats,
ggplot2,
matrixStats,
plyr,
knitr,
survey,
jtools,
mice,
stargazer,
gtools,
cobalt,
stringr
stringr,
huxtable,
officer,
flextable
Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
Expand Down
32 changes: 0 additions & 32 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,4 @@ export(get_reference_values)
export(make_love_plot)
export(perform_multiple_comparison_correction)
export(trimWeights)
importFrom(WeightIt,weightitMSM)
importFrom(dplyr,"%>%")
importFrom(dplyr,bind_rows)
importFrom(dplyr,filter)
importFrom(dplyr,mutate)
importFrom(dplyr,select)
importFrom(ggplot2,aes)
importFrom(ggplot2,element_blank)
importFrom(ggplot2,element_rect)
importFrom(ggplot2,element_text)
importFrom(ggplot2,geom_histogram)
importFrom(ggplot2,geom_point)
importFrom(ggplot2,geom_text)
importFrom(ggplot2,geom_vline)
importFrom(ggplot2,ggplot)
importFrom(ggplot2,ggsave)
importFrom(ggplot2,ggtitle)
importFrom(ggplot2,guide_axis)
importFrom(ggplot2,scale_y_discrete)
importFrom(ggplot2,theme)
importFrom(ggplot2,xlab)
importFrom(ggplot2,xlim)
importFrom(gtools,permutations)
importFrom(jtools,export_summs)
importFrom(knitr,kable)
importFrom(marginaleffects,avg_predictions)
importFrom(marginaleffects,hypotheses)
importFrom(mice,pool)
importFrom(stargazer,stargazer)
importFrom(stats,p.adjust)
importFrom(stringr,str_count)
importFrom(survey,svydesign)
importFrom(survey,svyglm)
277 changes: 142 additions & 135 deletions R/assessBalance.R

Large diffs are not rendered by default.

178 changes: 92 additions & 86 deletions R/calcBalStats.R

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions R/compareHistories.R
Original file line number Diff line number Diff line change
Expand Up @@ -618,18 +618,18 @@ compareHistories <- function(home_dir, exposure, exposure_time_pts, outcome, mod
comparisons <- comparisons[order(comparisons$dose), ]

if (length(colors) > 1) { # If user input a list of colors
p <- ggplot2::ggplot(data = comparisons, aes(x = estimate, y = history, color = dose)) +
p <- ggplot2::ggplot(data = comparisons, ggplot2::aes(x = estimate, y = history, color = dose)) +
ggplot2::geom_point(size = 5) +
ggplot2::scale_color_manual(values = colors) +
ggplot2::scale_y_discrete(limits = c(as.character(comparisons$history)), expand = c(0, 0.2)) +
ggplot2::geom_errorbarh(aes(xmin = low_ci, xmax = high_ci), height = 0.6) +
ggplot2::geom_errorbarh(ggplot2::aes(xmin = low_ci, xmax = high_ci), height = 0.6) +
ggplot2::xlab(paste0("Predicted ", out_lab, " Value")) +
ggplot2::ylab(paste0(exp_lab, " Exposure History")) +
ggplot2::xlim(min(comparisons$low_ci) - 1 * sd(comparisons$low_ci),
max(comparisons$high_ci) + 1 * sd(comparisons$high_ci)) +
ggplot2::theme(text = ggplot2::element_text(size = 14)) +
ggplot2::theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = ggplot2::element_line(colour = "black"))
ggplot2::theme(panel.grid.major = ggplot2::element_blank(), panel.grid.minor = element_blank(),
panel.background = ggplot2::element_blank(), axis.line = ggplot2::element_line(colour = "black"))

if (save.out){
ggplot2::ggsave(paste0(home_dir, "/plots/", exposure, "-", outcome, ".jpeg"), plot = p)
Expand All @@ -639,18 +639,18 @@ compareHistories <- function(home_dir, exposure, exposure_time_pts, outcome, mod
}

} else { # If user lists a palette (default)
p <- ggplot2::ggplot(data = comparisons, aes(x = estimate, y = history, color = dose)) +
p <- ggplot2::ggplot(data = comparisons, ggplot2::aes(x = estimate, y = history, color = dose)) +
ggplot2::geom_point(size = 5) +
ggplot2::scale_colour_brewer(palette = colors) +
ggplot2::scale_y_discrete(limits = c(as.character(comparisons$history)), expand = c(0, 0.2)) +
ggplot2::geom_errorbarh(aes(xmin = low_ci, xmax = high_ci), height = 0.6) +
ggplot2::geom_errorbarh(ggplot2::aes(xmin = low_ci, xmax = high_ci), height = 0.6) +
ggplot2::xlab(paste0("Predicted ", out_lab, " Value")) +
ggplot2::ylab(paste0(exp_lab, " Exposure History")) +
ggplot2::xlim(min(comparisons$low_ci) - 1 * sd(comparisons$low_ci),
max(comparisons$high_ci) + 1 * sd(comparisons$high_ci)) +
ggplot2::theme(text = ggplot2::element_text(size = 14)) +
ggplot2::theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = ggplot2::element_line(colour = "black")) +
ggplot2::theme(panel.grid.major = ggplot2::element_blank(), panel.grid.minor = ggplot2::element_blank(),
panel.background = ggplot2::element_blank(), axis.line = ggplot2::element_line(colour = "black")) +
ggplot2::guides(fill = ggplot2::guide_legend(title="Dosage"))

if (save.out){
Expand Down
5 changes: 3 additions & 2 deletions R/createFormulas.R
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,16 @@ createFormulas <- function(home_dir, exposure, exposure_time_pts, outcome, type,
# as.numeric(covar_time) > 0) %>% # Finds any lagged imbalanced covars
# dplyr::select(covariate)

new <- bal_stats[bal_stats$balanced ==0 &
new <- bal_stats[bal_stats$balanced == 0 &
bal_stats$exp_time == time &
as.numeric(bal_stats$covar_time) < exposure_time_pts[x-1] &
as.numeric(bal_stats$covar_time) > 0, ]
new <- new[, "covariate"]


if (nrow(new) > 0) {

new <- new[, "covariate"]

new <- as.character(unlist(new))

time_var_include <- c(time_var_include, new)
Expand Down
10 changes: 5 additions & 5 deletions R/createWeights.R
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ createWeights <- function(home_dir, data, exposure, outcome, formulas, method =
calculate_weights <- function(data, form, weights_method, SL.library) {

if(weights_method == "super"){
fit <- weightitMSM(form,
fit <- WeightIt::weightitMSM(form,
data = data,
method = weights_method,
stabilize = TRUE,
Expand All @@ -183,7 +183,7 @@ createWeights <- function(home_dir, data, exposure, outcome, formulas, method =
over = FALSE)
}
else{
fit <- weightitMSM(form,
fit <- WeightIt::weightitMSM(form,
data = data,
method = weights_method,
stabilize = TRUE,
Expand Down Expand Up @@ -227,7 +227,7 @@ createWeights <- function(home_dir, data, exposure, outcome, formulas, method =
}

# Writes image of the histogram of weights to assess heavy tails
p <- ggplot2::ggplot(data = as.data.frame(fit$weight), aes(x = fit$weight)) +
p <- ggplot2::ggplot(data = as.data.frame(fit$weight), ggplot2::aes(x = fit$weight)) +
ggplot2::geom_histogram(color = 'black', bins = 15) +
ggplot2::xlab("Weights") +
ggplot2::ggtitle(paste0("Distribution of ", weights_method, " weights"))
Expand Down Expand Up @@ -283,7 +283,7 @@ createWeights <- function(home_dir, data, exposure, outcome, formulas, method =
}

# Writes image of the histogram of weights to assess heavy tails
p <- ggplot2::ggplot(data = as.data.frame(fit$weight), aes(x = fit$weight)) +
p <- ggplot2::ggplot(data = as.data.frame(fit$weight), ggplot2::aes(x = fit$weight)) +
ggplot2::geom_histogram(color = 'black', bins = 15) +
ggplot2::xlab("Weights") +
ggplot2::ggtitle(paste0("Distribution of ", weights_method, " weights"))
Expand Down Expand Up @@ -340,7 +340,7 @@ createWeights <- function(home_dir, data, exposure, outcome, formulas, method =
}

# Writes image of the histogram of weights to assess heavy tails
p <- ggplot2::ggplot(data = as.data.frame(weights[[1]]$weights), aes(x = weights[[1]]$weights)) +
p <- ggplot2::ggplot(data = as.data.frame(weights[[1]]$weights), ggplot2::aes(x = weights[[1]]$weights)) +
ggplot2::geom_histogram(color = 'black', bins = 15) +
ggplot2::xlab("Weights") +
ggplot2::ggtitle(paste0("Distribution of ", weights_method, " weights"))
Expand Down
1 change: 1 addition & 0 deletions R/fitModel.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#' weights.
#' @seealso {[survey::svyglm()] for more on family/link specifications,
#' <https://www.rdocumentation.org/packages/survey/versions/4.2-1/topics/svyglm>}
#' @importFrom survey svyglm
#' @param home_dir path to home directory (required if 'save.out' = TRUE)
#' @param data data in wide format as: a data frame, list of imputed data
#' frames, or mids object
Expand Down
6 changes: 3 additions & 3 deletions R/make_love_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ make_love_plot <- function(home_dir, folder, exposure, exposure_time_pt, exposur
}

# Make love plot per exposure time point
lp <- ggplot2::ggplot(aes(x = avg_bal, y = reorder(as.factor(covariate), avg_bal)), data = balance_stats) +
ggplot2::geom_point(aes(y = reorder(as.factor(covariate), avg_bal), x = avg_bal, fill = "white", alpha = 1)) +
ggplot2::geom_text(aes(label = labels, hjust = -0.2, vjust = 0.2), size = 1.5, color = "red") +
lp <- ggplot2::ggplot(ggplot2::aes(x = avg_bal, y = reorder(as.factor(covariate), avg_bal)), data = balance_stats) +
ggplot2::geom_point(ggplot2::aes(y = reorder(as.factor(covariate), avg_bal), x = avg_bal, fill = "white", alpha = 1)) +
ggplot2::geom_text(ggplot2::aes(label = labels, hjust = -0.2, vjust = 0.2), size = 1.5, color = "red") +
ggplot2::xlab(x_lab) +
ggplot2::ylab("Covariate") +
ggplot2::xlim(min_val, max_val) +
Expand Down
6 changes: 3 additions & 3 deletions R/trimWeights.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ trimWeights <- function(home_dir, exposure, outcome, weights, quantile = 0.95, v
}

# Save histogram of new weights
p <- ggplot2::ggplot(as.data.frame(t), aes(x = t)) +
p <- ggplot2::ggplot(as.data.frame(t), ggplot2::aes(x = t)) +
ggplot2::geom_histogram(color = 'black', bins = 15)

if(verbose){
Expand Down Expand Up @@ -181,9 +181,9 @@ trimWeights <- function(home_dir, exposure, outcome, weights, quantile = 0.95, v
}

# Save histogram of new weights
p <- ggplot2::ggplot(as.data.frame(t), aes(x = t)) +
p <- ggplot2::ggplot(as.data.frame(t), ggplot2::aes(x = t)) +
ggplot2::geom_histogram(color = 'black', bins = 15) +
ggtitle(paste0("Weights trimmed at the ", quantile, "th value"))
ggplot2::ggtitle(paste0("Weights trimmed at the ", quantile, "th value"))

if(verbose){
print(p)
Expand Down
8 changes: 4 additions & 4 deletions examplePipelineRevised.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ library(devMSMsHelpers)
#prior to re-installing using the code above. Sorry, this is annoying!
#conducting package checks & tests from documentation --just for IS to run; comment out when testing the workflow
# devtools::check()
devtools::check()
```


Expand Down Expand Up @@ -198,9 +198,9 @@ data <- mice::complete(imputed_data, 1) #just for testing purposes
#IS testing multi-level factors
data$"PmMrSt2" = as.factor(data$"PmMrSt2")
data$HomeOwnd = as.factor(data$HomeOwnd)
# library(dplyr)
# data <- data %>%
# dplyr::select(-c(contains(c(":", "Childhood", "Infancy", "Toddlerhood", "pcx")))) #removing these --came from old dataset
library(dplyr)
data <- data %>%
dplyr::select(-c(contains(c(":", "Childhood", "Infancy", "Toddlerhood", "pcx")))) #removing these --came from old dataset
```

Expand Down
1 change: 0 additions & 1 deletion man/make_love_plot.Rd

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

0 comments on commit 13ed508

Please sign in to comment.