⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mvrcv.r

📁 做主成分回归和偏最小二乘回归
💻 R
字号:
### mvrCv.R: The basic cross-validation function### $Id: mvrCv.R 142 2007-10-01 13:10:25Z bhm $mvrCv <- function(X, Y, ncomp,                  method = pls.options()$mvralg,                  scale = FALSE, segments = 10,                  segment.type = c("random", "consecutive", "interleaved"),                  length.seg, jackknife = FALSE, trace = FALSE, ...){    ## Initialise:    Y <- as.matrix(Y)    ## Save dimnames:    dnX <- dimnames(X)    dnY <- dimnames(Y)    ## Remove dimnames for performance (doesn't seem to matter; in fact,    ## as far as it has any effect, it hurts a tiny bit in most situations).    ## dimnames(X) <- dimnames(Y) <- NULL    nobj <- dim(X)[1]    npred <- dim(X)[2]    nresp <- dim(Y)[2]    ## Check the `scale' parameter:    if (!is.logical(scale) || length(scale) != 1)        stop("'scale' must be 'TRUE' or 'FALSE'")    ## Set up segments:    if (is.list(segments)) {        if (is.null(attr(segments, "type")))            attr(segments, "type") <- "user supplied"    } else {        if (missing(length.seg)) {            segments <- cvsegments(nobj, k = segments, type = segment.type)        } else {            segments <- cvsegments(nobj, length.seg = length.seg,                                   type = segment.type)        }    }    ## Reduce ncomp, if neccessary:    ncomp <- min(ncomp, nobj - max(sapply(segments, length)) - 1)    ## Select fit function:    method <- match.arg(method,c("kernelpls", "widekernelpls", "simpls", "oscorespls", "svdpc"))    fitFunc <- switch(method,                      kernelpls = kernelpls.fit,                      widekernelpls = widekernelpls.fit,                      simpls = simpls.fit,                      oscorespls = oscorespls.fit,                      svdpc = svdpc.fit)    ## Variables to save CV results in:    adj <- matrix(0, nrow = nresp, ncol = ncomp)    cvPred <- pred <- array(0, dim = c(nobj, nresp, ncomp))    if (jackknife)        cvCoef <- array(dim = c(npred, nresp, ncomp, length(segments)))    if (trace) cat("Segment: ")    for (n.seg in 1:length(segments)) {        if (trace) cat(n.seg, "")        ## Set up train data:        seg <- segments[[n.seg]]        Xtrain <- X[-seg,]        if (scale) {            ntrain <- nrow(Xtrain)            ## This is faster than sd(X), but cannot handle missing values:            sdtrain <-                sqrt(colSums((Xtrain - rep(colMeans(Xtrain), each = ntrain))^2) /                     (ntrain - 1))            if (any(abs(sdtrain) < .Machine$double.eps^0.5))                warning("Scaling with (near) zero standard deviation")            Xtrain <- Xtrain / rep(sdtrain, each = ntrain)        }        ## Fit the model:        fit <- fitFunc(Xtrain, Y[-seg,], ncomp, stripped = TRUE, ...)        ## Optionally collect coefficients:        if (jackknife) cvCoef[,,,n.seg] <- fit$coefficients        ## Set up test data:        Xtest <- X        if (scale) Xtest <- Xtest / rep(sdtrain, each = nobj)        Xtest <- Xtest - rep(fit$Xmeans, each = nobj)        ## Predict test data:        Ymeansrep <- rep(fit$Ymeans, each = nobj)        for (a in 1:ncomp)            pred[,,a] <- Xtest %*% fit$coefficients[,,a] + Ymeansrep        ## Save the cross-validated predictions:        cvPred[seg,,] <- pred[seg,,, drop=FALSE]        adj <- adj + length(seg) * colSums((pred - c(Y))^2)    }    if (trace) cat("\n")    ## Calculate validation statistics:    PRESS0 <- apply(Y, 2, var) * nobj^2 / (nobj - 1) # FIXME: Only correct for loocv!    PRESS <- colSums((cvPred - c(Y))^2)    ## Add dimnames:    objnames <- dnX[[1]]    if (is.null(objnames)) objnames <- dnY[[1]]    respnames <- dnY[[2]]    nCompnames <- paste(1:ncomp, "comps")    names(PRESS0) <- respnames    dimnames(adj) <- dimnames(PRESS) <-        list(respnames, nCompnames)    dimnames(cvPred) <- list(objnames, respnames, nCompnames)    if (jackknife)        dimnames(cvCoef) <- list(dnX[[2]], respnames, nCompnames,                                 paste("Seg", seq.int(along = segments)))    list(method = "CV", pred = cvPred, coefficients = if (jackknife) cvCoef,         PRESS0 = PRESS0, PRESS = PRESS, adj = adj / nobj^2,         segments = segments, ncomp = ncomp)}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -