【发布时间】:2018-04-02 02:48:07
【问题描述】:
我正在研究 cluster 包的 pam 函数,其中的某些内容似乎很尴尬:该函数缺少对象。让我解释一下我的意思。
这是cluster::pam在终端中获取的函数代码示例。
function (x, k, diss = inherits(x, "dist"), metric = "euclidean",
medoids = NULL, stand = FALSE, cluster.only = FALSE, do.swap = TRUE,
keep.diss = !diss && !cluster.only && n < 100, keep.data = !diss &&
!cluster.only, pamonce = FALSE, trace.lev = 0)
{
stopifnot(length(cluster.only) == 1, length(trace.lev) ==
1)
nMax <- 65536
if ((diss <- as.logical(diss))) {
if (anyNA(x))
stop("NA values in the dissimilarity matrix not allowed.")
if (data.class(x) != "dissimilarity") {
if (!is.null(dim(x))) {
x <- as.dist(x)
}
else {
if (!is.numeric(x) || is.na(n <- sizeDiss(x)))
stop("'x' is not and cannot be converted to class \"dissimilarity\"")
attr(x, "Size") <- n
}
class(x) <- dissiCl
if (is.null(attr(x, "Metric")))
attr(x, "Metric") <- "unspecified"
}
if (keep.data)
stop("Cannot keep data when 'x' is a dissimilarity!")
n <- attr(x, "Size")
if (n > nMax)
stop(gettextf("have %d observations, but not more than %d are allowed",
n, nMax))
dv <- x[lower.to.upper.tri.inds(n)]
dv <- c(0, dv)
storage.mode(dv) <- "double"
jp <- 1
mdata <- FALSE
ndyst <- 0
... it continues
当通过在 Global Env 中创建我自己的函数在包外运行它时,它指责对象 dissiCl 没有创建。所以,我想知道为什么当以cluster::pam 运行该函数时,它并没有指责该对象丢失。
您可以通过运行这两个函数来看到这种差异。 myPam 是在 Global Env 中创建的函数,只需处理来自 cluster::pam 的代码即可。
myPam(dist(mtcars), 12)
cluster::pam(dist(mtcars), 12)
另外,谁能解释一下我怎么可能有这种情况,它在函数代码中,如 TRUE:data.class(x) != "dissimilarity"。
当将x 输入为(例如)dist(myBase) 时,该对象的类为diss。但是,如何创建类 dissimilarity 的对象?
【问题讨论】:
标签: r cluster-analysis packages