我正在研究营销组合建模,我正在关注这篇文章
https://analyticsartist.wordpress.com/2014/01/31/adstock-rate-deriving-with-analytical-methods/
本文将adstock函数定义如下:
adstock <- function(x, rate=0){
return(as.numeric(filter(x=x, filter=rate, method="recursive")))
}
并进一步使用R中的minpack.lm
包中的nlsm
来计算速率和系数。
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))
但是,该模型仍然失败,并出现以下错误
Error in filter_(.data, .dots = compat_as_lazy_dots(...)) :
argument ".data" is missing, with no default
错误似乎来自adstock函数,但我不确定如何修复它。
我真的希望如果有人能帮助解决这个问题。
提前谢谢你!
发布于 2019-03-20 08:54:02
(这是一个常见的问题,但由于我找不到副本,我将暂时提供答案。)
您在这里看到的错误来自dplyr::filter
,而不是您期望使用的:stats::filter
。在加载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
他们绕过这一点(在将包发布到CRAN时鼓励/强制)是在使用非基本函数时显式。我通常认为stats::
不会受此影响,但dplyr
的使用肯定要求这样做。
因此,在dplyr
附近使用filter
anywhere时,代码的修复方法就是显式
adstock <- function(x, rate=0){
return(as.numeric(stats::filter(x=x, filter=rate, method="recursive")))
}
FWIW,R的名称空间管理和与python的更显式的方法大致等价:
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
https://stackoverflow.com/questions/55251597
复制相似问题