Description Usage Arguments Examples
Detrending aggregated time series data using natural cubic splines. This function takes output from the mortality_rate_fun().
1 2 3 4 | detrending_fun(df,
time.aggregate = c("WOD", "MOD"),
dof = 4,
scale.aggregate = c("Province", "Countrywide"))
|
df |
Aggregated mortality rates |
time.aggregate |
Whether to aggregate weekly ("WOD") or monthly ("MOD") |
dof |
Degrees of freedom for the spline |
by_province |
Whether to aggregate by province or country level |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | ## The function is currently defined as
detrending_fun <- function(df,
time.aggregate = c("WOD", "MOD"),
dof = 4,
scale.aggregate = c("Province", "Countrywide")) {
time.aggregate = rlang::arg_match(time.aggregate)
time.cycle <- if_else(time.aggregate == "WOD", 52.17, 12)
quo.time.aggregate = rlang::sym(time.aggregate)
scale.aggregate = rlang::arg_match(scale.aggregate)
det.data <- if (scale.aggregate == "Province")
{group_by(df, DeathProv) %>%
mutate(tt = seq(from = 1997, to = 2016.999, by = 1/time.cycle),
t = row_number()) %>%
ungroup() %>%
gather(key = "death",
value = "rates",
All_deaths_rate:RSV_deaths_rate,
na.rm = T) %>%
group_by(DeathProv, death) %>%
nest()
}
else {mutate(df,
tt = seq(from = 1997.000, to = 2016.999, by = 1/time.cycle),
t = row_number()) %>%
gather(key = "death",
value = "rates",
All_deaths_rate:RSV_deaths_rate,
na.rm = T) %>%
group_by(death) %>%
nest()}
det.data %>%
mutate(fit = map(data, ~ lm(rates ~ ns(t,dof),data = .)),
trend = map(fit, predict)) %>%
unnest(c(data,trend)) %>%
select(-fit) %>%
mutate(detrended = rates - trend,
mean_death_rate = mean(rates),
detrended_plus_mean = detrended + mean_death_rate) %>%
ungroup()
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.