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

poststratify() should return posterior samples #21

Closed
jamesdunham opened this issue Jun 10, 2017 · 1 comment · Fixed by #22
Closed

poststratify() should return posterior samples #21

jamesdunham opened this issue Jun 10, 2017 · 1 comment · Fixed by #22
Assignees

Comments

@jamesdunham
Copy link
Owner

jamesdunham commented Jun 10, 2017

The current behavior is to return poststratified means. In typical use, we want poststratified means and CIs. To make this straightforward, first allow returning poststratified posterior samples. Then summarize the poststratified posterior.

@jamesdunham
Copy link
Owner Author

Basic workaround:

library(dgo)
library(data.table)

data(toy_dgirtfit)
d = as.data.frame(toy_dgirtfit)
stopifnot(is.data.table(d))

samples = lapply(unique(d$iteration), function(x) {
  poststratify(d[iteration == x], annual_state_race_targets, 
    strata_names = c('state', 'year'), aggregated_names = 'race3')
})
samples = rbindlist(samples, idcol = 'iteration')

head(samples)
#    iteration state year        value
# 1:         1    SC 2009 -0.252734127
# 2:         1    SC 2010 -0.651696989
# 3:         1    VA 2009 -0.263817901
# 4:         1    VA 2010 -0.006465143
# 5:         2    SC 2009 -0.169913600
# 6:         2    SC 2010 -0.342846234

# The means of these poststratified posterior samples should equal the
# postratified means returned by poststratify()

means = poststratify(toy_dgirtfit, annual_state_race_targets, 
  strata_names = c('state', 'year'), aggregated_names = 'race3')
means
#    state year      value
# 1:    SC 2009 -0.6391259
# 2:    SC 2010 -0.7397078
# 3:    VA 2009 -0.4158158
# 4:    VA 2010 -0.3251222

mean_samples = samples[, .(value = mean(value)), by = c('state', 'year')]
mean_samples 
#    state year      value
# 1:    SC 2009 -0.6391259
# 2:    SC 2010 -0.7397078
# 3:    VA 2009 -0.4158158
# 4:    VA 2010 -0.3251222

stopifnot(all.equal(means, mean_samples, check.attributes = FALSE))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant