奇怪的错误:方法定义中省略的形式参数不能在签名中

时间:2014-02-25 20:34:33

标签: r s4

我试图在一个R文件中定义泛型方法,并在另一个R文件中实现它们。 但我得到一个我不明白的错误。

问题:

  1. 错误的来源是什么?
  2. 如何修复错误?
  3. 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
    

1 个答案:

答案 0 :(得分:8)

尝试:

setMethod("FirstTimeBuyProbability",
          signature(object = "BuyProbabilityBasic", 
                    period = "numeric", price = "numeric"), 
          function(object, period, price) {
            full.price.probability * price^elasticity
          })

注意我在函数定义形式中如何将t更改为period。基本上,您试图定义一个函数,其中形式与签名冲突。