0

I am trying to produce a list of graphical objects (grobs), suitable for passing to gridExtra::grid.arrange (or some ggequivalent if that is easier). I have redrafted my example to eliminate multiple problems that were kindly pointed out by @MrFlick and @Ronak Shah. However, I still get the same error message:

Error in .f(.x[[i]], ...) : unused argument (NULL)

(and I don't get a single google hit for "unused argument (NULL)" r | purrr. The closest I find is unused argument (.null = NA).)

Each object is a set of four plots (plotted together) that are output as side effects from the acf or pacf functions, showing the autocorelation and autocovarience between two variables and their various lags. I am trying to write a function that will take a tsibble, a vector of independent variable column names from that tsibble, and the name of a single outcome variable (also a column in the input tsibble) and, using purrr, take the acf or pacf of the pairs of variables defined by the single name with each of the specified columns, convert the plot to a grob, and output a list of the grobs.

Here is my non-working function, acf version:

acf_grobs <- function(dat., mod_nms, outcome){
  depend <-  select(dat., all_of(mod_nms))
  out <- map(depend, ~ grob(acf(x=cbind(.x, y))), y = dat.[[outcome]])
  out
}

And a reproducible example:

library(tidyverse)
library(fpp3)

I know that this loads a lot more packages than needed for this example, but I am loading all these packages for other reasons.

aa  <- 1:4
bb  <- 1:4
cc  <- 1:4
day <- 1:4
tb  <- tibble(aa, bb, cc, day)
tsb <- as_tsibble(tb, index = day)

var_of_interest <- "aa"
mod.nms <- c("bb", "cc")

pacf_grobs(dat. = tsb, mod_nms = mod.nms, outcome = var_of_interest)

I still believe the error message is caused by my failure to pass the single argument outcome to the function argument of purrr::map correctly, though this belief is shaken because I am now passing it a different way, and still getting the same error message.

andrewH
  • 2,281
  • 2
  • 22
  • 32
  • 1
    `tsb <- tsibble(aa, bb, cc, day, index = day)` This line returns an error `Error: Column names \`\`, \`\`, and \`\` must not be duplicated.` – Ronak Shah Jul 01 '20 at 04:40
  • What are you trying to do with `.data[[outcome]]`? There is no `.data` defined anywhere (though it is defined by `dplyr` -- is that how you are trying to use it? Because `.data[["anything"]]` will return NULL there. And then you seem to be passing that to `map()` to a function that only expects one parameter. That's where the unused argument is coming from. For example `map(1:3, function(.) .+2, 4:6)` – MrFlick Jul 01 '20 at 04:57
  • I'm sorry, @Ronak Shah. I accidentally omitted a line. I have no idea why creating a tibble and then converting it with as_tsibble works while doing directly, i.e. tsibble(aa, bb, cc, day, index = day), does not, but it doesn't. – andrewH Jul 02 '20 at 20:05
  • 1
    Thanks, @MrFlick ! I am always amazed by people who can figure out what I am confused about, when I myself am not merely confused, but also confused about what I am confused about. You hit the nail on the head. – andrewH Jul 02 '20 at 20:22
  • Here is what I was trying to do: data[[outcome]] is intended to refer to a column in the data set depend. Somewhere( in the map vignette, I think) it says that what you are iterating over goes before the function, while constant parameters come after. What I want the .data[[*]] function to do is to act as a pronoun, evaluate outcome to its content string, treat the string as a name, & refer back to to the data: depend assigned positionally The select step is superfluous here, but needed to subset my real data. – andrewH Jul 02 '20 at 20:32
  • The function does a redundant acf of outcome = aa with itself, because I did not know a reliable way to get map's post-function parameter to refer back to .dat, here tsb. If I did, I would drop aa from depend and just have it be outcome. – andrewH Jul 02 '20 at 20:36
  • I figured that the parameter could not find .dat because of map's nonstandard evaluation. – andrewH Jul 02 '20 at 20:40
  • You haven't really changed the line which was causing error `tsb <- tsibble(aa, bb, cc, day, index = day)` is still there and gives the same error. You are calling `pacf_grobs` function whereas what you have defined is `acf_grobs`. Is that a typo? – Ronak Shah Jul 02 '20 at 23:59
  • @Ronak Shah It works if you do tibble (without index = day) first, followed by as_tsibble with index = day second. For me at least. – andrewH Jul 03 '20 at 19:54
  • you are right about the acf/pacf mixup. I have two of these functions, one with each. I fixed it above to be consistent. – andrewH Jul 04 '20 at 06:13

0 Answers0