0

I'm trying to understand this code from R and I am running the code step by step. When I simply call the function, the code works perfectly but if I do it step by step, there is an error in the line with red color. I don't understand the usage of dots before functions in R. The question is about this kind of lines x_sort <- .sort.missing(x, x_nonmiss). Thank you.

> GSE

function (x, tol = 1e-04, maxiter = 150, method = c("bisquare", 
    "rocke"), init = c("emve", "qc", "huber", "imputed", "emve_c"), 
    mu0, S0, ...) 

{

    xcall <- match.call()
    method <- match.arg(method)
    init <- match.arg(init)
    if (is.data.frame(x) | is.matrix(x)) 
        x <- data.matrix(x)
    else stop("Data matrix must be of class matrix or data.frame")
    p <- ncol(x)
    if (p > 200 | p < 2) 
        stop("Column dimension of 'x' must be in between 2 and 200.")
    x_nonmiss <- is.na(x) * -1 + 1
    pp <- rowSums(x_nonmiss)
    pp_col <- colSums(x_nonmiss)
    if (all(pp == 0)) 
        stop("All observations have missing values!")
    if (any(pp_col == 0)) 
        stop("Data matrix cannot contain column(s) with completely missing data!")
    ok <- which(pp > 0)
    x_orig <- x
    x <- x[ok, ]
    x_nonmiss <- x_nonmiss[ok, ]
    x_sort <- .sort.missing(x, x_nonmiss)
    EM.mu0 <- colMeans(x_sort$x, na.rm = T)
    EM.S0 <- diag(apply(x_sort$x, 2, var, na.rm = T))
    x_sort <- c(x_sort, .CovEM.setparam(p, EM.mu0, EM.S0))
    n <- nrow(x_sort$x)
    p <- ncol(x_sort$x)
    if (n <= p + 1) 
        stop(if (n <= p) 
            "n <= p -- you can't be serious!"
        else "n == p+1  is too small sample size")
    if (n < 2 * p) 
        warning("n < 2 * p, i.e., possibly too small sample size")
    if (xor(missing(mu0), missing(S0))) 
        warning("Both 'mu0' and 'S0' must be provided. Default 'init' is used...")
    if (missing(mu0) || missing(S0)) {
        init.res <- switch(init, emve = {
            emve(x, sampling = "uniform", ...)
        }, qc = {
            HuberPairwise(x, psi = "sign", computePmd = FALSE)
        }, huber = {
            HuberPairwise(x, psi = "huber", computePmd = FALSE, 
                ...)
        }, imputed = {
            ImpS(x, ...)
        }, emve_c = {
            emve(x, sampling = "cluster", ...)
        })
        S0 <- init.res@S
        mu0 <- init.res@mu
    }
    S0.chol <- tryCatch(chol(S0), error = function(e) NA)
    if (!is.matrix(S0.chol)) 
        stop("Estimated initial covariance matrix 'S0' is not positive definite.")
    bdp <- 0.5
    print.step <- 0
    tol.scale <- 1e-09
    miter.scale <- 300
    res <- with(x_sort, .GSE.init(x, x_nonmiss, bdp, pu, n, p, 
        miss.group.unique, miss.group.counts, mu0, S0, tol, maxiter, 
        tol.scale, miter.scale, print.step = print.step, method))
    pmd <- pmd.adj <- rep(NA, nrow(x_orig))
    pmd[ok] <- res$pmd[x_sort$id.ro]
    pmd.adj[ok] <- res$pmd.adj[x_sort$id.ro]
    wgts <- rep(NA, nrow(x_orig))
    wgts[ok] <- res$weights[x_sort$id.ro]
    wgtsp <- rep(NA, nrow(x_orig))
    wgtsp[ok] <- res$weightsprm[x_sort$id.ro]
    ximp <- matrix(NA, nrow(x_orig), ncol(x_orig))
    ximp[ok, ] <- res$ximp[x_sort$id.ro, ]
    res <- new("GSE", call = xcall, S = res$S, mu = res$mu, sc = res$stilde0, 
        mu0 = mu0, S0 = S0, iter = res$iter, eps = res$ep, estimator = "Generalized S-Estimator", 
        x = x_orig, ximp = ximp, weights = wgts, weightsp = wgtsp, 
        pmd = pmd, pmd.adj = pmd.adj, p = p, pu = pp)
    res
}

This is another example with the same situation.

> partial.mahalanobis
function (x, mu, Sigma) 
{

    xcall <- match.call()
    if (is.data.frame(x) | is.matrix(x)) 
        x <- data.matrix(x)
    else stop("Data matrix must be of class matrix or data.frame")
    n <- nrow(x)
    p <- ncol(x)
    x_nonmiss <- is.na(x) * -1 + 1
    pp <- rowSums(x_nonmiss)
    pp_col <- colSums(x_nonmiss)
    if (all(pp == 0)) 
        stop("All observations have missing values!")
    if (any(pp_col == 0)) 
        stop("Data matrix cannot contain column(s) with completely missing data!")
    ok <- which(pp > 0)
    not.ok <- which(pp == 0)
    x.orig <- x
    x <- x[ok, ]
    x_nonmiss <- x_nonmiss[ok, ]
    x_sort <- .sort.missing(x, x_nonmiss)
    if (any(x_nonmiss == 0)) {
        x_cent <- sweep(x_sort$x, 2, mu, "-")
        pmd.tmp <- .partial.mahalanobis.Rcpp(x_cent, Sigma, x_sort$miss.group.unique, 
            x_sort$miss.group.counts)
        pmd.tmp <- pmd.tmp[x_sort$id.ro]
    }
    else {
        pmd.tmp <- mahalanobis(x_sort$x, mu, Sigma)
        pmd.tmp <- pmd.tmp[x_sort$id.ro]
    }
    pmd <- rep(NA, nrow(x.orig))
    pmd[ok] <- pmd.tmp
    pu <- rowSums(!is.na(x.orig))
    pmd.adj <- qchisq(pchisq(pmd, df = pu, log.p = T, lower.tail = F), 
        df = p, log.p = T, lower.tail = F)
    pmd.adj[which(pu == p)] <- pmd[which(pu == p)]
    new("CovRobMiss", mu = mu, S = Sigma, call = xcall, estimator = "unknown", 
        x = x.orig, pmd = pmd, pmd.adj = pmd.adj, p = p, pu = pu)
}

<environment: namespace:GSE>
albert
  • 8,285
  • 3
  • 19
  • 32
ecab
  • 1
  • 2
  • 3
    A name that starts with a `.` will be hidden by default - that's the only difference. It's works just like any other function, but the author is indicating that it's intended purpose is for internal use. – Gregor Thomas Oct 24 '17 at 14:50
  • 3
    Your "line of red color" doesn't come through - so you should indicate the line of the error and also provide the error message. Error messages are usually helpful. If this is in a package (please mention the package!) it is possible that the function uses an *unexported* function. You can still call it but you will need to use `package_name:::function_name` to use it. – Gregor Thomas Oct 24 '17 at 14:51
  • @Gregor sorry about the red color! The error message is this: > x_sort <- .sort.missing(x, x_nonmiss) Error: could not find function ".sort.missing" Also, the funcion is GSE {GSE} Can you explain me how to use "package_name:::function_name" in this case? Thanks. – ecab Oct 24 '17 at 15:01
  • 2
    My guess is that you have re-assigned this function to your global environment. When the function resides within its own package namespace ([`GSE`](https://cran.r-project.org/web/packages/GSE/index.html) if I'm not mistaken), then all unexported functions are available. Otherwise, the reference to [`.sort.missing`](https://github.com/cran/GSE/blob/master/R/misc.R#L1) is not finding it in any namespace as exported. If you are editing your own version of these functions, try replacing `.sort.missing` with `GSE:::.sort.missing`, realizing the author may have it hidden for a reason. – r2evans Oct 24 '17 at 15:05
  • I now see `namespace:GSE` for the second function, so if it's the same for the first function ... idk. – r2evans Oct 24 '17 at 15:06
  • @r2evans thank you very much! It works! I can even see the code of the function! – ecab Oct 24 '17 at 15:13

0 Answers0