-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda_functions.R
62 lines (48 loc) · 1.77 KB
/
lambda_functions.R
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
## Contains the anonymous functions for various usage
##
## Dependency: base
################################################################################
## Normalize in [0, 1]...
norm01 <- function(z) {
if (min(z) != 0) { z <- z - min(z) }
z <- z / max(z)
z
}
## Normalize dataframe (or matrix) columns in [0, 1]...
norm_data <- function(Z) as.data.frame(apply(Z, MARGIN = 2, FUN = norm01))
## Standardize dataframe (or matrix) columns...
zscore <- function(Z) as.data.frame(apply(Z, MARGIN = 2, FUN = scale))
## Restrict data in [0, 1]...
confined <- function(y, lims = c(0, 1)) {
y[y < lims[1]] <- lims[1]; y[y > lims[2]] <- lims[2]
y
}
## Evaluate regression model performance...
get_perf <- function(y_obs, y_pred, measures = c("NRMSE", "NMAE", "SCC")) {
## Initialize results array...
perf_vals <- c()
for (mes in measures) {
## Calculate squared error...
if (grepl(pattern = "MSE", mes, ignore.case = TRUE)) {
num <- mean((y_obs - y_pred)^2)
den <- if (mes == "NRMSE") mean((y_obs - mean(y_obs))^2) else 1
pow <- if (mes == "MSE") 1 else 0.5
perf_vals[mes] <- (num / den)^pow
}
## Calculate absolute error...
else if (grepl(pattern = "MAE", mes, ignore.case = TRUE)) {
num <- mean(abs(y_obs - y_pred))
den <- if (mes == "NMAE") mean(abs(y_obs - mean(y_obs))) else 1
perf_vals[mes] <- num / den
}
## Calculate similarity measures...
else if (grepl(pattern = "CC", mes, ignore.case = TRUE)) {
alg <- if (mes == "SCC") "spearman" else "pearson"
perf_vals[mes] <- stats::cor(y_obs, y_pred, method = alg)
}
## Doesn't match any...
else
stop("Invalid measure! Please use common variants of MSE, MAE or correlation coefficients.")
}
perf_vals
}