Skip to content

Commit a3f2b51

Browse files
format with updated air
1 parent 7f872c6 commit a3f2b51

File tree

4 files changed

+49
-14
lines changed

4 files changed

+49
-14
lines changed

R/check_balance.R

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,11 @@ check_balance <- function(
193193
if (any(categorical_check)) {
194194
categorical_cols <- original_vars_data[categorical_check]
195195
binary_check <- purrr::map_lgl(categorical_cols, \(x) {
196-
n_levels <- if (is.factor(x)) length(levels(x)) else
196+
n_levels <- if (is.factor(x)) {
197+
length(levels(x))
198+
} else {
197199
length(unique(x))
200+
}
198201
n_levels == 2
199202
})
200203
binary_categorical_names <- names(categorical_cols)[binary_check]
@@ -511,8 +514,11 @@ compute_single_balance_metric <- function(
511514
} else {
512515
setdiff(
513516
group_levels,
514-
if (reference_group %in% group_levels) reference_group else
517+
if (reference_group %in% group_levels) {
518+
reference_group
519+
} else {
515520
group_levels[reference_group]
521+
}
516522
)[1]
517523
}
518524

R/compute_balance.R

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ bal_smd <- function(
6868
if (is.null(weights)) {
6969
if (any(is.na(covariate) | is.na(group))) return(NA_real_)
7070
} else {
71-
if (any(is.na(covariate) | is.na(group) | is.na(weights)))
71+
if (any(is.na(covariate) | is.na(group) | is.na(weights))) {
7272
return(NA_real_)
73+
}
7374
}
7475
}
7576

@@ -238,8 +239,11 @@ bal_vr <- function(
238239
mr <- sum(wr * xr) / sum(wr)
239240
# Use Bessel's correction for weighted sample variance
240241
denom <- sum(wr) - sum(wr^2) / sum(wr)
241-
if (denom <= 0) sum(wr * (xr - mr)^2) / sum(wr) else
242+
if (denom <= 0) {
243+
sum(wr * (xr - mr)^2) / sum(wr)
244+
} else {
242245
sum(wr * (xr - mr)^2) / denom
246+
}
243247
}
244248
var_other <- if (is.null(weights)) {
245249
stats::var(covariate[idx_other])
@@ -249,8 +253,11 @@ bal_vr <- function(
249253
mo <- sum(wo * xo) / sum(wo)
250254
# Use Bessel's correction for weighted sample variance
251255
denom <- sum(wo) - sum(wo^2) / sum(wo)
252-
if (denom <= 0) sum(wo * (xo - mo)^2) / sum(wo) else
256+
if (denom <= 0) {
257+
sum(wo * (xo - mo)^2) / sum(wo)
258+
} else {
253259
sum(wo * (xo - mo)^2) / denom
260+
}
254261
}
255262
}
256263
# Return ratio
@@ -386,10 +393,16 @@ bal_ks <- function(
386393
# Extract and weight
387394
x_ref <- covariate[idx_ref]
388395
x_other <- covariate[idx_other]
389-
w_ref <- if (is.null(weights)) rep(1, length(x_ref)) else
396+
w_ref <- if (is.null(weights)) {
397+
rep(1, length(x_ref))
398+
} else {
390399
extract_weight_data(weights)[idx_ref]
391-
w_other <- if (is.null(weights)) rep(1, length(x_other)) else
400+
}
401+
w_other <- if (is.null(weights)) {
402+
rep(1, length(x_other))
403+
} else {
392404
extract_weight_data(weights)[idx_other]
405+
}
393406
w_ref <- w_ref / sum(w_ref)
394407
w_other <- w_other / sum(w_other)
395408
# Sort and CDF
@@ -464,7 +477,9 @@ bal_corr <- function(x, y, weights = NULL, na.rm = FALSE) {
464477
if (!is.null(weights)) weights <- extract_weight_data(weights)[idx]
465478
} else {
466479
# Extract weight data if needed
467-
if (!is.null(weights)) weights <- extract_weight_data(weights)
480+
if (!is.null(weights)) {
481+
weights <- extract_weight_data(weights)
482+
}
468483
# Check for missing values
469484
if (is.null(weights)) {
470485
if (any(is.na(x) | is.na(y))) return(NA_real_)
@@ -1053,7 +1068,9 @@ bal_energy_continuous <- function(
10531068

10541069
# Avoid division by zero
10551070
covariate_vars[covariate_vars == 0] <- 1
1056-
if (treatment_var == 0) treatment_var <- 1
1071+
if (treatment_var == 0) {
1072+
treatment_var <- 1
1073+
}
10571074

10581075
# Scale covariates and treatment
10591076
scaled_covariates <- scale(covariates, scale = sqrt(covariate_vars))

R/plot_ess.R

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,20 @@ plot_ess <- function(
191191
p <- p +
192192
ggplot2::labs(
193193
x = "method",
194-
y = if (percent_scale) "effective sample size (%)" else
194+
y = if (percent_scale) {
195+
"effective sample size (%)"
196+
} else {
195197
"effective sample size"
198+
}
196199
) +
197200
ggplot2::scale_y_continuous(
198201
limits = c(0, y_upper),
199202
expand = c(0, 0),
200-
labels = if (percent_scale) \(x) scales::percent(x / 100) else
203+
labels = if (percent_scale) {
204+
\(x) scales::percent(x / 100)
205+
} else {
201206
scales::number
207+
}
202208
)
203209

204210
p

R/utils-validation.R

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ validate_weights <- function(
2323
arg_name = "weights",
2424
call = rlang::caller_env()
2525
) {
26-
if (is.null(weights)) return(invisible(weights))
26+
if (is.null(weights)) {
27+
return(invisible(weights))
28+
}
2729

2830
# Accept both numeric vectors and psw objects from propensity package
2931
is_valid_weights <- is.numeric(weights) || propensity::is_psw(weights)
@@ -162,15 +164,19 @@ validate_column_exists <- function(
162164

163165
# NA handling helpers
164166
check_na_return <- function(..., na.rm = FALSE) {
165-
if (na.rm) return(FALSE)
167+
if (na.rm) {
168+
return(FALSE)
169+
}
166170

167171
values <- list(...)
168172
any(vapply(values, function(x) any(is.na(x)), logical(1)))
169173
}
170174

171175
# Filter indices based on NA values
172176
filter_na_indices <- function(indices, data, weights = NULL, na.rm = FALSE) {
173-
if (!na.rm) return(indices)
177+
if (!na.rm) {
178+
return(indices)
179+
}
174180

175181
if (is.null(weights)) {
176182
indices[!is.na(data[indices])]

0 commit comments

Comments
 (0)