【发布时间】:2017-12-05 19:28:57
【问题描述】:
我正在使用 approxfun() 函数来获得线性插值。我想编写一个函数,它获取 approxfun() 的结果,然后按我指定的量移动和缩放它。我需要能够像调用任何其他函数一样调用这个新函数。
我的尝试的简化版:
set.seed(42)
x = rnorm(50)
y = rnorm(50, 5, 2)
fhat = approxfun(x, y, rule = 2)
new_function = function(fhat, a, b){
new_fhat <- (function(fhat, a, b) a * fhat() + b)()
return(new_fhat)
}
我希望结果是一样的
2 * fhat(1) + 3
但是当我运行我的函数时
new_function(fhat, a = 2, b = 3)
我收到一条错误消息:
* (function(fhat, a, b) a * fhat() + b)() 中的错误: 缺少参数“a”,没有默认值*
【问题讨论】:
标签: r function functional-programming statistics