[R] Help with another ggplot error

Bill Poling B|||@Po||ng @end|ng |rom ze||@@com
Thu Jun 13 16:14:17 CEST 2019


#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 have created a function called autocorrelate.

When I run it with ggplot I get this error:
#Error in autocorrelate(., NetEditRev, lags = 0:nrow(.)) :   unused argument (lags = 0:nrow(.))

Using what I learned from Rui yesterday I have tried to track down the problem myself

#---------------------------------- ggplot ISSUE 2 -----------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------#
getAnywhere('lags')

# A single object matching 'lags' was found
# It was found in the following places
# package:TTR
# namespace:TTR
# with value

function (x, n = 1)
{
  x <- as.matrix(x)
  if (is.null(colnames(x)))
    colnames(x) <- paste("V", 1:NCOL(x), sep = "")
  out <- embed(x, n + 1)
  if (n == 1)
    lag.names <- 1
  else if (NCOL(x) == 1)
    lag.names <- 1:n
  else lag.names <- rep(1:n, NCOL(x))
  colnames(out) <- c(colnames(x), paste(colnames(x), sort(lag.names),
                                        sep = "."))
  return(out)
}
<bytecode: 0x0000021163065658>
# <environment: namespace:TTR>

#Here are references I have perused but not sure they help, or at least I do not understand how they help me?

https://stackoverflow.com/questions/32056718/annotate-ggplot-bar-chart-error-unused-arguments
#https://stackoverflow.com/questions/24004974/sudden-unused-argument-error

find("lags")
#"package:TTR"

#Here are the Pkgs/libraries loaded, and I see TTR_0.23-4
# I did not load this library so it must be coming in with another library?

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
 [1] plotly_4.9.0               tseries_0.10-46            timeDate_3043.102          h2o_3.22.1.1               lime_0.4.1
 [6] parsnip_0.0.2              sweep_0.2.1.1              forecast_8.7               riem_0.1.1                 timetk_0.1.1.1
[11] tidyquant_0.5.6            forcats_0.4.0              stringr_1.4.0              dplyr_0.8.1                purrr_0.3.2
[16] readr_1.3.1                tidyr_0.8.3                tibble_2.1.1               ggplot2_3.1.1              tidyverse_1.2.1
[21] quantmod_0.4-14            TTR_0.23-4                 PerformanceAnalytics_1.5.2 xts_0.11-2                 zoo_1.8-5
[26] lubridate_1.7.4            yardstick_0.0.3

?`TTR-package`

These are the libraries I have loaded

library(yardstick)
library(tidyquant)
library(timetk)
library(riem)
library(forecast)
library(sweep)
library(parsnip)
library(lime)
library(h2o)
library(timeDate)
library(tseries)
library(ggplot2)
library(tidyverse)
library(lubridate)
library(plotly)

I tried changing lags to lags1 in the function but that did not help

Here are the script and a data sample to assist anyone who might try to help me.

Thank you for any advice.

WHP


#Create a function----

autocorrelate <- function(dftmp, value, Lags = 0.20) {

  value_expr <- enquo(value)

  acf_values <- dftmp %>%
    select(!! value_expr) %>%
    pull() %>%
    acf(lag.max = tail(lags, 1), plot = FALSE) %>%
    .$acf %>%
    .[,,1]

  ret <- tibble(acf = acf_values) %>%
    rowid_to_column(var = "lag") %>%
    mutate(lag = lag - 1) %>%
    filter(lag %in% lags)

  return(ret)
}

g2 <- as_tibble(dftmp) %>%
  autocorrelate(NetEditRev,lags = 0:nrow(.)) %>%
  ggplot(aes(lag,acf)) +
  geom_point(alpha = 0.5, color = "#2c3e50") +
  expand_limits(y = c(-1,1)) +
  theme_tq() +
  labs(title  = "Autocorrelation For Net Edit Revenue")
#And I get --> Error in autocorrelate(., NetEditRev, lags = 0:nrow(.)) :   unused argument (lags = 0:nrow(.))
#Notice in the error it looks like autocorrelate(., yet there is no ., in my script?

ggplotly(g2)



str(dftmp)

'data.frame':892 obs. of  10 variables:
 $ Date2     : Date, format: "2017-01-01" "2017-01-02" "2017-01-03" "2017-01-04" ...
 $ NetEditRev: num  -923 19222 -8397 37697 46075 ...
 $ index.num : int  1483228800 1483315200 1483401600 1483488000 1483574400 1483660800 1483747200 1483833600 1483920000 1484006400 ...
 $ year      : int  2017 2017 2017 2017 2017 2017 2017 2017 2017 2017 ...
 $ half      : int  1 1 1 1 1 1 1 1 1 1 ...
 $ quarter   : int  1 1 1 1 1 1 1 1 1 1 ...
 $ month.lbl : Ord.factor w/ 12 levels "January"<"February"<..: 1 1 1 1 1 1 1 1 1 1 ...
 $ day       : int  1 2 3 4 5 6 7 8 9 10 ...
 $ wday.lbl  : Ord.factor w/ 7 levels "Sunday"<"Monday"<..: 1 2 3 4 5 6 7 1 2 3 ...
 $ holiday   : num  0 1 0 0 0 0 0 0 0 0 ...

Here is a sample of my df

head(dftmp,n=35)
        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
8  2017-01-08      21.39 1483833600 2017    1       1   January   8    Sunday       0
9  2017-01-09   63569.67 1483920000 2017    1       1   January   9    Monday       0
10 2017-01-10   50777.00 1484006400 2017    1       1   January  10   Tuesday       0
11 2017-01-11   55548.60 1484092800 2017    1       1   January  11 Wednesday       0
12 2017-01-12   70217.73 1484179200 2017    1       1   January  12  Thursday       0
13 2017-01-13   62536.15 1484265600 2017    1       1   January  13    Friday       0
14 2017-01-14    2696.83 1484352000 2017    1       1   January  14  Saturday       0
15 2017-01-15    2230.00 1484438400 2017    1       1   January  15    Sunday       0
16 2017-01-16   56310.90 1484524800 2017    1       1   January  16    Monday       1
17 2017-01-17   58521.93 1484611200 2017    1       1   January  17   Tuesday       0
18 2017-01-18   50057.73 1484697600 2017    1       1   January  18 Wednesday       0
19 2017-01-19   57366.70 1484784000 2017    1       1   January  19  Thursday       0
20 2017-01-20   61390.94 1484870400 2017    1       1   January  20    Friday       0
21 2017-01-21    1839.09 1484956800 2017    1       1   January  21  Saturday       0
22 2017-01-22     807.57 1485043200 2017    1       1   January  22    Sunday       0
23 2017-01-23   72209.44 1485129600 2017    1       1   January  23    Monday       0
24 2017-01-24   70799.21 1485216000 2017    1       1   January  24   Tuesday       0
25 2017-01-25   70240.88 1485302400 2017    1       1   January  25 Wednesday       0
26 2017-01-26   51462.66 1485388800 2017    1       1   January  26  Thursday       0
27 2017-01-27   46852.37 1485475200 2017    1       1   January  27    Friday       0
28 2017-01-28    2396.67 1485561600 2017    1       1   January  28  Saturday       0
29 2017-01-29    2051.62 1485648000 2017    1       1   January  29    Sunday       0
30 2017-01-30   72561.32 1485734400 2017    1       1   January  30    Monday       0
31 2017-01-31   69630.69 1485820800 2017    1       1   January  31   Tuesday       0
32 2017-02-01  -17304.22 1485907200 2017    1       1  February   1 Wednesday       0
33 2017-02-02   11773.84 1485993600 2017    1       1  February   2  Thursday       0
34 2017-02-03   20703.69 1486080000 2017    1       1  February   3    Friday       0
35 2017-02-04    2762.87 1486166400 2017    1       1  February   4  Saturday       0


Confidentiality Notice This message is sent from Zelis. ...{{dropped:13}}



More information about the R-help mailing list