Clase 1

library(tidyverse) # data science
library(fpp3) # ts
library(plotly) # interactive plots
library(patchwork) # handling many plots in one figure
library(tidyquant) # financial analisys and downloading data
?aus_accommodation
starting httpd help server ... done
aus_accommodation
# A tsibble: 592 x 5 [1Q]
# Key:       State [8]
      Date State                        Takings Occupancy   CPI
     <qtr> <chr>                          <dbl>     <dbl> <dbl>
 1 1998 Q1 Australian Capital Territory    24.3      65    67  
 2 1998 Q2 Australian Capital Territory    22.3      59    67.4
 3 1998 Q3 Australian Capital Territory    22.5      58    67.5
 4 1998 Q4 Australian Capital Territory    24.4      59    67.8
 5 1999 Q1 Australian Capital Territory    23.7      58    67.8
 6 1999 Q2 Australian Capital Territory    25.4      61    68.1
 7 1999 Q3 Australian Capital Territory    28.2      66    68.7
 8 1999 Q4 Australian Capital Territory    25.8      60    69.1
 9 2000 Q1 Australian Capital Territory    27.3      60.9  69.7
10 2000 Q2 Australian Capital Territory    30.1      64.7  70.2
# ℹ 582 more rows

592 filas, 5 columnas, Key:State(8) son 8 estados, osea, 8 series de tiempo

aus_accommodation |> 
  distinct(State)
# A tibble: 8 × 1
  State                       
  <chr>                       
1 Australian Capital Territory
2 New South Wales             
3 Northern Territory          
4 Queensland                  
5 South Australia             
6 Tasmania                    
7 Victoria                    
8 Western Australia           
tasmania <- aus_accommodation |>
  filter(State == "Tasmania") |> 
  # select(Date, Takings, Occupancy)  option 1
  # select(Date, Takings:Occupnacy)
  select(-c(State, CPI)) |> 
  mutate(
    'Año unión N°1'=1
  ) |> 
  janitor::clean_names()
tasmania
# A tsibble: 74 x 4 [1Q]
      date takings occupancy ano_union_n_1
     <qtr>   <dbl>     <dbl>         <dbl>
 1 1998 Q1    28.7      67               1
 2 1998 Q2    19.0      45               1
 3 1998 Q3    16.1      39               1
 4 1998 Q4    25.9      56               1
 5 1999 Q1    28.4      66               1
 6 1999 Q2    20.1      48               1
 7 1999 Q3    17.3      41               1
 8 1999 Q4    24.3      56               1
 9 2000 Q1    30.0      66.2             1
10 2000 Q2    21.7      49.7             1
# ℹ 64 more rows
tasmania |> 
  autoplot(takings, color='#2E79E8', size=2)

we see that the Takings have an upward trend, yearly seasonality, and an increasing variance. We can try using a log transformation

p <- tasmania |> 
  autoplot(log(takings), color='#2E79E8', size=1)+
  geom_point()
ggplotly(p, dynamicTicks = TRUE) |> 
  rangeslider()
tasmania |> 
  gg_season(log(takings)) # para ver la seasonality

tasmania |> 
  gg_subseries(log(takings))

tasmania |> 
  gg_tsdisplay(log(takings), plot_type="auto")

stocks <- c('AAPL','GOOGL','TSLA','MSFT')

stocks_data <- tq_get(
  x = stocks,
  get = "stock.prices",
  from = "2010-01-01",
  to = "2024-01-25"
  )


stocks_data <- stocks_data |> 
  as_tsibble(index = date, key = symbol)

stocks_data
# A tsibble: 14,030 x 8 [1D]
# Key:       symbol [4]
   symbol date        open  high   low close    volume adjusted
   <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
 1 AAPL   2010-01-04  7.62  7.66  7.59  7.64 493729600     6.48
 2 AAPL   2010-01-05  7.66  7.70  7.62  7.66 601904800     6.49
 3 AAPL   2010-01-06  7.66  7.69  7.53  7.53 552160000     6.39
 4 AAPL   2010-01-07  7.56  7.57  7.47  7.52 477131200     6.38
 5 AAPL   2010-01-08  7.51  7.57  7.47  7.57 447610800     6.42
 6 AAPL   2010-01-11  7.60  7.61  7.44  7.50 462229600     6.36
 7 AAPL   2010-01-12  7.47  7.49  7.37  7.42 594459600     6.29
 8 AAPL   2010-01-13  7.42  7.53  7.29  7.52 605892000     6.38
 9 AAPL   2010-01-14  7.50  7.52  7.47  7.48 432894000     6.34
10 AAPL   2010-01-15  7.53  7.56  7.35  7.35 594067600     6.23
# ℹ 14,020 more rows
stocks_data |> 
  ggplot(aes(x = date, y = close, color = symbol))+
  geom_line()

stocks_data |> 
  ggplot(aes(x = date, open = open, high = high, low = low, close = close))+
  geom_candlestick()+
  facet_wrap(~ symbol, ncol = 2)

stocks2 <- 'AAPL'

stocks_data2 <- tq_get(
  x = stocks2,
  get = "stock.prices",
  from = "2010-01-01",
  to = "2024-01-30"
  )


stocks_data2 <- stocks_data2 |> 
  as_tsibble(index = date, key = symbol)

stocks_data2 |> 
  ggplot(aes(x = date, open = open, high = high, low = low, close = close))+
  geom_candlestick()+
  facet_wrap(~ symbol, ncol = 2)

Cedasticidad

  • Homocedasticidad para varianza constante
  • Heterocedasticidad para varianza variable

Componentes

  • Estacionalidad: Es una periodicidad fija.
  • Ciclos: No se sabe con exactitud cuando va a ser la siguiente