Viitama Viitama - 2 months ago 11
R Question

Creating fts object in R

I'm aware that this question already exists here: Creating an fts object in R with ftsa to functional data analysis of a time series

but reproducible example and answer is missing, so hopefully my example and data gets answer. My data looks following:

> data
date GDBR2 GDBR5 GDBR10 GDBR30
1 2015-10-15 -0.261 -0.032 0.550 1.310
2 2015-11-15 -0.370 -0.107 0.558 1.414
3 2015-12-15 -0.339 -0.049 0.641 1.411
4 2016-01-15 -0.394 -0.158 0.540 1.314
5 2016-02-15 -0.521 -0.312 0.237 0.928
6 2016-03-15 -0.453 -0.243 0.316 1.062
7 2016-04-15 -0.510 -0.382 0.127 0.805
8 2016-05-15 -0.513 -0.381 0.124 0.829
9 2016-06-15 -0.594 -0.483 -0.010 0.542
10 2016-07-15 -0.652 -0.551 0.006 0.530
11 2016-08-15 -0.613 -0.521 -0.074 0.433
12 2016-09-15 -0.640 -0.478 0.047 0.654


I'm able to create fts1 object that claims to be fts / zoo, but not able to run
ftsm
, as
is.fts
returns FALSE

library(ftsa)
library(fts)

fts1 <- as.fts(data.frame(asofdate=data$date,data[,-1]))
class(fts1)
[1] "fts" "zoo"

ftsm(fts1, order=3)
Error in 1:ncol(y$y) : argument of length 0

is.fts(fts1)
[1] FALSE


Any help to how to create fts object to be used in ftsm function is appreciated.

here dput of data:

> dput(data)
structure(list(date = structure(c(16723L, 16754L, 16784L, 16815L,
16846L, 16875L, 16906L, 16936L, 16967L, 16997L, 17028L, 17059L
), class = "Date"), GDBR2 = c(-0.261, -0.37, -0.339, -0.394,
-0.521, -0.453, -0.51, -0.513, -0.594, -0.652, -0.613, -0.64),
GDBR5 = c(-0.032, -0.107, -0.049, -0.158, -0.312, -0.243,
-0.382, -0.381, -0.483, -0.551, -0.521, -0.478), GDBR10 = c(0.55,
0.558, 0.641, 0.54, 0.237, 0.316, 0.127, 0.124, -0.01, 0.006,
-0.074, 0.047), GDBR30 = c(1.31, 1.414, 1.411, 1.314, 0.928,
1.062, 0.805, 0.829, 0.542, 0.53, 0.433, 0.654)), row.names = c(NA,
-12L), class = "data.frame", .Names = c("date", "GDBR2", "GDBR5",
"GDBR10", "GDBR30"))

Answer

There are different packages that define a class "fts" and you are using the wrong one.

library(ftsa)

#see the link in the documentation for the correct constructor
fts1 <- rainbow::fts(x = data$date, y = as.matrix(data[,-1]))

#apparently colnames must be numbers of the given frequency
#this appears to be at least an documentation bug
#but I know nothing about this kind of analysis
colnames(fts1$y) <- seq_along(colnames(fts1$y))

ftsm(fts1, order=3)
#Functional time series model
#
#Call: ftsm(y = fts1, order = 3) 
#
#Main effects: Mean 
#Order of interaction: 3
#   y: as.matrix(data[, -1]) 
#   x: data$date 
Comments