Importando datos de precios de cierre de Starbucks y Microsoft

library(readr)
## Warning: package 'readr' was built under R version 4.1.3
sbux.df <- read_csv("C:/Users/LUIS 1/Desktop/Series de Tiempo/Serie temporal en R y Python/R/sbuxPrices.csv")
## Rows: 181 Columns: 2
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (1): Date
## dbl (1): Adj.Close
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
head(sbux.df)
## # A tibble: 6 x 2
##   Date      Adj.Close
##   <chr>         <dbl>
## 1 3/31/1993      1.13
## 2 4/1/1993       1.15
## 3 5/3/1993       1.43
## 4 6/1/1993       1.46
## 5 7/1/1993       1.41
## 6 8/2/1993       1.44

Convirtiendo a objeto serie de tiempo.

sbux.ts = ts(data=sbux.df$Adj.Close, frequency = 12,
             start=c(1993,3), end=c(2008,3))
class(sbux.ts) 
## [1] "ts"
msft.df <- read_csv("C:/Users/LUIS 1/Desktop/Series de Tiempo/Serie temporal en R y Python/R/msftPrices.csv")
## Rows: 181 Columns: 2
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (1): Date
## dbl (1): Adj.Close
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
head(sbux.df, 10)
## # A tibble: 10 x 2
##    Date      Adj.Close
##    <chr>         <dbl>
##  1 3/31/1993      1.13
##  2 4/1/1993       1.15
##  3 5/3/1993       1.43
##  4 6/1/1993       1.46
##  5 7/1/1993       1.41
##  6 8/2/1993       1.44
##  7 9/1/1993       1.63
##  8 10/1/1993      1.59
##  9 11/1/1993      1.32
## 10 12/1/1993      1.32
msft.ts = ts(data=msft.df$Adj.Close, frequency = 12,
             start=c(1993,3), end=c(2008,3))

Fechas y frecuencia de la serie

sbux.ts 
##        Jan   Feb   Mar   Apr   May   Jun   Jul   Aug   Sep   Oct   Nov   Dec
## 1993              1.13  1.15  1.43  1.46  1.41  1.44  1.63  1.59  1.32  1.32
## 1994  1.43  1.38  1.45  1.77  1.69  1.50  1.72  1.68  1.37  1.61  1.59  1.63
## 1995  1.43  1.42  1.43  1.40  1.73  2.12  2.22  2.38  2.25  2.33  2.51  2.50
## 1996  1.99  2.09  2.77  3.22  3.22  3.36  3.09  3.89  3.92  3.86  4.12  3.40
## 1997  4.07  4.00  3.52  3.55  3.74  4.63  4.87  4.87  4.97  3.92  4.15  4.56
## 1998  4.35  4.70  5.39  5.72  5.71  6.35  4.98  3.75  4.30  5.16  5.48  6.67
## 1999  6.19  6.29  6.67  8.78  8.77  8.93  5.53  5.44  5.89  6.46  6.31  5.76
## 2000  7.61  8.35 10.65  7.19  8.08  9.08  8.91  8.71  9.52 10.62 10.83 10.52
## 2001 11.87 11.32 10.09  9.20  9.28 10.94  8.58  8.02  7.10  8.14  8.42  9.06
## 2002 11.30 10.94 11.00 10.85 11.54 11.81  9.33  9.56  9.82 11.33 10.34  9.69
## 2003 10.80 11.15 12.25 11.18 11.73 11.67 12.99 13.50 13.69 15.02 15.30 15.77
## 2004 17.41 17.78 18.01 18.50 19.30 20.68 22.34 20.56 21.61 25.14 26.75 29.65
## 2005 25.67 24.63 24.56 23.54 26.05 24.56 24.98 23.31 23.82 26.89 28.95 28.54
## 2006 30.14 34.54 35.78 35.44 33.90 35.91 32.55 29.49 32.38 35.90 33.56 33.68
## 2007 33.22 29.38 29.82 29.50 27.40 24.95 25.37 26.20 24.91 25.37 22.24 19.46
## 2008 17.98 17.10 16.64
start(sbux.ts) 
## [1] 1993    3
end(sbux.ts) 
## [1] 2008    3
frequency(sbux.ts) 
## [1] 12

Subconjunto de la serie de tiempo

tmp = sbux.ts[1:5] 
class(tmp)
## [1] "numeric"
tmp = window(sbux.ts, start=c(1993, 3), end=c(1993,8))
class(tmp)
## [1] "ts"

Combinando dos series (dos columnas)

sbuxmsft.ts = cbind(sbux.ts, msft.ts) 
class(sbuxmsft.ts)  #mts: multiple time series
## [1] "mts"    "ts"     "matrix"

Seleccionando las primeras 5 filas:

window(sbuxmsft.ts, start=c(1993, 3), end=c(1993,7)) 
##          sbux.ts  msft.ts
## Mar 1993    1.13 1.849489
## Apr 1993    1.15 1.859486
## May 1993    1.43 1.794505
## Jun 1993    1.46 1.829494
## Jul 1993    1.41 1.794505

Plot objeto ts

plot(sbux.ts, col="blue", lwd=2, ylab="Adjusted close",
     main="Monthly closing price of SBUX") 

Dibujar un subconjunto (Acercar)

plot(window(sbux.ts, start=c(2000,3), end=c(2008,3)),
      ylab="Adjusted close",col="blue", lwd=2,
      main="Monthly closing price of SBUX") 

Plot para múltiples columnas

plot(sbuxmsft.ts)#En gráficos diferentes

En el mismo gráfico

plot(sbuxmsft.ts, plot.type="single",
      main="Monthly closing prices on SBUX and MSFT",
      ylab="Adjusted close price",
      col=c("blue", "red"), lty=1:2)
legend(1994, 35, legend=c("SBUX","MSFT"), col=c("blue", "red"),
          lty=1:2)

Usando la librería zoo

library(zoo) 
## Warning: package 'zoo' was built under R version 4.1.3
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric

Fecha

td = seq(as.Date("1993/3/1"), as.Date("2008/3/1"), "months") 
class(td) 
## [1] "Date"
head(td)
## [1] "1993-03-01" "1993-04-01" "1993-05-01" "1993-06-01" "1993-07-01"
## [6] "1993-08-01"

Alternativa

td2 = as.Date(sbux.df$Date, format="%m/%d/%Y") 
head(td2) 
## [1] "1993-03-31" "1993-04-01" "1993-05-03" "1993-06-01" "1993-07-01"
## [6] "1993-08-02"

Combinando el índice de tiempo a las dos series de precios

sbux.z = zoo(x=sbux.df$Adj.Close, order.by=td) 
msft.z = zoo(x=msft.df$Adj.Close, order.by=td) 

class(sbux.z) 
## [1] "zoo"
str(sbux.z) 
## 'zoo' series from 1993-03-01 to 2008-03-01
##   Data: num [1:181] 1.13 1.15 1.43 1.46 1.41 1.44 1.63 1.59 1.32 1.32 ...
##   Index:  Date[1:181], format: "1993-03-01" "1993-04-01" "1993-05-01" "1993-06-01" "1993-07-01" ...
head(sbux.z)
## 1993-03-01 1993-04-01 1993-05-01 1993-06-01 1993-07-01 1993-08-01 
##       1.13       1.15       1.43       1.46       1.41       1.44

Extrayendo el indice de tiempo y los datos

index(sbux.z) 
##   [1] "1993-03-01" "1993-04-01" "1993-05-01" "1993-06-01" "1993-07-01"
##   [6] "1993-08-01" "1993-09-01" "1993-10-01" "1993-11-01" "1993-12-01"
##  [11] "1994-01-01" "1994-02-01" "1994-03-01" "1994-04-01" "1994-05-01"
##  [16] "1994-06-01" "1994-07-01" "1994-08-01" "1994-09-01" "1994-10-01"
##  [21] "1994-11-01" "1994-12-01" "1995-01-01" "1995-02-01" "1995-03-01"
##  [26] "1995-04-01" "1995-05-01" "1995-06-01" "1995-07-01" "1995-08-01"
##  [31] "1995-09-01" "1995-10-01" "1995-11-01" "1995-12-01" "1996-01-01"
##  [36] "1996-02-01" "1996-03-01" "1996-04-01" "1996-05-01" "1996-06-01"
##  [41] "1996-07-01" "1996-08-01" "1996-09-01" "1996-10-01" "1996-11-01"
##  [46] "1996-12-01" "1997-01-01" "1997-02-01" "1997-03-01" "1997-04-01"
##  [51] "1997-05-01" "1997-06-01" "1997-07-01" "1997-08-01" "1997-09-01"
##  [56] "1997-10-01" "1997-11-01" "1997-12-01" "1998-01-01" "1998-02-01"
##  [61] "1998-03-01" "1998-04-01" "1998-05-01" "1998-06-01" "1998-07-01"
##  [66] "1998-08-01" "1998-09-01" "1998-10-01" "1998-11-01" "1998-12-01"
##  [71] "1999-01-01" "1999-02-01" "1999-03-01" "1999-04-01" "1999-05-01"
##  [76] "1999-06-01" "1999-07-01" "1999-08-01" "1999-09-01" "1999-10-01"
##  [81] "1999-11-01" "1999-12-01" "2000-01-01" "2000-02-01" "2000-03-01"
##  [86] "2000-04-01" "2000-05-01" "2000-06-01" "2000-07-01" "2000-08-01"
##  [91] "2000-09-01" "2000-10-01" "2000-11-01" "2000-12-01" "2001-01-01"
##  [96] "2001-02-01" "2001-03-01" "2001-04-01" "2001-05-01" "2001-06-01"
## [101] "2001-07-01" "2001-08-01" "2001-09-01" "2001-10-01" "2001-11-01"
## [106] "2001-12-01" "2002-01-01" "2002-02-01" "2002-03-01" "2002-04-01"
## [111] "2002-05-01" "2002-06-01" "2002-07-01" "2002-08-01" "2002-09-01"
## [116] "2002-10-01" "2002-11-01" "2002-12-01" "2003-01-01" "2003-02-01"
## [121] "2003-03-01" "2003-04-01" "2003-05-01" "2003-06-01" "2003-07-01"
## [126] "2003-08-01" "2003-09-01" "2003-10-01" "2003-11-01" "2003-12-01"
## [131] "2004-01-01" "2004-02-01" "2004-03-01" "2004-04-01" "2004-05-01"
## [136] "2004-06-01" "2004-07-01" "2004-08-01" "2004-09-01" "2004-10-01"
## [141] "2004-11-01" "2004-12-01" "2005-01-01" "2005-02-01" "2005-03-01"
## [146] "2005-04-01" "2005-05-01" "2005-06-01" "2005-07-01" "2005-08-01"
## [151] "2005-09-01" "2005-10-01" "2005-11-01" "2005-12-01" "2006-01-01"
## [156] "2006-02-01" "2006-03-01" "2006-04-01" "2006-05-01" "2006-06-01"
## [161] "2006-07-01" "2006-08-01" "2006-09-01" "2006-10-01" "2006-11-01"
## [166] "2006-12-01" "2007-01-01" "2007-02-01" "2007-03-01" "2007-04-01"
## [171] "2007-05-01" "2007-06-01" "2007-07-01" "2007-08-01" "2007-09-01"
## [176] "2007-10-01" "2007-11-01" "2007-12-01" "2008-01-01" "2008-02-01"
## [181] "2008-03-01"
coredata(sbux.z)
##   [1]  1.13  1.15  1.43  1.46  1.41  1.44  1.63  1.59  1.32  1.32  1.43  1.38
##  [13]  1.45  1.77  1.69  1.50  1.72  1.68  1.37  1.61  1.59  1.63  1.43  1.42
##  [25]  1.43  1.40  1.73  2.12  2.22  2.38  2.25  2.33  2.51  2.50  1.99  2.09
##  [37]  2.77  3.22  3.22  3.36  3.09  3.89  3.92  3.86  4.12  3.40  4.07  4.00
##  [49]  3.52  3.55  3.74  4.63  4.87  4.87  4.97  3.92  4.15  4.56  4.35  4.70
##  [61]  5.39  5.72  5.71  6.35  4.98  3.75  4.30  5.16  5.48  6.67  6.19  6.29
##  [73]  6.67  8.78  8.77  8.93  5.53  5.44  5.89  6.46  6.31  5.76  7.61  8.35
##  [85] 10.65  7.19  8.08  9.08  8.91  8.71  9.52 10.62 10.83 10.52 11.87 11.32
##  [97] 10.09  9.20  9.28 10.94  8.58  8.02  7.10  8.14  8.42  9.06 11.30 10.94
## [109] 11.00 10.85 11.54 11.81  9.33  9.56  9.82 11.33 10.34  9.69 10.80 11.15
## [121] 12.25 11.18 11.73 11.67 12.99 13.50 13.69 15.02 15.30 15.77 17.41 17.78
## [133] 18.01 18.50 19.30 20.68 22.34 20.56 21.61 25.14 26.75 29.65 25.67 24.63
## [145] 24.56 23.54 26.05 24.56 24.98 23.31 23.82 26.89 28.95 28.54 30.14 34.54
## [157] 35.78 35.44 33.90 35.91 32.55 29.49 32.38 35.90 33.56 33.68 33.22 29.38
## [169] 29.82 29.50 27.40 24.95 25.37 26.20 24.91 25.37 22.24 19.46 17.98 17.10
## [181] 16.64

Start and End

start(sbux.z) 
## [1] "1993-03-01"
end(sbux.z)
## [1] "2008-03-01"

Ventaja de zoo: extraer subconjunto indexando con las fechas

sbux.z[as.Date(c("2000/3/1", "2003/3/1"))]
## 2000-03-01 2003-03-01 
##      10.65      12.25

#window() también funciona

window(sbux.z, start=as.Date("2000/3/1"), end=as.Date("2003/3/1")) 
## 2000-03-01 2000-04-01 2000-05-01 2000-06-01 2000-07-01 2000-08-01 2000-09-01 
##      10.65       7.19       8.08       9.08       8.91       8.71       9.52 
## 2000-10-01 2000-11-01 2000-12-01 2001-01-01 2001-02-01 2001-03-01 2001-04-01 
##      10.62      10.83      10.52      11.87      11.32      10.09       9.20 
## 2001-05-01 2001-06-01 2001-07-01 2001-08-01 2001-09-01 2001-10-01 2001-11-01 
##       9.28      10.94       8.58       8.02       7.10       8.14       8.42 
## 2001-12-01 2002-01-01 2002-02-01 2002-03-01 2002-04-01 2002-05-01 2002-06-01 
##       9.06      11.30      10.94      11.00      10.85      11.54      11.81 
## 2002-07-01 2002-08-01 2002-09-01 2002-10-01 2002-11-01 2002-12-01 2003-01-01 
##       9.33       9.56       9.82      11.33      10.34       9.69      10.80 
## 2003-02-01 2003-03-01 
##      11.15      12.25

Combinando dos series

sbuxmsft.z = cbind(sbux.z, msft.z) 
class(sbuxmsft.z) 
## [1] "zoo"
head(sbuxmsft.z) 
##            sbux.z   msft.z
## 1993-03-01   1.13 1.849489
## 1993-04-01   1.15 1.859486
## 1993-05-01   1.43 1.794505
## 1993-06-01   1.46 1.829494
## 1993-07-01   1.41 1.794505
## 1993-08-01   1.44 1.804501

Plot

plot(sbux.z, col="blue", lty=1, lwd=2, ylim=c(0,50),main="Monthly closing prices of SBUX and MFST",
     ylab="Adjusted close price")
lines(msft.z, col="red", lty=2, lwd=2)
legend(x="topleft", legend=c("SBUX","MSFT"), col=c("blue","red"),
         lty=1:2)

Alternativa, las dos a la vez

plot(sbuxmsft.z, plot.type="single", col=c("blue","red"), lty=1:2,
         lwd=2,main="Monthly closing prices of SBUX and MFST",
         ylab="Adjusted close price")
legend(x="topleft", legend=c("SBUX","MSFT"), col=c("blue","red"),
         lty=1:2) 

Importar datos directamente como objeto zoo

sbux.z2 = read.zoo("C:/Users/LUIS 1/Desktop/Series de Tiempo/Serie temporal en R y Python/R/sbuxPrices.csv",
                   format="%m/%d/%Y", sep=",", header=T) 

Importar datos de Yahoo Finance

library(tseries)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
SBUX.z = get.hist.quote(instrument="sbux", start="1993-03-01",
                           end="2020-06-01", quote="AdjClose",
                           provider="yahoo", origin="1970-01-01",
                           compression="d", retclass="zoo") 
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
## 
## This message is shown once per session and may be disabled by setting 
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
## time series ends   2020-05-29
View(SBUX.z)
MSFT.z = get.hist.quote(instrument="msft", start="1993-03-01",
                        end="2020-06-01", quote="AdjClose",
                        provider="yahoo", origin="1970-01-01",
                        compression="d", retclass="zoo") 
## time series ends   2020-05-29

Plot

plot(cbind(SBUX.z,MSFT.z), plot.type="single", col=c("blue","red"), lty=1:2,
     lwd=2,main="Monthly closing prices of SBUX and MFST",
     ylab="Adjusted close price")
legend(x="topleft", legend=c("SBUX","MSFT"), col=c("blue","red"),
       lty=1:2)

Gráficos con la Libería dygraphs

library(dygraphs)
dygraph(SBUX.z, "Monthly closing prices of SBUX")
dygraph(cbind(SBUX.z,MSFT.z), "Monthly closing prices of SBUX and MFST")

3 Datos diarios

Generamos datos aleatorios

datos <- rnorm(78, 0, 10)
fechas <- seq(as.Date("2020-03-06"), as.Date("2020-05-22"), by = "day")
as.numeric(format(fechas[1], "%j"))
## [1] 66
miserie.ts<-ts(datos,start=c(2016,66), frequency=365)
plot(miserie.ts)

library(zoo)
miserie.z=zoo(datos, fechas)
plot(miserie.z)

dygraph(miserie.z)