|
36 | 36 |
|
37 | 37 | if ((!is.character(method) && !is.function(method)) || |
38 | 38 | (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))))) { |
40 | 41 | .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", |
41 | 42 | word_list(names(.weightit_methods), and.or = FALSE, quotes = 2L)), |
42 | 43 | tidy = FALSE) |
|
53 | 54 |
|
54 | 55 | .check_method_treat.type <- function(method, treat.type) { |
55 | 56 | if (is_not_null(method) && is.character(method) && |
56 | | - (method %in% names(.weightit_methods)) && |
| 57 | + utils::hasName(.weightit_methods, method) && |
57 | 58 | (treat.type %nin% .weightit_methods[[method]]$treat_type)) { |
58 | 59 | .err(sprintf("%s can only be used with a %s treatment", |
59 | 60 | .method_to_phrase(method), |
|
63 | 64 |
|
64 | 65 | .check_required_packages <- function(method) { |
65 | 66 | if (is_not_null(method) && is.character(method) && |
66 | | - (method %in% names(.weightit_methods))) { |
| 67 | + utils::hasName(.weightit_methods, method)) { |
67 | 68 |
|
68 | 69 | pkgs <- .weightit_methods[[method]]$packages_needed |
69 | 70 |
|
|
85 | 86 | return(s.weights) |
86 | 87 | } |
87 | 88 |
|
88 | | - if (!is.character(s.weights) || length(s.weights) != 1L) { |
| 89 | + if (!chk::vld_string(s.weights)) { |
89 | 90 | .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") |
90 | 91 | } |
91 | 92 |
|
92 | 93 | if (is_null(data)) { |
93 | 94 | .err("`s.weights` was specified as a string but there was no argument to `data`") |
94 | 95 | } |
95 | 96 |
|
96 | | - if (s.weights %nin% names(data)) { |
| 97 | + if (!utils::hasName(data, s.weights)) { |
97 | 98 | .err("the name supplied to `s.weights` is not the name of a variable in `data`") |
98 | 99 | } |
99 | 100 |
|
|
111 | 112 |
|
112 | 113 | .method_to_phrase <- function(method) { |
113 | 114 |
|
114 | | - if (is_null(method)) |
| 115 | + if (is_null(method)) { |
115 | 116 | return("no weighting") |
| 117 | + } |
116 | 118 |
|
117 | | - if (is.function(method)) |
| 119 | + if (is.function(method)) { |
118 | 120 | return("a user-defined method") |
| 121 | + } |
119 | 122 |
|
120 | 123 | method <- .method_to_proper_method(method) |
121 | 124 |
|
122 | | - if (method %nin% names(.weightit_methods)) |
| 125 | + if (!utils::hasName(.weightit_methods, method)) { |
123 | 126 | return("the chosen method of weighting") |
| 127 | + } |
124 | 128 |
|
125 | 129 | .weightit_methods[[method]]$description |
126 | 130 | } |
|
315 | 319 |
|
316 | 320 | .missing_to_phrase <- function(missing) { |
317 | 321 | switch(missing, |
318 | | - "ind" = "missingness indicators", |
319 | | - "saem" = "SAEM", |
320 | | - "surr" = "surrogate splitting", |
| 322 | + ind = "missingness indicators", |
| 323 | + saem = "SAEM", |
| 324 | + surr = "surrogate splitting", |
321 | 325 | missing) |
322 | 326 | } |
323 | 327 |
|
|
350 | 354 | .err("`ps` was specified as a string but there was no argument to `data`") |
351 | 355 | } |
352 | 356 |
|
353 | | - if (ps %nin% names(data)) { |
| 357 | + if (!utils::hasName(data, ps)) { |
354 | 358 | .err("the name supplied to `ps` is not the name of a variable in `data`") |
355 | 359 | } |
356 | 360 |
|
@@ -571,7 +575,7 @@ get_treated_level <- function(treat, estimand, focal = NULL) { |
571 | 575 | by <- NULL |
572 | 576 | by.name <- NULL |
573 | 577 | } |
574 | | - else if (chk::vld_string(by) && by %in% names(data)) { |
| 578 | + else if (chk::vld_string(by) && utils::hasName(data, by)) { |
575 | 579 | by.name <- by |
576 | 580 | by <- data[[by]] |
577 | 581 | } |
@@ -1108,7 +1112,7 @@ stabilize_w <- function(weights, treat) { |
1108 | 1112 | else { |
1109 | 1113 | if (is_null(density)) .density <- function(x, log = FALSE) dnorm(x, log = log) |
1110 | 1114 | 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) |
1112 | 1116 | else if (log) log(density(x)) |
1113 | 1117 | else density(x) |
1114 | 1118 | } |
@@ -1136,7 +1140,7 @@ stabilize_w <- function(weights, treat) { |
1136 | 1140 | } |
1137 | 1141 |
|
1138 | 1142 | .density <- function(x, log = FALSE) { |
1139 | | - if ("log" %in% names(formals(splitdens1))) { |
| 1143 | + if (utils::hasName(formals(splitdens1), "log")) { |
1140 | 1144 | out <- tryCatch(do.call(splitdens1, c(list(x, log = log), as.list(str2num(splitdens[-1L])))), |
1141 | 1145 | error = function(e) { |
1142 | 1146 | .err(sprintf("Error in applying density:\n %s", |
|
0 commit comments