运行 具有非默认选项的现有函数
Running existing function with non-default option
下面从 ResourceSelection::hoslem.test 粘贴的代码执行 Hosmer 和 Lemeshow 拟合优度检验。在调查为什么输出与另一个软件 (Stata) 执行的输出不完全一致时,我发现差异与分位数函数 (type=7) 使用默认 R 参数有关。我想使用具有不同默认值的此函数来计算分位数(类型=6)。
FWIW,可以在以下位置找到对 R 使用的 9 种可能方法的参考:
https://www.amherst.edu/media/view/129116/original/Sample+Quantiles.pdf
Stata manual for pctile指的是默认方法和'altdef'方法。我发现很难将这两种方法映射到相应的 R 类型。
然而,
hoslem.test(yhat, y, type=6)
产生:
> hl <- hoslem.test(y, yhat, type=6)
Error in hoslem.test(y, yhat, type = 6) : unused argument (type = 6)
有没有办法 运行 下面的函数使用分位数函数的非默认参数?
即。允许在下面的行中添加 ', type=6':
qq <- unique(quantile(yhat, probs = seq(0, 1, 1/g), type=6))
有问题的函数是:
> 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")
}
最简单的方法是重新输入您自己的函数:
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")
}
这里的关键变化是:
qq <- unique(quantile(yhat, probs = seq(0, 1, 1/g), type = mytype))
并允许 mytype 作为函数的参数,默认值为 6
我们可以修改一些函数。看函数体
as.list(body(hoslem.test))
看到我们要修改的元素是body中的第6个元素
[[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)))
将第6个元素修改成你想要的
body(hoslem.test)[[6]] = substitute(qq <- unique(quantile(yhat,
probs = seq(0, 1, 1/g), type = 6)))
两个答案建议一个包装函数来灵活修改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)
}
下面从 ResourceSelection::hoslem.test 粘贴的代码执行 Hosmer 和 Lemeshow 拟合优度检验。在调查为什么输出与另一个软件 (Stata) 执行的输出不完全一致时,我发现差异与分位数函数 (type=7) 使用默认 R 参数有关。我想使用具有不同默认值的此函数来计算分位数(类型=6)。
FWIW,可以在以下位置找到对 R 使用的 9 种可能方法的参考:
https://www.amherst.edu/media/view/129116/original/Sample+Quantiles.pdf
Stata manual for pctile指的是默认方法和'altdef'方法。我发现很难将这两种方法映射到相应的 R 类型。
然而,
hoslem.test(yhat, y, type=6)
产生:
> hl <- hoslem.test(y, yhat, type=6)
Error in hoslem.test(y, yhat, type = 6) : unused argument (type = 6)
有没有办法 运行 下面的函数使用分位数函数的非默认参数?
即。允许在下面的行中添加 ', type=6':
qq <- unique(quantile(yhat, probs = seq(0, 1, 1/g), type=6))
有问题的函数是:
> 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")
}
最简单的方法是重新输入您自己的函数:
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")
}
这里的关键变化是:
qq <- unique(quantile(yhat, probs = seq(0, 1, 1/g), type = mytype))
并允许 mytype 作为函数的参数,默认值为 6
我们可以修改一些函数。看函数体
as.list(body(hoslem.test))
看到我们要修改的元素是body中的第6个元素
[[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)))
将第6个元素修改成你想要的
body(hoslem.test)[[6]] = substitute(qq <- unique(quantile(yhat,
probs = seq(0, 1, 1/g), type = 6)))
两个答案建议一个包装函数来灵活修改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)
}