train_tsrf: Train a time series random forest

Description Usage Arguments Value Examples

View source: R/train_tsrf.R

Description

Train a time series random forest

Usage

1
train_tsrf(tsfeats, tslabels, tsid)

Arguments

tsfeats

time series features (data.frame) as generated by extract_features

tsid

name of id column(s) in tsfeats (character)

labels

time series labels (vector)

Value

train object, as generated by train

Examples

 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
# Generate 10 time series of two different classes, each 100 points long
tsnum <- 10
tslbl <- factor(rep(c("A", "B"), each = tsnum))
tslen <- 100
Aval <- replicate(tsnum, rnorm(tslen))
Bval <- replicate(tsnum, c(rnorm(tslen / 2), cumsum(rnorm(tslen / 2, 0.1))))
tsdat <- data.frame(
  id = rep(1:(2 * tsnum), each = tslen),
  val = c(as.numeric(Aval), as.numeric(Bval))
)

# Plot time series
plot(seq(tslen), tsdat$val[tsdat$id == 1],
     type = "l", col = "#FF000088", ylim = range(tsdat$val))
for (i in 2:(tsnum * 2)) {
  lines(seq(tslen), tsdat$val[tsdat$id == i],
        col = if (i <= tsnum) "#FF000088" else "#0000FF88")
}

# Extract features
tsints <- sample_intervals(tslen)
tsfeat <- extract_features(tsdat, "id", tsints)

# Split data
train_index <- sample(seq(nrow(tsfeat)), 0.9 * nrow(tsfeat))
train_feat <- tsfeat[train_index, ]
test_feat <- tsfeat[-train_index, ]
train_lbl <- tslbl[train_index]
test_lbl <- tslbl[-train_index]

# Train model
tsrf <- train_tsrf(train_feat, train_lbl, "id")

# Test predictions
pred_feat <- predict(tsrf, test_feat)
caret::confusionMatrix(pred_feat, test_lbl)

FlukeAndFeather/tsrf documentation built on Dec. 17, 2021, 8:29 p.m.