-
Notifications
You must be signed in to change notification settings - Fork 5
/
time_series.R
29 lines (28 loc) · 1.2 KB
/
time_series.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
#-------------------- time_series --------------------#
time_series <- function(data, nlags, nfeatures) {
for (i in 1:nlags) {
for (j in 1:nfeatures) {
if (i==nlags) {
ntemp = paste("feat",j,"(t)",sep="")
data[ntemp] = shift(data[[j]],i)
break
} else {
ntemp = paste("feat",j,"(t-",nlags-i,")",sep="")
if (is.factor(data[[j]])) {
data[ntemp] = factor(shift(as.numeric(as.character(data[[j]])),i), levels = levels(data[[j]]))
} else {
data[ntemp] = shift(data[[j]],i)
}
}
}
}
for (k in 1:nfeatures) {
colnames(data)[k] = c(paste("feat",k,"(t-",nlags,")",sep=""))
}
data = head(data,-nlags)
return(data)
}
#-------------------- Shift function --------------------#
shift <- function(data, steps) {
c(data[-(seq(steps))], rep(NA,steps))
}