Skip to content

Commit

Permalink
Merge pull request #137 from markmfredrickson/issue-127
Browse files Browse the repository at this point in the history
PR For Issue 127
  • Loading branch information
jwbowers authored Jun 28, 2024
2 parents 93e03b5 + ad5f14e commit 35c8d0f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
6 changes: 6 additions & 0 deletions R/Design.R
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,12 @@ makeDesigns <- function(fmla, data) {
data.data <- model.frame(data.fmla, data, na.action = na.pass) #
data.data$'(weights)' <- data$'(weights)'

# Convert remaining character columns to factors so make behavior consistent (issue #127)
chrs <- colnames(data.data)[sapply(data.data, is.character)]
for (f in chrs) {
data.data[, f] <- as.factor(data.data[, f])
}

# knock out any levels that are not used
fcts <- colnames(data.data)[sapply(data.data, is.factor)]
for (f in fcts) {
Expand Down
11 changes: 9 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,15 @@ slm.wfit.csr <- function(x, y, weights, ...) {
## @return matrix of \code{ncol(mat)} rows and col rank (mat) columns
## @author Ben Hansen
## @keywords internal
XtX_pseudoinv_sqrt <- function(mat, mat.is.XtX = FALSE, tol = .Machine$double.eps^0.5) {
pst.svd <- try(svd(mat, nu = 0))
XtX_pseudoinv_sqrt <- function(mat, mat.is.XtX = FALSE, tol = .Machine$double.eps^0.5)
{

if (nrow(mat) == 0 && ncol(mat) == 0)
{
stop("Cannot calculate pseudoinverse: perhaps all covariates are constant (within strata)?")
}

pst.svd <- try(svd(mat, nu=0))

if (inherits(pst.svd, "try-error")) {
pst.svd <- propack.svd(mat)
Expand Down
39 changes: 39 additions & 0 deletions tests/testthat/test.balanceTest.R
Original file line number Diff line number Diff line change
Expand Up @@ -401,3 +401,42 @@ test_that("balanceTest agrees with other methods where appropriate", {
expect_equivalent(bt2m$overall[1, "p.value"], summary(cr2)$sctest[["pvalue"]])
})
})

test_that("Constant variables", {

set.seed(393911)
d <- data.frame(xc = rep(1, 100),
xv = runif(n = 100),
s = as.factor(sample(letters[1:3], 100, replace = TRUE)),
z = rep(c(1,0), 50))

## this should be ok, no error
bt <- balanceTest(z ~ xv + xc, data = d)

## but this gives problems
expect_error(balanceTest(z ~ xc, data = d),
"Cannot calculate pseudoinverse")

## this too
expect_error(balanceTest(z ~ s + strata(s), data = d),
"Cannot calculate pseudoinverse")
})


test_that("Characters and factors", {

## generally we expect characters and factors to behave the same.

set.seed(393911)
tmp <- sample(letters[1:3], 100, replace = TRUE)
d <- data.frame(char = tmp,
fact = as.factor(tmp),
z = rep(c(1,0), 50),
stringsAsFactors = FALSE)

btc <- balanceTest(z ~ char, data = d)
btf <- balanceTest(z ~ fact, data = d)

expect_equal(dim(btc$results), dim(btf$results))
expect_equal(btc$overall, btf$overall)
})

0 comments on commit 35c8d0f

Please sign in to comment.