r - 从 R 中的包中编辑函数?
问题描述
我正在使用 R 中的 referenceIntervals 包进行一些数据分析。
特别是我正在使用计算参考和置信区间的 refLimit 函数。我想编辑它以删除某些功能(例如,它运行 shapiro normalitiy 测试,如果数据大于 5000,它将停止整个代码,它不允许您对小于 120 的样本进行参数测试)。为此,我一直在终端中输入 refLimit - 复制函数定义,然后将其保存为单独的文件(以下是函数的完整原始定义)。
singleRefLimit =
function (data, dname = "default", out.method = "horn", out.rm = FALSE,
RI = "p", CI = "p", refConf = 0.95, limitConf = 0.9)
{
if (out.method == "dixon") {
output = dixon.outliers(data)
}
else if (out.method == "cook") {
output = cook.outliers(data)
}
else if (out.method == "vanderLoo") {
output = vanderLoo.outliers(data)
}
else {
output = horn.outliers(data)
}
if (out.rm == TRUE) {
data = output$subset
}
outliers = output$outliers
n = length(data)
mean = mean(data, na.rm = TRUE)
sd = sd(data, na.rm = TRUE)
norm = NULL
if (RI == "n") {
methodRI = "Reference Interval calculated nonparametrically"
data = sort(data)
holder = nonparRI(data, indices = 1:length(data), refConf)
lowerRefLimit = holder[1]
upperRefLimit = holder[2]
if (CI == "p") {
CI = "n"
}
}
if (RI == "r") {
methodRI = "Reference Interval calculated using Robust algorithm"
holder = robust(data, 1:length(data), refConf)
lowerRefLimit = holder[1]
upperRefLimit = holder[2]
CI = "boot"
}
if (RI == "p") {
methodRI = "Reference Interval calculated parametrically"
methodCI = "Confidence Intervals calculated parametrically"
refZ = qnorm(1 - ((1 - refConf)/2))
limitZ = qnorm(1 - ((1 - limitConf)/2))
lowerRefLimit = mean - refZ * sd
upperRefLimit = mean + refZ * sd
se = sqrt(((sd^2)/n) + (((refZ^2) * (sd^2))/(2 * n)))
lowerRefLowLimit = lowerRefLimit - limitZ * se
lowerRefUpperLimit = lowerRefLimit + limitZ * se
upperRefLowLimit = upperRefLimit - limitZ * se
upperRefUpperLimit = upperRefLimit + limitZ * se
shap_normalcy = shapiro.test(data)
shap_output = paste(c("Shapiro-Wilk: W = ", format(shap_normalcy$statistic,
digits = 6), ", p-value = ", format(shap_normalcy$p.value,
digits = 6)), collapse = "")
ks_normalcy = suppressWarnings(ks.test(data, "pnorm",
m = mean, sd = sd))
ks_output = paste(c("Kolmorgorov-Smirnov: D = ", format(ks_normalcy$statistic,
digits = 6), ", p-value = ", format(ks_normalcy$p.value,
digits = 6)), collapse = "")
if (shap_normalcy$p.value < 0.05 | ks_normalcy$p.value <
0.05) {
norm = list(shap_output, ks_output)
}
else {
norm = list(shap_output, ks_output)
}
}
if (CI == "n") {
if (n < 120) {
cat("\nSample size too small for non-parametric confidence intervals, \n \t\tbootstrapping instead\n")
CI = "boot"
}
else {
methodCI = "Confidence Intervals calculated nonparametrically"
ranks = nonparRanks[which(nonparRanks$SampleSize ==
n), ]
lowerRefLowLimit = data[ranks$Lower]
lowerRefUpperLimit = data[ranks$Upper]
upperRefLowLimit = data[(n + 1) - ranks$Upper]
upperRefUpperLimit = data[(n + 1) - ranks$Lower]
}
}
if (CI == "boot" & (RI == "n" | RI == "r")) {
methodCI = "Confidence Intervals calculated by bootstrapping, R = 5000"
if (RI == "n") {
bootresult = boot::boot(data = data, statistic = nonparRI,
refConf = refConf, R = 5000)
}
if (RI == "r") {
bootresult = boot::boot(data = data, statistic = robust,
refConf = refConf, R = 5000)
}
bootresultlower = boot::boot.ci(bootresult, conf = limitConf,
type = "basic", index = 1)
bootresultupper = boot::boot.ci(bootresult, conf = limitConf,
type = "basic", index = 2)
lowerRefLowLimit = bootresultlower$basic[4]
lowerRefUpperLimit = bootresultlower$basic[5]
upperRefLowLimit = bootresultupper$basic[4]
upperRefUpperLimit = bootresultupper$basic[5]
}
RVAL = list(size = n, dname = dname, out.method = out.method,
out.rm = out.rm, outliers = outliers, methodRI = methodRI,
methodCI = methodCI, norm = norm, refConf = refConf,
limitConf = limitConf, Ref_Int = c(lowerRefLimit = lowerRefLimit,
upperRefLimit = upperRefLimit), Conf_Int = c(lowerRefLowLimit = lowerRefLowLimit,
lowerRefUpperLimit = lowerRefUpperLimit, upperRefLowLimit = upperRefLowLimit,
upperRefUpperLimit = upperRefUpperLimit))
class(RVAL) = "interval"
return(RVAL)
}
但是,当我随后执行此文件时,大量术语最终未定义,例如,当我使用该函数时,我得到'找不到对象'nonparRanks'。
如何编辑包中的函数?我已经研究过尝试重视包命名空间和环境,但这并没有帮助。我也尝试在我的目录中的包文件中找到实际功能,但无法找到。
我在 R 方面的经验相当丰富,但我以前从未编辑过包。我显然遗漏了一些关于如何在包中定义函数的内容,但我不确定是什么。
解决方案
在包的开头有一行
data(sysdata, envir=environment())
见这里:https ://github.com/cran/referenceIntervals/tree/master/data/sysdata.rda
我怀疑“nonparRanks”是在那里定义的,因为我没有看到它在其他任何地方定义。所以也许你可以下载那个文件,编写你自己的函数,然后在运行你的函数之前运行同一行,它可能会工作。
编辑:下载文件然后运行:
load("C:/sysdata.rda")
使用您的文件路径,然后您的功能将起作用。
推荐阅读
- javascript - ReplaySubject(1) 和 AsyncSubject() 一样吗?
- java - 类对象未显示导入文件中的正确信息;找不到数据丢失的位置
- python - 需要帮助将推文数据转换为 JSON 文本文件
- r - How to bin factor(character values-aphla_numeric) variables with many levels in r
- spring-boot - spring-boot 上的集成测试抛出连接被拒绝
- python-3.x - 我的代码不能正常工作,但我不确定问题出在哪里
- ios - IOS - 应用程序未运行时如何处理通知
- sequelize.js - 嵌套和续集
- macos - Visual Studio MacOS 上的 Web Essentials
- node.js - 如何通过单个角度应用程序在每个子域级别动态添加脚本标签?