0

I am using the R programming language. I am trying to follow the R tutorial over here on neural networks (lstm) and time series: https://blogs.rstudio.com/ai/posts/2018-06-25-sunspots-lstm/

I decided to create my own time series data ("y.mon") for this tutorial (the same format and the same variable names) :

library(tidyverse)
library(glue)
library(forcats)
library(timetk)
library(tidyquant)
library(tibbletime)
library(cowplot)
library(recipes)
library(rsample)
library(yardstick) 
library(keras)
library(tfruns)
library(dplyr)
library(lubridate)
library(tibbletime)
library(timetk)

index = seq(as.Date("1749/1/1"), as.Date("2016/1/1"),by="day")


index <- format(as.Date(index), "%Y/%m/%d")

value <- rnorm(97520,27,2.1)

final_data <- data.frame(index, value)

y.mon<-aggregate(value~format(as.Date(index),
                              format="%Y/%m"),data=final_data, FUN=sum)

y.mon$index = y.mon$`format(as.Date(index), format = "%Y/%m")`
y.mon$`format(as.Date(index), format = "%Y/%m")` = NULL


y.mon %>%
  mutate(index = paste0(index, '/01')) %>%
  tk_tbl() %>%
  mutate(index = as_date(index)) %>%
  as_tbl_time(index = index) ->  y.mon

From here on, I follow the instructions in the tutorial (replacing the "sun_spots data" with "y.mon". Everything works fine until this point (I posted a question yesterday that got closed for being too detailed https://stackoverflow.com/questions/65527230/r-error-in-is-symbolx-object-not-found-keras - the code can be followed from the rstudio tutorial) :

#ERROR
coln <- colnames(compare_train)[4:ncol(compare_train)]
cols <- map(coln, quo(sym(.)))
rsme_train <-
  map_dbl(cols, function(col)
    rmse(
      compare_train,
      truth = value,
      estimate = !!col,
      na.rm = TRUE
    )) %>% mean()

rsme_train
Error in is_symbol(x) : object '.' not found

I found another stackoverflow post which deals with a similar problem:Getting error message while calculating rmse in a time series analysis

According to this stackoverflow post, this first error can be resolved like this:

coln <- colnames(compare_train)[4:ncol(compare_train)]
rsme_train <-
    map_df(coln, function(col)
        rmse(
            compare_train,
            truth = value,
            estimate = !!col,
            na.rm = TRUE
        )) %>% 
    pull(.estimate) %>%
    mean()

rsme_train

However, the following section of the tutorial has a similar section in which the same error persists even after applying the corrections:

    compare_test %>% write_csv(str_replace(model_path, ".hdf5", ".test.csv"))
    compare_test[FLAGS$n_timesteps:(FLAGS$n_timesteps + 10), c(2, 4:8)] %>% print()

cols <- map(coln, quo(sym(.)))
    rsme_test <-
      map_dbl(cols, function(col)
        rmse(
          compare_test,
          truth = value,
          estimate = !!col,
          na.rm = TRUE
        )) %>% mean()
    
    rsme_test
    
    #errors:
    Error in stri_replace_first_regex(string, pattern, fix_replacement(replacement),  : 
      object 'model_path' not found
    Error in is_symbol(x) : object '.' not found

These errors are preventing me from finishing the rest of the tutorial. Can someone please show me how to fix these? Thanks

stats_noob
  • 5,401
  • 4
  • 27
  • 83
  • 1
    Why are you loading all those packages? – Elin Jan 02 '21 at 00:51
  • Thank you for your reply! They are all required for the tutorial – stats_noob Jan 02 '21 at 00:53
  • 4
    They don't seem to be used here (not all of them) , it will help you solve your problem if you know what packages are actually involved. Also don't ask me to install a bunch of unneeded packages to help you. I think the obvious question is: what is it that you think the '.' is representing? Clearly R does not know, so if you can tell it that, things will work better. – Elin Jan 02 '21 at 00:56
  • Sorry - to get to the point in this tutorial where i need help in, many of those packages are required. I just wanted to make this post as reproducible as possible. I can remove them in this question if you would like. – stats_noob Jan 02 '21 at 01:05
  • 4
    Part of learning is really understanding what is happening in your code. Creating a minimal example will both help you understand but also isolate where the problem is. I will say one thing which is that a lot has changed in rlang since 2018 so it would not surprise me if a lot of changes are needed. Also what happens when you make the changes to the last code block? Such as using pull(.estimate)? – Elin Jan 02 '21 at 01:09
  • For this same problem, I posted a lengthy (minimal example) question with the full code and it got removed yesterday. I tried to post a shorter version of the same question. Thank you for your suggestions. – stats_noob Jan 02 '21 at 01:13

1 Answers1

2

Try using coln in map_dbl :

rsme_test <- map_dbl(coln, function(col)
    rmse(
      compare_test,
      truth = value,
      estimate = !!col,
      na.rm = TRUE
    )) %>% mean()
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • thank you for your reply. I could not reach to this step due to errors from the line above: – stats_noob Jan 02 '21 at 22:50
  • # build a dataframe that has both actual and predicted values for (i in 1:nrow(pred_test)) { varname <- paste0("pred_test", i) compare_test <- mutate(compare_test,!!varname := c( rep(NA, FLAGS$n_timesteps + i - 1), pred_test[i,], rep(NA, nrow(compare_test) - FLAGS$n_timesteps * 2 - i + 1) )) } compare_test %>% write_csv(str_replace(model_path, ".hdf5", ".test.csv")) Error in stri_replace_first_regex(string, pattern, fix_replacement(replacement), : object 'model_path' not found – stats_noob Jan 02 '21 at 22:50
  • coln <- colnames(compare_test)[4:ncol(compare_test)] cols <- map(coln, quo(sym(.))) Error in is_symbol(x) : object '.' not found – stats_noob Jan 02 '21 at 22:51
  • Run `rlang::last_error()` to see where the error occurred. rsme_test <- map_dbl(coln, function(col) rmse( compare_test, truth = value, estimate = !!col, na.rm = TRUE )) %>% mean() Error: Result 1 must be a single double, not a vector of class `tbl_df/tbl/data.frame` and of length 3 Run `rlang::last_error()` to see where the error occurred. – stats_noob Jan 02 '21 at 22:51
  • Sorry, I don't think it is fair to ask us to run the entire tutorial for you. Each post should contain only one specific question. Reading your post I thought you are stuck at `rsme_test` step hence I answered for that part. I am not aware of what happens before or after that part. – Ronak Shah Jan 03 '21 at 01:43
  • if you have time, can you please take a look at this question? https://stackoverflow.com/questions/68644940/r-error-optimization-resulting-in-unused-argumentx thank you! – stats_noob Aug 05 '21 at 03:51