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

fix incorrect handling of contrasts in stan_glm #459

Merged
merged 1 commit into from
Aug 5, 2020

Conversation

jgabry
Copy link
Member

@jgabry jgabry commented Aug 5, 2020

closes #444
closes #445

@bgoodri This fixes an important bug reported by @rvlenth. It turns out the same problem was causing both #444 and #445.

The fix is to make sure contrasts info isn't accidentally dropped when subsetting the model matrix.

This now gives correct results:

contrasts <- list(wool = "contr.sum", tension = "contr.sum")
fit <-  stan_glm(breaks ~ wool * tension, data = warpbreaks,
                 contrasts = contrasts, chains = 1, refresh = 0)
> fit$contrasts
$wool
[1] "contr.sum"

$tension
[1] "contr.sum"
> predict(fit, newdata = warpbreaks[1:3, ])
       1        2        3 
44.55111 44.55111 44.55111 
> model.matrix(fit)
   (Intercept) wool1 tension1 tension2 wool1:tension1 wool1:tension2
1            1     1        1        0              1              0
2            1     1        1        0              1              0
3            1     1        1        0              1              0
4            1     1        1        0              1              0
5            1     1        1        0              1              0
6            1     1        1        0              1              0
7            1     1        1        0              1              0
8            1     1        1        0              1              0
9            1     1        1        0              1              0
10           1     1        0        1              0              1
11           1     1        0        1              0              1
12           1     1        0        1              0              1
13           1     1        0        1              0              1
14           1     1        0        1              0              1
15           1     1        0        1              0              1
16           1     1        0        1              0              1
17           1     1        0        1              0              1
18           1     1        0        1              0              1
19           1     1       -1       -1             -1             -1
20           1     1       -1       -1             -1             -1
21           1     1       -1       -1             -1             -1
22           1     1       -1       -1             -1             -1
23           1     1       -1       -1             -1             -1
24           1     1       -1       -1             -1             -1
25           1     1       -1       -1             -1             -1
26           1     1       -1       -1             -1             -1
27           1     1       -1       -1             -1             -1
28           1    -1        1        0             -1              0
29           1    -1        1        0             -1              0
30           1    -1        1        0             -1              0
31           1    -1        1        0             -1              0
32           1    -1        1        0             -1              0
33           1    -1        1        0             -1              0
34           1    -1        1        0             -1              0
35           1    -1        1        0             -1              0
36           1    -1        1        0             -1              0
37           1    -1        0        1              0             -1
38           1    -1        0        1              0             -1
39           1    -1        0        1              0             -1
40           1    -1        0        1              0             -1
41           1    -1        0        1              0             -1
42           1    -1        0        1              0             -1
43           1    -1        0        1              0             -1
44           1    -1        0        1              0             -1
45           1    -1        0        1              0             -1
46           1    -1       -1       -1              1              1
47           1    -1       -1       -1              1              1
48           1    -1       -1       -1              1              1
49           1    -1       -1       -1              1              1
50           1    -1       -1       -1              1              1
51           1    -1       -1       -1              1              1
52           1    -1       -1       -1              1              1
53           1    -1       -1       -1              1              1
54           1    -1       -1       -1              1              1
attr(,"assign")
[1] 0 1 2 2 3 3
attr(,"contrasts")
attr(,"contrasts")$wool
[1] "contr.sum"

attr(,"contrasts")$tension
[1] "contr.sum"

@jgabry
Copy link
Member Author

jgabry commented Aug 5, 2020

@bgoodri What's your plan for the next rstanarm release? There are some important bug fixes including this one that need to get released.

@bgoodri bgoodri merged commit 45271c3 into master Aug 5, 2020
@bgoodri bgoodri deleted the fix-contrasts-handling branch August 5, 2020 21:26
jgabry added a commit that referenced this pull request Oct 15, 2020
add news items for #409, #415, and #459
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect model matrix with user-supplied contrasts Incorrect predictions with alternative contrasts
2 participants