Error finding in R code with example

Hi R comm

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

PS.
the scrip comes from:

devtools::install_github('catboost/catboost', subdir = 'catboost/R-package')
devtools::install_github("AlbertoAlmuinha/boostime")

# install.packages("boostime")
library(boostime)
library(timetk)
library(lubridate)
library(modeltime)
library(tidymodels)
library(tidyverse)
install.packages("sknifedatar")
library(sknifedatar)
library(gt)

url <- "https://analisi.transparenciacatalunya.cat/api/views/j7xc-3kfh/rows.csv?accessType=DOWNLOAD"

df <- read_csv(url)

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')

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')




# 


# devtools::install_github("gadenbuie/xaringanExtra")
# install.packages("anomalize")
library(anomalize)

# install.packages('devtools',dependencies = T)
library('devtools')





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)
         ))

the output from running the last line is ::


so no plot generated....

This creates a tibble, which has embedded plotly objects that need to be extracted.

Hi technocrat

And how is that done in practice? what should be added and in what line ?

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.

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.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.