pivot_celldraws_longer: Take output from brms prediction and turn to tidy form

View source: R/take-draws_poststrat.R

pivot_celldraws_longerR Documentation

Take output from brms prediction and turn to tidy form

Description

Take output from brms prediction and turn to tidy form

Usage

pivot_celldraws_longer(mod_draws, data_strat, yhat_name = "pred_n_yes")

Arguments

mod_draws

Output from posterior_*pred, which is of dimension Iter (in rows) by Cells (in columns)

data_strat

Covariates to append to. The number of rows should be the number of Cells

yhat_name

The name of the value for the predicted outcome.

Value

A tidy long dataset with Iter x Cells rows. Columns will include the columns of data_strat, the iteration and cell ID, and the predicted value for that cell at that iteration.

Examples

library(dplyr)
library(brms)

pred_df <- rename(acs_GA, n_response = count) # match variables with model call
posterior_draws <- posterior_epred(object = fit_GA,  # stanfit
                                   newdata = pred_df)

# output is matrix, not tidy form
class(posterior_draws)
dim(posterior_draws)

# tidy out
out <- pivot_celldraws_longer(mod_draws = posterior_draws,
                              data_strat = pred_df,
                              yhat_name = "pred_n_yes")

dim(out)
select(out, cd, n_response, cell, iter, pred_n_yes)



kuriwaki/ccesMRPrun documentation built on Sept. 24, 2024, 2:15 a.m.