Help with rpart--how to figure out dropped participants

Hey y'all. I'm not much of a biostats wiz, never took to R very naturally, and I'm also sort of digging this project out of the closet, so I hope you will forgive me for describing my problem in a general sense.

But a while back I was able to muster up enough competency in R to perform a recursive partitioning analysis with a decent size dataset, using the 'rpart' package. However, after I completed the analysis, I realized that two of my subjects were dropped from the computation (likely due to rules within the function on missing data). Now, my problem: I could never, for the life of me, figure out which subjects were dropped from analysis.

This is relevant because I need to change my demographics tables to reflect my population MINUS THOSE TWO MYSTERY PARTICIPANTS. My hope for an easy solution at the time was to figure out what function I needed to just spit out who was dropped from the original dataset, and then I could just create a separate dataset subtracted those two participants and therefore had an accurate table 1 for the computation that was performed.

Does anyone have any insight as to functions that I might use to figure out who was dropped? This is a problem that has plagued my subconscious for like two years at this point. Apologies again for not having more specifics on the functions I used previously to try to rectify this, much of this is on an old computer that I am pulling out of storage.

Posted Aug 6, 2022, 12:59 AM UTC

12
Tarqon•

The documentation for ?rpart says:

na.action the default action deletes all observations for which y is missing, but keeps those in which one or more predictors are missing.

This is implemented in the na.rpart function.

The implementation of na.rpart is:

function (x) 
{
    Terms <- attr(x, "terms")
    if (!is.null(Terms)) 
        yvar <- attr(Terms, "response")
    else yvar <- 0L
    if (yvar == 0L) {
        xmiss <- is.na(x)
        keep <- (xmiss %*% rep(1, ncol(xmiss))) < ncol(xmiss)
    }
    else {
        xmiss <- is.na(x[-yvar])
        ymiss <- is.na(x[[yvar]])
        keep <- if (is.matrix(ymiss)) 
            ((xmiss %*% rep(1, ncol(xmiss))) < ncol(xmiss)) & 
                ((ymiss %*% rep(1, ncol(ymiss))) == 0)
        else ((xmiss %*% rep(1, ncol(xmiss))) < ncol(xmiss)) & 
            !ymiss
    }
    if (all(keep)) 
        x
    else {
        temp <- seq(keep)[!keep]
        names(temp) <- row.names(x)[!keep]
        class(temp) <- c("na.rpart", "omit")
        structure(x[keep, , drop = FALSE], na.action = temp)
    }
}
1
Tarqon•

So try finding rows for which part of the response is missing, or all the predictors are missing. If that doesn't work, manually construct x and yvar and try running it though the relevant part of the above.

    xmiss <- is.na(x[-yvar])
    ymiss <- is.na(x[[yvar]])
    keep <- if (is.matrix(ymiss)) 
        ((xmiss %*% rep(1, ncol(xmiss))) < ncol(xmiss)) & 
            ((ymiss %*% rep(1, ncol(ymiss))) == 0)
    else ((xmiss %*% rep(1, ncol(xmiss))) < ncol(xmiss)) & 
        !ymiss

Another alternative is re-running rpart with x = TRUE and model = TRUE. The output object should then have the model frame and the input that you can compare.

1