boost-R / mboost

Boosting algorithms for fitting generalized linear, additive and interaction models to potentially high-dimensional data. The current relase version can be found on CRAN (http://cran.r-project.org/package=mboost).
73 stars 27 forks source link

Use linear extrapolation for P-splines predictions #2

Closed hofnerb closed 9 years ago

hofnerb commented 9 years ago

Is it possible to implement linear extrapolation for P-spline predictions as implemented in mgcv::Predict.matrix.pspline.smooth:

Predict.matrix.pspline.smooth <- function(object,data)
# prediction method function for the p.spline smooth class
{ ##require(splines)
  m <- object$m[1]+1
  ## find spline basis inner knot range...
  ll <- object$knots[m+1];ul <- object$knots[length(object$knots)-m]
  m <- m + 1
  x <- data[[object$term]]
  n <- length(x)
  ind <- x<=ul & x>=ll ## data in range
  if (sum(ind)==n) { ## all in range
    X <- splines::spline.des(object$knots,x,m)$design
  } else { ## some extrapolation needed
    ## matrix mapping coefs to value and slope at end points...
    D <- splines::spline.des(object$knots,c(ll,ll,ul,ul),m,c(0,1,0,1))$design
    X <- matrix(0,n,ncol(D)) ## full predict matrix
    if (sum(ind)>0) X[ind,] <-
         splines::spline.des(object$knots,x[ind],m)$design ## interior rows
    ## Now add rows for linear extrapolation...
    ind <- x < ll
    if (sum(ind)>0) X[ind,] <- cbind(1,x[ind]-ll)%*%D[1:2,]
    ind <- x > ul
    if (sum(ind)>0) X[ind,] <- cbind(1,x[ind]-ul)%*%D[3:4,]
  }
  X
} ## Predict.matrix.pspline.smooth

Currently the effect eventually drops to zero.

(feature request by Ben Hanowell).

hofnerb commented 9 years ago

Commit 236ed9... closes this issue.