Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quasibinomial family fixes #419

Open
wants to merge 1 commit into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 31 additions & 17 deletions R/Lrnr_gam.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Lrnr_gam <- R6Class(
}
),
private = list(
.properties = c("continuous", "binomial"),
.properties = c("continuous", "binomial", "weights", "offset"),
.train = function(task) {
# load args
args <- self$params
Expand All @@ -87,21 +87,27 @@ Lrnr_gam <- R6Class(
Y <- data.frame(outcome_type$format(task$Y))
colnames(Y) <- task$nodes$outcome
args$data <- cbind(task$X, Y)
## family


# family
if (is.null(args$family)) {
if (outcome_type$type == "continuous") {
args$family <- stats::gaussian()
} else if (outcome_type$type == "binomial") {
args$family <- stats::binomial()
} else if (outcome_type$type == "categorical") {
# TODO: implement categorical?
# NOTE: must specify (#{categories}-1)+linear_predictors) in formula
stop("Categorical outcomes are unsupported by Lrnr_gam for now.")
} else {
stop("Specified outcome type is unsupported by Lrnr_gam.")
}
args$family <- outcome_type$glm_family(return_object = TRUE)
}
family_name <- args$family$family
linkinv_fun <- args$family$linkinv
link_fun <- args$family$linkfun

# weights
if (task$has_node("weights")) {
args$weights <- task$weights
}
## formula

# offset
if (task$has_node("offset")) {
args$offset <- task$offset_transformed(link_fun)
}

# formula
if (is.null(args$formula)) {
covars_type <- lapply(
task$X,
Expand All @@ -111,9 +117,8 @@ Lrnr_gam <- R6Class(
X_discrete <- task$X[, ..i_discrete]
i_continuous <- covars_type == "continuous"
X_continuous <- task$X[, ..i_continuous]
"y ~ s(x1) + s(x2) + x3"
X_smooth <- sapply(colnames(X_continuous), function(x) {
unique_x <- unlist(unique(task$X[, x, with = F]))
unique_x <- unlist(unique(task$X[, x, with = FALSE]))
if (length(unique_x) < 10) {
paste0("s(", x, ", k=", length(unique_x), ")")
} else {
Expand Down Expand Up @@ -151,7 +156,7 @@ Lrnr_gam <- R6Class(
collapse = " ~ "
))
} else {
stop("Specified covariates types are unsupported in Lrnr_gam.")
stop("Specified covariates types are unsupported in Lrnr_gam")
}
} else if (is.list(args$formula)) {
args$formula <- lapply(args$formula, as.formula)
Expand All @@ -164,6 +169,15 @@ Lrnr_gam <- R6Class(
return(fit_object)
},
.predict = function(task) {
# offset check
if (self$fit_object$training_offset) {
offset <- task$offset_transformed(
self$fit_object$link_fun,
for_prediction = TRUE
)
warning("Lrnr_gam: offset given in training is ignored in prediction")
}

# get predictions
predictions <- stats::predict(
private$.fit_object,
Expand Down
1 change: 0 additions & 1 deletion R/Lrnr_glm.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ Lrnr_glm <- R6Class(
}
args$y <- outcome_type$format(task$Y)


if (task$has_node("weights")) {
args$weights <- task$weights
}
Expand Down
4 changes: 2 additions & 2 deletions R/Lrnr_svm.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Lrnr_svm <- R6Class(
# set SVM type based on detected outcome family
outcome_type <- self$get_outcome_type(task)
if (is.null(args$type)) {
if (outcome_type$type == "continuous") {
if (outcome_type$type %in% c("continuous", "quasibinomial")) {
args$type <- "eps-regression"
} else if (outcome_type$type %in% c("binomial", "categorical")) {
args$type <- "C-classification"
Expand Down Expand Up @@ -126,7 +126,7 @@ Lrnr_svm <- R6Class(
if (outcome_type == "categorical") {
predictions <- pack_predictions(predictions)
} else if (outcome_type == "binomial") {
predictions <- as.numeric(predictions[, 2]) # P(Y=1|X)
predictions <- as.numeric(predictions[, 2])
}
} else {
predictions <- as.numeric(predictions)
Expand Down