R/inverse_pmf.R
Compute the probability mass function for a specified categorical variable conditional on values of the primary random variable.
inverse_pmf(x, values, id, sample.args)
x | an rvtable. |
---|---|
values | range of values of the continuous random variable in the rvtable. |
id | the categorical variable for which to compute the pmf given |
sample.args | optional arguments used when sampling. |
an rvtable.
This function computes the pmf of a categorical variable, providing probabilities corresponding to the levels of the variable, conditional on values of the primary random variable in the rvtable. When the primary random variable is continuous, `values` must be a length-2 vector giving a valid range. When discrete, `values` can be a range or a single discrete value. If conditioning on a value or range of values restricts the conditional support for `id` to one where `id` has probability zero everywhere, a warning will be thrown and the returned rvtable will have zero rows.
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 y1 <- inverse_pmf(x, c(5, 8), "id1", sample.args=list(n=5)) y1#> # A tibble: 20 x 4 #> id1 id2 id3 Prob #> <fct> <fct> <int> <dbl> #> 1 A high 1 0.0667 #> 2 A high 2 0.308 #> 3 A low 1 0 #> 4 A low 2 0 #> 5 B high 1 0.200 #> 6 B high 2 0.308 #> 7 B low 1 0.200 #> 8 B low 2 0.143 #> 9 C high 1 0.267 #> 10 C high 2 0.154 #> 11 C low 1 0.300 #> 12 C low 2 0.286 #> 13 D high 1 0.267 #> 14 D high 2 0.154 #> 15 D low 1 0.200 #> 16 D low 2 0.286 #> 17 E high 1 0.200 #> 18 E high 2 0.0769 #> 19 E low 1 0.300 #> 20 E low 2 0.286x <- filter(x, id2=="low" & id3==1) %>% select(-id2, -id3) %>% rvtable y2 <- inverse_pmf(x, c(5,8), "id1", sample.args=list(n=5)) y2#> # A tibble: 5 x 2 #> id1 Prob #> <fct> <dbl> #> 1 A 0 #> 2 B 0.364 #> 3 C 0.182 #> 4 D 0.364 #> 5 E 0.0909