From 1b5e70091851261739757c472bacaeedca324d76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Javier=20Luna=20V=C3=A1zquez?= Date: Sun, 21 Oct 2018 12:25:52 -0500 Subject: [PATCH 1/3] include position of prediction in output --- R/IBCF.R | 250 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 126 insertions(+), 124 deletions(-) diff --git a/R/IBCF.R b/R/IBCF.R index 561fcc0..521757a 100644 --- a/R/IBCF.R +++ b/R/IBCF.R @@ -1,124 +1,126 @@ -#' @title IBCF -#' @description Item Based Collaborative Filtering for multi-trait and multi-environment data. -#' -#' @param object \code{list} CrossValidation object, is obtained from CV.RandomPartition function. -#' @param dec \code{integer} Number of decimals to print in the results. -#' -#' @return A list with the next components -#' \item{NPartitions}{\code{integer} Number of partitions used for testing data} -#' \item{predictions_Summary}{\code{data.frame} A data.frame with the results of the test} -#' \item{Predictions}{\code{list} A list with the predicted results for each partition} -#' -#' @export -#' -#' @importFrom stats cor na.omit sd -#' @examples -#' \dontrun{ -#' library(IBCF.MTME) -#' data('Wheat_IBCF') -#' -#' CV <- CV.RandomPart(Wheat_IBCF) -#' IBCF(CV) -#' } -#' -IBCF <- function(object, dec = 4) { - if (!inherits(object, 'CrossValidation')) stop("This function only works for objects of class 'CrossValidation'") - time.init <- proc.time()[3] - nIL <- ncol(object$DataSet) - 1 - - Y_avr <- matrix(0, ncol = nIL, nrow = nrow(object$DataSet)) - NPartitions <- length(object$CrossValidation_list) - - predicted <- vector('list', NPartitions) - names(predicted) <- paste0('Partition', 1:NPartitions) - - results <- data.frame() - for (j in seq_len(NPartitions)) { - Part <- object$CrossValidation_list[[j]] - - pos.NA <- which(Part == 2, arr.ind = T) - pos.NA[, 2] <- c(pos.NA[, 2]) + 1 - pos.No_NA <- which(Part == 1, arr.ind = T) - # pos.No_NA[, 2] <- c(pos.No_NA[, 2]) - - if (length(pos.NA) == 0) { - stop('An error ocurred with the CrossValidation data') - } - - Data.trn <- object$DataSet - - Data.trn[pos.NA] <- NA - - rows.Na <- which(apply(Data.trn, 1, function(x) any(is.na(x))) == TRUE) - - Means_trn <- apply(Data.trn[, -c(1)], 2, mean, na.rm = T) - SDs_trn <- apply(Data.trn[, -c(1)], 2, sd, na.rm = T) - - Scaled_Col <- scale(Data.trn[, -c(1)]) - - Means_trn_Row <- apply(Scaled_Col, 1, mean, na.rm = T) - SDs_trn_Row <- apply(Scaled_Col, 1, sd, na.rm = T) - - if (any(is.na(SDs_trn_Row))) { - Data.trn_scaled <- data.frame(ID = as.character(Data.trn[, c(1)]), Scaled_Col) - } else { - Scaled_Row <- t(scale(t(Scaled_Col))) - Data.trn_scaled <- data.frame(ID = as.character(Data.trn[, c(1)]), Scaled_Row) - } - - Hybrids.New <- Data.trn_scaled - Hybrids.New[, 2:ncol(Data.trn_scaled)] <- NA - - ratings <- Data.trn_scaled - - x <- ratings[, 2:(ncol(ratings))] - - x[is.na(x)] <- 0 - - item_sim <- lsa::cosine(as.matrix((x))) - - for (i in seq_len(length(rows.Na))) { - pos <- rows.Na[i] - ratings[pos, 2:ncol(ratings)] <- rec_itm_for_geno(pos, item_sim, ratings[,2:ncol(ratings)]) - } - - All.Pred <- data.matrix(ratings[,-1]) - - - if (any(is.na(SDs_trn_Row))) { - All.Pred_O <- sapply(1:ncol(All.Pred), function(i) (All.Pred[,i]*SDs_trn[i] + Means_trn[i])) - } else { - All.Pred_O_Row <- t(sapply(1:nrow(All.Pred), function(i) (All.Pred[i,]*SDs_trn_Row[i] + Means_trn_Row[i])) ) - All.Pred_O <- sapply(1:ncol(All.Pred_O_Row), function(i) (All.Pred_O_Row[,i]*SDs_trn[i] + Means_trn[i])) - } - - colnames(All.Pred_O) <- colnames(Data.trn_scaled[,-c(1)]) - All.Pred_O[pos.No_NA] <- NA - All.Pred_O_tst <- All.Pred_O[rows.Na, ] - - predicted[[paste0('Partition', j)]] <- c(All.Pred_O) - pos.No_NA[,2] <- pos.No_NA[,2] + 1 - DataSet_tst <- object$DataSet - DataSet_tst[pos.No_NA] <- NA - DataSet_tst <- DataSet_tst[rows.Na, ] - - Data.Obs_tst <- getTidyForm(DataSet_tst) - posTST <- which(complete.cases(Data.Obs_tst) == TRUE) - results <- rbind(results, data.frame(Partition = j, - Environment = Data.Obs_tst$Env[posTST], - Trait = Data.Obs_tst$Trait[posTST], - Observed = round(Data.Obs_tst$Response[posTST],dec), - Predicted = round(c(All.Pred_O_tst[which(!is.na(All.Pred_O_tst))]), dec))) - } - Yhat_Obs_pred <- data.frame(object$DataSet, Y_avr) - - out <- list(NPartitions = NPartitions, - predictions_Summary = results, - observed = getTidyForm(object$DataSet)$Response, - yHat = Y_avr, - predicted_Partition = predicted, - Data.Obs_Pred = Yhat_Obs_pred, - executionTime = proc.time()[3] - time.init) - class(out) <- 'IBCF' - return(out) -} +#' @title IBCF +#' @description Item Based Collaborative Filtering for multi-trait and multi-environment data. +#' +#' @param object \code{list} CrossValidation object, is obtained from CV.RandomPartition function. +#' @param dec \code{integer} Number of decimals to print in the results. +#' +#' @return A list with the next components +#' \item{NPartitions}{\code{integer} Number of partitions used for testing data} +#' \item{predictions_Summary}{\code{data.frame} A data.frame with the results of the test} +#' \item{Predictions}{\code{list} A list with the predicted results for each partition} +#' +#' @export +#' +#' @importFrom stats cor na.omit sd +#' @examples +#' \dontrun{ +#' library(IBCF.MTME) +#' data('Wheat_IBCF') +#' +#' CV <- CV.RandomPart(Wheat_IBCF) +#' IBCF(CV) +#' } +#' +IBCF <- function(object, dec = 4) { + if (!inherits(object, 'CrossValidation')) stop("This function only works for objects of class 'CrossValidation'") + time.init <- proc.time()[3] + nIL <- ncol(object$DataSet) - 1 + + Y_avr <- matrix(0, ncol = nIL, nrow = nrow(object$DataSet)) + NPartitions <- length(object$CrossValidation_list) + + predicted <- vector('list', NPartitions) + names(predicted) <- paste0('Partition', 1:NPartitions) + + results <- data.frame() + for (j in seq_len(NPartitions)) { + Part <- object$CrossValidation_list[[j]] + + pos.NA <- which(Part == 2, arr.ind = T) + pos.NA[, 2] <- c(pos.NA[, 2]) + 1 + pos.No_NA <- which(Part == 1, arr.ind = T) + # pos.No_NA[, 2] <- c(pos.No_NA[, 2]) + + if (length(pos.NA) == 0) { + stop('An error ocurred with the CrossValidation data') + } + + Data.trn <- object$DataSet + + Data.trn[pos.NA] <- NA + + rows.Na <- which(apply(Data.trn, 1, function(x) any(is.na(x))) == TRUE) + + Means_trn <- apply(Data.trn[, -c(1)], 2, mean, na.rm = T) + SDs_trn <- apply(Data.trn[, -c(1)], 2, sd, na.rm = T) + + Scaled_Col <- scale(Data.trn[, -c(1)]) + + Means_trn_Row <- apply(Scaled_Col, 1, mean, na.rm = T) + SDs_trn_Row <- apply(Scaled_Col, 1, sd, na.rm = T) + + if (any(is.na(SDs_trn_Row))) { + Data.trn_scaled <- data.frame(ID = as.character(Data.trn[, c(1)]), Scaled_Col) + } else { + Scaled_Row <- t(scale(t(Scaled_Col))) + Data.trn_scaled <- data.frame(ID = as.character(Data.trn[, c(1)]), Scaled_Row) + } + + Hybrids.New <- Data.trn_scaled + Hybrids.New[, 2:ncol(Data.trn_scaled)] <- NA + + ratings <- Data.trn_scaled + + x <- ratings[, 2:(ncol(ratings))] + + x[is.na(x)] <- 0 + + item_sim <- lsa::cosine(as.matrix((x))) + + for (i in seq_len(length(rows.Na))) { + pos <- rows.Na[i] + ratings[pos, 2:ncol(ratings)] <- rec_itm_for_geno(pos, item_sim, ratings[,2:ncol(ratings)]) + } + + All.Pred <- data.matrix(ratings[,-1]) + + + if (any(is.na(SDs_trn_Row))) { + All.Pred_O <- sapply(1:ncol(All.Pred), function(i) (All.Pred[,i]*SDs_trn[i] + Means_trn[i])) + } else { + All.Pred_O_Row <- t(sapply(1:nrow(All.Pred), function(i) (All.Pred[i,]*SDs_trn_Row[i] + Means_trn_Row[i])) ) + All.Pred_O <- sapply(1:ncol(All.Pred_O_Row), function(i) (All.Pred_O_Row[,i]*SDs_trn[i] + Means_trn[i])) + } + + colnames(All.Pred_O) <- colnames(Data.trn_scaled[,-c(1)]) + All.Pred_O[pos.No_NA] <- NA + All.Pred_O_tst <- All.Pred_O[rows.Na, ] + + predicted[[paste0('Partition', j)]] <- c(All.Pred_O) + pos.No_NA[,2] <- pos.No_NA[,2] + 1 + DataSet_tst <- object$DataSet + DataSet_tst[pos.No_NA] <- NA + DataSet_tst <- DataSet_tst[rows.Na, ] + + Data.Obs_tst <- getTidyForm(DataSet_tst) + posTST <- which(complete.cases(Data.Obs_tst) == TRUE) + + results <- rbind(results, data.frame(Position = posTST, + Partition = j, + Environment = Data.Obs_tst$Env[posTST], + Trait = Data.Obs_tst$Trait[posTST], + Observed = round(Data.Obs_tst$Response[posTST],dec), + Predicted = round(c(All.Pred_O_tst[which(!is.na(All.Pred_O_tst))]), dec))) + } + Yhat_Obs_pred <- data.frame(object$DataSet, Y_avr) + + out <- list(NPartitions = NPartitions, + predictions_Summary = results, + observed = getTidyForm(object$DataSet)$Response, + yHat = Y_avr, + predicted_Partition = predicted, + Data.Obs_Pred = Yhat_Obs_pred, + executionTime = proc.time()[3] - time.init) + class(out) <- 'IBCF' + return(out) +} From 414f8f9383a1fc44edfecbdbe1b65f08dc1cc50f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Javier=20Luna=20V=C3=A1zquez?= Date: Sun, 21 Oct 2018 12:32:22 -0500 Subject: [PATCH 2/3] update cran comments --- cran-comments.md | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/cran-comments.md b/cran-comments.md index 66d0291..b69bd08 100644 --- a/cran-comments.md +++ b/cran-comments.md @@ -1,21 +1,10 @@ -## Test environments -* Linux - Manjaro R-base [3.4.3] -* Windows 10 R [3.4.3] -* win-builder (devel and release) - -## R CMD check results -There were no ERRORs or WARNINGs. - -There was 1 NOTE: - -* checking CRAN incoming feasibility ... NOTE -Maintainer: 'Francisco Javier Luna-Vazquez ' - -## Special Comments - -The last version have a several issue with the predictions, this new version has already corrected that detail. -Sorry for this detail. - -## Last round comments -For your next version: -Please do not capitalize "Item" in your description. [Solved] \ No newline at end of file +## Test environments +* Linux - Manjaro R-base [3.5.1] +* Windows 10 R [3.5.1] +* win-builder (devel and release) + +## R CMD check results + +0 errors | 0 warnings | 0 notes + +R CMD check succeeded From cc12cd6643bf587d0910870a4742d42184c7d640 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Javier=20Luna=20V=C3=A1zquez?= Date: Sun, 21 Oct 2018 12:32:58 -0500 Subject: [PATCH 3/3] update functions --- R/methods.R | 364 ++++++++++++++++++++++++++-------------------------- 1 file changed, 182 insertions(+), 182 deletions(-) diff --git a/R/methods.R b/R/methods.R index 7c65cab..b237e83 100644 --- a/R/methods.R +++ b/R/methods.R @@ -1,183 +1,183 @@ -#' @title Summary -#' -#' @description Summary of IBCF object -#' -#' @param object \code{IBCF object} IBCF object, result of use the IBCF() function -#' @param information \code{string} ... -#' @param digits \code{numeric} ... -#' @param ... Further arguments passed to or from other methods. -#' -#' @importFrom stats cor -#' @importFrom dplyr summarise group_by select '%>%' mutate_if funs -#' -#' @export -summary.IBCF <- function(object, information = 'compact', digits = 4, ...){ - if (!inherits(object, "IBCF")) stop("This function only works for objects of class 'IBCF'") - - object$predictions_Summary %>% - group_by(Environment, Trait, Partition) %>% - summarise(Pearson = cor(Predicted, Observed, use = 'pairwise.complete.obs'), - MAAPE = mean(atan(abs(Observed-Predicted)/abs(Observed)))) %>% - select(Environment, Trait, Partition, Pearson, MAAPE) %>% - mutate_if(is.numeric, funs(round(., digits))) %>% - as.data.frame() -> presum - - presum %>% group_by(Environment, Trait) %>% - summarise(SE_MAAPE = sd(MAAPE, na.rm = T)/sqrt(n()), MAAPE = mean(MAAPE, na.rm = T), - SE_Pearson = sd(Pearson, na.rm = T)/sqrt(n()), Pearson = mean(Pearson, na.rm = T)) %>% - select(Environment, Trait, Pearson, SE_Pearson, MAAPE, SE_MAAPE) %>% - mutate_if(is.numeric, funs(round(., digits))) %>% - as.data.frame() -> finalSum - - out <- switch(information, - compact = finalSum, - complete = presum, - extended = { - finalSum$Partition <- 'All' - presum$Partition <- as.character(presum$Partition) - presum$SE_Pearson <- NA - presum$SE_MAAPE <- NA - rbind(presum, finalSum) - } - ) - return(out) -} - -#' @title Summary -#' -#' @description Summary of IBCFY object -#' -#' @param object \code{IBCFY object} IBCFY object, result of use the IBCF.Years() function -#' @param digits \code{numeric} Number of digits of the output. -#' @param ... Further arguments passed to or from other methods. -#' -#' @importFrom stats cor -#' @importFrom dplyr summarise group_by select '%>%' mutate_if funs -#' -#' @export -summary.IBCFY <- function(object, digits = 4, ...) { - if (!inherits(object, "IBCFY")) stop("This function only works for objects of class 'IBCFY'") - - object$predictions_Summary %>% - group_by(Environment, Trait) %>% - summarise(Pearson = cor(Predicted, Observed, use = 'pairwise.complete.obs'), - MAAPE = mean(atan(abs(Observed-Predicted)/abs(Observed)))) %>% - select(Environment, Trait, Pearson, MAAPE) %>% - mutate_if(is.numeric, funs(round(., digits))) %>% - as.data.frame() -> out - - return(out) -} - -#' @title Plot IBCF graph -#' -#' @description Plot from IBCF object -#' -#' @param x \code{IBCF object} IBCF object, result of use the IBCF() function -#' @param select \code{character} By default ('Pearson'), plot the Pearson Correlations of the IBCF Object, else ('MAAPE'), plot the MAAPE of the IBCF Object. -#' @param ... Further arguments passed to or from other methods. -#' -#' @importFrom graphics arrows axis plot -#' @export -plot.IBCF <- function(x, select = 'Pearson', ...){ - ### Check that object is compatible - if (!inherits(x, "IBCF")) stop("This function only works for objects of class 'IBCF'") - - results <- summary(x) - results[, select] <- results[order(results[, select]), select] - - if (select == "Pearson") { - results$SE <- results$SE_Pearson * 1.96 - ylab <- "Pearson's Correlation" - } else if (select == "MAAPE") { - results$SE <- results$SE_MAAPE * 1.96 - ylab <- select - } - - x.labels <- paste0(results$Trait, '_', results$Env) - plot.x <- 1:length(x.labels) - - plot(plot.x, results[, select], ylim = range(c(results[, select] - results$SE, results[, select] + results$SE)), - type = 'p', ylab = ylab, xlab = '', xaxt = "n", ...) - axis(1, at = plot.x, labels = x.labels, las = 2) - arrows(plot.x, results[, select] - results$SE, plot.x, results[, select] + results$SE, code = 3, length = 0.02, angle = 90) -} - - -#' @title barplot.IBCFY -#' -#' @description Barplot of the results from IBCFY object -#' -#' @param height \code{IBCFY object} IBCFY object, result of use the IBCF.Years() function -#' @param select \code{character} By default ('Pearson'), plot the Pearson Correlations of the IBCF Object, else ('MAAPE'), plot the MAAPE of the IBCF Object. -#' @param ... Further arguments passed to or from other methods. -#' -#' @importFrom graphics barplot -#' @export -barplot.IBCFY <- function(height, select = 'Pearson', ...){ - ### Check that object is compatible - if (!inherits(height, "IBCFY")) stop("This function only works for objects of class 'IBCF'") - - results <- summary(height) - vector <- as.numeric(paste(results[, select])) - names(vector) <- results[, 1] - vector <- vector[order(vector)] - - if (select == 'Pearson') - select <- 'Pearson Correlation' - else - select <- 'MAAPE' - - barplot(vector, ylab = select, ...) -} - -#' Print IBCF information object -#' -#' @param x object a -#' @param ... more objects -#' -#' @return test -#' @importFrom utils head -#' @export -#' -print.IBCF <- function(x, ...){ - cat('Item Based Collaborative Filtering Model: \n', - 'Fitted with ', x$NPartitions, ' random partitions\n', - 'Runtime: ', x$executionTime ,' seconds \n\n', - 'Some predicted values: \n') - - print.default(format(head(x$predictions_Summary$Predicted, 20), digits = 3), print.gap = 2L, quote = FALSE) - - cat('\nPredictive capacity of the model: \n') - - print.data.frame(summary(x, 'compact', digits = 3), print.gap = 2L, quote = FALSE) - - cat('\n Use str() function to found more datailed information.') - invisible(x) -} - -#' Print IBCFY information object -#' -#' @param x object a -#' @param ... more objects -#' -#' @return test -#' @importFrom utils head -#' @export -#' -print.IBCFY <- function(x, ...){ - cat('Item Based Collaborative Filtering Model: \n', - 'Evaluated Environment/Year (s): ', x$Years.testing, '\n', - 'Evaluated Trait (s): ', x$Traits.testing, '\n', - 'Runtime: ', x$executionTime ,' seconds \n\n', - 'Some predicted values: \n') - - print.default(format(head(x$predictions_Summary$Predicted, 20), digits = 3), print.gap = 2L, quote = FALSE) - - cat('\nPredictive capacity of the model: \n') - - print.data.frame(summary(x, 'compact', digits = 3), print.gap = 2L, quote = FALSE) - - cat('\n Use str() function to found more datailed information.') - invisible(x) +#' @title Summary +#' +#' @description Summary of IBCF object +#' +#' @param object \code{IBCF object} IBCF object, result of use the IBCF() function +#' @param information \code{string} ... +#' @param digits \code{numeric} ... +#' @param ... Further arguments passed to or from other methods. +#' +#' @importFrom stats cor +#' @importFrom dplyr summarise group_by select '%>%' mutate_if funs +#' +#' @export +summary.IBCF <- function(object, information = 'compact', digits = 4, ...){ + if (!inherits(object, "IBCF")) stop("This function only works for objects of class 'IBCF'") + + object$predictions_Summary %>% + group_by(Environment, Trait, Partition) %>% + summarise(Pearson = cor(Predicted, Observed, use = 'pairwise.complete.obs'), + MAAPE = mean(atan(abs(Observed-Predicted)/abs(Observed)))) %>% + select(Environment, Trait, Partition, Pearson, MAAPE) %>% + mutate_if(is.numeric, funs(round(., digits))) %>% + as.data.frame() -> presum + + presum %>% group_by(Environment, Trait) %>% + summarise(SE_MAAPE = sd(MAAPE, na.rm = T)/sqrt(n()), MAAPE = mean(MAAPE, na.rm = T), + SE_Pearson = sd(Pearson, na.rm = T)/sqrt(n()), Pearson = mean(Pearson, na.rm = T)) %>% + select(Environment, Trait, Pearson, SE_Pearson, MAAPE, SE_MAAPE) %>% + mutate_if(is.numeric, funs(round(., digits))) %>% + as.data.frame() -> finalSum + + out <- switch(information, + compact = finalSum, + complete = presum, + extended = { + finalSum$Partition <- 'All' + presum$Partition <- as.character(presum$Partition) + presum$SE_Pearson <- NA + presum$SE_MAAPE <- NA + rbind(presum, finalSum) + } + ) + return(out) +} + +#' @title Summary +#' +#' @description Summary of IBCFY object +#' +#' @param object \code{IBCFY object} IBCFY object, result of use the IBCF.Years() function +#' @param digits \code{numeric} Number of digits of the output. +#' @param ... Further arguments passed to or from other methods. +#' +#' @importFrom stats cor +#' @importFrom dplyr summarise group_by select '%>%' mutate_if funs +#' +#' @export +summary.IBCFY <- function(object, digits = 4, ...) { + if (!inherits(object, "IBCFY")) stop("This function only works for objects of class 'IBCFY'") + + object$predictions_Summary %>% + group_by(Environment, Trait) %>% + summarise(Pearson = cor(Predicted, Observed, use = 'pairwise.complete.obs'), + MAAPE = mean(atan(abs(Observed-Predicted)/abs(Observed)))) %>% + select(Environment, Trait, Pearson, MAAPE) %>% + mutate_if(is.numeric, funs(round(., digits))) %>% + as.data.frame() -> out + + return(out) +} + +#' @title Plot IBCF graph +#' +#' @description Plot from IBCF object +#' +#' @param x \code{IBCF object} IBCF object, result of use the IBCF() function +#' @param select \code{character} By default ('Pearson'), plot the Pearson Correlations of the IBCF Object, else ('MAAPE'), plot the MAAPE of the IBCF Object. +#' @param ... Further arguments passed to or from other methods. +#' +#' @importFrom graphics arrows axis plot +#' @export +plot.IBCF <- function(x, select = 'Pearson', ...){ + ### Check that object is compatible + if (!inherits(x, "IBCF")) stop("This function only works for objects of class 'IBCF'") + + results <- summary(x) + results[, select] <- results[order(results[, select]), select] + + if (select == "Pearson") { + results$SE <- results$SE_Pearson * 1.96 + ylab <- "Pearson's Correlation" + } else if (select == "MAAPE") { + results$SE <- results$SE_MAAPE * 1.96 + ylab <- select + } + + x.labels <- paste0(results$Trait, '_', results$Env) + plot.x <- 1:length(x.labels) + + plot(plot.x, results[, select], ylim = range(c(results[, select] - results$SE, results[, select] + results$SE)), + type = 'p', ylab = ylab, xlab = '', xaxt = "n", ...) + axis(1, at = plot.x, labels = x.labels, las = 2) + arrows(plot.x, results[, select] - results$SE, plot.x, results[, select] + results$SE, code = 3, length = 0.02, angle = 90) +} + + +#' @title barplot.IBCFY +#' +#' @description Barplot of the results from IBCFY object +#' +#' @param height \code{IBCFY object} IBCFY object, result of use the IBCF.Years() function +#' @param select \code{character} By default ('Pearson'), plot the Pearson Correlations of the IBCF Object, else ('MAAPE'), plot the MAAPE of the IBCF Object. +#' @param ... Further arguments passed to or from other methods. +#' +#' @importFrom graphics barplot +#' @export +barplot.IBCFY <- function(height, select = 'Pearson', ...){ + ### Check that object is compatible + if (!inherits(height, "IBCFY")) stop("This function only works for objects of class 'IBCF'") + + results <- summary(height) + vector <- as.numeric(paste(results[, select])) + names(vector) <- results[, 1] + vector <- vector[order(vector)] + + if (select == 'Pearson') + select <- 'Pearson Correlation' + else + select <- 'MAAPE' + + barplot(vector, ylab = select, ...) +} + +#' Print IBCF information object +#' +#' @param x IBCF object +#' @param ... Further arguments passed to or from other methods. +#' +#' @return printeable object +#' @importFrom utils head +#' @export +#' +print.IBCF <- function(x, ...){ + cat('Item Based Collaborative Filtering Model: \n', + 'Fitted with ', x$NPartitions, ' random partitions\n', + 'Runtime: ', x$executionTime ,' seconds \n\n', + 'Some predicted values: \n') + + print.default(format(head(x$predictions_Summary$Predicted, 20), digits = 3), print.gap = 2L, quote = FALSE) + + cat('\nPredictive capacity of the model: \n') + + print.data.frame(summary(x, 'compact', digits = 3), print.gap = 2L, quote = FALSE) + + cat('\n Use str() function to found more datailed information.') + invisible(x) +} + +#' Print IBCFY information object +#' +#' @param x IBCFY object +#' @param ... Further arguments passed to or from other methods. +#' +#' @return printeable object +#' @importFrom utils head +#' @export +#' +print.IBCFY <- function(x, ...){ + cat('Item Based Collaborative Filtering Model: \n', + 'Evaluated Environment/Year (s): ', x$Years.testing, '\n', + 'Evaluated Trait (s): ', x$Traits.testing, '\n', + 'Runtime: ', x$executionTime ,' seconds \n\n', + 'Some predicted values: \n') + + print.default(format(head(x$predictions_Summary$Predicted, 20), digits = 3), print.gap = 2L, quote = FALSE) + + cat('\nPredictive capacity of the model: \n') + + print.data.frame(summary(x, 'compact', digits = 3), print.gap = 2L, quote = FALSE) + + cat('\n Use str() function to found more datailed information.') + invisible(x) } \ No newline at end of file