Compute the Expected Count Matrix from a Contingency Table
Source:R/RcppExports.R
get_expected_counts.Rd
This function computes the expected counts matrix \(E_{ij}\) from a given \(I \times J\) contingency table using the formula: $$E_{ij} = \frac{n_{i.} n_{.j}}{n_{..}}$$ where \(n_{i.}\) is the sum of the \(i\)-th row, \(n_{.j}\) is the sum of the \(j\)-th column, and \(n_{..}\) is the total sum of the table.
Value
A numeric matrix of the same dimension as continTable
,
containing the expected counts for each cell \((i, j)\) of the
contingency table. The expected counts are based on the row and column
marginal sums of the contingency table.
Examples
# Create a 6 by 4 contingency table
set.seed(42)
dat_mat <- matrix(rpois(6*4, 20), nrow=6)
dat_mat
#> [,1] [,2] [,3] [,4]
#> [1,] 26 19 25 18
#> [2,] 17 29 24 12
#> [3,] 20 19 23 27
#> [4,] 15 25 22 21
#> [5,] 19 30 24 21
#> [6,] 26 13 16 18
# Assign row and column names
contin_table <- check_and_fix_contin_table(dat_mat)
contin_table
#> drug_1 drug_2 drug_3 drug_4
#> AE_1 26 19 25 18
#> AE_2 17 29 24 12
#> AE_3 20 19 23 27
#> AE_4 15 25 22 21
#> AE_5 19 30 24 21
#> AE_6 26 13 16 18
# Compute the expected counts of the contingency table
get_expected_counts(contin_table)
#> [,1] [,2] [,3] [,4]
#> [1,] 21.26523 23.33988 23.16699 20.22790
#> [2,] 19.81532 21.74853 21.58743 18.84872
#> [3,] 21.50688 23.60511 23.43026 20.45776
#> [4,] 20.05697 22.01375 21.85069 19.07859
#> [5,] 22.71513 24.93124 24.74656 21.60707
#> [6,] 17.64047 19.36149 19.21807 16.77996