Skip to content

Commit e56598f

Browse files
committed
update residual calculation
1 parent 6304ec8 commit e56598f

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

R/predict_couterfactual.R

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,8 @@ predict_counterfactual.lm <- function(fit, treatment, data = find_data(fit)) {
5959

6060
# Create residual based on the
6161
# prediction-unbiased response
62-
residual <- vector(length = nrow(ret), mode = "numeric")
63-
for (i in seq_along(trt_lvls)) {
64-
trt_idx <- which(data$treatment == trt_lvls[i])
65-
residual[trt_idx] <- y[trt_idx] - ret[trt_idx, i]
66-
}
62+
trt_idx <- match(data[[trt_vars$treatment]], trt_lvls)
63+
residual <- y - ret[cbind(seq_len(nrow(ret)), trt_idx)]
6764

6865
structure(
6966
.Data = colMeans(ret),

0 commit comments

Comments
 (0)