Just type
nest_data
to see what's contained.
Then, to plot the first ts_plots variable, as shown below, subset like
nest_data[1,3][[1]]
for the first row.
library(boostime)
#> Error in library(boostime): there is no package called 'boostime'
library(timetk)
#> Registered S3 method overwritten by 'tune':
#> method from
#> required_pkgs.model_spec parsnip
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>
#> date, intersect, setdiff, union
library(modeltime)
library(tidymodels)
library(tidyverse)
library(sknifedatar)
#>
#> Attaching package: 'sknifedatar'
#> The following object is masked from 'package:rsample':
#>
#> sliding_window
library(gt)
url <- "https://analisi.transparenciacatalunya.cat/api/views/j7xc-3kfh/rows.csv?accessType=DOWNLOAD"
DF <- read_csv(url)
#> Rows: 199 Columns: 40
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> chr (3): Data, CDEEBC_TotalEBCMercatRegulat, CDEEBC_TotalEBCMercatLliure
#> dbl (37): PBEE_Hidroelectr, PBEE_Carbo, PBEE_GasNat, PBEE_Fuel-Oil, PBEE_Cic...
#>
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
DF <- DF %>%
select(Data, starts_with("FEEI")) %>%
mutate(Data = mdy_hms(Data) %>% date()) %>%
rename(date = Data) %>%
rename_with(
.cols = starts_with("FEEI"),
.fn = ~ str_replace(., "FEEI_", "")
)
head(df) %>%
gt() %>%
tab_header(title = md("**Data by Sector (Catalunya)**")) %>%
opt_align_table_header("left")
#> Error in UseMethod("group_vars"): no applicable method for 'group_vars' applied to an object of class "noquote"
DF <- DF %>%
na.omit()
#
DF <- DF %>%
pivot_longer(-date) %>%
rename(id = name) %>%
mutate(id = as.factor(id))
head(df) %>%
gt() %>%
tab_header(title = md("**Panel Data**")) %>%
opt_align_table_header("left")
#> Error in UseMethod("group_vars"): no applicable method for 'group_vars' applied to an object of class "noquote"
#
# devtools::install_github("gadenbuie/xaringanExtra")
# install.packages("anomalize")
library(anomalize)
#> ══ Use anomalize to improve your Forecasts by 50%! ═════════════════════════════
#> Business Science offers a 1-hour course - Lab #18: Time Series Anomaly Detection!
#> </> Learn more at: https://university.business-science.io/p/learning-labs-pro </>
# install.packages('devtools',dependencies = T)
library("devtools")
#> Loading required package: usethis
#>
#> Attaching package: 'devtools'
#> The following object is masked from 'package:recipes':
#>
#> check
nest_data <- DF %>%
nest(data = -id) %>%
mutate(
ts_plots = map(
data,
~ plot_time_series(
.data = .x,
.date_var = date,
.value = value,
.smooth = FALSE
)
),
ts_anomaly = map(
data,
~ plot_anomaly_diagnostics(
.data = .x,
.date_var = date,
.value = value,
.alpha = 0.05
)
)
)
#> frequency = 12 observations per 1 year
#> trend = 60 observations per 5 years
#> frequency = 12 observations per 1 year
#> trend = 60 observations per 5 years
#> frequency = 12 observations per 1 year
#> trend = 60 observations per 5 years
#> frequency = 12 observations per 1 year
#> trend = 60 observations per 5 years
#> frequency = 12 observations per 1 year
#> trend = 60 observations per 5 years
#> frequency = 12 observations per 1 year
#> trend = 60 observations per 5 years
#> frequency = 12 observations per 1 year
#> trend = 60 observations per 5 years
#> frequency = 12 observations per 1 year
#> trend = 60 observations per 5 years
#> frequency = 12 observations per 1 year
#> trend = 60 observations per 5 years
#> frequency = 12 observations per 1 year
#> trend = 60 observations per 5 years
#> frequency = 12 observations per 1 year
#> trend = 60 observations per 5 years
#> frequency = 12 observations per 1 year
#> trend = 60 observations per 5 years
#> frequency = 12 observations per 1 year
#> trend = 60 observations per 5 years
nest_data[1,3][[1]]
#> [[1]]
This doesn't reprex well. Here's the plot
I'd encourage exploring other approaches before getting too invested in the timetk package.