cluster::pam 的开头是如何工作的?

How does the begining of cluster::pam works?

我正在研究 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

当运行它在包外通过在全局环境中创建我自己的函数时它指责对象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) 时,此对象的 class 为 diss。但是,如何创建 class 不相似性 的对象?

dissiCl 是加载 cluster 时附加的命名空间中的非导出对象。 cluster::pam 函数正在访问它。如果需要,请在 dissiCl 的函数中使用 cluster:::dissiCl

您将 运行 解决该函数中其他对象的这个问题。如果你想 运行 它没有 运行 宁 library(cluster)。您可以对所有对象执行 find/replace,在每个导出的对象前添加 cluster::,并为每个未导出的对象添加 cluster:::

您可以使用相同的策略将对象转换为 class "dissimilarity"。如果它已经是 class diss,或者如果它是 class "dist",我建议使用 pam 强制它的方式。只需设置 class(myBase) <- cluster:::dissiCl

您可能正在使用 class dist 而不是 diss 的对象(dist() returns class 的对象dist),但看起来应该不是问题。