-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Marco Zanotti
committed
Jan 9, 2024
1 parent
ef1add3
commit d8381cd
Showing
8 changed files
with
348 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# function to extract significance stars | ||
extract_significance <- function(p_value) { | ||
|
||
res <- dplyr::case_when( | ||
p_value < 0.001 ~ "***", | ||
p_value >= 0.001 & p_value < 0.01 ~ "**", | ||
p_value >= 0.01 & p_value < 0.05 ~ "*", | ||
p_value >= 0.05 & p_value < 0.1 ~ ".", | ||
TRUE ~ "" | ||
) | ||
return(res) | ||
|
||
} | ||
|
||
# function to perform hypothesis testing | ||
compute_hptests <- function(data, digits = 4) { | ||
|
||
# urca::ur.kpss() type = c("mu", "tau"), use.lag = NULL | ||
# urca::ur.pp() type = c("Z-alpha", "Z-tau"), model = c("constant", "trend"), use.lag = NULL | ||
# urca::ur.df() type = c("none", "drift", "trend"), lags = 1 | ||
|
||
x <- data$value | ||
k <- trunc((length(x) - 1) ^ (1 / 3)) | ||
|
||
res <- tibble::tibble( | ||
"Type" = c(rep("normality", 2), rep("autocorrelation", 2), rep("stationarity", 3)), | ||
"Test" = c( | ||
"Jarque-Bera", "Shapiro-Wilk", | ||
"Box-Pierce", "Ljung-Box", | ||
"Augmented Dickey-Fuller", "Phillips-Perron", "KPSS" | ||
), | ||
"H0" = c( | ||
"Normality", "Normality", | ||
"No autocorrelation", "No autocorrelation", | ||
"No stationarity", "No stationarity", "Stationarity" | ||
), | ||
"Statistic" = rep(0, 7), | ||
"P_value" = rep(0, 7), | ||
"Signif" = rep("", 7) | ||
) | ||
|
||
# normality tests | ||
tmp <- tseries::jarque.bera.test(x) | ||
res[1, 4] <- unname(tmp$statistic) | ||
res[1, 5] <- tmp$p.value | ||
tmp <- stats::shapiro.test(x) | ||
res[2, 4] <- unname(tmp$statistic) | ||
res[2, 5] <- tmp$p.value | ||
|
||
# autocorrelation tests | ||
tmp <- stats::Box.test(x, type = "Box-Pierce", lag = k) | ||
res[3, 4] <- unname(tmp$statistic) | ||
res[3, 5] <- tmp$p.value | ||
tmp <- stats::Box.test(x, type = "Ljung-Box", lag = k) | ||
res[4, 4] <- unname(tmp$statistic) | ||
res[4, 5] <- tmp$p.value | ||
|
||
# stationarity | ||
tmp <- suppressWarnings(tseries::adf.test(x, k = k)) | ||
res[5, 4] <- unname(tmp$statistic) | ||
res[5, 5] <- tmp$p.value | ||
tmp <- suppressWarnings(tseries::pp.test(x)) | ||
res[6, 4] <- unname(tmp$statistic) | ||
res[6, 5] <- tmp$p.value | ||
tmp <- suppressWarnings(tseries::kpss.test(x)) | ||
res[7, 4] <- unname(tmp$statistic) | ||
res[7, 5] <- tmp$p.value | ||
|
||
res <- res |> | ||
dplyr::mutate(Signif = extract_significance(P_value)) |> | ||
dplyr::mutate( | ||
Statistic = round(Statistic, digits), | ||
P_value = round(P_value, digits) | ||
) |> | ||
# dplyr::mutate( | ||
# result = paste0( | ||
# round(statistic, digits), " ", signif, " \n", "(", round(p_value, digits), ")" | ||
# ) | ||
# ) |> | ||
dplyr::select(-Type) | ||
|
||
return(res) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# function to impute missing values | ||
impute_data <- function(data, params, freq) { | ||
|
||
if (params$impute == FALSE) { | ||
return(data) | ||
} else { | ||
n2f <- trunc(nrow(data) / freq) | ||
p <- ifelse(n2f < 1, 1, 2) | ||
data_impute <- data |> mutate(value = ts_impute_vec(value, period = p, lambda = "auto")) | ||
return(data_impute) | ||
} | ||
|
||
} | ||
|
||
# function to transform data | ||
transform_data <- function(data, section, params, freq) { | ||
|
||
if (section == "viz_transf") { | ||
|
||
trf_prm <- getOption("tsf.dashboard.transfs") | ||
if (!all(trf_prm %in% names(params))) { | ||
stop(paste("Unknown transformations!")) | ||
} | ||
transf_params <- c( | ||
params$log, params$boxcox, params$norm, | ||
params$stand, params$diff, params$sdiff | ||
) |> as.logical() | ||
|
||
if (all(!transf_params)) { | ||
return(data) | ||
} else { | ||
data_transf <- data | ||
if (params$log) { # Log | ||
data_transf <- data_transf |> mutate(value = log1p(value)) | ||
} | ||
if (params$boxcox) { # Box-Cox | ||
data_transf <- data_transf |> mutate(value = box_cox_vec(value + 1, lambda = "auto")) | ||
} | ||
if (params$norm) { # Normalization | ||
data_transf <- data_transf |> mutate(value = normalize_vec(value)) | ||
} | ||
if (params$stand) { # Standardization | ||
data_transf <- data_transf |> mutate(value = standardize_vec(value)) | ||
} | ||
if (params$diff) { # Differencing | ||
data_transf <- data_transf |> mutate(value = diff_vec(value, difference = 1)) |> drop_na() | ||
} | ||
if (params$sdiff) { # Seasonal differencing | ||
data_transf <- data_transf |> mutate(value = diff_vec(value, difference = 1, lag = freq)) |> drop_na() | ||
} | ||
return(data_transf) | ||
} | ||
|
||
} else if (section == "test_hp") { | ||
|
||
trf_prm <- getOption("tsf.dashboard.test_transfs") | ||
if (!all(trf_prm %in% names(params))) { | ||
stop(paste("Unknown transformations!")) | ||
} | ||
transf_params <- c( | ||
params$test_log, params$test_diff, params$test_sdiff | ||
) |> as.logical() | ||
|
||
if (all(!transf_params)) { | ||
return(data) | ||
} else { | ||
data_transf <- data | ||
if (params$test_log) { # Log | ||
data_transf <- data_transf |> mutate(value = log1p(value)) | ||
} | ||
if (params$test_diff) { # Differencing | ||
data_transf <- data_transf |> mutate(value = diff_vec(value, difference = 1)) |> drop_na() | ||
} | ||
if (params$test_sdiff) { # Seasonal differencing | ||
data_transf <- data_transf |> mutate(value = diff_vec(value, difference = 1, lag = freq)) |> drop_na() | ||
} | ||
return(data_transf) | ||
} | ||
|
||
} else { | ||
stop(paste("Unknown section", section)) | ||
} | ||
|
||
} | ||
|
||
# function to clean data from anomalies | ||
clean_data <- function(data, params) { | ||
|
||
if (params$clean == FALSE) { | ||
return(data) | ||
} else { | ||
data_clean <- data |> mutate(value = ts_clean_vec(value)) | ||
return(data_clean) | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.