Do weights work in `stan_glm()`?
Nobody has claimed this yet.
- Dominant language
- R
- Stars
- 401
- Forks
- 136
- PR merge metrics
- No merged PRs in 30d
Description
Summary:
I'm trying to use the weights argument to stan_glm() and stan_glmer() but it does not appear to affect the fit.
The same data in lm() or glm() works as desired.
Reproducible Steps:
Load the attached data weights_test.txt:
df <- read_csv("weights_test.txt")
head(df)
| x | y |
|---|---|
| 1 | 0.0 |
| 1 | 0.0 |
| 0 | 387.0 |
| 0 | 309.6 |
| 0 | 0.0 |
| 0 | 154.8 |
Then fit the model to all the data:
> stan_glm(y ~ x, data=df, iter=500)
Median MAD_SD
(Intercept) 95.7 14.0
x 38.5 20.0
Now create a weighted dataset:
df_weighted <- df %>%
group_by(x,y) %>%
summarize(n=n())
head(df_weighted)
| x | y | n |
|---|---|---|
| 0 | 0.00 | 435 |
| 0 | 25.80 | 3 |
| 0 | 30.96 | 2 |
| 0 | 51.59 | 4 |
| 0 | 77.39 | 5 |
| 0 | 103.19 | 16 |
Then fit the weighted data:
>stan_glm(y ~ x, data=df_weighted, weights=n, iter=500)
Median MAD_SD
(Intercept) 779.2 167.0
x -72.7 228.0
This is very similar to what I get with df_weighted but not specifying any weights. On the other hand using lm() or glm(), the weighted fit is the same as the original fit.
RStanARM Version:
2.19.2
R Version:
3.6.1
Operating System:
Debian Linux (Stretch)
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the supplied comparison between stan_glm() and lm()/glm() using the grouped data and weights=n, then trace how weights are handled in stan_glm() and stan_glmer(). Done means weighted grouped data produces the same fit as the ungrouped observations, with regression tests covering the behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- r
- Domain
- data
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100