Skip to content

Commit

Permalink
nfultz recs
Browse files Browse the repository at this point in the history
  • Loading branch information
lukesonnet committed Mar 17, 2018
1 parent 4b0aee3 commit 33e3cd7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
3 changes: 1 addition & 2 deletions R/estimatr_difference_in_means.R
Expand Up @@ -240,8 +240,7 @@ difference_in_means <-
data$cluster <- model_data$cluster
# rescale weights for convenience
if (is.numeric(model_data$weights)) {
weight_mean <- mean(model_data$weights)
data$weights <- model_data$weights / weight_mean
data$weights <- model_data$weights / mean(model_data$weights)
}
data$block <- model_data$block

Expand Down
11 changes: 3 additions & 8 deletions R/estimatr_lm_lin.R
Expand Up @@ -265,15 +265,10 @@ lm_lin <- function(formula,
if (is.numeric(weights)) {
center <- apply(demeaned_covars, 2, weighted.mean, weights)
} else {
center <- TRUE
center <- colMeans(demeaned_covars)
}

demeaned_covars <-
scale(
demeaned_covars,
center = center,
scale = FALSE
)
demeaned_covars <- sweep(demeaned_covars, 2, center)

original_covar_names <- colnames(demeaned_covars)

Expand Down Expand Up @@ -349,7 +344,7 @@ lm_lin <- function(formula,
formula = formula
)

return_list[["scaled_center"]] <- attr(demeaned_covars, "scaled:center")
return_list[["scaled_center"]] <- center
setNames(return_list[["scaled_center"]], original_covar_names)

return_list[["call"]] <- match.call()
Expand Down
8 changes: 7 additions & 1 deletion tests/testthat/test-lm-lin.R
Expand Up @@ -312,7 +312,7 @@ test_that("Test LM Lin", {
)
})

test_that("lm_lin weights appropriately", {
test_that("lm_lin same as sampling perspective", {

# Unweighted matches sampling view
lmo <- lm_lin(mpg ~ am, ~ hp, data = mtcars)
Expand All @@ -329,6 +329,9 @@ test_that("lm_lin weights appropriately", {
ate,
lmo$coefficients["am"]
)
})

test_that("weighted lm_lin same as with one covar sampling view", {

# Weighted matches (one covar)
lmwo <- lm_lin(mpg ~ am, ~ hp, weights = wt, data = mtcars)
Expand All @@ -345,6 +348,9 @@ test_that("lm_lin weights appropriately", {
wate,
lmwo$coefficients["am"]
)
})

test_that("weighted lm_lin same as with two covar sampling view", {

# Weighted matches (two covars)
lmw2o <- lm_lin(mpg ~ am, ~ hp + cyl, weights = wt, data = mtcars)
Expand Down

0 comments on commit 33e3cd7

Please sign in to comment.