title: “Time series R Notebook” output: html_notebook

Creating a time series The ts() function will convert a numeric vector into an R time series object. The format is ts(vector, start=, end=, frequency=) where start and end are the times of the first and last observation and frequency is the number of observations per unit time (1=annual, 4=quartly, 12=monthly, etc.).

the file http://robjhyndman.com/tsdldata/data/fancy.dat contains monthly sales for a souvenir shop at a beach resort town in Queensland, Australia, for January 1987-December 1993 (original data from Wheelwright and Hyndman, 1998). We can read the data into R by typing:

#install.packages("readr)
library(readr)
## Warning: package 'readr' was built under R version 4.2.3
fancy_dat <- read_csv("fancy.dat.txt")
## Rows: 84 Columns: 1
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (1): monthly_sales
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
head(fancy_dat)
## # A tibble: 6 × 1
##   monthly_sales
##           <dbl>
## 1         1665.
## 2         2398.
## 3         2841.
## 4         3547.
## 5         3753.
## 6         3715.
souvenirtimeseries <- ts(fancy_dat, frequency=12, start=c(1987,1))
 souvenirtimeseries
##            Jan       Feb       Mar       Apr       May       Jun       Jul
## 1987   1664.81   2397.53   2840.71   3547.29   3752.96   3714.74   4349.61
## 1988   2499.81   5198.24   7225.14   4806.03   5900.88   4951.34   6179.12
## 1989   4717.02   5702.63   9957.58   5304.78   6492.43   6630.80   7349.62
## 1990   5921.10   5814.58  12421.25   6369.77   7609.12   7224.75   8121.22
## 1991   4826.64   6470.23   9638.77   8821.17   8722.37  10209.48  11276.55
## 1992   7615.03   9849.69  14558.40  11587.33   9332.56  13082.09  16732.78
## 1993  10243.24  11266.88  21826.84  17357.33  15997.79  18601.53  26155.15
##            Aug       Sep       Oct       Nov       Dec
## 1987   3566.34   5021.82   6423.48   7600.60  19756.21
## 1988   4752.15   5496.43   5835.10  12600.08  28541.72
## 1989   8176.62   8573.17   9690.50  15151.84  34061.01
## 1990   7979.25   8093.06   8476.70  17914.66  30114.41
## 1991  12552.22  11637.39  13606.89  21822.11  45060.69
## 1992  19888.61  23933.38  25391.35  36024.80  80721.71
## 1993  28586.52  30505.41  30821.33  46634.38 104660.67
 plot.ts(souvenirtimeseries)

Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.

kings_dat <- read_csv("kings.dat.txt")
## Warning: One or more parsing issues, call `problems()` on your data frame for details,
## e.g.:
##   dat <- vroom(...)
##   problems(dat)
## Rows: 44 Columns: 1
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): Age of Death of Successive Kings of England
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
head(kings_dat)
## # A tibble: 6 × 1
##   `Age of Death of Successive Kings of England`    
##   <chr>                                            
## 1 "#starting with William the Conqueror"           
## 2 "#Source: McNeill, \"Interactive Data Analysis\""
## 3 "60"                                             
## 4 "43"                                             
## 5 "67"                                             
## 6 "50"
kingstimeseries <- ts(kings_dat)
kingstimeseries
## Time Series:
## Start = 1 
## End = 44 
## Frequency = 1 
##       Age of Death of Successive Kings of England
##  [1,]                                           2
##  [2,]                                           1
##  [3,]                                          20
##  [4,]                                          10
##  [5,]                                          22
##  [6,]                                          14
##  [7,]                                          18
##  [8,]                                           9
##  [9,]                                          14
## [10,]                                          21
## [11,]                                          23
## [12,]                                          10
## [13,]                                          21
## [14,]                                           6
## [15,]                                          11
## [16,]                                           6
## [17,]                                          13
## [18,]                                           8
## [19,]                                           3
## [20,]                                           7
## [21,]                                          16
## [22,]                                          18
## [23,]                                           4
## [24,]                                          10
## [25,]                                          24
## [26,]                                          19
## [27,]                                          12
## [28,]                                          19
## [29,]                                          29
## [30,]                                          17
## [31,]                                          23
## [32,]                                          15
## [33,]                                           5
## [34,]                                          13
## [35,]                                          22
## [36,]                                          27
## [37,]                                          28
## [38,]                                          22
## [39,]                                          26
## [40,]                                          28
## [41,]                                          23
## [42,]                                          25
## [43,]                                          27
## [44,]                                          18
  plot(kingstimeseries)