Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 3.16 KB | None | 0 0
  1. function (weights)
  2. {
  3.     if (!is.null(attr(X, "deriv")))
  4.         stop("fitting of derivatives of B-splines not implemented")
  5.     weights[!Complete.cases(mf)] <- 0
  6.     w <- weights
  7.     if (!is.null(index))
  8.         w <- .Call("R_ysum", as.double(weights), as.integer(index),
  9.             PACKAGE = "mboost")
  10.     XtX <- crossprod(X * w, X)
  11.     lambdadf <- df2lambda(X, df = args$df, lambda = args$lambda,
  12.         dmat = K, weights = w, XtX = XtX)
  13.     lambda <- lambdadf["lambda"]
  14.     XtX <- XtX + lambda * K
  15.     if (is(X, "Matrix") && !extends(class(XtX), "dgeMatrix")) {
  16.         XtXC <- Cholesky(forceSymmetric(XtX))
  17.         mysolve <- function(y) {
  18.             if (is.null(attr(X, "Ts_constraint")))
  19.                 return(solve(XtXC, crossprod(X, y)))
  20.             return(nnls1D(as(XtX, "matrix"), as(X, "matrix"),
  21.                 y))
  22.         }
  23.     }
  24.     else {
  25.         if (is(X, "Matrix")) {
  26.             X <- as(X, "matrix")
  27.             XtX <- as(XtX, "matrix")
  28.         }
  29.         mysolve <- function(y) {
  30.             if (is.null(attr(X, "Ts_constraint")))
  31.                 return(solve(XtX, crossprod(X, y), LINPACK = FALSE))
  32.             return(nnls1D(XtX, X, y))
  33.         }
  34.     }
  35.     fit <- function(y) {
  36.         if (!is.null(index)) {
  37.             y <- .Call("R_ysum", as.double(weights * y), as.integer(index),
  38.                 PACKAGE = "mboost")
  39.         }
  40.         else {
  41.             y <- y * weights
  42.         }
  43.         coef <- mysolve(y)
  44.         ret <- list(model = coef, fitted = function() {
  45.             ret <- as.vector(X %*% coef)
  46.             if (is.null(index)) return(ret)
  47.             return(ret[index])
  48.         })
  49.         class(ret) <- c("bm_lin", "bm")
  50.         ret
  51.     }
  52.     hatvalues <- function() {
  53.         ret <- as.matrix(tcrossprod(X %*% solve(XtX), X * w))
  54.         if (is.null(index))
  55.             return(ret)
  56.         return(ret[index, index])
  57.     }
  58.     df <- function() lambdadf
  59.     predict <- function(bm, newdata = NULL, aggregate = c("sum",
  60.         "cumsum", "none")) {
  61.         cf <- sapply(bm, coef)
  62.         if (!is.matrix(cf))
  63.             cf <- matrix(cf, nrow = 1)
  64.         if (!is.null(newdata)) {
  65.             index <- NULL
  66.             nm <- names(blg)
  67.             if (any(duplicated(nm)))
  68.                 nm <- unique(nm)
  69.             newdata <- newdata[, nm, drop = FALSE]
  70.             if (nrow(newdata) > options("mboost_indexmin")[[1]]) {
  71.                 index <- get_index(newdata)
  72.                 newdata <- newdata[index[[1]], , drop = FALSE]
  73.                 index <- index[[2]]
  74.             }
  75.             X <- newX(newdata)$X
  76.         }
  77.         aggregate <- match.arg(aggregate)
  78.         pr <- switch(aggregate, sum = as(X %*% rowSums(cf), "matrix"),
  79.             cumsum = {
  80.                 as(X %*% .Call("R_mcumsum", as(cf, "matrix"),
  81.                   PACKAGE = "mboost"), "matrix")
  82.             }, none = as(X %*% cf, "matrix"))
  83.         if (is.null(index))
  84.             return(pr[, , drop = FALSE])
  85.         return(pr[index, , drop = FALSE])
  86.     }
  87.     ret <- list(fit = fit, hatvalues = hatvalues, predict = predict,
  88.         df = df, Xnames = colnames(X))
  89.     class(ret) <- c("bl_lin", "bl")
  90.     return(ret)
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement