### Install recommenderlab Source: https://rdrr.io/github/mhahsler/recommenderlab/f/README.Rmd Installs the recommenderlab package. ```R pkg_install(pkg) ``` -------------------------------- ### Install recommenderlab from GitHub Source: https://rdrr.io/github/mhahsler/recommenderlab Use the remotes package to install the latest development version of recommenderlab from the mhahsler repository. ```R install.packages("remotes") remotes::install_github("mhahsler/recommenderlab") ``` -------------------------------- ### Calculate (dis)similarity examples Source: https://rdrr.io/github/mhahsler/recommenderlab/man/dissimilarity.html Examples demonstrating calculation of similarity and dissimilarity between users and items using the MSWeb dataset. ```R data(MSWeb) ## between 5 users dissimilarity(MSWeb[1:5,], method = "jaccard") similarity(MSWeb[1:5,], method = "jaccard") ## between first 3 items dissimilarity(MSWeb[,1:3], method = "jaccard", which = "items") similarity(MSWeb[,1:3], method = "jaccard", which = "items") ## cross-similarity between first 2 users and users 10-20 similarity(MSWeb[1:2,], MSWeb[10:20,], method="jaccard") ``` -------------------------------- ### Create and Use Hybrid Recommenders Source: https://rdrr.io/github/mhahsler/recommenderlab/man/HybridRecommender.html Examples demonstrating direct initialization and the Recommender interface approach for hybrid models. ```R data("MovieLense") MovieLense100 <- MovieLense[rowCounts(MovieLense) >100,] train <- MovieLense100[1:100] test <- MovieLense100[101:103] ## mix popular movies with a random recommendations for diversity and ## rerecommend some movies the user liked. recom <- HybridRecommender( Recommender(train, method = "POPULAR"), Recommender(train, method = "RANDOM"), Recommender(train, method = "RERECOMMEND"), weights = c(.6, .1, .3) ) recom getModel(recom) as(predict(recom, test), "list") ## create a hybrid recommender using the regular Recommender interface. ## This is needed to use hybrid recommenders with evaluate(). recommenders <- list( RANDOM = list(name = "POPULAR", param = NULL), POPULAR = list(name = "RANDOM", param = NULL), RERECOMMEND = list(name = "RERECOMMEND", param = NULL) ) weights <- c(.6, .1, .3) recom <- Recommender(train, method = "HYBRID", parameter = list(recommenders = recommenders, weights = weights)) recom as(predict(recom, test), "list") ``` -------------------------------- ### Example usage of getList and getData.frame Source: https://rdrr.io/github/mhahsler/recommenderlab/man/getList.html Demonstrates extracting data from a subset of the Jester5k dataset. ```R data(Jester5k) getList(Jester5k[1,]) getData.frame(Jester5k[1,]) ``` -------------------------------- ### Sparse Matrix NA Conversion Example Source: https://rdrr.io/github/mhahsler/recommenderlab/man/sparseNAMatrix.html Demonstrates creating a matrix with NAs, converting it to a sparse representation, and performing back-conversion and NA detection. ```R m <- matrix(sample(c(NA,0:5),50, replace=TRUE, prob=c(.5,rep(.5/6,6))), nrow=5, ncol=10, dimnames = list(users=paste('u', 1:5, sep=''), items=paste('i', 1:10, sep=''))) m ## drop all NAs in the representation. Zeros are represented by very small values. sparse <- dropNA(m) sparse ## convert back to matrix dropNA2matrix(sparse) ## Note: be careful with the sparse representation! ## Do not use is.na, but use dropNAis.na(sparse) ``` -------------------------------- ### Test Cross-Validation Setup Source: https://rdrr.io/github/mhahsler/recommenderlab/src/tests/testthat/test-eval.R Sets up an evaluation scheme for cross-validation using a subset of the Jester5k dataset. It then verifies that the combined known and unknown data matrices match the original data matrix after appropriate NA handling. ```R e <- evaluationScheme( Jester5k[1:10, ], method = "cross", k = 2, given = given ) m <- as(e@knownData@data + e@unknownData@data, "matrix") m[m == 0] <- NA expect_equal(as(e@data, "matrix"), m) ``` -------------------------------- ### Create evaluation schemes for ratingMatrix Source: https://rdrr.io/github/mhahsler/recommenderlab/man/evaluationScheme.html Examples demonstrating how to create split and cross-validation schemes using the MSWeb dataset. ```R data("MSWeb") MSWeb10 <- sample(MSWeb[rowCounts(MSWeb) >10,], 50) MSWeb10 ## simple split with 3 items given esSplit <- evaluationScheme(MSWeb10, method="split", train = 0.9, k=1, given=3) esSplit ## 4-fold cross-validation with all-but-1 items for learning. esCross <- evaluationScheme(MSWeb10, method="cross-validation", k=4, given=-1) esCross ``` -------------------------------- ### Analyze Jester5k Dataset Source: https://rdrr.io/github/mhahsler/recommenderlab/man/Jester5k.html Provides examples for analyzing the Jester5k dataset, including counting ratings, summarizing ratings per user, visualizing rating distribution, and identifying the best-rated joke. ```r Jester5k ``` ```r ## number of ratings nratings(Jester5k) ``` ```r ## number of ratings per user summary(rowCounts(Jester5k)) ``` ```r ## rating distribution hist(getRatings(Jester5k), main="Distribution of ratings") ``` ```r ## 'best' joke with highest average rating best <- which.max(colMeans(Jester5k)) cat(JesterJokes[best]) ``` -------------------------------- ### Get list from topNList Source: https://rdrr.io/github/mhahsler/recommenderlab/src/R/topNList.R Retrieves the list of recommended items from a topNList object. Can decode item IDs to labels. ```R setMethod("getList", signature(from = "topNList"), function(from, decode = TRUE, ...) if (decode) lapply(from@items, function(y) from@itemLabels[y]) else from@items) ``` -------------------------------- ### Predict ratings for new users Source: https://rdrr.io/github/mhahsler/recommenderlab/man/predict.html Predicts ratings for new users using a trained recommender model. This example demonstrates how to obtain predicted ratings instead of a top-N list. ```R ## predict ratings for new users pre <- predict(rec, MovieLense100[101:102], type="ratings") pre as(pre, "matrix")[,1:10] ``` -------------------------------- ### Load and Preprocess MovieLens Data Source: https://rdrr.io/github/mhahsler/recommenderlab/src/Work/data/MovieLense/prepare_data_for_arules.R Loads the MovieLens dataset, preprocesses it by adding movie labels and genres, removes duplicate movies, and converts it into a binary item matrix. This is a common setup for recommender system tasks. ```R library("recommenderlab") ## read db (3+ stars is good) data <- read.table("ml-data/u.data", col.names=c("user", "item", "rating", "time")) db <- as(data, "ratingMatrix") ## add movie labels movies <- read.table("ml-data/u.item", sep ="|", quote=""") genres <- read.table("ml-data/u.genre", sep ="|", quote=""") colnames(genres) <- c("genre", "id") colnames(movies) <- c("id", "name", "date", "NA", "URL", as.character(genres$genre)) m <- match(itemLabels(db), movies$id) movies <- movies[m,] ilabels <- movies$name gen <- movies[6:24] ## remove duplicated movies dup <- which(duplicated(ilabels)) # movies$name[dup] ilabels <- ilabels[-dup] db <- db[,-dup] gen <- gen[-dup,] itemLabels(db) <- ilabels itemInfo(db) <- cbind(itemInfo(db), gen) dim(db) rm(movies, ilabels, m, dup) MovieLenseBin <- as(db, "itemMatrix") save(MovieLenseBin, file = "MovieLenseBin.rda") ``` -------------------------------- ### Initialize package environment Source: https://rdrr.io/github/mhahsler/recommenderlab/f/README.Rmd Sets up the package environment and retrieves metadata. ```R pkg <- 'recommenderlab' source("https://raw.githubusercontent.com/mhahsler/pkg_helpers/main/pkg_helpers.R") pkg_title(pkg) ``` -------------------------------- ### GET getList Source: https://rdrr.io/github/mhahsler/recommenderlab/src/R/topNList.R Retrieves the items from a topNList object. ```APIDOC ## GET getList ### Description Extracts the recommendation lists from a topNList object. ### Parameters #### Query Parameters - **from** (topNList) - Required - The topNList object. - **decode** (boolean) - Optional - If TRUE, returns item labels; if FALSE, returns item indices (default: TRUE). ``` -------------------------------- ### Create and Manage Recommender Models Source: https://rdrr.io/github/mhahsler/recommenderlab/man/Recommender.html Demonstrates training a model, inspecting its parameters, and persisting the model to disk. ```R data("MSWeb") MSWeb10 <- sample(MSWeb[rowCounts(MSWeb) >10,], 100) rec <- Recommender(MSWeb10, method = "POPULAR") rec getModel(rec) ## save and read a recommender model saveRDS(rec, file = "rec.rds") rec2 <- readRDS("rec.rds") rec2 unlink("rec.rds") ``` -------------------------------- ### GET getTopNLists Source: https://rdrr.io/github/mhahsler/recommenderlab/src/R/topNList.R Retrieves top-N recommendation lists from a realRatingMatrix object. ```APIDOC ## GET getTopNLists ### Description Generates a topNList object containing the top-N recommendations for each user in a realRatingMatrix. ### Parameters #### Query Parameters - **x** (realRatingMatrix) - Required - The rating matrix to generate recommendations from. - **n** (integer) - Optional - The number of recommendations to return (default: 10). - **randomize** (numeric) - Optional - If provided, randomizes recommendations based on rating values. - **minRating** (numeric) - Optional - Minimum rating threshold for recommendations. ``` -------------------------------- ### Get length of topNList Source: https://rdrr.io/github/mhahsler/recommenderlab/src/R/topNList.R Returns the number of users for whom recommendations are stored in the topNList. ```R setMethod("length", signature(x = "topNList"), function(x) length(x@items)) ``` -------------------------------- ### Get list representation of binaryRatingMatrix Source: https://rdrr.io/github/mhahsler/recommenderlab/src/R/binaryRatingMatrix.R Retrieves the internal data as a list structure. ```R setMethod("getList", signature(from = "binaryRatingMatrix"), function(from, decode = TRUE, ...) { LIST(from@data, decode = decode) }) ``` -------------------------------- ### Initialize Recommender Registry Source: https://rdrr.io/github/mhahsler/recommenderlab/src/R/AAA_registry.R Creates a new registry instance for recommender methods and defines the required fields for method registration. ```R recommenderRegistry <- registry(registry_class="recommender_registry", entry_class="recommender_method") recommenderRegistry$set_field("method", type = "character", is_key = TRUE, index_FUN = match_partial_ignorecase) recommenderRegistry$set_field("dataType", type = "character", is_key = TRUE, index_FUN = match_exact) recommenderRegistry$set_field("fun", type = "function", is_key = FALSE) recommenderRegistry$set_field("description", type = "character", is_key = FALSE) recommenderRegistry$set_field("reference", type = "character", is_key = FALSE) recommenderRegistry$set_field("parameters", type = "list", is_key = FALSE) ``` -------------------------------- ### GET /getModel Source: https://rdrr.io/github/mhahsler/recommenderlab/src/R/Recommender.R Retrieves the internal model details from a trained Recommender object. ```APIDOC ## GET /getModel ### Description Extracts the model information from a Recommender object. ### Method GET ### Endpoint /getModel ### Parameters #### Query Parameters - **x** (Recommender) - Required - The Recommender object to query. ### Response #### Success Response (200) - **model** (list) - The internal model structure. ``` -------------------------------- ### Evaluate Binary Recommender Systems Source: https://rdrr.io/github/mhahsler/recommenderlab/src/tests/testthat/test-eval.R Demonstrates setting up an evaluation scheme for binary data, generating top-N recommendations, and validating prediction accuracy. ```R library("testthat") library("recommenderlab") ## Evaluate top-N list for binary recommender context("Evaluate top-N list for binary recommender") data(MSWeb) MSWeb10 <- sample(MSWeb[rowCounts(MSWeb) > 10,], 50) set.seed(1234) given <- 3 e <- evaluationScheme( MSWeb10, method = "split", train = 0.9, k = 1, given = given ) ## create a user-based CF recommender using training data r <- Recommender(getData(e, "train"), "UBCF") ## create predictions for the test data using known ratings (see given above) p <- predict(r, getData(e, "known"), type = "topNList", n = 10) p check_predictions <- function(e, p, given) { acc <- calcPredictionAccuracy(p, getData(e, "unknown"), given = getData(e, "given"), byUser = TRUE) acc for (i in 1:length(p)) { ground <- as(getData(e, "unknown"), "matrix")[i, ] ground <- factor(ground, levels = c("TRUE", "FALSE")) sum(ground == "TRUE") # given_items given_items <- as(getData(e, "known"), "matrix")[i,] ground[given_items] sum(given_items) g <- getData(e, "given")[i] expect_equal(sum(given_items), unname(g)) if (given > 0) { expect_equal(sum(given_items), given) } # predicted items (given items should have NA) pred <- as.logical(as(p, "matrix")[i, ]) expect_true(all(is.na(pred[given_items]))) pred[is.na(pred)] <- FALSE pred <- factor(pred, levels = c("TRUE", "FALSE")) ground <- ground[!given_items] pred <- pred[!given_items] tbl <- table(ground, pred) tbl comp <- rbind(func = acc[i,][c("TP" , "FP", "FN", "TN", "N")], test = c(as.vector(tbl), sum(tbl))) expect_equal(comp[1, ], comp[2, ]) } } check_predictions(e, p, given) # check evaluate with keepModel = TRUE res <- evaluate(e, "POPULAR", progress = FALSE) avg(res) res <- evaluate(e, list( RANDOM = list(name = "RANDOM", param = NULL), POPULAR = list(name = "POPULAR", param = NULL), UBCF = list(name = "UBCF", param = NULL) ), progress = FALSE) avg(res) res <- evaluate(e, "POPULAR", progress = FALSE, keepModel = TRUE) getModel(res) res <- evaluate( e, list( RANDOM = list(name = "RANDOM", param = NULL), POPULAR = list(name = "POPULAR", param = NULL), UBCF = list(name = "UBCF", param = NULL) ), keepModel = TRUE, progress = FALSE ) getModel(res[[1]]) ## test cross-validation (train should be unkown + known) e <- evaluationScheme( MSWeb10, method = "cross", k = 2, given = given ) expect_equal(as(e@data, "matrix"), as(e@knownData, "matrix") | as(e@unknownData, "matrix")) ## Evaluate all-but-x set.seed(1234) given <- -1 e <- evaluationScheme( MSWeb10, method = "split", train = 0.9, k = 1, given = given ) ## create a user-based CF recommender using training data r <- Recommender(getData(e, "train"), "UBCF") ## create predictions for the test data using known ratings (see given above) p <- predict(r, getData(e, "known"), type = "topNList", n = 10) p check_predictions(e, p, given) ``` -------------------------------- ### Evaluate top-N recommendations on binary data Source: https://rdrr.io/github/mhahsler/recommenderlab/man/evaluate.html Demonstrates evaluating a single algorithm and comparing multiple algorithms including a hybrid model on binary data. ```R ### evaluate top-N list recommendations on a 0-1 data set ## Note: we sample only 100 users to make the example run faster data("MSWeb") MSWeb10 <- sample(MSWeb[rowCounts(MSWeb) >10,], 100) ## create an evaluation scheme (10-fold cross validation, given-3 scheme) es <- evaluationScheme(MSWeb10, method="cross-validation", k=10, given=3) ## run evaluation ev <- evaluate(es, "POPULAR", n=c(1,3,5,10)) ev ## look at the results (the length of the topNList is shown as column n) getResults(ev) ## get a confusion matrices averaged over the 10 folds avg(ev) plot(ev, annotate = TRUE) ## evaluate several algorithms (including a hybrid recommender) with a list algorithms <- list( RANDOM = list(name = "RANDOM", param = NULL), POPULAR = list(name = "POPULAR", param = NULL), HYBRID = list(name = "HYBRID", param = list(recommenders = list( RANDOM = list(name = "RANDOM", param = NULL), POPULAR = list(name = "POPULAR", param = NULL) ) ) ) ) evlist <- evaluate(es, algorithms, n=c(1,3,5,10)) evlist names(evlist) ## select the first results by index evlist[[1]] avg(evlist[[1]]) plot(evlist, legend="topright") ``` -------------------------------- ### Create top-N recommendations for new users Source: https://rdrr.io/github/mhahsler/recommenderlab/man/predict.html Generates top-N recommendations for new users using a trained recommender model. Requires the MovieLense dataset and a trained 'POPULAR' recommender. ```R data("MovieLense") MovieLense100 <- MovieLense[rowCounts(MovieLense) >100,] train <- MovieLense100[1:50] rec <- Recommender(train, method = "POPULAR") rec ## create top-N recommendations for new users pre <- predict(rec, MovieLense100[101:102], n = 10) pre as(pre, "list") ``` -------------------------------- ### Get Normalization Settings from ratingMatrix Source: https://rdrr.io/github/mhahsler/recommenderlab/src/R/ratingMatrix.R Implements the 'getNormalize' method to retrieve the normalization settings (row or column) applied to the ratingMatrix. ```R setMethod("getNormalize", signature(x = "ratingMatrix"), function(x, ...) x@normalize) ``` -------------------------------- ### Define Clustering Parameters and Evaluate Source: https://rdrr.io/github/mhahsler/recommenderlab/src/Work/NBMiner/test_epub.R Sets up a list of parameters for different clustering methods (jaccard, pearson, cosine, etc.) and then evaluates the recommender model using these parameters. Results are saved to an .rda file. ```R if(FALSE) { param <- list( CLUSTER_jaccard_k20 = list(k = 20, method="jaccard"), CLUSTER_jaccard_k10 = list(k = 10, method="jaccard"), CLUSTER_jaccard_k30 = list(k = 30, method="jaccard"), CLUSTER_pearson_k20 = list(k = 20, method="pearson"), CLUSTER_cosine = list(k = 20, method="cosine"), CLUSTER_affinity_k20 = list(k = 20, method="affinity"), CLUSTER_dice_k20 = list(k = 20, method="dice"), CLUSTER_matching_k20 = list(k = 20, method="matching") ) res <- lapply(param, FUN = function(p) evaluate(es, "CLUSTER", N=Ns, parameter=p)) save(res, file="results_epub_CLUSTER.rda") } ``` -------------------------------- ### Get rating list from realRatingMatrix Source: https://rdrr.io/github/mhahsler/recommenderlab/src/R/realRatingMatrix.R Retrieves a list of ratings from a realRatingMatrix. Can optionally decode item IDs to names and exclude ratings. ```R setMethod("getList", signature(from = "realRatingMatrix"), function(from, decode = TRUE, ratings = TRUE, ...) { trip <- as(from, "dgTMatrix") lst <- split(trip@j + 1L, factor(trip@i, levels = 0:(nrow(trip) - 1L)), drop = FALSE) if (decode) lst <- lapply(lst, function(y) colnames(from)[y]) else names(lst) <- NULL if (!ratings) return(lst) rts <- split(trip@x, factor(trip@i, levels = 0:(nrow(trip) - 1L)), drop = FALSE) for (i in 1:length(rts)) { names(rts[[i]]) <- lst[[i]] } rts }) ``` -------------------------------- ### Get Underlying Data from ratingMatrix Source: https://rdrr.io/github/mhahsler/recommenderlab/src/R/ratingMatrix.R Implements the 'getRatingMatrix' method to access the raw rating data stored within the 'ratingMatrix' object. ```R setMethod("getRatingMatrix", signature(x = "ratingMatrix"), function(x, ...) x@data) ``` -------------------------------- ### Initialize and predict with HybridRecommender Source: https://rdrr.io/github/mhahsler/recommenderlab/src/tests/testthat/test-recom.R Creates a hybrid recommender model using four different methods and performs predictions on test datasets. ```R recom <- HybridRecommender( Recommender(train, method = "POPULAR"), Recommender(train, method = "RANDOM"), Recommender(train, method = "AR"), Recommender(train, method = "RERECOMMEND"), ### not implemented for binary data weights = c(.25, .25, .25, .25) ) #recom #getModel(recom) predict(recom, test1) predict(recom, test3) predict(recom, test1, type = "ratings") predict(recom, test3, type = "ratings") ``` -------------------------------- ### SVDF Recommender Initialization Source: https://rdrr.io/github/mhahsler/recommenderlab/src/R/RECOM_SVDF.R Initializes a Funk SVD recommender model with specific hyperparameters for training on realRatingMatrix data. ```APIDOC ## REAL_SVDF(data, parameter = NULL) ### Description Constructs a Recommender object using the Funk SVD algorithm. It supports row normalization and various training parameters. ### Parameters #### Request Body - **data** (realRatingMatrix) - Required - The training dataset. - **parameter** (list) - Optional - Configuration parameters including k, gamma, lambda, min_epochs, max_epochs, min_improvement, normalize, and verbose. ``` -------------------------------- ### Get Recommender Model Source: https://rdrr.io/github/mhahsler/recommenderlab/src/R/Recommender.R Defines the 'getModel' method for the 'Recommender' object. This function extracts the underlying trained model from a recommender object. ```R setMethod("getModel", signature(x = "Recommender"), function(x, ...) x@model) ``` -------------------------------- ### Load Libraries and Data Source: https://rdrr.io/github/mhahsler/recommenderlab/src/tests/testthat/test-recom.R Loads the necessary libraries and the MovieLense dataset for testing recommender algorithms. ```R library("testthat") library("recommenderlab") data("MovieLense") ``` -------------------------------- ### Create Top-N Recommendations Source: https://rdrr.io/github/mhahsler/recommenderlab/man/calcPredictionAccuracy.html Generates a list of top N recommended items for a given dataset. Ensure the recommender model 'r' and evaluation data 'e' are properly set up. ```r p <- predict(r, getData(e, "known"), type="topNList", n = 10) p ``` -------------------------------- ### Get Ratings as Vector from ratingMatrix Source: https://rdrr.io/github/mhahsler/recommenderlab/src/R/ratingMatrix.R Implements the 'getRatings' method to extract the actual rating values from the 'ratingMatrix' as a vector, removing zero values. ```R setMethod("getRatings", signature(x = "ratingMatrix"), function(x, ...) zapzero(as(x, "dgCMatrix")@x)) ``` -------------------------------- ### Get Parameters Function Signature - R Source: https://rdrr.io/github/mhahsler/recommenderlab/man/internal.html Signature for the getParameters function, a helper for checking parameter consistency and providing default values in the Recommender constructor. ```R getParameters(defaults, parameter) ``` -------------------------------- ### Create and Coerce Matrix to binaryRatingMatrix Source: https://rdrr.io/github/mhahsler/recommenderlab/man/binaryRatingMatrix-class.html Demonstrates creating a 0-1 matrix and coercing it into a binaryRatingMatrix object. Also shows coercion back to a matrix to verify. ```R ## create a 0-1 matrix m <- matrix(sample(c(0,1), 50, replace=TRUE), nrow=5, ncol=10, dimnames=list(users=paste("u", 1:5, sep=''), items=paste("i", 1:10, sep=''))) m ## coerce it into a binaryRatingMatrix b <- as(m, "binaryRatingMatrix") b ## coerce it back to see if it worked as(b, "matrix") ``` -------------------------------- ### Test NBMiner Recommender Parameters Source: https://rdrr.io/github/mhahsler/recommenderlab/src/Work/NBMiner/test_epub.R This snippet tests various parameter settings for the NBMiner recommender algorithm, focusing on 'maxlen', 'pi' (probability threshold), and 'trim' values. It also includes an option to plot the results directly from NBMinerParameters. The results are aggregated and saved. ```R if(FALSE) { trim <- 0.02 NBMinerParameters(es$data, trim=trim, plot=TRUE, verb=TRUE) p <- NBMinerParameters(es$data, pi=0.8, theta=0.5, maxlen=2, minlen=2, trim=trim, rules=TRUE) r <- NBMiner(evaluation_data(es), parameter=p) param <- list( NBMiner_maxlen2_pi0.8 = list(NBMiner = NBMinerParameters(es$data, pi=0.8, theta=0.5, maxlen=2, minlen=2, trim=trim, rules=TRUE), verb=TRUE), NBMiner_maxlen3_pi0.8 = list(NBMiner = NBMinerParameters(es$data, pi=0.8, theta=0.5, maxlen=3, minlen=2, trim=trim, rules=TRUE), verb=TRUE), NBMiner_maxlen4_pi0.8 = list(NBMiner = NBMinerParameters(es$data, pi=0.8, theta=0.5, maxlen=4, minlen=2, trim=trim, rules=TRUE), verb=TRUE) ) NBMiner_maxlen2_pi0.5 = list(NBMiner = NBMinerParameters(es$data, pi=0.8, theta=0.5, maxlen=4, minlen=2, trim=trim, rules=TRUE), verb=TRUE), NBMiner_maxlen2_pi0.9 = list(NBMiner = NBMinerParameters(es$data, pi=0.8, theta=0.5, maxlen=4, minlen=2, trim=trim, rules=TRUE), verb=TRUE), NBMiner_maxlen2_pi0.99 = list(NBMiner = NBMinerParameters(es$data, pi=0.8, theta=0.5, maxlen=4, minlen=2, trim=trim, rules=TRUE), verb=TRUE) ) res <- lapply(param, FUN = function(p) evaluate(es, "NBMiner", N=Ns, parameter=p)) res <- evaluation_list(res) save(res, file="results_epub_NBMiner.rda") } ``` -------------------------------- ### Run recommenderlab package tests Source: https://rdrr.io/github/mhahsler/recommenderlab/src/tests/testthat.R Executes the test suite for the recommenderlab package using the testthat framework. ```R library("testthat") library("recommenderlab") test_check("recommenderlab") ``` -------------------------------- ### Manage parameter lists with defaults Source: https://rdrr.io/github/mhahsler/recommenderlab/src/R/AAAparameter.R Use .nodots to warn about unknown arguments and getParameters to merge user-provided parameters with default values. ```R ####################################################################### # Code to check parameter/control objects # Copyright (C) 2011 Michael Hahsler # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ## helper to parse parameter lists with defaults .nodots <- function(...) { l <- list(...) if (length(l) > 0L) warning("Unknown arguments: ", paste(names(l), "=", l, collapse = ", ")) } getParameters <- function(defaults, parameter) { defaults <- as.list(defaults) parameter <- as.list(parameter) ## add verbose if (is.null(defaults$verbose)) defaults$verbose <- FALSE if (length(parameter) != 0) { o <- pmatch(names(parameter), names(defaults)) ## unknown parameter if (any(is.na(o))) { warning(sprintf( ngettext( length(is.na(o)), "Unknown parameter: %s", "Unknown parameters: %s" ), paste(names(parameter)[is.na(o)], collapse = ", ") ), call. = FALSE, immediate. = TRUE) cat("Available parameter (with default values):\n") #print(defaults) cat(rbind(names(defaults), " = ", gsub("\n", " ", as.character(defaults))), sep = c("\t", " ", "\n")) } defaults[o[!is.na(o)]] <- parameter[!is.na(o)] } if (defaults$verbose) { cat("Used parameters:\n") #print(defaults) cat(rbind(names(defaults), " = ", gsub("\n", " ", as.character(defaults))), sep = c("\t", " ", "\n")) } defaults } ``` -------------------------------- ### Calculate Prediction Accuracy for Real Ratings Source: https://rdrr.io/github/mhahsler/recommenderlab/man/calcPredictionAccuracy.html Computes error metrics (RMSE, MSE, MAE) for predicted ratings compared to unknown ratings. Use `byUser = TRUE` to get per-user metrics. ```R data(Jester5k) ## create 90/10 split (known/unknown) for the first 500 users in Jester5k e <- evaluationScheme(Jester5k[1:500, ], method = "split", train = 0.9, k = 1, given = 15) e ## create a user-based CF recommender using training data r <- Recommender(getData(e, "train"), "UBCF") ## create predictions for the test data using known ratings (see given above) p <- predict(r, getData(e, "known"), type = "ratings") p ## compute error metrics averaged per user and then averaged over all ## recommendations calcPredictionAccuracy(p, getData(e, "unknown")) head(calcPredictionAccuracy(p, getData(e, "unknown"), byUser = TRUE)) ``` -------------------------------- ### Use Methods on binaryRatingMatrix Source: https://rdrr.io/github/mhahsler/recommenderlab/man/binaryRatingMatrix-class.html Illustrates using common methods like `dim`, `dimnames`, `rowCounts`, `colCounts`, `image`, `sample`, and subsetting on a binaryRatingMatrix object. ```R ## use some methods defined in ratingMatrix dim(b) dimnames(b) ## counts rowCounts(b) ## number of ratings per user colCounts(b) ## number of ratings per item ## plot image(b) ## sample and subset sample(b,2) b[1:2,1:5] ``` -------------------------------- ### Create binaryRatingMatrix from User/Item Tuples Source: https://rdrr.io/github/mhahsler/recommenderlab/man/binaryRatingMatrix-class.html Demonstrates creating a binaryRatingMatrix from a data frame containing user and item identifiers. Includes coercion back to a matrix for verification. ```R ## creation from user/item tuples df <- data.frame(user=c(1,1,2,2,2,3), items=c(1,4,1,2,3,5)) df b2 <- as(df, "binaryRatingMatrix") b2 as(b2, "matrix") ``` -------------------------------- ### Implement show for evaluationScheme Source: https://rdrr.io/github/mhahsler/recommenderlab/src/R/evaluationScheme.R Provides a formatted summary display of an evaluationScheme object, including configuration details like the number of runs, training proportions, and good rating thresholds. ```R setMethod("show", signature(object = "evaluationScheme"), function(object) { if (length(object@given) == 1) { if (object@given >= 0) writeLines(sprintf("Evaluation scheme with %d items given", object@given)) else writeLines(sprintf( "Evaluation scheme using all-but-%d items", abs(object@given) )) } else{ writeLines(c("Evaluation scheme with multiple items given", "Summary:")) print(summary(object@given)) } writeLines(sprintf("Method: %s with %d run(s).", sQuote(object@method), object@k)) if (!is.na(object@train)) { writeLines(sprintf("Training set proportion: %1.3f", object@train)) } if (!is.na(object@goodRating)) writeLines(sprintf("Good ratings: >=%f", object@goodRating)) else writeLines(sprintf("Good ratings: NA")) writeLines("Data set: ", sep = '') show(object@data) invisible(NULL) }) ``` -------------------------------- ### Get k nearest neighbors Source: https://rdrr.io/github/mhahsler/recommenderlab/src/R/RECOM_UBCF.R Helper function to retrieve the indices of the k nearest neighbors for each row in a similarity matrix. It orders neighbors by similarity in descending order and selects the top k. NA similarities are ignored. ```R .knn <- function(sim, k) lapply( 1:nrow(sim), FUN = function(i) head(order( sim[i,], decreasing = TRUE, na.last = NA ), k) ) ``` -------------------------------- ### POST /Recommender Source: https://rdrr.io/github/mhahsler/recommenderlab/src/R/Recommender.R Creates a new recommender object by finding a registered method and training it on the provided ratingMatrix data. ```APIDOC ## POST /Recommender ### Description Creates a recommender object using a specified method and training data. ### Method POST ### Endpoint /Recommender ### Parameters #### Request Body - **data** (ratingMatrix) - Required - The training data for the recommender. - **method** (string) - Required - The name of the recommender method to use. - **parameter** (list) - Optional - Additional parameters for the recommender method. ### Response #### Success Response (200) - **Recommender** (object) - A valid Recommender object. ``` -------------------------------- ### Initialize HybridRecommender Source: https://rdrr.io/github/mhahsler/recommenderlab/man/HybridRecommender.html Constructor for creating a hybrid recommender object. ```R HybridRecommender(..., weights = NULL, aggregation_type = "sum") ``` -------------------------------- ### Create and Inspect a realRatingMatrix Source: https://rdrr.io/github/mhahsler/recommenderlab/man/realRatingMatrix-class.html Demonstrates creating a matrix with sample ratings, coercing it into a realRatingMatrix, and then inspecting its properties like dimensions, rating counts, and average ratings. ```R ## create a matrix with ratings m <- matrix(sample(c(NA,0:5),100, replace=TRUE, prob=c(.7,rep(.3/6,6))), nrow=10, ncol=10, dimnames = list( user=paste('u', 1:10, sep=''), item=paste('i', 1:10, sep='') )) m ## coerce into a realRatingMAtrix r <- as(m, "realRatingMatrix") r ## get some information dimnames(r) rowCounts(r) ## number of ratings per user colCounts(r) ## number of ratings per item colMeans(r) ## average item rating nratings(r) ## total number of ratings hasRating(r) ## user-item combinations with ratings ``` -------------------------------- ### View First Two Users as List Source: https://rdrr.io/github/mhahsler/recommenderlab/man/MSWeb.html Converts the first two users' data from the MSWeb dataset into a list format for detailed inspection. ```r as(MSWeb[1:2,], "list") ``` -------------------------------- ### Display Recommender Object Source: https://rdrr.io/github/mhahsler/recommenderlab/src/R/Recommender.R Defines the 'show' method for the 'Recommender' object. This method provides a concise summary of the recommender, including its type, data type, and the number of users it was trained on. ```R setMethod("show", signature(object = "Recommender"), function(object) { cat("Recommender of type", sQuote(object@method), "for", sQuote(object@dataType), "\nlearned using", object@ntrain, "users.\n") invisible(NULL) }) ``` -------------------------------- ### Show Method Source: https://rdrr.io/github/mhahsler/recommenderlab/src/R/ratingMatrix.R Method for displaying a summary of the ratingMatrix object. ```APIDOC ## show setMethod("show", signature(object = "ratingMatrix"), function(object) { cat( nrow(object), 'x', ncol(object), "rating matrix of class", sQuote(class(object)), "with", nratings(object), "ratings.\n" ) if (!is.null(object@normalize$row)) cat("Normalized using", object@normalize$row$method, "on rows.\n") if (!is.null(object@normalize$col)) cat("Normalized using", object@normalize$col$method, "on columns.\n") invisible(NULL) }) ``` -------------------------------- ### Evaluate Real-Valued Recommender Systems Source: https://rdrr.io/github/mhahsler/recommenderlab/src/tests/testthat/test-eval.R Demonstrates evaluating recommenders on real-valued ratings, including error metric calculation and top-N list evaluation with specific rating thresholds. ```R # Evaluate recommender for real-valued ratings context("Evaluate real valued recommenders") data(Jester5k) ## create 90/10 split (known/unknown) for the first 500 users in Jester5k e <- evaluationScheme( Jester5k[1:500, ], method = "split", train = 0.9, k = 1, given = 15 ) e ## create a user-based CF recommender using training data r <- Recommender(getData(e, "train"), "UBCF") ## create predictions for the test data using known ratings (see given above) p <- predict(r, getData(e, "known"), type = "ratings") p ## compute error metrics averaged per user and then averaged over all ## recommendations calcPredictionAccuracy(p, getData(e, "unknown")) head(calcPredictionAccuracy(p, getData(e, "unknown"), byUser = TRUE)) ## evaluate topNLists instead (you need to specify given and goodRating!) p <- predict(r, getData(e, "known"), type = "topNList") p calcPredictionAccuracy(p, getData(e, "unknown"), given = 15, goodRating = 5) ``` -------------------------------- ### Prepare Data for UBCF Source: https://rdrr.io/github/mhahsler/recommenderlab/src/tests/testthat/test-RECOM_UBCF.R Initializes a realRatingMatrix from a data frame for use with recommenderlab. Ensure data is in the correct format before proceeding. ```R library("testthat") library("recommenderlab") context("Test UBCF") ### Example from vignette db <- rbind( c(NA, 4, 4, 2, 1, 2, NA, NA), c( 3, NA, NA, NA, 5, 1, NA, NA), c( 3, NA, NA, 3, 2, 2, NA, 3), c( 4, NA, NA, 2, 1, 1, 2, 4), c( 1, 1, NA, NA, NA, NA, NA, 1), c(NA, 1, NA, NA, 1, 1, NA, 1) ) dimnames(db) <- list(paste0("u", 1:6), paste0("i", 1:8)) u_a <- rbind( c(NA, NA, 4, 3, NA, 1, NA, 5) ) dimnames(u_a) <- list("u_a", paste0("i", 1:8)) r_db <- as(db, "realRatingMatrix") r_a <- as(u_a, "realRatingMatrix") ``` -------------------------------- ### Implement Funk SVD Recommender in R Source: https://rdrr.io/github/mhahsler/recommenderlab/src/R/RECOM_SVDF.R Defines the default parameters, the model construction function, and registers the SVDF method in the recommenderlab registry. ```R .REAL_SVDF_param <- list( k = 10, gamma = 0.015, lambda = 0.001, min_epochs = 50, max_epochs = 200, min_improvement = 1e-6, normalize = "center", verbose = FALSE ) REAL_SVDF <- function(data, parameter= NULL) { p <- getParameters(.REAL_SVDF_param, parameter) ### row normalization? if(!is.null(p$normalize) && is(data, "realRatingMatrix")) data <- normalize(data, method=p$normalize) svd <- funkSVD(as(data, "matrix"), k =p$k, gamma = p$gamma, lambda = p$lambda, min_epochs = p$min_epochs, max_epochs= p$max_epochs, min_improvement = p$min_improvement, verbose = p$verbose) model <- c(list( description = "Truncated Funk SVD", svd = svd ), p) predict <- function(model, newdata, n = 10, data=NULL, type=c("topNList", "ratings", "ratingMatrix"), ...) { type <- match.arg(type) n <- as.integer(n) ### newdata are userid if(is.vector(newdata)) { if(is.null(data) || !is(data, "ratingMatrix")) stop("If newdata is a user id then you need to specify data.") newdata <- data[newdata, , drop=FALSE] } if(ncol(newdata) != nrow(model$svd$V)) stop("number of items in newdata does not match model.") if(!is.null(model$normalize) && is(newdata, "realRatingMatrix")) newdata <- normalize(newdata, method=model$normalize) ratings <- predict.funkSVD(model$svd, as(newdata, "matrix")) ratings <- new("realRatingMatrix", data=dropNA(ratings), normalize = getNormalize(newdata)) ratings <- denormalize(ratings) returnRatings(ratings, newdata, type, n) } ## construct recommender object new("Recommender", method = "SVDF", dataType = class(data), ntrain = nrow(data), model = model, predict = predict) } recommenderRegistry$set_entry( method="SVDF", dataType = "realRatingMatrix", fun=REAL_SVDF, description="Recommender based on Funk SVD with gradient descend (https://sifter.org/~simon/journal/20061211.html).", parameters = .REAL_SVDF_param) ``` -------------------------------- ### Load and Parse Transaction Data Source: https://rdrr.io/github/mhahsler/recommenderlab/src/Work/data/MSWeb/prepare_data.R Loads transaction data from a file and parses it into a list of item IDs per transaction. Requires the 'recommenderlab' library. Ensure the 'anonymous-msweb.data' file is in the correct directory. ```R library("recommenderlab") tab <- scan("anonymous-msweb.data", what = "character", sep="\n", quote=""") tab <- sapply(tab, strsplit, ",") ##A,1288,1,"library","/library" ##C,"10017",10017 ##V,1027,1 trans <- list() items <- integer() cust <- NULL t <- 1 id_to_name <- list() for(i in 1:length(tab)) { if(tab[[i]][1] == "C") { if(length(items)>0) { trans[[t]] <- items t <- t+1 } items <- integer() cust <- as.integer(tab[[i]][3]) } if(tab[[i]][1] == "A") { id_to_name[[tab[[i]][2]]]] <- tab[[i]][4] } if(tab[[i]][1] == "V") { items <- c(items, as.integer(tab[[i]][2])) } } ``` -------------------------------- ### Recommender System Generics Source: https://rdrr.io/github/mhahsler/recommenderlab/src/R/AllGenerics.R Core S4 generics for initializing recommender models and evaluating performance. ```APIDOC ## Recommender ### Description Initializes a new recommender model based on the provided data. ### Parameters - **data** (object) - Required - The training data for the recommender. ## evaluate ### Description Evaluates the performance of a recommender model using a specified method. ### Parameters - **x** (object) - Required - The recommender model or evaluation scheme. - **method** (string) - Required - The evaluation method to apply. ``` -------------------------------- ### Inspect Recommender Registry Source: https://rdrr.io/github/mhahsler/recommenderlab/man/Recommender.html Retrieves available recommender methods and their specific configurations from the registry. ```R ## look at registry and a few methods recommenderRegistry$get_entry_names() recommenderRegistry$get_entry("POPULAR", dataType = "binaryRatingMatrix") recommenderRegistry$get_entry("SVD", dataType = "realRatingMatrix") ```