Clase_4

Author

JAFL

library(tidyverse)
── 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.4.4     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.0
✔ 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(fpp3)
── Attaching packages ────────────────────────────────────────────── fpp3 0.5 ──
✔ tsibble     1.1.3     ✔ fable       0.3.3
✔ tsibbledata 0.4.1     ✔ fabletools  0.3.4
✔ feasts      0.3.1     
── Conflicts ───────────────────────────────────────────────── fpp3_conflicts ──
✖ lubridate::date()    masks base::date()
✖ dplyr::filter()      masks stats::filter()
✖ tsibble::intersect() masks base::intersect()
✖ tsibble::interval()  masks lubridate::interval()
✖ dplyr::lag()         masks stats::lag()
✖ tsibble::setdiff()   masks base::setdiff()
✖ tsibble::union()     masks base::union()
library(plotly)

Attaching package: 'plotly'

The following object is masked from 'package:ggplot2':

    last_plot

The following object is masked from 'package:stats':

    filter

The following object is masked from 'package:graphics':

    layout
library(patchwork)
library(tidyquant)
Loading required package: PerformanceAnalytics
Loading required package: xts
Loading required package: zoo

Attaching package: 'zoo'

The following object is masked from 'package:tsibble':

    index

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


Attaching package: 'PerformanceAnalytics'

The following object is masked from 'package:fpp3':

    prices

The following object is masked from 'package:graphics':

    legend

Loading required package: quantmod
Loading required package: TTR
Registered S3 method overwritten by 'quantmod':
  method            from
  as.zoo.data.frame zoo 

Attaching package: 'tidyquant'

The following object is masked from 'package:fable':

    VAR
?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
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           
tas <-aus_accommodation |> 
  filter(State == "Tasmania") |> 


  #select(Date, Takings:CPI)
  select(-c(State,CPI)) |> 
  mutate(
    'Año union n°1' = 1
  ) |> 
  janitor::clean_names() 
  

tas
# 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
p <-tas |> 
  autoplot(takings)+
  geom_point()


ggplotly(p, dynamicTicks = TRUE) |> 
  rangeslider()
tas |> 
  gg_season(log(takings))

tas |> 
  gg_subseries(log(takings))

tas |> 
  gg_tsdisplay(log(takings),plot_type = 'auto')

stocks <- c('MCK','META','NVDA')

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

stocks_data
# A tibble: 10,015 × 8
   symbol date        open  high   low close  volume adjusted
   <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>
 1 MCK    2010-01-04  63.0  63.5  62.5  63.4 2035200     56.4
 2 MCK    2010-01-05  63.3  63.6  63.2  63.6 2133200     56.7
 3 MCK    2010-01-06  62.8  62.8  60.8  61   4230000     54.3
 4 MCK    2010-01-07  62.1  62.1  60.4  61.1 3789500     54.5
 5 MCK    2010-01-08  61.2  61.4  60.2  60.8 2608500     54.1
 6 MCK    2010-01-11  60.7  61.6  60.7  61.5 1276100     54.8
 7 MCK    2010-01-12  61.4  62.5  61.0  62.3 3582000     55.5
 8 MCK    2010-01-13  62.3  62.9  61.9  62.5 2064300     55.7
 9 MCK    2010-01-14  62.6  62.6  61.9  62.0 1846900     55.2
10 MCK    2010-01-15  61.9  62.2  61.5  61.9 2652000     55.2
# ℹ 10,005 more rows
stocks_data |> 
  distinct(symbol)
# A tibble: 3 × 1
  symbol
  <chr> 
1 MCK   
2 META  
3 NVDA  
stocks_data <- stocks_data |> 
  as_tsibble(index=date, key= symbol)

stocks_data
# A tsibble: 10,015 x 8 [1D]
# Key:       symbol [3]
   symbol date        open  high   low close  volume adjusted
   <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>
 1 MCK    2010-01-04  63.0  63.5  62.5  63.4 2035200     56.4
 2 MCK    2010-01-05  63.3  63.6  63.2  63.6 2133200     56.7
 3 MCK    2010-01-06  62.8  62.8  60.8  61   4230000     54.3
 4 MCK    2010-01-07  62.1  62.1  60.4  61.1 3789500     54.5
 5 MCK    2010-01-08  61.2  61.4  60.2  60.8 2608500     54.1
 6 MCK    2010-01-11  60.7  61.6  60.7  61.5 1276100     54.8
 7 MCK    2010-01-12  61.4  62.5  61.0  62.3 3582000     55.5
 8 MCK    2010-01-13  62.3  62.9  61.9  62.5 2064300     55.7
 9 MCK    2010-01-14  62.6  62.6  61.9  62.0 1846900     55.2
10 MCK    2010-01-15  61.9  62.2  61.5  61.9 2652000     55.2
# ℹ 10,005 more rows
stocks_data |> 
  ggplot(aes(x = date, y = close, color = symbol))+
  theme_dark()+
  geom_line()

stocks_data |> 
  ggplot(aes(x = date, open = open, high = high, low = low, close = close))+
  theme_dark()+
  geom_candlestick()+
  facet_wrap(~ symbol, ncol=2)
Warning: The following aesthetics were dropped during statistical transformation: open,
high, low, close
ℹ This can happen when ggplot fails to infer the correct grouping structure in
  the data.
ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
  variable into a factor?
The following aesthetics were dropped during statistical transformation: open,
high, low, close
ℹ This can happen when ggplot fails to infer the correct grouping structure in
  the data.
ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
  variable into a factor?
The following aesthetics were dropped during statistical transformation: open,
high, low, close
ℹ This can happen when ggplot fails to infer the correct grouping structure in
  the data.
ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
  variable into a factor?
Warning: The following aesthetics were dropped during statistical transformation: x,
open, high, low, close
ℹ This can happen when ggplot fails to infer the correct grouping structure in
  the data.
ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
  variable into a factor?
The following aesthetics were dropped during statistical transformation: x,
open, high, low, close
ℹ This can happen when ggplot fails to infer the correct grouping structure in
  the data.
ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
  variable into a factor?
The following aesthetics were dropped during statistical transformation: x,
open, high, low, close
ℹ This can happen when ggplot fails to infer the correct grouping structure in
  the data.
ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
  variable into a factor?
Warning: Using the `size` aesthetic in this geom was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` in the `default_aes` field and elsewhere instead.