i have some troubles running the following script, and its often a problem pinning down what causes the problem. So is there any good advice on how to solve the paticular problem and for future error correcting in R
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]]
Thanks for the advice, i'm trying to forecast on product level, meaning i am to develop forecasts for 100+ products . is there a good alternative to the modeltime package? anything u can recommend?
I use the fpp3 suite of packages. They extend the established {forecast} package with a data frame superclass adapted to time series, along with good model manipulation facilities. The greatest advantage is the accompanying free e-text that expands on the documentation. You can work from a single object with a time series of dates and historical data on the products, select one product, generate and evaluate models for one, then generalize that workflow to handle the rest. You still need to eyeball which model is best suited to the particular product, of course, but it will be much easier than doing a separate script for each product individually.
Take a look and open a new topic for any questions.