Skip to content

Commit

Permalink
Merge pull request #50 from ropensci/issue49
Browse files Browse the repository at this point in the history
soften requirements on binary y variables
  • Loading branch information
bcjaeger authored Apr 12, 2024
2 parents 7f9462f + e8f6922 commit 8d86f63
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
13 changes: 13 additions & 0 deletions R/orsf_R6.R
Original file line number Diff line number Diff line change
Expand Up @@ -2282,12 +2282,25 @@ ObliqueForest <- R6::R6Class(
x_original = names_x_data)


# coerce logicals to 0/1 integers.
# only do this for outcomes because changing predictor types here
# would cause issues down the road. E.g., if X[,1] is an integer
# in the training data and X[,1] is a logical in the testing data.
for(.name in c(names_y_data)){

if(self$get_var_type(.name) == 'logical'){
self$data[[.name]] <- as.integer(self$data[[.name]])
}

}

types_y_data <- vapply(names_y_data, self$get_var_type, character(1))
types_x_data <- vapply(names_x_data, self$get_var_type, character(1))

valid_y_types <- c('numeric', 'integer', 'units', 'factor', "Surv")
valid_x_types <- c(valid_y_types, 'ordered')


private$check_var_types(types_y_data, names_y_data, valid_y_types)
private$check_var_types(types_x_data, names_x_data, valid_x_types)

Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-orsf.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ test_that(
}
)

test_that(
desc = 'logical outcomes are allowed',
code = {

pbc$status2 <- as.logical(pbc$status)
fit_surv <- orsf(pbc, time + status2 ~ ., no_fit = TRUE)
expect_s3_class(fit_surv, "ObliqueForestSurvival")
pbc$status2 <- NULL

}
)

test_that(
desc = 'potential user-errors with outcome types are caught',
code = {
Expand Down

0 comments on commit 8d86f63

Please sign in to comment.