Skip to content

Commit

Permalink
Addressed #6 by maintaining original variables in modelframe.
Browse files Browse the repository at this point in the history
  • Loading branch information
josherrickson committed Jul 7, 2016
1 parent 8a543eb commit f4a93e8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
6 changes: 4 additions & 2 deletions R/mlm.R
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,12 @@ parseMatchingProblem <- function(formula, data, na.action = na.pass, ...) {

mname <- colnames(mf)[isMatch]

newf <- update(formula, as.formula(paste(".~. -", mname)), data=mf)
newf <- update(formula, as.formula(paste(".~. -", mname)),
data = mf)

# now make a new model frame, using the reduce form of the formula
mf <- model.frame(newf, data, na.action = na.action, ...)
mf <- data[,all.vars(newf), drop = FALSE]
attributes(mf)$terms <- terms(newf)

return(
list(
Expand Down
35 changes: 28 additions & 7 deletions tests/testthat/test.mlm.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ test_that("parseMatchingProblem", {

n2 <- cbind(nuclearplants, mmm)
res <- parseMatchingProblem(cost ~ pr*pt + mmm, n2)

expect_is(res$fmla, "formula")
expect_is(res$mf, "data.frame")
expect_is(res$match, "optmatch")
Expand All @@ -29,7 +29,7 @@ test_that("parseMatchingProblem", {
res2 <- parseMatchingProblem(cost ~ pr + mmm, n3)
expect_equivalent(res2$match, mmm)
expect_error(parseMatchingProblem(cost ~ pr + mmm + mmm2, n3), "one")

expect_error(parseMatchingProblem(cost ~ pr, n3), "include")
})

Expand Down Expand Up @@ -113,7 +113,7 @@ test_that("optmatch -> matrix.csr", {
expect_true(all(rowSums(csrm) == 0))

expect_equal(as.vector(csr %*% rep(1, length(mmm))), rep(0, nlevels(mmm)))

### remove only a treated member
mmm <- fullmatch(pr ~ t1 + t2 + cap, data = nuclearplants)
mmm <- mmm[!(mmm == levels(mmm)[1] & nuclearplants$pr == 1)]
Expand Down Expand Up @@ -142,7 +142,7 @@ test_that("optmatch -> matrix.csr", {
})

test_that("mlm", {

data(nuclearplants)
mmm <- fullmatch(pr ~ t1 + t2 + cap, data = nuclearplants)

Expand All @@ -156,7 +156,7 @@ test_that("mlm", {
mlm(cost ~ t1 + t2 + mmm, data = n2, ms.weights = harmonic)

expect_true(all(names(coef(mt1t2)) %in% c("(Treatment)", "t1", "t2")))

ppp <- pairmatch(pr ~ t1 + t2 + cap, data = nuclearplants)
n3 <- cbind(nuclearplants, ppp)

Expand Down Expand Up @@ -194,7 +194,7 @@ test_that("variances", {
coef(summary(modell))["pr", ])

expect_equal(vcov(modell)["pr", "pr"], vcov(modelm)[1,1])

})

test_that("model.matrix.mlm", {
Expand All @@ -205,7 +205,7 @@ test_that("model.matrix.mlm", {
mx <- model.matrix(mm)

expect_equal(dim(mx), c(nlevels(ppp), 2))

})

test_that("matches without a treated or control unit", {
Expand All @@ -223,3 +223,24 @@ test_that("matches without a treated or control unit", {
res <- mlm(cost ~ m, data = cbind(nuclearplants, m = mmm))
})

test_that("#6 mlm with computed variable in formula", {

data(nuclearplants)
mmm <- fullmatch(pr ~ t1 + t2 + cap, data = nuclearplants)

n2 <- cbind(nuclearplants, mmm)

m1 <- mlm(cost ~ cap + mmm, data = n2)
m2 <- mlm(cost ~ log(cap) + mmm, data = n2)
n2$lcap <- log(n2$cap)
m3 <- mlm(cost ~ lcap + mmm, data = n2)

# Different names, same results.
expect_false(isTRUE(all.equal(m2$coef, m3$coef)))
expect_false(isTRUE(all.equal(m2$coef, m3$coef)))
expect_true(isTRUE(all.equal(m2$coef, m3$coef, check.attributes = FALSE)))

# m1 different
expect_false(isTRUE(all.equal(m1$coef, m3$coef, check.attributes = FALSE)))

})

0 comments on commit f4a93e8

Please sign in to comment.