title: “Clase 01”
author: “Raúl Oviedo”
date: “23/01/2024”
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

Acciones

Las “tsibbles” son data frames (‘tibbles’ para series de tiempo). Tienen dos argumentos caracteristicos para series de tiempo:

1) index una columna que contiene la variable temporal. En este caso es la columna ‘Date’.

1) key: no es obligatorio cuando solo tiene una serie de tiempo. Si la tabla contiene más de una serie de tiempo (en filas), si es obligatorio definir el ‘key’

gafa_stock
# A tsibble: 5,032 x 8 [!]
# Key:       Symbol [4]
   Symbol Date        Open  High   Low Close Adj_Close    Volume
   <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>     <dbl>
 1 AAPL   2014-01-02  79.4  79.6  78.9  79.0      67.0  58671200
 2 AAPL   2014-01-03  79.0  79.1  77.2  77.3      65.5  98116900
 3 AAPL   2014-01-06  76.8  78.1  76.2  77.7      65.9 103152700
 4 AAPL   2014-01-07  77.8  78.0  76.8  77.1      65.4  79302300
 5 AAPL   2014-01-08  77.0  77.9  77.0  77.6      65.8  64632400
 6 AAPL   2014-01-09  78.1  78.1  76.5  76.6      65.0  69787200
 7 AAPL   2014-01-10  77.1  77.3  75.9  76.1      64.5  76244000
 8 AAPL   2014-01-13  75.7  77.5  75.7  76.5      64.9  94623200
 9 AAPL   2014-01-14  76.9  78.1  76.8  78.1      66.1  83140400
10 AAPL   2014-01-15  79.1  80.0  78.8  79.6      67.5  97909700
# ℹ 5,022 more rows
gafa_stock |> #pipe
  distinct(Symbol)
# A tibble: 4 × 1
  Symbol
  <chr> 
1 AAPL  
2 AMZN  
3 FB    
4 GOOG  
gafa_stock |>
  #pasar las columnas a filas
  pivot_longer(cols = -c(Symbol, Date), names_to = "tipo",
               values_to = "valor") |>
  #convertir a tibble
  as_tibble() |>
  #agrupar por acción y tipo de var.
  group_by(Symbol, tipo) |>
  #Sacar la media por grupos
  summarise(media = mean(valor)) |>
  # pasar los tipos a columnas
  pivot_wider(names_from = Symbol, values_from = media)
`summarise()` has grouped output by 'Symbol'. You can override using the
`.groups` argument.
# A tibble: 6 × 5
  tipo           AAPL     AMZN        FB     GOOG
  <chr>         <dbl>    <dbl>     <dbl>    <dbl>
1 Adj_Close      126.     823.      120.     787.
2 Close          131.     823.      120.     787.
3 High           132.     832.      122.     794.
4 Low            130.     814.      119.     780.
5 Open           131.     824.      120.     787.
6 Volume    42930579. 4231814. 28890180. 1922629.
g1 <- gafa_stock |>
  autoplot(Close) +
  facet_wrap(~Symbol, scales = "free_y") + #libera el eje de la y
  theme(legend.position = "none")
g1

ggplotly(g1)   #convertir la grafica a plotly(grafica animada)
gafa_stock |>
  select(-Volume) |>
  pivot_longer(cols = -c(Symbol,Date), names_to= "tipo", values_to = "valor")|>

  autoplot(valor) + 
  facet_grid(Symbol ~ tipo,scales = "free_y") +
  theme(legend.position = "none")