Skip to content

Commit 581ce55

Browse files
committed
Code and doc cleaning
1 parent ca97846 commit 581ce55

File tree

8 files changed

+54
-52
lines changed

8 files changed

+54
-52
lines changed

R/functions_for_processing.R

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636

3737
if ((!is.character(method) && !is.function(method)) ||
3838
(is.character(method) && (length(method) > 1L ||
39-
.method_to_proper_method(method) %nin% names(.weightit_methods)))) {
39+
!utils::hasName(.weightit_methods,
40+
.method_to_proper_method(method))))) {
4041
.err(sprintf("`method` must be a string of length 1 containing the name of an acceptable weighting method or a function that produces weights. Allowable methods:\n%s",
4142
word_list(names(.weightit_methods), and.or = FALSE, quotes = 2L)),
4243
tidy = FALSE)
@@ -53,7 +54,7 @@
5354

5455
.check_method_treat.type <- function(method, treat.type) {
5556
if (is_not_null(method) && is.character(method) &&
56-
(method %in% names(.weightit_methods)) &&
57+
utils::hasName(.weightit_methods, method) &&
5758
(treat.type %nin% .weightit_methods[[method]]$treat_type)) {
5859
.err(sprintf("%s can only be used with a %s treatment",
5960
.method_to_phrase(method),
@@ -63,7 +64,7 @@
6364

6465
.check_required_packages <- function(method) {
6566
if (is_not_null(method) && is.character(method) &&
66-
(method %in% names(.weightit_methods))) {
67+
utils::hasName(.weightit_methods, method)) {
6768

6869
pkgs <- .weightit_methods[[method]]$packages_needed
6970

@@ -85,15 +86,15 @@
8586
return(s.weights)
8687
}
8788

88-
if (!is.character(s.weights) || length(s.weights) != 1L) {
89+
if (!chk::vld_string(s.weights)) {
8990
.err("the argument to `s.weights` must be a vector or data frame of sampling weights or the (quoted) names of the variable in `data` that contains sampling weights")
9091
}
9192

9293
if (is_null(data)) {
9394
.err("`s.weights` was specified as a string but there was no argument to `data`")
9495
}
9596

96-
if (s.weights %nin% names(data)) {
97+
if (!utils::hasName(data, s.weights)) {
9798
.err("the name supplied to `s.weights` is not the name of a variable in `data`")
9899
}
99100

@@ -111,16 +112,19 @@
111112

112113
.method_to_phrase <- function(method) {
113114

114-
if (is_null(method))
115+
if (is_null(method)) {
115116
return("no weighting")
117+
}
116118

117-
if (is.function(method))
119+
if (is.function(method)) {
118120
return("a user-defined method")
121+
}
119122

120123
method <- .method_to_proper_method(method)
121124

122-
if (method %nin% names(.weightit_methods))
125+
if (!utils::hasName(.weightit_methods, method)) {
123126
return("the chosen method of weighting")
127+
}
124128

125129
.weightit_methods[[method]]$description
126130
}
@@ -315,9 +319,9 @@
315319

316320
.missing_to_phrase <- function(missing) {
317321
switch(missing,
318-
"ind" = "missingness indicators",
319-
"saem" = "SAEM",
320-
"surr" = "surrogate splitting",
322+
ind = "missingness indicators",
323+
saem = "SAEM",
324+
surr = "surrogate splitting",
321325
missing)
322326
}
323327

@@ -350,7 +354,7 @@
350354
.err("`ps` was specified as a string but there was no argument to `data`")
351355
}
352356

353-
if (ps %nin% names(data)) {
357+
if (!utils::hasName(data, ps)) {
354358
.err("the name supplied to `ps` is not the name of a variable in `data`")
355359
}
356360

@@ -571,7 +575,7 @@ get_treated_level <- function(treat, estimand, focal = NULL) {
571575
by <- NULL
572576
by.name <- NULL
573577
}
574-
else if (chk::vld_string(by) && by %in% names(data)) {
578+
else if (chk::vld_string(by) && utils::hasName(data, by)) {
575579
by.name <- by
576580
by <- data[[by]]
577581
}
@@ -1108,7 +1112,7 @@ stabilize_w <- function(weights, treat) {
11081112
else {
11091113
if (is_null(density)) .density <- function(x, log = FALSE) dnorm(x, log = log)
11101114
else if (is.function(density)) .density <- function(x, log = FALSE) {
1111-
if ("log" %in% names(formals(density))) density(x, log = log)
1115+
if (utils::hasName(formals(density), "log")) density(x, log = log)
11121116
else if (log) log(density(x))
11131117
else density(x)
11141118
}
@@ -1136,7 +1140,7 @@ stabilize_w <- function(weights, treat) {
11361140
}
11371141

11381142
.density <- function(x, log = FALSE) {
1139-
if ("log" %in% names(formals(splitdens1))) {
1143+
if (utils::hasName(formals(splitdens1), "log")) {
11401144
out <- tryCatch(do.call(splitdens1, c(list(x, log = log), as.list(str2num(splitdens[-1L])))),
11411145
error = function(e) {
11421146
.err(sprintf("Error in applying density:\n %s",

R/glm_weightit-methods.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ update.glm_weightit <- function(object, formula. = NULL, ..., evaluate = TRUE) {
788788
}
789789
}
790790

791-
if ("s.weights" %in% names(extras)) {
791+
if (utils::hasName(extras, "s.weights")) {
792792
extras[["s.weights"]] <- NULL
793793
}
794794
}

R/plot.weightit.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#'
1414
#' @details
1515
#'
16-
#' ## [`method = "gbm"`](method_gbm)
16+
#' ## `method = "gbm"`
1717
#'
1818
#' After weighting with generalized boosted modeling, `plot()` displays the
1919
#' results of the tuning process used to find the optimal number of trees (and
@@ -25,7 +25,7 @@
2525
#' `weightit()`, the plot is faceted by the `by` variable. See [`method_gbm`]
2626
#' for more information on selecting tuning parameters.
2727
#'
28-
#' ## [`method = "optweight"`](method_optweight)
28+
#' ## `method = "optweight"`
2929
#'
3030
#' After estimating stable balancing weights, `plot()` displays the values of
3131
#' the dual variables for each balance constraint in a bar graph. Large values
@@ -37,7 +37,7 @@
3737
#' each covariate after weighting is equal to its unweighted mean) and those for
3838
#' balance (i.e., ensuring the treatment-covariate correlations are no larger
3939
#' than the imbalance tolerance). This is essentially a wrapper for
40-
#' \pkgfun{optweight}{plot.optweight}.
40+
#' \pkgfun{optweight}{plot.optweight}. See [`method_optweight`] for details.
4141
#'
4242
#' @seealso [weightit()], [plot.summary.weightit()]
4343
#'
@@ -52,13 +52,13 @@ plot.weightit <- function(x, ...) {
5252
.err("`plot(.)` can currently only be used with `weightit()` output objects. To view the distribution of weights, use `plot(summary(.))`")
5353
}
5454

55-
if (!is.character(x$method) || !.weightit_methods[[x$method]]$plot.weightit_ok) {
55+
if (!chk::vld_string(x$method) || !.weightit_methods[[x$method]]$plot.weightit_ok) {
5656
.err(sprintf("`plot(.)` cannot be used with %s. To view the distribution of weights, use `plot(summary(.))`",
5757
.method_to_phrase(x$method)))
5858
}
5959

6060
switch(x$method,
61-
"gbm" = .plot_tune_gbm(x$info, x$by),
62-
"optweight" = .plot_duals_optweight(x$info, x$by)
61+
gbm = .plot_tune_gbm(x$info, x$by),
62+
optweight = .plot_duals_optweight(x$info, x$by)
6363
)
6464
}

R/utils.R

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ round_df_char <- function(df, digits, pad = "0", na_vals = "") {
170170
df[nas] <- na_vals
171171
df[infs] <- "N/A"
172172

173-
if (length(rn) > 0L) rownames(df) <- rn
174-
if (length(cn) > 0L) names(df) <- cn
173+
if (is_not_null(rn)) rownames(df) <- rn
174+
if (is_not_null(cn)) names(df) <- cn
175175

176176
df
177177
}
@@ -591,28 +591,28 @@ hasbar <- function(term) {
591591
#treat/covs
592592
get_covs_and_treat_from_formula <- function(f, data = NULL, terms = FALSE, sep = "", ...) {
593593

594+
if (!rlang::is_formula(f)) {
595+
.err("`formula` must be a formula")
596+
}
597+
598+
env <- environment(f)
599+
594600
#Check if data exists
595601
if (is_not_null(data)) {
596602
if (is.data.frame(data)) {
597603
data.specified <- TRUE
598604
}
599605
else {
600606
.wrn("the argument supplied to `data` is not a data.frame object. This may causes errors or unexpected results")
601-
data <- environment(f)
607+
data <- env
602608
data.specified <- FALSE
603609
}
604610
}
605611
else {
606-
data <- environment(f)
612+
data <- env
607613
data.specified <- FALSE
608614
}
609615

610-
env <- environment(f)
611-
612-
if (!rlang::is_formula(f)) {
613-
.err("`formula` must be a formula")
614-
}
615-
616616
eval.model.matrx <- !hasbar(f)
617617

618618
tryCatch({
@@ -628,6 +628,9 @@ get_covs_and_treat_from_formula <- function(f, data = NULL, terms = FALSE, sep =
628628
.err(msg)
629629
})
630630

631+
treat <- ...get("treat")
632+
treat.name <- NULL
633+
631634
#Check if response exists
632635
if (rlang::is_formula(tt, lhs = TRUE)) {
633636
resp.var.mentioned <- attr(tt, "variables")[[2L]]
@@ -648,25 +651,18 @@ get_covs_and_treat_from_formula <- function(f, data = NULL, terms = FALSE, sep =
648651
}
649652

650653
if (resp.var.failed) {
651-
if (is_null(...get("treat"))) {
654+
if (is_null(treat)) {
652655
.err(sprintf("the given response variable, %s, is not a variable in %s",
653656
add_quotes(resp.var.mentioned.char),
654657
word_list(c("data", "the global environment")[c(data.specified, TRUE)], "or")))
655658
}
656659
tt <- delete.response(tt)
657660
}
658-
}
659-
else {
660-
resp.var.failed <- TRUE
661-
}
662661

663-
if (resp.var.failed) {
664-
treat <- ...get("treat")
665-
treat.name <- NULL
666-
}
667-
else {
668-
treat.name <- resp.var.mentioned.char
669-
treat <- eval(resp.var.mentioned, data, env)
662+
if (!resp.var.failed) {
663+
treat.name <- resp.var.mentioned.char
664+
treat <- eval(resp.var.mentioned, data, env)
665+
}
670666
}
671667

672668
#Check if RHS variables exist
@@ -762,7 +758,7 @@ get_covs_and_treat_from_formula <- function(f, data = NULL, terms = FALSE, sep =
762758
.err(conditionMessage(e), tidy = FALSE)
763759
})
764760

765-
if (is_not_null(treat.name) && treat.name %in% names(covs)) {
761+
if (is_not_null(treat.name) && utils::hasName(covs, treat.name)) {
766762
.err("the variable on the left side of the formula appears on the right side too")
767763
}
768764
}

R/weightit.fit.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,8 @@ weightitMSM.fit <- function(covs.list, treat.list, method = "glm", s.weights = N
463463

464464
obj <- NULL
465465

466+
.check_required_packages(method)
467+
466468
if (is.function(method)) {
467469
fun <- "weightitMSM2user"
468470
}

R/weightit2user.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ weightit2user <- function(Fun, covs, treat, s.weights, subset, estimand, focal,
150150

151151
#Get a list of function args for the user-defined function Fun
152152
Fun_formal <- as.list(formals(Fun))
153-
has_dots <- ("..." %in% names(Fun_formal))
153+
has_dots <- utils::hasName(Fun_formal, "...")
154154
if (has_dots) {
155155
Fun_formal[["..."]] <- NULL
156156
}
@@ -160,7 +160,7 @@ weightit2user <- function(Fun, covs, treat, s.weights, subset, estimand, focal,
160160
if (exists(i, inherits = FALSE)) {
161161
fun_args[i] <- list(get0(i, inherits = FALSE))
162162
}
163-
else if (i %in% names(A)) {
163+
else if (utils::hasName(A, i)) {
164164
fun_args[i] <- A[i]
165165
A[[i]] <- NULL
166166
}
@@ -227,7 +227,7 @@ weightitMSM2user <- function(Fun, covs.list, treat.list, s.weights, subset, stab
227227

228228
#Get a list of function args for the user-defined function Fun
229229
Fun_formal <- as.list(formals(Fun))
230-
has_dots <- ("..." %in% names(Fun_formal))
230+
has_dots <- utils::hasName(Fun_formal, "...")
231231
if (has_dots) {
232232
Fun_formal[["..."]] <- NULL
233233
}

_dev/R/_aux_functions.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ process.ps <- function(ps, data = NULL, treat) {
324324
if (is_null(data)) {
325325
stop("'ps' was specified as a string but there was no argument to 'data'.", call. = FALSE)
326326
}
327-
else if (ps %in% names(data)) {
327+
else if (utils::hasName(data, ps)) {
328328
ps <- data[[ps]]
329329
}
330330
else stop("The name supplied to 'ps' is not the name of a variable in 'ps'.", call. = FALSE)

man/plot.weightit.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)