Search code examples
rfunctionnls

filter functions keeps failing with missing data argument


I am working on Marketing Mix Modeling and I am following the article

https://analyticsartist.wordpress.com/2014/01/31/adstock-rate-deriving-with-analytical-methods/

The article defines the adstock function as below :

adstock <- function(x, rate=0){
  return(as.numeric(filter(x=x, filter=rate, method="recursive")))
}

and further uses nlsm from minpack.lm package in R which calculates the rates and the coefficients.

model1 <- nlsLM(Applications~b0 + b1 * adstock(Media1, r1) + b2 * adstock(Media2, r2) +
                  b3 * adstock(Media3, r3) + b4 * adstock(Media4, r4) + b5 * adstock(Media5, r5) +
                  b6 * adstock(Media6, r6) + b7 * adstock(Media7, r7),
                algorithm = "LM",
                start     = c(b0=   1, b1=   1, b2=   1, b3 = 1, b4 = 1, b5 =1, b6= 1, b7= 1, r1=0, r2=0, r3=0, r4=0, r5=0, r6=0, r7=0),
                lower     = c(b0=-Inf, b1=-Inf, b2=-Inf, b3 = -Inf, b4 = -Inf, b5 =-Inf, b6= -Inf, b7= -Inf, r1=0, r2=0, r3=0, r4=0, r5=0, r6=0,     r7=0),
                upper     = c(b0= Inf, b1= Inf, b2= Inf, b3 = Inf, b4 = Inf, b5 =Inf, b6= Inf, b7= Inf, r1=0.5, r2=0.5, r3=0.5, r4=0.5, r5=0.5, r6=0.5, r7=0.5))

However, the model keeps failing with the below error

Error in filter_(.data, .dots = compat_as_lazy_dots(...)) : 
argument ".data" is missing, with no default

It seems that the error is coming the from the adstock function but I am not sure how to fix it.

I am really hoping if someone could please help to get this resolved.

Thanks a lot in advance!!


Solution

  • (This is a common question, but since I cannot find the duplicate, I'll provide an answer for now.)

    The error you're seeing here is from dplyr::filter, not what you expect to be using: stats::filter. You should have seen something like the following at some point when you loaded dplyr:

    library(dplyr)
    # Attaching package: 'dplyr'
    # The following objects are masked from 'package:stats':
    #     filter, lag
    # The following objects are masked from 'package:base':
    #     intersect, setdiff, setequal, union
    

    They way around this (and encouraged/forced when publishing packages to CRAN) is to be explicit when using non-base functions. I would generally have thought that stats:: would be immune from this, but the use of dplyr certainly mandates it.

    So the fix for your code is to simply be explicit when using filter anywhere near dplyr:

    adstock <- function(x, rate=0){
      return(as.numeric(stats::filter(x=x, filter=rate, method="recursive")))
    }
    

    FWIW, R's namespace management and rough equivalency with python's more explicit methods:

    R                          Python
    ----------------------     ----------------------
                               import pkgname         | explicit namespace use
    pkgname::function(...)     pkgname.function(...)  |
    
                               import pkgname as p    | no R equivalent?
                               p.function(...)        |
    
    library(pkgname)           import * from pkgname  | permissive namespace,
    function(...)              function(...)          |   enables masking