Skip to content

Bootstrapping: More tests #281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions tests/testthat/test_inputchecks_newcustomer.R
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,21 @@ test_that("newcustomer fits the type of fitted model", {

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

# transactions
expect_error(predict(p.cdnow, newdata=newcustomer(12), prediction.end=12), regexp = "No other parameters")
expect_error(predict(p.cdnow, newdata=newcustomer(12), continuous.discount.factor=0.1), regexp = "No other parameters")
expect_error(predict(p.cdnow, newdata=newcustomer(12), predict.spending=TRUE), regexp = "No other parameters")

# spending
expect_error(predict(gg.cdnow, newdata=newcustomer.spending(), uncertainty="none"), regexp = "No other parameters")
expect_error(predict(gg.cdnow, newdata=newcustomer.spending(), num.boots=12), regexp = "No other parameters")
expect_error(predict(gg.cdnow, newdata=newcustomer.spending(), level=0.8), regexp = "No other parameters")

for(m in list(p.cdnow, gg.cdnow)){
if(is(m, "clv.pnbd")){
nc <- newcustomer(12)

expect_error(predict(m, newdata=nc, prediction.end=12), regexp = "No other parameters")
expect_error(predict(m, newdata=nc, continuous.discount.factor=0.1), regexp = "No other parameters")
expect_error(predict(m, newdata=nc, predict.spending=TRUE), regexp = "No other parameters")
}else{
nc <- newcustomer.spending()
}

expect_error(predict(m, newdata=nc, uncertainty="boots"), regexp = "No other parameters")
expect_error(predict(m, newdata=nc, num.boots=12), regexp = "No other parameters")
expect_error(predict(m, newdata=nc, level=0.8), regexp = "No other parameters")
}
})

test_that("predict vs newcustomer: dyn/static cov data names are not the same as parameters", {
Expand Down
56 changes: 53 additions & 3 deletions tests/testthat/test_runability_bootstrapping.R
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,14 @@ for(clv.fitted in list(
}


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

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

})



# predict(uncertainty=boots) works with various inputs ------------------------------------

test_that("predict(uncertainty=boots) works with predict.spending, newdata, prediction.end", {

p.cdnow <- fit.cdnow(optimx.args = optimx.args.NM)

fn.predict.boots <- function(predict.spending=TRUE, newdata=NULL, prediction.end=NULL){
expect_warning(dt.pred <- predict(
p.cdnow,
verbose=FALSE,
uncertainty='boots',
num.boots=2,
newdata=newdata,
prediction.end=prediction.end,
predict.spending=predict.spending
), regexp = "recommended to run")
return(dt.pred)
}

# predict.spending
fn.predict.boots(predict.spending = TRUE)
fn.predict.boots(predict.spending = FALSE)
fn.predict.boots(predict.spending = gg)
fn.predict.boots(predict.spending = fit.cdnow(model = gg))

# newdata
clv.apparel.nocov <- fct.helper.create.clvdata.apparel.nocov()
dt.pred <- fn.predict.boots(newdata=clv.apparel.nocov)
# really did predict for the apparel dataset and not the cdnow
expect_true(dt.pred[, .N] == nobs(clv.apparel.nocov))

# prediction.end
clv.cdnow.noholdout <- fct.helper.create.clvdata.cdnow(estimation.split = NULL)

# with holdout, no prediction.end is required
fn.predict.boots(prediction.end=NULL)
# with holdout, can also with prediction.end
fn.predict.boots(prediction.end=10)

# without holdout, prediction.end is required
expect_error(
predict(p.cdnow, uncertainty='boots', newdata=clv.cdnow.noholdout),
regexp = "Cannot predict without prediction.end"
)
# without holdout, works if prediction.end is given
fn.predict.boots(newdata=clv.cdnow.noholdout, prediction.end=10)

})
Loading