I am trying to define generic methods in one R file and implement them in another R file. But I get an error which I don't understand.
Questions:
Contents of BuyProbabilityGeneric.R
:
setGeneric("FirstTimeBuyProbability",
valueClass = "numeric",
function(object, period, price) {
standardGeneric("FirstTimeBuyProbability")
})
setGeneric("ReturningBuyProbability",
valueClass = "numeric",
function(object, period1, price1, period2, price2) {
standardGeneric("ReturningBuyProbability")
})
Contents of 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 console output:
> 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
try:
setMethod("FirstTimeBuyProbability",
signature(object = "BuyProbabilityBasic",
period = "numeric", price = "numeric"),
function(object, period, price) {
full.price.probability * price^elasticity
})
Notice how I changed t
to period
in your function definition formals. Basically, you were trying to define a function where the formals conflicted with the signature.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With