Re: [R] Help with another ggplot error

2019-06-13 Thread Bill Poling
UGH!

I stared and stared at this thinking I may have a typo and did not see it!

Thank you very much Sir!

WHP

autocorrelate <- function(data, value, lags = 0.20) { #lags Not Lags! 
Sheesh!

  value_expr <- enquo(value)

  acf_values <- data %>%
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")







From: William Dunlap 
Sent: Thursday, June 13, 2019 10:38 AM
To: Bill Poling 
Cc: r-help (r-help@r-project.org) 
Subject: Re: [R] Help with another ggplot error

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

This means that autocorrelate does not have an argument called lags.  E.g.
   > f <- function(x) x+1
   > f(y=10)
   Error in f(y = 10) : unused argument (y = 10)
It looks like your autocorrelate has an argument called 'Lags', not 'lags'.


Use traceback() after an error
Bill Dunlap
TIBCO Software
wdunlap http://tibco.com


On Thu, Jun 13, 2019 at 7:14 AM Bill Poling <mailto:bill.pol...@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 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)
}

# 

#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-46timeDate_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.6forcats_0.4.0  stringr_1.4.0
  dplyr_0.8.1purrr_0.3.2
[16] readr_1.3.1tidyr_0.8.3tibble_2.1.1 
  ggplot2_3.1.1  tidyverse_1.2.1
[21] quantmod_0.4-14TTR_0.23-4 
PerformanceAnalytics_1.5.2 xts_0.11-2 zoo_1.8-5
[26] lubridate_1.7.4yardstick_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 &l

Re: [R] Help with another ggplot error

2019-06-13 Thread William Dunlap via R-help
> 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(.))

This means that autocorrelate does not have an argument called lags.  E.g.
   > f <- function(x) x+1
   > f(y=10)
   Error in f(y = 10) : unused argument (y = 10)
It looks like your autocorrelate has an argument called 'Lags', not 'lags'.


Use traceback() after an error
Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Thu, Jun 13, 2019 at 7:14 AM Bill Poling  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 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)
> }
> 
> # 
>
> #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.6forcats_0.4.0  stringr_1.4.0
> dplyr_0.8.1purrr_0.3.2
> [16] readr_1.3.1tidyr_0.8.3tibble_2.1.1
>ggplot2_3.1.1  tidyverse_1.2.1
> [21] quantmod_0.4-14TTR_0.23-4
>  PerformanceAnalytics_1.5.2 xts_0.11-2 zoo_1.8-5
> [26] lubridate_1.7.4yardstick_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 ...
>