【问题标题】:Editing a function from a package in R?从 R 中的包中编辑函数?
【发布时间】:2018-06-19 00:18:57
【问题描述】:

我正在使用 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 方面的经验相当丰富,但我以前从未编辑过包。我显然遗漏了一些关于如何在包中定义函数的内容,但我不确定是什么。

【问题讨论】:

标签: r packages


【解决方案1】:

package的开头有一行

data(sysdata, envir=environment())

请看这里:https://github.com/cran/referenceIntervals/tree/master/data/sysdata.rda

我怀疑“nonparRanks”是在那里定义的,因为我没有看到它在其他任何地方定义。所以也许你可以下载那个文件,编写你自己的函数,然后在运行你的函数之前运行同一行,它可能会工作。

编辑: 下载文件然后运行:

load("C:/sysdata.rda")

使用您的文件路径,然后您的功能将起作用。

【讨论】:

  • 优秀。这已经解决了,谢谢。可能有一些关于我不了解的环境的更深层次的东西,但这已经加载了我需要的一切。
  • 很好,如果对您有帮助,您可以接受answer。我应该提一下,从 sysdata.rda 加载的数据帧在样本大小低于 119 时没有条目(我认为这与您的问题的一部分“它不允许您对小于 120 的样本进行参数测试”有关)。所以这可能仍然会给你带来问题。
【解决方案2】:

nonparRanksreferenceIntervals 包中的一个函数:

Table that dictate the ranks for the confidence intervals around thecalculated reference interval

您保存和编辑函数的方法很好,但请确保您也加载了所有必要的底层函数来运行它。

最简单的方法可能是:

  1. 将复制和粘贴的 R 函数另存为其他名称,例如singleRefLimit2,然后
  2. 调用library("referenceIntervals"),它将加载您需要的所有底层函数,然后
  3. 加载你的函数source("singelRefLimit2.R"),无论你选择做什么编辑。

【讨论】:

  • 感谢您的回答。这大致是我想要做的,但即使我加载库,nonParRanks 仍然出现未定义?似乎 R 只是在进行函数计算时将其存储在内部,因为它在其他时间无法访问。
  • 这不起作用,因为该函数试图引用在 referenceIntervals 包(而不是全局)环境中的对象。
  • 尝试直接加载它,然后通过相同的方法,或使用@AidanGawronski 答案中的 rda 文件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-26
  • 2017-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多