Skip to content

Commit 50c4864

Browse files
committed
dont drop dimension
1 parent 3821ec8 commit 50c4864

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

R/verify_optimize_controls_inputs.R

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ verify_inputs <- function(X, importances, ratio, q_s, st, z, treated, integer, s
3737
stop("\"treated\" must be one of the values in \"z\".")
3838
}
3939
n_s <- table(z, st)
40-
if (min(n_s[group == treated, ]) == 0) {
40+
if (min(n_s[group == treated, , drop = FALSE]) == 0) {
4141
warning("Note that at least one stratum has no treated individuals.")
4242
}
4343
if (!is.null(q_s)) {
4444
if (is.vector(q_s)) {
45-
q_s <- matrix(c(q_s, n_s[group == treated, ]), byrow = TRUE, nrow = 2, dimnames = list(NULL, names(q_s)))
45+
q_s <- matrix(c(q_s, n_s[group == treated, , drop = FALSE]), byrow = TRUE, nrow = 2, dimnames = list(NULL, names(q_s)))
4646
if (group[1] == treated) {
4747
q_s <- q_s[c(2, 1), ]
4848
}
@@ -92,43 +92,43 @@ verify_multi_comp_inputs <- function(q_s, q_star_s, n_s, treated, treated_star,
9292
if (correct_sizes) {
9393
warning("Sample sizes are only correct in expectation for multiple comparisons. \"correct_sizes\" has thus been switched to `FALSE`.")
9494
}
95-
95+
9696
if (!all(treated_star %in% group)) {
9797
stop("Each entry of \"treated_star\" must be one of the values in \"z\".")
9898
}
99-
99+
100100
for (t in treated_star) {
101-
if (min(n_s[group == t, ]) == 0) {
101+
if (min(n_s[group == t, , drop = FALSE]) == 0) {
102102
warning("Note that at least one stratum has no treated individuals for at least one supplemental comparison.")
103103
}
104104
}
105-
105+
106106
if (!is.null(q_star_s)) {
107107
if (!is.list(q_star_s)) {
108108
q_star_s <- list(q_star_s)
109109
}
110110
# Set up q_s for first comparison
111111
if (is.vector(q_s)) {
112-
q_s <- matrix(c(q_s, n_s[group == treated, ]), byrow = TRUE, nrow = 2, dimnames = list(NULL, names(q_s)))
112+
q_s <- matrix(c(q_s, n_s[group == treated, , drop = FALSE]), byrow = TRUE, nrow = 2, dimnames = list(NULL, names(q_s)))
113113
if (group[1] == treated) {
114114
q_s <- q_s[c(2, 1), ]
115115
}
116116
}
117117
Q_s <- q_s
118-
118+
119119
# Check whether a sample size within a comparison is too large
120120
for (comp in 1:length(q_star_s)) {
121121
q <- q_star_s[[comp]]
122122
t <- treated_star[comp]
123123
if (is.vector(q)) {
124-
q <- matrix(c(q, n_s[group == t, ]), byrow = TRUE, nrow = 2, dimnames = list(NULL, names(q)))
124+
q <- matrix(c(q, n_s[group == t, , drop = FALSE]), byrow = TRUE, nrow = 2, dimnames = list(NULL, names(q)))
125125
if (group[1] == t) {
126126
q <- q[c(2, 1), ]
127127
}
128128
}
129-
129+
130130
Q_s <- Q_s + q
131-
131+
132132
if (any(q[, colnames(n_s)] > n_s)) {
133133
stop("At least one of the entries for `q_star_s` is greater than the number of units available in the stratum.
134134
Please lower `q_star_s` such that all entries are at most the number of available units.",
@@ -160,7 +160,7 @@ verify_multi_comp_inputs <- function(q_s, q_star_s, n_s, treated, treated_star,
160160
process_qs <- function(ratio, q_s, n_s, treated, k, group, st_vals, stratios) {
161161
if (!is.null(q_s) & is.vector(q_s)) {
162162
q_s <- matrix(rep(q_s, k), byrow = TRUE, nrow = k, dimnames = list(NULL, names(q_s)))
163-
q_s[group == treated, ] <- n_s[group == treated, ]
163+
q_s[group == treated, , drop = FALSE] <- n_s[group == treated, , drop = FALSE]
164164
}
165165
if (!is.null(ratio) & length(ratio) == 1) {
166166
ratio <- rep(ratio, k)
@@ -171,17 +171,17 @@ process_qs <- function(ratio, q_s, n_s, treated, k, group, st_vals, stratios) {
171171
if (is.null(ratio)) {
172172
ratio <- sapply(1:nrow(stratios), function(i) min(1, min(stratios[i, ])))
173173
}
174-
q_s <- round(ratio %*% t(n_s[group == treated, ]))
174+
q_s <- round(ratio %*% t(n_s[group == treated, , drop = FALSE]))
175175
if (any(q_s > n_s)) {
176176
stop("The ratio you specified is not feasible.
177177
Please supply `q_s` instead of `ratio` or lower the `ratio` input.",
178178
call. = FALSE)
179179
}
180180
colnames(q_s) <- st_vals
181181
} else {
182-
q_s <- q_s[, st_vals]
182+
q_s <- q_s[, st_vals, drop = FALSE]
183183
}
184-
184+
185185
return(q_s)
186186
}
187187

0 commit comments

Comments
 (0)