[R] Help with a third ggplot error

Eric Berger er|cjberger @end|ng |rom gm@||@com
Sat Jun 15 21:34:00 CEST 2019


reprex = reproducible example

On Sat, Jun 15, 2019 at 10:20 PM Bill Poling <Bill.Poling using zelis.com> wrote:

> Hello Eric.
>
> I am unfamiliar with the term reprex, however, I am googling it now.
>
> In the meantime, here is the source DF sample and the train test code I
> sure hope it helps.
>
> head(dftmp,n=7)
> Date2 NetEditRev  index.num year half quarter month.lbl day  wday.lbl
> holiday
> 1  2017-01-01    -923.40 1483228800 2017    1       1   January   1
> Sunday       0
> 2  2017-01-02   19222.09 1483315200 2017    1       1   January   2
> Monday       1
> 3  2017-01-03   -8396.82 1483401600 2017    1       1   January   3
>  Tuesday       0
> 4  2017-01-04   37696.58 1483488000 2017    1       1   January   4
> Wednesday       0
> 5  2017-01-05   46075.34 1483574400 2017    1       1   January   5
> Thursday       0
> 6  2017-01-06   38329.35 1483660800 2017    1       1   January   6
> Friday       0
> 7  2017-01-07    3110.51 1483747200 2017    1       1   January   7
> Saturday       0
>
> #Train/Test split----
> #Predict next 92 days
>
> train_test_split_date <- "2019-03-12"
> #arbitrary, looking for split to be 3 months from the end of current time
> period. etc depends on our date period
>
> train_tbl <- dftmp %>%
>   filter(Date2 < ymd(train_test_split_date))
>
> test_tbl <- dftmp %>%
>   filter(Date2 >= ymd(train_test_split_date))
>
> From: Eric Berger <ericjberger using gmail.com>
> Sent: Saturday, June 15, 2019 2:54 PM
> To: Bill Poling <Bill.Poling using zelis.com>
> Cc: r-help (r-help using r-project.org) <r-help using r-project.org>
> Subject: Re: [R] Help with a third ggplot error
>
> Is this supposed to be a reprex? I don't see where train_tbl is defined.
>
> On Fri, Jun 14, 2019 at 10:30 PM Bill Poling <mailto:Bill.Poling using zelis.com>
> wrote:
> #RStudio Version 1.2.1335
> sessionInfo()
> #R version 3.5.3 (2019-03-11)
> #Platform: x86_64-w64-mingw32/x64 (64-bit)
> #Running under: Windows >= 8 x64 (build 9200)
>
> Hello I am fitting an Arima model and all appears to go well until I get
> to the ggplot, (again, lots of laughs).
> Deja Vu all over again! (God I hope it's not a typo!)
>
> The error at the point of the plot is:
> # Don't know how to automatically pick scale for object of type function.
> Defaulting to continuous.
> # Error: All columns in a tibble must be 1d or 2d objects:
> # * Column `x` is function
>
> I hope someone recognizes my problem.
>
> Thank you for any assistance.
>
> #Here is the code and particulars of the data being plotted
>
> #Fit the arima model
>
> fit_arima2 <- train_tbl %>%
>   tk_ts(select = NetEditRev, frequency = 364) %>%
>   Arima(order = c(1,0,2),
>         seasonal=c(0,1,0),
>         include.drift = TRUE)
>
> #Forecast with Sweep Functions----
>
> fcast_arima_tbl <- forecast(fit_arima2, h = nrow(test_tbl)) %>%
>   sw_sweep(timetk_idx = TRUE, rename_index = "date")
>
> #Save the DF
>
> fs::dir_create("00_model")
>
> fcast_arima_tbl %>% write_rds("00_model/fcast_arima_tbl.rds")
>
> fcast_arima_tbl1 <- read_rds("00_model/fcast_arima_tbl.rds")
>
> head(fcast_arima_tbl1)
>
> # A tibble: 6 x 7
>   date       key    NetEditRev lo.80 lo.95 hi.80 hi.95
>   <date>     <chr>       <dbl> <dbl> <dbl> <dbl> <dbl>
> 1 2017-01-01 actual      -923.    NA    NA    NA    NA
> 2 2017-01-02 actual     19222.    NA    NA    NA    NA
> 3 2017-01-03 actual     -8397.    NA    NA    NA    NA
> 4 2017-01-04 actual     37697.    NA    NA    NA    NA
> 5 2017-01-05 actual     46075.    NA    NA    NA    NA
> 6 2017-01-06 actual     38329.    NA    NA    NA    NA
>
> str(fcast_arima_tbl1)
> Classes 'tbl_df', 'tbl' and 'data.frame':892 obs. of  7 variables:
>  $ date      : Date, format: "2017-01-01" "2017-01-02" "2017-01-03"
> "2017-01-04" ...
>  $ key       : chr  "actual" "actual" "actual" "actual" ...
>  $ NetEditRev: num  -923 19222 -8397 37697 46075 ...
>  $ lo.80     : num  NA NA NA NA NA NA NA NA NA NA ...
>  $ lo.95     : num  NA NA NA NA NA NA NA NA NA NA ...
>  $ hi.80     : num  NA NA NA NA NA NA NA NA NA NA ...
>  $ hi.95     : num  NA NA NA NA NA NA NA NA NA NA ...
>
> #Plot the model
>
> g4 <- fcast_arima_tbl1 %>%
>   ggplot(aes(date, NetEditRev, color = key)) +
>   geom_point(data = test_tbl %>% mutate(key = "actual")) +
>   geom_point(alpha = 0.5) +
>   theme_tq() +
>   scale_color_tq() +
>   labs(title = "ARIMA(1,0,2)(0,1,0 with Drift For Net Edit Revenue")
>
> g4
> # Don't know how to automatically pick scale for object of type function.
> Defaulting to continuous.
> # Error: All columns in a tibble must be 1d or 2d objects:
> # * Column `x` is function
>
> ggplotly(g4) %>%
>   layout(xaxis = list(rangeslider = list(type = "date")))
> # Don't know how to automatically pick scale for object of type function.
> Defaulting to continuous.
> # Error: All columns in a tibble must be 1d or 2d objects:
> # * Column `x` is function
>
>
> #Alternative changes that I have tried based on google searches, but have
> their own errors.
>
> # g4 <- fcast_arima_tbl1 %>%
> #   ggplot(aes(date, NetEditRev, color = key)) +
> #   geom_point(data = test_tbl %>% filter(key = "actual")) + #Try using
> filter
> #   geom_point(alpha = 0.5) +
> #   theme_tq() +
> #   scale_color_tq() +
> #   labs(title = "ARIMA(1,0,2)(0,1,0 with Drift For Net Edit Revenue")
> # #Error: `key` (`key = "actual"`) must not be named, do you need `==`?
> #
> # g4 <- fcast_arima_tbl1 %>%
> #   ggplot(aes(date, NetEditRev, color = key)) +
> #   geom_point(data = test_tbl %>% filter(key == "actual")) + #Try using
> filter with ==
> #   geom_point(alpha = 0.5) +
> #   theme_tq() +
> #   scale_color_tq() +
> #   labs(title = "ARIMA(1,0,2)(0,1,0 with Drift For Net Edit Revenue")
> # #Error: object 'key' not found
> #
> # g4 <- fcast_arima_tbl1 %>%
> #   ggplot(aes(date, NetEditRev, color = key)) +
> #   geom_point(data = test_tbl) +
> #   filter(fcast_arima_tbl1$key = "actual") + #Try filter with
> fcast_arima_tbl1$key and repositioned with +
> #   geom_point(alpha = 0.5) +
> #   theme_tq() +
> #   scale_color_tq() +
> #   labs(title = "ARIMA(1,0,2)(0,1,0 with Drift For Net Edit Revenue")
> # #Error: Cannot add ggproto objects together. Did you forget to add this
> object to a ggplot object?
>
>
> Confidentiality Notice This message is sent from Zelis. ...{{dropped:13}}
>
> ______________________________________________
> mailto:R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
> Confidentiality Notice This message is sent from Zelis. This transmission
> may contain information which is privileged and confidential and is
> intended for the personal and confidential use of the named recipient only.
> Such information may be protected by applicable State and Federal laws from
> this disclosure or unauthorized use. If the reader of this message is not
> the intended recipient, or the employee or agent responsible for delivering
> the message to the intended recipient, you are hereby notified that any
> disclosure, review, discussion, copying, or taking any action in reliance
> on the contents of this transmission is strictly prohibited. If you have
> received this transmission in error, please contact the sender immediately.
> Zelis, 2018.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list