Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function (weights)
- {
- if (!is.null(attr(X, "deriv")))
- stop("fitting of derivatives of B-splines not implemented")
- weights[!Complete.cases(mf)] <- 0
- w <- weights
- if (!is.null(index))
- w <- .Call("R_ysum", as.double(weights), as.integer(index),
- PACKAGE = "mboost")
- XtX <- crossprod(X * w, X)
- lambdadf <- df2lambda(X, df = args$df, lambda = args$lambda,
- dmat = K, weights = w, XtX = XtX)
- lambda <- lambdadf["lambda"]
- XtX <- XtX + lambda * K
- if (is(X, "Matrix") && !extends(class(XtX), "dgeMatrix")) {
- XtXC <- Cholesky(forceSymmetric(XtX))
- mysolve <- function(y) {
- if (is.null(attr(X, "Ts_constraint")))
- return(solve(XtXC, crossprod(X, y)))
- return(nnls1D(as(XtX, "matrix"), as(X, "matrix"),
- y))
- }
- }
- else {
- if (is(X, "Matrix")) {
- X <- as(X, "matrix")
- XtX <- as(XtX, "matrix")
- }
- mysolve <- function(y) {
- if (is.null(attr(X, "Ts_constraint")))
- return(solve(XtX, crossprod(X, y), LINPACK = FALSE))
- return(nnls1D(XtX, X, y))
- }
- }
- fit <- function(y) {
- if (!is.null(index)) {
- y <- .Call("R_ysum", as.double(weights * y), as.integer(index),
- PACKAGE = "mboost")
- }
- else {
- y <- y * weights
- }
- coef <- mysolve(y)
- ret <- list(model = coef, fitted = function() {
- ret <- as.vector(X %*% coef)
- if (is.null(index)) return(ret)
- return(ret[index])
- })
- class(ret) <- c("bm_lin", "bm")
- ret
- }
- hatvalues <- function() {
- ret <- as.matrix(tcrossprod(X %*% solve(XtX), X * w))
- if (is.null(index))
- return(ret)
- return(ret[index, index])
- }
- df <- function() lambdadf
- predict <- function(bm, newdata = NULL, aggregate = c("sum",
- "cumsum", "none")) {
- cf <- sapply(bm, coef)
- if (!is.matrix(cf))
- cf <- matrix(cf, nrow = 1)
- if (!is.null(newdata)) {
- index <- NULL
- nm <- names(blg)
- if (any(duplicated(nm)))
- nm <- unique(nm)
- newdata <- newdata[, nm, drop = FALSE]
- if (nrow(newdata) > options("mboost_indexmin")[[1]]) {
- index <- get_index(newdata)
- newdata <- newdata[index[[1]], , drop = FALSE]
- index <- index[[2]]
- }
- X <- newX(newdata)$X
- }
- aggregate <- match.arg(aggregate)
- pr <- switch(aggregate, sum = as(X %*% rowSums(cf), "matrix"),
- cumsum = {
- as(X %*% .Call("R_mcumsum", as(cf, "matrix"),
- PACKAGE = "mboost"), "matrix")
- }, none = as(X %*% cf, "matrix"))
- if (is.null(index))
- return(pr[, , drop = FALSE])
- return(pr[index, , drop = FALSE])
- }
- ret <- list(fit = fit, hatvalues = hatvalues, predict = predict,
- df = df, Xnames = colnames(X))
- class(ret) <- c("bl_lin", "bl")
- return(ret)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement