Merge conditional distributions of a random variable in an rvtable over levels of ungrouped categorical variables.

merge_rvtable(x, density.args, sample.args)

Arguments

x

an rvtable.

density.args

optional arguments passed to density. If supplied, overrides the density.args attribute of x.

sample.args

optional arguments used when sampling. If supplied, overrides the sample.args attribute of x.

Value

an rvtable.

Details

Distributions are merged using a cycle of bootstrap resampling followed by density re-estimation. Merging relies simply on what x is grouped by. All levels of all ungrouped variables are merged. Weights are also ignored by merge_rvtable. This is why the behavior is referred to as merging rather than marginalizing. If an ungrouped variable has levels with unequal weights, weights are ignored, the merging is still forced, but a warning is thrown.

This function is also called internally by marginalize, in which case it does not ignore weights and will calculate a proper marginal distribution even when there are unequal weights. If calling this function directly, it is important to know that it is intended for simple merging. Recommended practice is to use marginalize to ensure proper marginal distributions are always obtained by default regardless of equal or unequal weights.

Examples

# NOT RUN {
library(dplyr)
x <- data.frame(
  id1=rep(LETTERS[1:5], each=4),
  id2=factor(c("low", "high")),
  id3=rep(1:2, each=2),
  Val=rep(1:10, each=20), Prob=rep(sqrt(1:10), each=20)) %>% rvtable
merge_rvtable(x)
x %>% group_by(id1) %>% merge_rvtable
x %>% group_by(id1, id2) %>% merge_rvtable
# }