显示R语言函数的源代码 [重复]

我可以使用lmclass::knn来查看源代码,但我在尝试显示princomp的代码时失败了。这个函数(或其他什么)是用R语言编写的,还是使用了其他字节码?我也无法通过如何显示包中S4函数的源代码?中的建议找到源代码。感谢任何帮助。

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

回答:

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

princomp # 这是你所做的,但没有得到足够好的答案
methods(princomp) # 下一步,询问方法:'princomp.default'
getAnywhere('princomp.default') # 这将显示代码

你要找的代码是:

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>

我想这就是你所问的。

Related Posts

Keras Dense层输入未被展平

这是我的测试代码: from keras import…

无法将分类变量输入随机森林

我有10个分类变量和3个数值变量。我在分割后直接将它们…

如何在Keras中对每个输出应用Sigmoid函数?

这是我代码的一部分。 model = Sequenti…

如何选择类概率的最佳阈值?

我的神经网络输出是一个用于多标签分类的预测类概率表: …

在Keras中使用深度学习得到不同的结果

我按照一个教程使用Keras中的深度神经网络进行文本分…

‘MatMul’操作的输入’b’类型为float32,与参数’a’的类型float64不匹配

我写了一个简单的TensorFlow代码,但不断遇到T…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注