Call .Fortran in R for hclust

Viewed 38

I'm still a newbie to R and I need the complete syntax of the hclust function. I don't know how to get the full syntax of the .Fortran function contained in the hclust function. Below are .Fortran functions that are used there are 2:

hcl <- .Fortran(C_hclust, n = n, len = len, method = as.integer(i.meth), 
        ia = integer(n), ib = integer(n), crit = double(n), members = as.double(members), 
        nn = integer(n), disnn = double(n), diss = d)
hcass <- .Fortran(C_hcass2, n = n, ia = hcl$ia, ib = hcl$ib, 
        order = integer(n), iia = integer(n), iib = integer(n))

and below hclust function that i get from run syntax hclust in R

function (d, method = "complete", members = NULL) 
{
    METHODS <- c("ward.D", "single", "complete", 
        "average", "mcquitty", "median", "centroid", 
        "ward.D2")
    if (method == "ward") {
        message("The \"ward\" method has been renamed to \"ward.D\"; note new \"ward.D2\"")
        method <- "ward.D"
    }
    i.meth <- pmatch(method, METHODS)
    if (is.na(i.meth)) 
        stop("invalid clustering method", paste("", 
            method))
    if (i.meth == -1) 
        stop("ambiguous clustering method", paste("", 
            method))
    n <- as.integer(attr(d, "Size"))
    if (is.null(n)) 
        stop("invalid dissimilarities")
    if (is.na(n) || n > 65536L) 
        stop("size cannot be NA nor exceed 65536")
    if (n < 2) 
        stop("must have n >= 2 objects to cluster")
    len <- as.integer(n * (n - 1)/2)
    if (length(d) != len) 
        (if (length(d) < len) 
            stop
        else warning)("dissimilarities of improper length")
    if (is.null(members)) 
        members <- rep(1, n)
    else if (length(members) != n) 
        stop("invalid length of members")
    storage.mode(d) <- "double"
    hcl <- .Fortran(C_hclust, n = n, len = len, method = as.integer(i.meth), 
        ia = integer(n), ib = integer(n), crit = double(n), members = as.double(members), 
        nn = integer(n), disnn = double(n), diss = d)
    hcass <- .Fortran(C_hcass2, n = n, ia = hcl$ia, ib = hcl$ib, 
        order = integer(n), iia = integer(n), iib = integer(n))
    structure(list(merge = cbind(hcass$iia[1L:(n - 1)], hcass$iib[1L:(n - 
        1)]), height = hcl$crit[1L:(n - 1)], order = hcass$order, 
        labels = attr(d, "Labels"), method = METHODS[i.meth], 
        call = match.call(), dist.method = attr(d, "method")), 
        class = "hclust")
}

If you know how to get the full syntax fo .Fortran, let me know and thank you so muchhhh.

1 Answers

.Fortran is a base R function used to call functions written in Fortran from within packages (or user scripts, but that's really rare). In the code you posted, it calls the functions referenced by C_hclust and C_hcass2. Those are likely private variables in the hclust package.

Because those variables are private, there probably isn't any documentation for how to use them. You'll need to obtain the source code to hclust, and look in the src directory for the .f files containing the Fortran source code.

However, it's always a bad idea to call private functions. Package authors may change those without notice at the next update, and your code would stop working. Even if the function doesn't change, there may be undocumented limitations on it, so your calls may not work properly.

Related