Applies a 2D moving average smooth to the trends over age and period, and fills any remaining missing values by approximating the age pattern within a particular year.
Arguments
- data
Data table containing "age", "year" and a variable bounded by 0 and 1.
- value_var
Character - the name of the variable to be smoothed.
- window_size
- Integer - must be an odd number - the number of years covered by the moving average window.
- blank_zeros
Logical - if TRUE (the default) values of 0 and 1 are set to NA before smoothing and then filled by interpolating the age pattern. This treats a zero as "nothing was observed here" rather than "the probability is zero". Set to FALSE when the caller has already smoothed the values and a zero means what it says. See the note below.
Details
On blank_zeros. The default TRUE is the original behaviour and is kept so that prep_relapse is unaffected. It is not a safe default when the input contains a lot of exact zeros. Blanking a zero makes this function discontinuous: a cell holding 0 is dropped from the moving average entirely, while a cell holding 1e-9 contributes in full. When roughly half the matrix sits on that boundary, as it does for the initiation density, an arbitrarily small change to the input flips cells in and out of every window covering them.
Examples
if (FALSE) { # \dontrun{
test_data <- data.frame(expand.grid(age = c(12:40, 42:89), year = 2001:2016))
setDT(test_data)
test_data[ , prob := runif(nrow(test_data))]
test_data[sample(1:nrow(test_data), 100, replace = F), prob := NA]
data_sm <- p_smooth(test_data, "prob", 3)
} # }
