comp_dens_to_multi_dens.Rd
Converts infection probability densities to multinomial probabilities
comp_dens_to_multi_dens(pci_list, densities)
pci_list | List of length 3:
* `perms`: list of permutations as generated by
`lapply(1:n, |
---|---|
densities | List of numeric vectors describing the probability density of each infection composition within `pci_list$perms`. |
Returns a matrix with n columns, and m rows, where m is the maximum moi specified in your coccurence test..
Creates a matrix with each row being the mutlinomial distribution describing the probability of each infection type for a given moi. These rows are calculated by summing the probability densities in `densities` that correspond to each infection type which is contained within `pci_list$infs`.
if (FALSE) { # our given probs probs <- c("a" = 0.5, "b" = 0.3, "c" = 0.2) comps <- generate_composition_levels(probs) # generate some perumtations of infection compositions up to 2 perms <- lapply(1:2, moi_perms, n = 3) perms #> [[1]] #> [,1] [,2] [,3] #> [1,] 0 0 1 #> [2,] 1 0 0 #> [3,] 0 1 0 #> [[2]] #> [,1] [,2] [,3] #> [1,] 0 0 2 #> [2,] 1 0 1 #> [3,] 2 0 0 #> [4,] 0 1 1 #> [5,] 1 1 0 #> [6,] 0 2 0 # and the infection types infs <- infection_types(names(probs), perms) infs #> [[1]] #> [1] "c" "a" "b" #> #> [[2]] #> [1] "c" "a/c" "a" "b/c" "a/b" "b" # create the pci_list pci_list <- list("perms" = perms, "comps" = comps, "infs" = infs) # and also let's work out the probability densities for these infections densities <- lapply(pci_list$perms, function(x){ dens <- apply(x, 1, dmultinom, size = sum(x[1,]), prob = probs) }) densities #> [[1]] #> [1] 0.2 0.5 0.3 #> #> [[2]] #> [1] 0.04 0.20 0.25 0.12 0.30 0.09 # and create the multinomial matrix comp_dens_to_multi_dens(pci_list, densities) #> a b c a/b a/c b/c a/b/c #> [1,] 0.50 0.30 0.20 0.0 0.0 0.00 0 #> [2,] 0.25 0.09 0.04 0.3 0.2 0.12 0 }