From 3f6ec09f873d20e96326a001d3c7f00b188b86a5 Mon Sep 17 00:00:00 2001 From: david-cortes Date: Fri, 17 Mar 2023 21:34:38 +0100 Subject: [PATCH] preemtive check and error message for start_with_ALS=TRUE fixes #41 --- R/fit.R | 2 +- R/helpers.R | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/R/fit.R b/R/fit.R index aafcdd5..620f0d7 100644 --- a/R/fit.R +++ b/R/fit.R @@ -1108,7 +1108,7 @@ validate.inputs <- function(model, implicit=FALSE, user_mapping <- character() item_mapping <- character() if (reindex) { - temp <- reindex.data(X, U, I, U_bin, I_bin) + temp <- reindex.data(X, U, I, U_bin, I_bin, (model %in% c("ContentBased", "OMF_explicit")) && start_with_ALS) X <- temp$X U <- temp$U I <- temp$I diff --git a/R/helpers.R b/R/helpers.R index 09108a6..7793f2b 100644 --- a/R/helpers.R +++ b/R/helpers.R @@ -100,7 +100,7 @@ cast.df.to.matrix <- function(df) { return(df) } -reindex.data <- function(X, U=NULL, I=NULL, U_bin=NULL, I_bin=NULL) { +reindex.data <- function(X, U=NULL, I=NULL, U_bin=NULL, I_bin=NULL, disallow_padding_U_I=FALSE) { out <- list( user_mapping = character(), item_mapping = character(), @@ -136,7 +136,11 @@ reindex.data <- function(X, U=NULL, I=NULL, U_bin=NULL, I_bin=NULL) { all_U <- unique(union(users_X, users_U)) if ((!is.null(U) || !is.null(U_bin)) && !NROW(in_X_and_U)) stop("'X' and 'U'/'U_bin' have no IDs in common.") - + if (disallow_padding_U_I) { + if (NROW(in_X_not_in_U)) { + stop("'U' must have all rows from 'X' when passing 'start_with_ALS=TRUE'.") + } + } ### Entries in I items_X <- unique(X[[2L]]) @@ -152,6 +156,11 @@ reindex.data <- function(X, U=NULL, I=NULL, U_bin=NULL, I_bin=NULL) { all_I <- unique(union(items_X, items_I)) if ((!is.null(I) || !is.null(I_bin)) && !NROW(in_X_and_I)) stop("'X' and 'I'/'I_bin' have no IDs in common.") + if (disallow_padding_U_I) { + if (NROW(in_X_not_in_I)) { + stop("'I' must have all columns from 'X' when passing 'start_with_ALS=TRUE'.") + } + } ### Now map and fill