# | message: falseTarea Descomposición
Quarto
Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.
Running Code
When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:
1 + 1[1] 2
You can add options to executable code like this
[1] 4
The echo: false option disables the printing of code (only output is displayed).
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()
global_economy# A tsibble: 15,150 x 9 [1Y]
# Key: Country [263]
Country Code Year GDP Growth CPI Imports Exports Population
<fct> <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Afghanistan AFG 1960 537777811. NA NA 7.02 4.13 8996351
2 Afghanistan AFG 1961 548888896. NA NA 8.10 4.45 9166764
3 Afghanistan AFG 1962 546666678. NA NA 9.35 4.88 9345868
4 Afghanistan AFG 1963 751111191. NA NA 16.9 9.17 9533954
5 Afghanistan AFG 1964 800000044. NA NA 18.1 8.89 9731361
6 Afghanistan AFG 1965 1006666638. NA NA 21.4 11.3 9938414
7 Afghanistan AFG 1966 1399999967. NA NA 18.6 8.57 10152331
8 Afghanistan AFG 1967 1673333418. NA NA 14.2 6.77 10372630
9 Afghanistan AFG 1968 1373333367. NA NA 15.2 8.90 10604346
10 Afghanistan AFG 1969 1408888922. NA NA 15.0 10.1 10854428
# ℹ 15,140 more rows
ejercicio 1
global_economy |>
autoplot(GDP/Population) +
theme(legend.position = "none")Warning: Removed 3242 rows containing missing values (`geom_line()`).
todas las economias tienen una tendencia a la alza a lo largo del tiempo
global_economy |>
mutate(gdp_per_capita = GDP/Population) |>
select(-c(Code, Growth:Exports)) |>
filter(Year %in% 2010:2017) |>
as_tibble() |>
group_by(Country) |>
summarise(mean_gdppc = mean(gdp_per_capita, na.rm = TRUE)) |>
arrange(desc(mean_gdppc))# A tibble: 263 × 2
Country mean_gdppc
<fct> <dbl>
1 Monaco 163978.
2 Liechtenstein 162023.
3 Luxembourg 108327.
4 Norway 88913.
5 Bermuda 86347.
6 Switzerland 82544.
7 Isle of Man 80996.
8 Macao SAR, China 76201.
9 Qatar 75931.
10 Australia 59099.
# ℹ 253 more rows
Con la información anterior nos dimos cuenta que Monaco es el país con mayor PIB per capita. # PIB EEUU sin transformar
global_economy |>
filter(Country == "United States") |>
select(GDP) |>
autoplot()Plot variable not specified, automatically selected `.vars = GDP`
PIB EEUU transformada
global_economy |>
filter(Country == "United States") |>
select(GDP) |>
autoplot(log(GDP))PIB México sin transformar
global_economy |>
filter(Country == "Mexico") |>
select(-c(Code, Growth:Population)) |>
autoplot()Plot variable not specified, automatically selected `.vars = GDP`
PIB México transformada
global_economy |>
filter(Country == "Mexico") |>
select(-c(Code, Growth:Population)) |>
autoplot(log(GDP))Demanda de electricidad en el estado de Victoria (Australia)
vic_elec |>
select(-c(Temperature:Holiday))# A tsibble: 52,608 x 2 [30m] <Australia/Melbourne>
Time Demand
<dttm> <dbl>
1 2012-01-01 00:00:00 4383.
2 2012-01-01 00:30:00 4263.
3 2012-01-01 01:00:00 4049.
4 2012-01-01 01:30:00 3878.
5 2012-01-01 02:00:00 4036.
6 2012-01-01 02:30:00 3866.
7 2012-01-01 03:00:00 3694.
8 2012-01-01 03:30:00 3562.
9 2012-01-01 04:00:00 3433.
10 2012-01-01 04:30:00 3359.
# ℹ 52,598 more rows
vic_elec |>
select(-c(Temperature:Holiday)) |>
autoplot()Plot variable not specified, automatically selected `.vars = Demand`
canadian gas
canadian_gas |>
autoplot()Plot variable not specified, automatically selected `.vars = Volume`
transformación con box cox
lambda <- canadian_gas |>
features(Volume, features = guerrero) |>
pull(lambda_guerrero)
canadian_gas |>
autoplot(box_cox(Volume, lambda))comparando ambas gráficas, antes y después de la transformación podemos ver que no hay cambios significativos por lon que no es de ayuda hacer la transformación.
ejercicio 4
i.
fma::plasticsRegistered S3 method overwritten by 'quantmod':
method from
as.zoo.data.frame zoo
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1 742 697 776 898 1030 1107 1165 1216 1208 1131 971 783
2 741 700 774 932 1099 1223 1290 1349 1341 1296 1066 901
3 896 793 885 1055 1204 1326 1303 1436 1473 1453 1170 1023
4 951 861 938 1109 1274 1422 1486 1555 1604 1600 1403 1209
5 1030 1032 1126 1285 1468 1637 1611 1608 1528 1420 1119 1013
plastico <- as_tsibble(fma::plastics)
plastico# A tsibble: 60 x 2 [1M]
index value
<mth> <dbl>
1 0001 Jan 742
2 0001 Feb 697
3 0001 Mar 776
4 0001 Apr 898
5 0001 May 1030
6 0001 Jun 1107
7 0001 Jul 1165
8 0001 Aug 1216
9 0001 Sep 1208
10 0001 Oct 1131
# ℹ 50 more rows
fma::plastics |>
autoplot()plastico |>
autoplot(value)en esta serie de tiempo del producto A se puede reconocer una tendencia alcista y una estacionalidad anual. # ii Descomposición clásica
plastico_fit <- plastico |>
model(
clasica = classical_decomposition(value, type = "multiplicative")
)
plastico_fit |>
components() |>
autoplot()Warning: Removed 6 rows containing missing values (`geom_line()`).
- Los resultados si coinciden # iv datos desestacionalizados
plastico_fit |>
components() |>
ggplot(aes(x = index, y = season_adjust)) +
geom_line()v.
outlier <- 15Cambiando la observación ‘r outlier’:
plastico2 <- plastico
plastico2$value[outlier] <- plastico$value[outlier] + 500
plastico2# A tsibble: 60 x 2 [1M]
index value
<mth> <dbl>
1 0001 Jan 742
2 0001 Feb 697
3 0001 Mar 776
4 0001 Apr 898
5 0001 May 1030
6 0001 Jun 1107
7 0001 Jul 1165
8 0001 Aug 1216
9 0001 Sep 1208
10 0001 Oct 1131
# ℹ 50 more rows
plastico2 |>
model(
clasica = classical_decomposition(value, type = "multiplicative")
) |>
components() |>
autoplot()Warning: Removed 6 rows containing missing values (`geom_line()`).