| mask_counts | R Documentation |
Identifies primary and secondary cells in a numeric vector and masks them according to the specified threshold.
mask_counts(x, threshold = 11, zero_masking = FALSE, secondary_cell = "min")
x |
A numeric vector. |
threshold |
A positive numeric value specifying the threshold below which values must be suppressed. Default is 11. |
zero_masking |
Logical; if |
secondary_cell |
Character string specifying the method for selecting secondary cells when necessary. Options are |
The function operates in two main steps: primary masking and secondary masking.
Primary Masking:
Values greater than 0 and less than the specified threshold are considered primary cells. These values are masked by replacing them with <threshold.
Secondary Masking: Secondary masking is applied to prevent the deduction of masked primary cells from the totals. The logic for identifying the need for secondary masking is based on the following conditions:
Condition A: Only one primary masked cell exists, and there are other counts greater than or equal to the threshold.
Condition B: Two or more counts of 1 are masked, and there are other counts greater than or equal to the threshold.
Condition C: The threshold is 11, and two or more counts of 10 are masked, and there are other counts greater than or equal to the threshold.
If any of these conditions are met, secondary masking is performed as follows:
If zero_masking is TRUE and zeros are present in the data, one zero is randomly selected and masked as <threshold.
If zeros are not to be masked or not present, a non-zero cell is selected for masking based on the secondary_cell parameter:
"min": The smallest unmasked count greater than zero is selected.
"max": The largest unmasked count is selected.
"random": A random unmasked count is selected.
The selected secondary cell is then masked by calculating a new masking threshold using the formula:
mask\_value = 5 \times \lceil (selected\_value + 1) / 5 \rceil
The formula calculates the masking threshold by first adding 1 to the selected value, then dividing by 5, and rounding up to the nearest whole number. This result is then multiplied by 5 to get the final mask_value. Essentially, it rounds the selected value up to the next multiple of 5 after incrementing it by 1.
The cell is then replaced with <mask_value.
A character vector with primary and/or secondary masked cells.
x1 <- c(5, 11, 43, 55, 65, 121, 1213, 0, NA)
x2 <- c(1, 1, 1, 55, 65, 121, 1213, 0, NA)
x3 <- c(11, 10, 10, 55, 65, 121, 1213, 0, NA)
mask_counts(x1)
mask_counts(x2)
mask_counts(x3)
if (requireNamespace("dplyr", quietly = TRUE) && requireNamespace("tidyr", quietly = TRUE)) {
data("countmaskr_data")
aggregate_table <- countmaskr_data %>%
dplyr::select(-c(id, age)) %>%
tidyr::gather(block, Characteristics) %>%
dplyr::group_by(block, Characteristics) %>%
dplyr::summarise(N = dplyr::n()) %>%
dplyr::ungroup()
aggregate_table %>%
dplyr::group_by(block) %>%
dplyr::mutate(N_masked = mask_counts(N))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.