-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.R
More file actions
115 lines (93 loc) · 2.33 KB
/
utils.R
File metadata and controls
115 lines (93 loc) · 2.33 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
addColumnAggregate <- function(table, func, row_name)
{
total_row <- lapply(table, function(col) if(is.numeric(col)) func(col) else row_name)
rbind(table, do.call("data.table", total_row))
}
roundTable <- function(table, digits=2)
{
res <- do.call(data.table, lapply(table, function(col) if(is.numeric(col)) round(col, digits) else col))
names(res) <- names(table)
res
}
addMonths <- function(date, months)
{
m <- data.table::month(date)-1+months
add_this <- m%%12+1
d_str <- paste0(data.table::year(date)+m%/%12, "-",
ifelse(nchar(add_this) == 1, paste0("0", add_this), add_this), "-",
data.table::mday(date))
tryCatch({as.Date(d_str)}, error = function(e) NA)
}
ymDate <- function(year, month)
{
as.Date(paste0(year, "-", month, "-1"))
}
dateToTime <- function(date)
{
data.table::year(date) + (data.table::month(date) - 1)/12
}
timeToDate <- function(time)
{
time <- round(time, 3)
ymDate(floor(time), round((time - floor(time)) / (1/12)) + 1)
}
startTime <- function(ts)
{
time(ts)[1]
}
endTime <- function(ts)
{
time(ts)[length(ts)]
}
startDate <- function(ts)
{
timeToDate(startTime(ts))
}
endDate <- function(ts)
{
timeToDate(endTime(ts))
}
frequencyDate <- function(date, frequency)
{
ymDate(year(date), (ceiling(month(date) / (12/frequency)) - 1) * (12/frequency) + 1)
}
toQuarterlySeries <- function(series)
{
dates <- zoo::as.Date(time(series))
is_quarter <- month(dates) %in% c(1,4,7,10)
ts(series[is_quarter], start=dateToTime(dates[is_quarter][1]), frequency=4)
}
toMonthlySeries <- function(x, fill_last_quarter = F)
{
vals <- unlist(lapply(as.vector(x), function(val) c(val, NA, NA)))
if(!fill_last_quarter) {
vals <- vals[1:(length(vals)-2)]
}
ts(vals, start = startTime(x), frequency = 12)
}
stdize <- function(x)
{
(x - mean(x, na.rm=T)) / sd(x, na.rm=T)
}
corTS <- function(a, b)
{
from <- dateToTime(max(startDate(a), startDate(b)))
to <- dateToTime(min(endDate(a), endDate(b)))
cor(window(a, from, to), window(b, from, to))
}
extractMonths <- function(series, months)
{
series[month(timeToDate(time(series))) %in% months]
}
mse <- function(a, b, months = NULL)
{
diff <- a - b
if(!is.null(months)) {
diff <- extractMonths(diff, months)
}
mean(diff^2)
}
rmse <- function(a, b, months = NULL)
{
sqrt(mse(a, b, months))
}