#install.packages("xts")
#install.packages("tidyverse")
rm(list=ls())
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.3
## Warning: package 'ggplot2' was built under R version 4.3.3
## Warning: package 'tidyr' was built under R version 4.3.3
## Warning: package 'readr' was built under R version 4.3.3
## Warning: package 'purrr' was built under R version 4.3.3
## Warning: package 'dplyr' was built under R version 4.3.3
## Warning: package 'stringr' was built under R version 4.3.3
## Warning: package 'lubridate' was built under R version 4.3.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(xts)
## Warning: package 'xts' was built under R version 4.3.3
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.3.3
##
## Attaching package: 'zoo'
##
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
##
## ######################### Warning from 'xts' package ##########################
## # #
## # The dplyr lag() function breaks how base R's lag() function is supposed to #
## # work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or #
## # source() into this session won't work correctly. #
## # #
## # Use stats::lag() to make sure you're not using dplyr::lag(), or you can add #
## # conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop #
## # dplyr from breaking base R's lag() function. #
## # #
## # Code in packages is not affected. It's protected by R's namespace mechanism #
## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning. #
## # #
## ###############################################################################
##
## Attaching package: 'xts'
##
## The following objects are masked from 'package:dplyr':
##
## first, last
data(AirPassengers)
AirPassengers
## Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
## 1949 112 118 132 129 121 135 148 148 136 119 104 118
## 1950 115 126 141 135 125 149 170 170 158 133 114 140
## 1951 145 150 178 163 172 178 199 199 184 162 146 166
## 1952 171 180 193 181 183 218 230 242 209 191 172 194
## 1953 196 196 236 235 229 243 264 272 237 211 180 201
## 1954 204 188 235 227 234 264 302 293 259 229 203 229
## 1955 242 233 267 269 270 315 364 347 312 274 237 278
## 1956 284 277 317 313 318 374 413 405 355 306 271 306
## 1957 315 301 356 348 355 422 465 467 404 347 305 336
## 1958 340 318 362 348 363 435 491 505 404 359 310 337
## 1959 360 342 406 396 420 472 548 559 463 407 362 405
## 1960 417 391 419 461 472 535 622 606 508 461 390 432
ap <- as.xts(AirPassengers)
ap
## m.c.seq.row..seq.n...seq.col..drop...FALSE.
## Jan 1949 112
## Feb 1949 118
## Mar 1949 132
## Apr 1949 129
## May 1949 121
## Jun 1949 135
## Jul 1949 148
## Aug 1949 148
## Sep 1949 136
## Oct 1949 119
## ...
## Mar 1960 419
## Apr 1960 461
## May 1960 472
## Jun 1960 535
## Jul 1960 622
## Aug 1960 606
## Sep 1960 508
## Oct 1960 461
## Nov 1960 390
## Dec 1960 432
apRel <- diff(ap)/ap[-length(ap)]
apRel
## e1
## Jan 1949 NA
## Feb 1949 0.05084746
## Mar 1949 0.10606061
## Apr 1949 -0.02325581
## May 1949 -0.06611570
## Jun 1949 0.10370370
## Jul 1949 0.08783784
## Aug 1949 0.00000000
## Sep 1949 -0.08823529
## Oct 1949 -0.14285714
## ...
## Feb 1960 -0.06649616
## Mar 1960 0.06682578
## Apr 1960 0.09110629
## May 1960 0.02330508
## Jun 1960 0.11775701
## Jul 1960 0.13987138
## Aug 1960 -0.02640264
## Sep 1960 -0.19291339
## Oct 1960 -0.10195228
## Nov 1960 -0.18205128
head(ap)
## [,1]
## Jan 1949 112
## Feb 1949 118
## Mar 1949 132
## Apr 1949 129
## May 1949 121
## Jun 1949 135
head(embed(ap,4))
## [,1] [,2] [,3] [,4]
## [1,] 129 132 118 112
## [2,] 121 129 132 118
## [3,] 135 121 129 132
## [4,] 148 135 121 129
## [5,] 148 148 135 121
## [6,] 136 148 148 135
createEmbedDS <- function(s, emb=4) {
d <- dim(s)
if (!is.null(d) && d[2] > 1) stop("Only applicable to uni-variate time series")
if (emb < 2 || emb > length(s)) stop("Invalid embed size")
e <- embed(s,emb)
colnames(e) <- c("T",paste("T",1:(emb-1),sep="_"))
if (is.xts(s)) return(xts(e,index(s)[emb:length(s)])) else return(e)
}
dataSet <- createEmbedDS(ap,emb=5)
head(dataSet)
## T T_1 T_2 T_3 T_4
## May 1949 121 129 132 118 112
## Jun 1949 135 121 129 132 118
## Jul 1949 148 135 121 129 132
## Aug 1949 148 148 135 121 129
## Sep 1949 136 148 148 135 121
## Oct 1949 119 136 148 148 135