Skip to content

Commit a327faf

Browse files
authored
Bootstrapping: More tests (#281)
* Inputchecks: Reject bootstrapping params during newcustomer prediction * predict(boots) runs when other params are given
1 parent b4e1ce8 commit a327faf

File tree

2 files changed

+68
-13
lines changed

2 files changed

+68
-13
lines changed

tests/testthat/test_inputchecks_newcustomer.R

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -183,16 +183,21 @@ test_that("newcustomer fits the type of fitted model", {
183183

184184
test_that("predict(): Error if other parameters are passed (spending & transactions)", {
185185

186-
# transactions
187-
expect_error(predict(p.cdnow, newdata=newcustomer(12), prediction.end=12), regexp = "No other parameters")
188-
expect_error(predict(p.cdnow, newdata=newcustomer(12), continuous.discount.factor=0.1), regexp = "No other parameters")
189-
expect_error(predict(p.cdnow, newdata=newcustomer(12), predict.spending=TRUE), regexp = "No other parameters")
190-
191-
# spending
192-
expect_error(predict(gg.cdnow, newdata=newcustomer.spending(), uncertainty="none"), regexp = "No other parameters")
193-
expect_error(predict(gg.cdnow, newdata=newcustomer.spending(), num.boots=12), regexp = "No other parameters")
194-
expect_error(predict(gg.cdnow, newdata=newcustomer.spending(), level=0.8), regexp = "No other parameters")
195-
186+
for(m in list(p.cdnow, gg.cdnow)){
187+
if(is(m, "clv.pnbd")){
188+
nc <- newcustomer(12)
189+
190+
expect_error(predict(m, newdata=nc, prediction.end=12), regexp = "No other parameters")
191+
expect_error(predict(m, newdata=nc, continuous.discount.factor=0.1), regexp = "No other parameters")
192+
expect_error(predict(m, newdata=nc, predict.spending=TRUE), regexp = "No other parameters")
193+
}else{
194+
nc <- newcustomer.spending()
195+
}
196+
197+
expect_error(predict(m, newdata=nc, uncertainty="boots"), regexp = "No other parameters")
198+
expect_error(predict(m, newdata=nc, num.boots=12), regexp = "No other parameters")
199+
expect_error(predict(m, newdata=nc, level=0.8), regexp = "No other parameters")
200+
}
196201
})
197202

198203
test_that("predict vs newcustomer: dyn/static cov data names are not the same as parameters", {

tests/testthat/test_runability_bootstrapping.R

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,14 +262,14 @@ for(clv.fitted in list(
262262
}
263263

264264

265-
# predict(boots) works on all model specifications -----------------------------
266-
# This also includes testing clv.bootstrapped.apply because it is used under the hood
265+
# predict(uncertainty=boots) works on all model specifications -----------------------------
266+
# This also includes testing `clv.bootstrapped.apply` because it is used under the hood
267267
# - fit with correlation
268268
# - constrained params
269269
# - regularization
270270
# - combinations
271271

272-
test_that("predict(boots) works on all model specifications", {
272+
test_that("predict(uncertainty=boots) works on all model specifications", {
273273
fn.predict.boots <- function(clv.fitted){
274274
expect_warning(predict(clv.fitted, uncertainty='boots', num.boots=2, predict.spending=TRUE, verbose=FALSE), regexp = 'recommended to run')
275275
}
@@ -307,3 +307,53 @@ test_that("predict(boots) works on all model specifications", {
307307

308308
})
309309

310+
311+
312+
# predict(uncertainty=boots) works with various inputs ------------------------------------
313+
314+
test_that("predict(uncertainty=boots) works with predict.spending, newdata, prediction.end", {
315+
316+
p.cdnow <- fit.cdnow(optimx.args = optimx.args.NM)
317+
318+
fn.predict.boots <- function(predict.spending=TRUE, newdata=NULL, prediction.end=NULL){
319+
expect_warning(dt.pred <- predict(
320+
p.cdnow,
321+
verbose=FALSE,
322+
uncertainty='boots',
323+
num.boots=2,
324+
newdata=newdata,
325+
prediction.end=prediction.end,
326+
predict.spending=predict.spending
327+
), regexp = "recommended to run")
328+
return(dt.pred)
329+
}
330+
331+
# predict.spending
332+
fn.predict.boots(predict.spending = TRUE)
333+
fn.predict.boots(predict.spending = FALSE)
334+
fn.predict.boots(predict.spending = gg)
335+
fn.predict.boots(predict.spending = fit.cdnow(model = gg))
336+
337+
# newdata
338+
clv.apparel.nocov <- fct.helper.create.clvdata.apparel.nocov()
339+
dt.pred <- fn.predict.boots(newdata=clv.apparel.nocov)
340+
# really did predict for the apparel dataset and not the cdnow
341+
expect_true(dt.pred[, .N] == nobs(clv.apparel.nocov))
342+
343+
# prediction.end
344+
clv.cdnow.noholdout <- fct.helper.create.clvdata.cdnow(estimation.split = NULL)
345+
346+
# with holdout, no prediction.end is required
347+
fn.predict.boots(prediction.end=NULL)
348+
# with holdout, can also with prediction.end
349+
fn.predict.boots(prediction.end=10)
350+
351+
# without holdout, prediction.end is required
352+
expect_error(
353+
predict(p.cdnow, uncertainty='boots', newdata=clv.cdnow.noholdout),
354+
regexp = "Cannot predict without prediction.end"
355+
)
356+
# without holdout, works if prediction.end is given
357+
fn.predict.boots(newdata=clv.cdnow.noholdout, prediction.end=10)
358+
359+
})

0 commit comments

Comments
 (0)