Obtain a marginal distribution of a random variable in an rvtable.

marginalize(x, margin, density.args, sample.args)

Arguments

x

an rvtable.

margin

variable(s) in rvtable to marginalize over.

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

Grouping variables are ignored when marginalizing the distribution of a random variable over explicit categorical variables. Unlike merge_rvtable, marginalize will not merge ungrouped ID variables. It will only marginalize over the ID variables specified in margin. It will also account for unequal weighting of an ID variable's levels, taken from the weights attribute of an rvtable, whereas merge_rvtable is a simpler function that ignores 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
marginalize(x, c("id1", "id2"))

get_levels(x, "id1")
get_weights(x, "id1")
marginalize(x, "id1")
wts <- data.frame(levels=LETTERS[1:5], weights=c(1, 1.5, 2, 4, 1))
x <- set_weights(x, "id1", wts)
get_weights(x, "id1")
marginalize(x, "id1")
# }