Running an existing function with a parameter other than the default

The code inserted below from ResourceSelection :: hoslem.test performs a validation of Hosmer and Lemeshow. Examining why an output that does not exactly match what is being done by other software (Stata), I found that the difference is due to the use of the default argument R for the quantile function (type = 7). I would like to use this function with a different default value to calculate quantiles (type = 6).

FWIW, a link to 9 possible methods used by R, can be found at:

https://www.amherst.edu/media/view/129116/original/Sample+Quantiles.pdf

The Stata manual for pctile refers to the default method and the 'altdef' method. It was difficult for me to compare these two methods with the corresponding types of R.

However

hoslem.test(yhat, y, type=6)

It produces:

> hl <- hoslem.test(y, yhat, type=6)
Error in hoslem.test(y, yhat, type = 6) : unused argument (type = 6)

Is there a way to execute the function below with the non-default argument for the quantile function?

Those. allows you to add the following line :, type = 6 ':

qq <- unique(quantile(yhat, probs = seq(0, 1, 1/g), type=6))

This function:

> ResourceSelection::hoslem.test
function (x, y, g = 10) 
{
    DNAME <- paste(deparse(substitute(x)), deparse(substitute(y)), 
        sep = ", ")
    METHOD <- "Hosmer and Lemeshow goodness of fit (GOF) test"
    yhat <- y
    y <- x
    qq <- unique(quantile(yhat, probs = seq(0, 1, 1/g)))
    cutyhat <- cut(yhat, breaks = qq, include.lowest = TRUE)
    observed <- xtabs(cbind(y0 = 1 - y, y1 = y) ~ cutyhat)
    expected <- xtabs(cbind(yhat0 = 1 - yhat, yhat1 = yhat) ~ 
        cutyhat)
    chisq <- sum((observed - expected)^2/expected)
    PVAL = 1 - pchisq(chisq, g - 2)
    PARAMETER <- g - 2
    names(chisq) <- "X-squared"
    names(PARAMETER) <- "df"
    structure(list(statistic = chisq, parameter = PARAMETER, 
        p.value = PVAL, method = METHOD, data.name = DNAME, observed = observed, 
        expected = expected), class = "htest")
}
+4
source share
3 answers

We can modify function pieces. Look at the function body

as.list(body(hoslem.test))

Let's see that the element we want to change is the 6th element in the body

[[1]]
`{`

[[2]]
DNAME <- paste(deparse(substitute(x)), deparse(substitute(y)), 
    sep = ", ")

[[3]]
METHOD <- "Hosmer and Lemeshow goodness of fit (GOF) test"

[[4]]
yhat <- y

[[5]]
y <- x

[[6]]
qq <- unique(quantile(yhat, probs = seq(0, 1, 1/g)))

Change the 6th element to what you want

body(hoslem.test)[[6]] = substitute(qq <- unique(quantile(yhat,
                                    probs = seq(0, 1, 1/g), type = 6)))
+2
source

The easiest way is to enable the function as your own:

myhoslem.test<-function(x, y, g = 10, mytype = 6){
    DNAME <- paste(deparse(substitute(x)), deparse(substitute(y)), 
        sep = ", ")
    METHOD <- "Hosmer and Lemeshow goodness of fit (GOF) test"
    yhat <- y
    y <- x
    qq <- unique(quantile(yhat, probs = seq(0, 1, 1/g), type = mytype))
    cutyhat <- cut(yhat, breaks = qq, include.lowest = TRUE)
    observed <- xtabs(cbind(y0 = 1 - y, y1 = y) ~ cutyhat)
    expected <- xtabs(cbind(yhat0 = 1 - yhat, yhat1 = yhat) ~ 
        cutyhat)
    chisq <- sum((observed - expected)^2/expected)
    PVAL = 1 - pchisq(chisq, g - 2)
    PARAMETER <- g - 2
    names(chisq) <- "X-squared"
    names(PARAMETER) <- "df"
    structure(list(statistic = chisq, parameter = PARAMETER, 
        p.value = PVAL, method = METHOD, data.name = DNAME, observed = observed, 
        expected = expected), class = "htest")
}

Key change here:

qq <- unique(quantile(yhat, probs = seq(0, 1, 1/g), type = mytype))

and allow mytype as an argument to the function with a default value of 6

+1

- hoslem.test

myhoslem.test<-function(x, y, g = 10, mytype = 6){
  body(hoslem.test)[[6]] = substitute(qq <- unique(quantile(yhat,
                                              probs = seq(0, 1, 1/g), type = mytype))) 
  hoslem.test(x,y, g=10)
}
0

All Articles