harrelfe / Hmisc

Harrell Miscellaneous
Other
208 stars 81 forks source link

summaryM - Error in specs[[tr$namefun]] #61

Closed tormodb closed 7 years ago

tormodb commented 7 years ago

Dear Prof. Harrel.

I am currently encountering an error with summaryM. I am using a separate categorical chi square test specified using catTest:

catTestchisq.sim_p <- function (tab) {
    st <- if (!is.matrix(tab) || nrow(tab) < 2 | ncol(tab) < 
        2) 
        list(p.value = NA, statistic = NA, parameter = NA)
    else {
        rowcounts <- tab %*% rep(1, ncol(tab))
        tab <- tab[rowcounts > 0, ]
        if (!is.matrix(tab)) 
            list(p.value = NA, statistic = NA, parameter = NA)
        else chisq.test(tab, correct = FALSE, simulate.p.value = T)
    }
    list(P = st$p.value, stat = st$statistic, df = st$parameter, 
        testname = "Pearson", statname = "Chi-square", latexstat = "\\chi^{2}_{df}", 
        plotmathstat = "chi[df]^2")
}

and referring to it using (in your example form the help file)

f <- summaryM(age + sex + sbp + Symptoms ~ treatment, test=TRUE, catTest = catTestchisq.sim_p).

This previously worked fine, but I am currently experiencing the following error:

Error in specs[[tr$namefun]] : 
  attempt to select less than one element in get1index

when running this on both my original analysis, and your sample from help. I am working on data with cells with few numbers, so the monte-carlo simulated p-values have been necessary for creating my table (at least, this is my current understanding).

harrelfe commented 7 years ago

There is one line that needs to be added for Hmisc_4.0, and one line to change to handle the fact that simulated p-values do not return d.f. See below.

require(Hmisc)
src <- example(summaryM, give.lines=TRUE)
eval(parse(text=src[1:34]))
# xless(f)    # see built-in cat. test

catTestchisq.sim_p <- function(tab) {
    st <- if (! is.matrix(tab) || nrow(tab) < 2 || ncol(tab) <  2) 
        list(p.value = NA, statistic = NA, parameter = NA)
    else {
        rowcounts <- tab %*% rep(1, ncol(tab))
        tab <- tab[rowcounts > 0, ]
        if (! is.matrix(tab)) 
            list(p.value = NA, statistic = NA, parameter = NA)
        else chisq.test(tab, correct = FALSE, simulate.p.value = TRUE)
    }
    list(P            = st$p.value,
         stat         = st$statistic,
         df           = (nrow(tab) - 1) * (ncol(tab) - 1),   # st$parameter NA
         testname     = "Pearson",
         namefun      = 'chisq',     # must add this line for Hmisc_4.0
         statname     = "Chi-square",
         latexstat    = "\\chi^{2}_{df}", 
         plotmathstat = "chi[df]^2")
}

f <- summaryM(age + sex + sbp + Symptoms ~ treatment, test=TRUE,
              catTest = catTestchisq.sim_p)
f