Skip to content

Commit 3d99493

Browse files
committed
Inputchecks for newcustomer in predict.spending and predict.transactions
1 parent 21e081d commit 3d99493

5 files changed

+49
-21
lines changed

R/f_generics_clvfittedspending.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ setMethod("clv.controlflow.check.newdata", signature(clv.fitted="clv.fitted.spen
5050
# . clv.controlflow.predict.new.customer -----------------------------------------------------------------------
5151
setMethod("clv.controlflow.predict.new.customer", signature(clv.fitted="clv.fitted.spending"), definition = function(clv.fitted, clv.newcustomer){
5252

53+
54+
# Only newcustomer.spending() is allowed
55+
if(!is(newdata, "clv.newcustomer.spending")){
56+
check_err_msg("To predict for new customers, 'newdata' has to be the output of 'newdata.spending()'!")
57+
}
58+
5359
return(drop(clv.model.predict.new.customer(
5460
clv.model = clv.fitted@clv.model,
5561
clv.fitted = clv.fitted,

R/f_generics_clvfittedtransactions.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ setMethod("clv.controlflow.predict.post.process.prediction.table", signature = s
329329
#' @include class_clv_fitted_transactions.R
330330
setMethod("clv.controlflow.predict.new.customer", signature = signature(clv.fitted="clv.fitted.transactions"), definition = function(clv.fitted, clv.newcustomer){
331331

332-
if(!is(clv.newcustomer, "clv.newcustomer.no.cov") | is(clv.newcustomer, "clv.newcustomer.static.cov")){
332+
if(!is(clv.newcustomer, "clv.newcustomer.no.cov") | is(clv.newcustomer, "clv.newcustomer.static.cov") | is(clv.newcustomer, "clv.newcustomer.dynamic.cov")){
333333
check_err_msg("Parameter newdata has to be output from calling `newcustomer()`!")
334334
}
335335

R/f_interface_newcustomer.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ NULL
156156
# considered VIRTUAL and cannot be instantiated. Inheriting from this class,
157157
# allows to define a class `newcustomer.spending` which has no slots and
158158
# otherwise would be considered VIRTUAL.
159+
# This base class additionally is handy to catch any type of newcustomer
160+
# instance when verifying parameters.
159161
# Making this class virtual is not required as having no slots and parent has
160162
# the same effect but better to be explicit.
161163
setClass("clv.newcustomer.base", contains = "VIRTUAL")

R/f_interface_predict_clvfittedspending.R

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,24 @@
6666
predict.clv.fitted.spending <- function(object, newdata=NULL, uncertainty=c("none", "boots"), level=0.9, num.boots=100, verbose=TRUE, ...){
6767

6868
check_err_msg(check_user_data_emptyellipsis(...))
69-
check_err_msg(check_user_data_uncertainty(uncertainty = uncertainty))
70-
# match uncertainty to one of the allowed values
71-
uncertainty <- match.arg(tolower(uncertainty), choices=c("none", "boots"), several.ok=FALSE)
72-
73-
7469

7570
# The usual prediction unless newdata indicates a new customer prediction (ie newdata=newcustomer.spending())
76-
if(is(newdata, "clv.newcustomer.spending")){
71+
# check for base class and not "clv.newcustomer.spending" as users likely will pass other newcustomer objects
72+
if(is(newdata, "clv.newcustomer.base")){
73+
# some type of newcustomer object passed
74+
75+
# No other parameters may be passed (all others must be missing)
76+
if(!all(missing(uncertainty), missing(level), missing(num.boots))){
77+
check_err_msg("No other parameters ('uncertainty', 'level', 'num.boots') may be specified when predicting for new customers!")
78+
}
7779

78-
# TODO: Implement parameter checks
79-
# not other parameters except object and newdata may be given (all others must be missing)
80-
# if(!all(missing(prediction.end), missing(predict.spending), missing(continuous.discount.factor))){
81-
# check_err_msg("Parameters prediction.end, predict.spending and continuous.discount.factor may not be specified when predicting for new customers.")
82-
# }
8380
return(clv.controlflow.predict.new.customer(clv.fitted = object, clv.newcustomer = newdata))
8481
}
8582

86-
83+
# match uncertainty to one of the allowed values. Only after newdata section
84+
# because after match.arg(), missing() is always false
85+
check_err_msg(check_user_data_uncertainty(uncertainty = uncertainty))
86+
uncertainty <- match.arg(tolower(uncertainty), choices=c("none", "boots"), several.ok=FALSE)
8787
return(clv.template.controlflow.predict(clv.fitted=object, verbose=verbose, user.newdata=newdata, uncertainty=uncertainty, num.boots=num.boots, level=level))
8888
}
8989

R/f_interface_predict_clvfittedtransactions.R

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,23 +185,43 @@ predict.clv.fitted.transactions <- function(object, newdata=NULL, prediction.end
185185
continuous.discount.factor=log(1+0.1), uncertainty=c("none", "boots"), level=0.9, num.boots=100, verbose=TRUE, ...){
186186

187187
check_err_msg(check_user_data_emptyellipsis(...))
188-
check_err_msg(check_user_data_uncertainty(uncertainty=uncertainty))
189-
# match uncertainty to one of the allowed values
190-
uncertainty <- match.arg(tolower(uncertainty), choices = c("none", "boots"), several.ok = FALSE)
191188

192189
# The usual prediction unless newdata indicates a new customer prediction (ie newdata=newcustomer())
193-
# the newdata classes for static and dyncov inherit from `clv.newcustomer.no.cov`
194-
if(is(newdata, "clv.newcustomer.no.cov")){
195-
# TODO: Check that is not newcustomer.spending()
190+
if(is(newdata, "clv.newcustomer.base")){
191+
196192
# not other parameters except object and newdata may be given (all others must be missing)
197-
if(!all(missing(prediction.end), missing(predict.spending), missing(continuous.discount.factor))){
198-
check_err_msg("Parameters prediction.end, predict.spending and continuous.discount.factor may not be specified when predicting for new customers.")
193+
if(!all(missing(prediction.end), missing(predict.spending),
194+
missing(continuous.discount.factor), missing(uncertainty),
195+
missing(level), missing(num.boots))){
196+
check_err_msg("No other parameters ('prediction.end', 'predict.spending', 'continuous.discount.factor', uncertainty', 'level', 'num.boots') may be specified when predicting for new customers!")
199197
}
200198

199+
# the newdata classes for static and dyncov inherit from `clv.newcustomer.no.cov`
200+
# but `clv.newcustomer.spending` does not
201+
if(!is(newdata, "clv.newcustomer.no.cov")){
202+
203+
if(is(object, "clv.fitted.transactions.dynamic.cov")){
204+
msg.newcustomer <- "newcustomer.dynamic()"
205+
}else{
206+
if(is(object, "clv.fitted.transactions.static.cov")){
207+
msg.newcustomer <- "newcustomer.static()"
208+
}else{
209+
msg.newcustomer <- "newcustomer()"
210+
}
211+
}
212+
check_err_msg(paste0("To predict for new customers, 'newdata' has to be the output of '",msg.newcustomer,"'!"))
213+
}
201214

202215
return(clv.controlflow.predict.new.customer(clv.fitted = object, clv.newcustomer = newdata))
203216
}
204217

218+
219+
# match uncertainty to one of the allowed values. Only after newdata section
220+
# because after match.arg(), missing() is always false
221+
check_err_msg(check_user_data_uncertainty(uncertainty=uncertainty))
222+
# match uncertainty to one of the allowed values
223+
uncertainty <- match.arg(tolower(uncertainty), choices = c("none", "boots"), several.ok = FALSE)
224+
205225
# If it was not explicitly passed in the call, the spending model should only be applied
206226
# it there is spending data. Otherwise, predict does not work out-of-the-box for
207227
# data object w/o spending

0 commit comments

Comments
 (0)