我试图在一个R文件中定义泛型方法,并在另一个R文件中实现它们。 但我得到一个我不明白的错误。
问题:
BuyProbabilityGeneric.R
的内容:
setGeneric("FirstTimeBuyProbability",
valueClass = "numeric",
function(object, period, price) {
standardGeneric("FirstTimeBuyProbability")
})
setGeneric("ReturningBuyProbability",
valueClass = "numeric",
function(object, period1, price1, period2, price2) {
standardGeneric("ReturningBuyProbability")
})
BuyProbabilityBasic.R
的内容:
setClass("BuyProbabilityBasic", representation(
full.price.probability = "numeric",
elasticity = "numeric"))
setMethod("FirstTimeBuyProbability",
signature(object = "BuyProbabilityBasic",
period = "numeric", price = "numeric"),
function(object, t, price) {
full.price.probability * price^elasticity
})
setMethod("ReturningBuyProbability",
signature(object = "BuyProbabilityBasic",
period1 = "numeric", price1 = "numeric",
period2 = "numeric", price2 = "numeric"),
function(object, period1, price1, period2, price2) {
max(0, FirstTimeBuyProbability(object, period2, price2) -
FirstTimeBuyProbability(object, period1, price1))
})
R控制台输出:
> source('BuyProbabilityGeneric.R')
> source('BuyProbabilityBasic.R')
Error in conformMethod(signature, mnames, fnames, f, fdef, definition) :
in method for ‘FirstTimeBuyProbability’ with signature
‘object="BuyProbabilityBasic",period="numeric",price="numeric"’:
formal arguments (period = "numeric") omitted in the method definition
cannot be in the signature
答案 0 :(得分:8)
尝试:
setMethod("FirstTimeBuyProbability",
signature(object = "BuyProbabilityBasic",
period = "numeric", price = "numeric"),
function(object, period, price) {
full.price.probability * price^elasticity
})
注意我在函数定义形式中如何将t
更改为period
。基本上,您试图定义一个函数,其中形式与签名冲突。