Skip to content

Commit

Permalink
Merge pull request #459 from stan-dev/fix-contrasts-handling
Browse files Browse the repository at this point in the history
fix incorrect handling of contrasts in stan_glm
  • Loading branch information
bgoodri authored Aug 5, 2020
2 parents 9155385 + 6e29951 commit 45271c3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
3 changes: 2 additions & 1 deletion R/stan_glm.R
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ stan_glm <-
if (is.empty.model(mt))
stop("No intercept or predictors specified.", call. = FALSE)
X <- model.matrix(mt, mf, contrasts)
contrasts <- attr(X, "contrasts")
weights <- validate_weights(as.vector(model.weights(mf)))
offset <- validate_offset(as.vector(model.offset(mf)), y = Y)
if (binom_y_prop(Y, family, weights)) {
Expand Down Expand Up @@ -277,7 +278,7 @@ stan_glm <-
fit <- nlist(stanfit, algorithm, family, formula, data, offset, weights,
x = X, y = Y, model = mf, terms = mt, call,
na.action = attr(mf, "na.action"),
contrasts = attr(X, "contrasts"),
contrasts = contrasts,
stan_function = "stan_glm")

out <- stanreg(fit)
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test_stan_glm.R
Original file line number Diff line number Diff line change
Expand Up @@ -457,3 +457,12 @@ test_that("posterior_predict compatible with glms", {
expect_linpred_equal(fit_igaus)

})


test_that("contrasts attribute isn't dropped", {
contrasts <- list(wool = "contr.sum", tension = "contr.sum")
fit <- stan_glm(breaks ~ wool * tension, data = warpbreaks,
contrasts = contrasts,
chains = 1, refresh = 0)
expect_equal(fit$contrasts, contrasts)
})

0 comments on commit 45271c3

Please sign in to comment.