Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Zanotti committed Jan 9, 2024
1 parent ef1add3 commit d8381cd
Show file tree
Hide file tree
Showing 8 changed files with 348 additions and 193 deletions.
12 changes: 10 additions & 2 deletions dashboard/R/generate_forecast.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# function to forecast using time series methods
generate_ts_forecast <- function(data, method, params, n_future, seed = 1992) {
generate_ts_forecast <- function(
data, method, params,
n_future, n_assess, assess_type,
seed = 1992
) {

future_tbl <- data |>
future_frame(.date_var = date, .length_out = n_future)
Expand Down Expand Up @@ -60,7 +64,11 @@ generate_ts_forecast <- function(data, method, params, n_future, seed = 1992) {
}

# function to forecast using machine learning methods
generate_ml_forecast <- function(data, method, params, n_future, seed = 1992) {
generate_ml_forecast <- function(
data, method, params,
n_future, n_assess, assess_type,
seed = 1992
) {

# time_scale <- data |>
# tk_index() |>
Expand Down
70 changes: 0 additions & 70 deletions dashboard/R/get_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,73 +50,3 @@ get_data <- function(dataset_name) { # Monthly
return(data)

}

# 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, params, freq) {

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) == FALSE) {
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)
}

}

# 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)
}

}

84 changes: 84 additions & 0 deletions dashboard/R/hp_testing.R
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)

}
97 changes: 97 additions & 0 deletions dashboard/R/transform_data.R
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)
}

}

3 changes: 2 additions & 1 deletion dashboard/R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ set_options <- function() {
"Linear Regression" = "none",
"Elastic Net" = c("penalty", "mixture")
),
tsf.dashboard.transfs = c("log", "boxcox", "norm", "stand", "diff", "sdiff")
tsf.dashboard.transfs = c("log", "boxcox", "norm", "stand", "diff", "sdiff"),
tsf.dashboard.test_transfs = c("test_log", "test_diff", "test_sdiff")
)
toset <- !(names(op.tsf.dashboard) %in% names(op))
if (any(toset)) options(op.tsf.dashboard[toset])
Expand Down
5 changes: 5 additions & 0 deletions dashboard/css/styles-default.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* {
box-sizing: border-box;
}

/* AUTH PANEL */
.panel-auth {
position: fixed;
Expand Down Expand Up @@ -51,6 +55,7 @@ body {
}
.sidebar {
width: 250px !important;
/*margin-right: 30px !important;*/
}
}

Expand Down
Loading

0 comments on commit d8381cd

Please sign in to comment.