r - 显示 R 中函数的源代码

标签 r open-source statistics machine-learning

我可以使用lmclass::knn查看源代码,但我未能显示 princomp 的代码。这个函数(或其他东西)是用 R 编写的还是使用了其他字节码。 我也无法使用 How do I show the source code of an S4 function in a package? 的建议找到源代码。感谢您的帮助。

> princomp
function (x, ...) 
UseMethod("princomp")
<bytecode: 0x9490010>
<environment: namespace:stats>

最佳答案

你必须使用该函数所使用的相应方法来询问。试试这个:

princomp # this is what you did without having a good enough answer
methods(princomp) # Next step, ask for the method: 'princomp.default'
getAnywhere('princomp.default') # this will show you the code

您要查找的代码是:

function (x, cor = FALSE, scores = TRUE, covmat = NULL, subset = rep(TRUE, 
    nrow(as.matrix(x))), ...) 
{
    cl <- match.call()
    cl[[1L]] <- as.name("princomp")
    if (!missing(x) && !missing(covmat)) 
        warning("both 'x' and 'covmat' were supplied: 'x' will be ignored")
    z <- if (!missing(x)) 
        as.matrix(x)[subset, , drop = FALSE]
    if (is.list(covmat)) {
        if (any(is.na(match(c("cov", "n.obs"), names(covmat))))) 
            stop("'covmat' is not a valid covariance list")
        cv <- covmat$cov
        n.obs <- covmat$n.obs
        cen <- covmat$center
    }
    else if (is.matrix(covmat)) {
        cv <- covmat
        n.obs <- NA
        cen <- NULL
    }
    else if (is.null(covmat)) {
        dn <- dim(z)
        if (dn[1L] < dn[2L]) 
            stop("'princomp' can only be used with more units than variables")
        covmat <- cov.wt(z)
        n.obs <- covmat$n.obs
        cv <- covmat$cov * (1 - 1/n.obs)
        cen <- covmat$center
    }
    else stop("'covmat' is of unknown type")
    if (!is.numeric(cv)) 
        stop("PCA applies only to numerical variables")
    if (cor) {
        sds <- sqrt(diag(cv))
        if (any(sds == 0)) 
            stop("cannot use cor=TRUE with a constant variable")
        cv <- cv/(sds %o% sds)
    }
    edc <- eigen(cv, symmetric = TRUE)
    ev <- edc$values
    if (any(neg <- ev < 0)) {
        if (any(ev[neg] < -9 * .Machine$double.eps * ev[1L])) 
            stop("covariance matrix is not non-negative definite")
        else ev[neg] <- 0
    }
    cn <- paste("Comp.", 1L:ncol(cv), sep = "")
    names(ev) <- cn
    dimnames(edc$vectors) <- if (missing(x)) 
        list(dimnames(cv)[[2L]], cn)
    else list(dimnames(x)[[2L]], cn)
    sdev <- sqrt(ev)
    sc <- if (cor) 
        sds
    else rep(1, ncol(cv))
    names(sc) <- colnames(cv)
    scr <- if (scores && !missing(x) && !is.null(cen)) 
        scale(z, center = cen, scale = sc) %*% edc$vectors
    if (is.null(cen)) 
        cen <- rep(NA_real_, nrow(cv))
    edc <- list(sdev = sdev, loadings = structure(edc$vectors, 
        class = "loadings"), center = cen, scale = sc, n.obs = n.obs, 
        scores = scr, call = cl)
    class(edc) <- "princomp"
    edc
}
<environment: namespace:stats>

我想这就是你所要求的。

关于r - 显示 R 中函数的源代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10782403/

相关文章:

python 相当于 R 中的 get() (=使用字符串检索符号的值)

r - 从 R 传递经度和纬度参数以读取 Google map 中的 URL 并提取路线

r - 如何提取具有最小值或最大值的行?

java - 开源项目静态分析

.net - ASP.NET MVC 应用程序可供学习。

r - 如何在R中使用对数刻度作为直方图的y轴?

algorithm - 如何操纵围绕中心值震荡的价格序列(指标)?

R:找到两个向量之间的交集

open-source - 向开源项目成员支付错误修复和功能的费用

r - 如何计算 R 中线性回归模型中斜率的 95% 置信区间