Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stan_clogit: allow y to be a factor #520

Merged
merged 2 commits into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ intercept only models by allowing data frames with 0 columns and multiple rows.

* Effective number of parameters are computed for K-fold CV not just LOO CV. (#462)

* `stan_clogit()` now allows outcome variable to be a factor. (#520)

# rstanarm 2.21.1

* Compatible with rstan v2.21.1
Expand Down
6 changes: 4 additions & 2 deletions R/stan_clogit.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ stan_clogit <- function(formula, data, subset, na.action = NULL, contrasts = NUL
group <- glmod$reTrms
group$strata <- glmod$strata <- as.factor(mf[,"(weights)"])
group$decov <- prior_covariance
}
else {
} else {
validate_glm_formula(formula)
mf[[1L]] <- as.name("model.frame")
mf$drop.unused.levels <- TRUE
Expand All @@ -137,6 +136,9 @@ stan_clogit <- function(formula, data, subset, na.action = NULL, contrasts = NUL
Y <- array1D_check(model.response(mf, type = "any"))
}
contrasts <- attr(X, "contrasts")
if (is.factor(Y)) {
Y <- fac2bin(Y)
}

ord <- order(group$strata)
if (any(diff(ord) <= 0)) {
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test_stan_clogit.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ test_that("stan_clogit runs for infert example", {
expect_stanreg(fit)
})

test_that("stan_clogit works when y is a factor", {
d <- infert[order(infert$stratum), ]
d$case <- factor(d$case, labels = c("A", "B"))
SW(fit_factor <- stan_clogit(case ~ spontaneous + induced, strata = stratum, prior = NULL,
data = infert[order(infert$stratum), ],
QR = TRUE, init_r = 0.5,
chains = CHAINS, iter = ITER, seed = SEED, refresh = 0))
expect_equal(coef(fit_factor), coef(fit))
})

test_that("stan_clogit throws error if data are not sorted", {
expect_error(update(fit, data = infert),
regexp = "Data must be sorted")
Expand Down