#' Format DCPO Data for Estimation Using DGIRT Stan Files
#'
#' \code{format_dgirt} formats DCPO data output by \code{dcpo_setup} for use with the \code{dgo}
#' package's \code{dgirt} Stan files.
#'
#' @param dcpo_data a data frame of survey responses generated by \code{dcpo_setup}
#'
#' @return a list of Stan data
#'
#' @importFrom reshape2 acast
#' @importFrom dplyr n_distinct mutate_all select
#' @importFrom tibble rownames_to_column
#'
#' @export
format_dgirt <- function(dcpo_data) {
n_tkqr <- reshape2::acast(dcpo_data,
year ~ country ~ item ~ r,
fun.aggregate = sum,
value.var = "n",
drop = FALSE)
unused_cp <- as.data.frame(apply(n_tkqr, c(3, 4), sum)) %>%
tibble::rownames_to_column() %>%
dplyr::mutate_all(~if_else(. > 0, 0, 1)) %>%
dplyr::select(-`1`, -rowname) %>%
as.matrix()
dgirt_stan <- new('modgirt_in',
items = attr(n_tkqr, "dimnames")[[3]],
time = "year",
geo = "country",
demo = "country",
stan_data = list( G = dplyr::n_distinct(dcpo_data$country),
T = max(dcpo_data$year) - min(dcpo_data$year) + 1,
Q = dplyr::n_distinct(dcpo_data$item),
K = max(dcpo_data$r),
D = 1,
SSSS = n_tkqr,
beta_sign = matrix(1, dplyr::n_distinct(dcpo_data$item), 1),
unused_cut = unused_cp,
N_nonzero = sum(n_tkqr != 0),
data = dcpo_data)
)
return(dgirt_stan)
}
#' @export
modgirt_in <- setClass("modgirt_in",
slots = list(
items = "character",
time = "character",
geo = "character",
demo = "character",
stan_data = "list")
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.