Skip to content

Commit

Permalink
fitted and predict always return a vector, also for offset models (fixes
Browse files Browse the repository at this point in the history
 #66 and fixes #69)
  • Loading branch information
hofnerb committed Feb 13, 2017
1 parent cbc756b commit 4016617
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
7 changes: 6 additions & 1 deletion R/mboost.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ mboost_fit <- function(blg, response, weights = rep(1, NROW(response)),
offsetarg <- offset
if (is.null(offset))
fit <- offset <- family@offset(y, weights)
if (length(fit) == 1)
fit <- rep(fit, NROW(y))
u <- ustart <- ngradient(y, fit, weights)

### vector of empirical risks for all boosting iterations
Expand Down Expand Up @@ -257,8 +259,11 @@ mboost_fit <- function(blg, response, weights = rep(1, NROW(response)),
RET$predict <- function(newdata = NULL, which = NULL,
aggregate = c("sum", "cumsum", "none")) {

if (mstop == 0)
if (mstop == 0) {
if (length(offset) == 1)
return(rep(offset, NROW(y)))
return(offset)
}
if (!is.null(xselect))
indx <- ((1:length(xselect)) <= mstop)
which <- thiswhich(which, usedonly = nw <- is.null(which))
Expand Down
8 changes: 4 additions & 4 deletions tests/regtest-gamboost.R
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,8 @@ mod2 <- mboost(DEXfat ~ bbs(age) + bols(waistcirc) + bbs(hipcirc),
mod3 <- mboost(DEXfat ~ bbs(age) + bols(waistcirc) + bbs(hipcirc),
data = bodyfat, control = boost_control(mstop = 1))
stopifnot(is.null(coef(mod)))
stopifnot(predict(mod) == mod$offset)
stopifnot(fitted(mod) == mod$offset)
stopifnot(predict(mod) == rep(mod$offset, nrow(bodyfat)))
stopifnot(fitted(mod) == rep(mod$offset, nrow(bodyfat)))
stopifnot(all.equal(residuals(mod), bodyfat$DEXfat - mean(bodyfat$DEXfat)))
stopifnot(is.null(selected(mod)))
stopifnot(all.equal(risk(mod), risk(mod2)[1]))
Expand All @@ -360,8 +360,8 @@ mod2 <- glmboost(DEXfat ~ age + waistcirc + hipcirc,
mod3 <- glmboost(DEXfat ~ age + waistcirc + hipcirc,
data = bodyfat, control = boost_control(mstop = 1))
stopifnot(is.null(coef(mod)))
stopifnot(!is.null(predict(mod))) ## should be offset
stopifnot(fitted(mod) == mod$offset)
stopifnot(predict(mod) == rep(mod$offset, nrow(bodyfat)))
stopifnot(fitted(mod) == rep(mod$offset, nrow(bodyfat)))
stopifnot(all.equal(residuals(mod), bodyfat$DEXfat - mean(bodyfat$DEXfat), check.attributes = FALSE))
stopifnot(is.null(selected(mod)))
stopifnot(all.equal(risk(mod), risk(mod2)[1]))
Expand Down

0 comments on commit 4016617

Please sign in to comment.