Skip to content

Commit

Permalink
Add probs as output of rgcca_predict
Browse files Browse the repository at this point in the history
  • Loading branch information
GFabien committed Apr 22, 2024
1 parent eefad3b commit 811bcac
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
24 changes: 14 additions & 10 deletions R/rgcca_predict.R
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,15 @@ rgcca_predict <- function(rgcca_res,
}))
})

probs <- lapply(c("train", "test"), function(mode) {
as.data.frame(lapply(results, function(res) {
res[["probs"]][[mode]]
}))
})

confusion <- results[[1]]$confusion

names(prediction) <- names(metric) <- c("train", "test")
names(prediction) <- names(metric) <- names(probs) <- c("train", "test")

model <- lapply(results, "[[", "model")
score <- mean(unlist(lapply(results, "[[", "score")), na.rm = TRUE)
Expand All @@ -169,6 +175,7 @@ rgcca_predict <- function(rgcca_res,
prediction = prediction,
confusion = confusion,
metric = metric,
probs = probs,
model = model,
score = score
)
Expand Down Expand Up @@ -221,22 +228,18 @@ core_prediction <- function(prediction_model, X_train, X_test,
idx_train <- !(is.na(prediction_train$obs) | is.na(prediction_train$pred))
idx_test <- !(is.na(prediction_test$obs) | is.na(prediction_test$pred))

probs_train <- probs_test <- NULL

if (classification) {
confusion_train <- confusionMatrix(prediction_train$pred,
reference = prediction_train$obs
)
confusion_test <- confusionMatrix(prediction_test$pred,
reference = prediction_test$obs
)
if (is.null(prediction_model$prob)) {
prediction_train <- data.frame(cbind(
prediction_train,
predict(model, X_train, type = "prob")
))
prediction_test <- data.frame(cbind(
prediction_test,
predict(model, X_test, type = "prob")
))
if (is.function(prediction_model$prob)) {
probs_train <- data.frame(predict(model, X_train, type = "prob"))
probs_test <- data.frame(predict(model, X_test, type = "prob"))
}
metric_train <- multiClassSummary(
data = prediction_train[idx_train, ],
Expand Down Expand Up @@ -268,6 +271,7 @@ core_prediction <- function(prediction_model, X_train, X_test,
return(list(
score = score,
model = model,
probs = list(train = probs_train, test = probs_test),
metric = list(train = metric_train, test = metric_test),
confusion = list(train = confusion_train, test = confusion_test),
prediction = list(train = prediction_train, test = prediction_test)
Expand Down
19 changes: 19 additions & 0 deletions tests/testthat/test_rgcca_predict.r
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ test_that("rgcca_predict with lm predictor gives the same prediction as
expect_equal(as.matrix(A[[response]] - res_predict$prediction$test), res_lm)
})

test_that("rgcca_predict returns an empty probs in regression", {
res_predict <- rgcca_predict(rgcca_res = fit_rgcca)
expect_equal(nrow(res_predict$probs$train), 0)
expect_equal(nrow(res_predict$probs$test), 0)
})

# Classification
#---------------
test_that("rgcca_predict with lda predictor gives the same prediction as
Expand All @@ -109,3 +115,16 @@ test_that("rgcca_predict with lda predictor gives the same prediction as
data.frame(politic = prediction_lda)
)
})

test_that("rgcca_predict returns probs in classification with adequate model", {
A <- lapply(blocks_classif, function(x) x[1:32, ])
B <- lapply(blocks_classif, function(x) x[33:47, ])
response <- 3
fit_rgcca <- rgcca(A, tau = 1, ncomp = c(3, 2, 1), response = response)
res_predict <- rgcca_predict(fit_rgcca,
blocks_test = B[-3],
prediction_model = "lda"
)
expect_equal(nrow(res_predict$probs$train), 32)
expect_equal(nrow(res_predict$probs$test), 15)
})

0 comments on commit 811bcac

Please sign in to comment.