我正在尝试实现

我正在尝试使用tryCatch语句在R中编写自己的“插补”函数:
1.输出包含功能名称的警告/错误消息,这样我可以更轻松地进行调试。
2.如果函数运行正常,但未估算所有缺失值,则发出警告。

ImputeVariables <- function(impute.var, impute.values,
                        filter.var){
# function to impute values.
# impute.var = variables with NAs
# impute.values = the missing value(s) to replace NAs, value labesl are levels
# filter.var = the variables to filter on.
# filter.levels = the categories of filter.var
tryCatch({
    filter.levels <- names(impute.values)
    # Validation
    stopifnot(class(impute.var) == class(impute.values),
             length(impute.values) > 0,
             sum(is.na(impute.values)) == 0)
    # Impute values
    for(level in filter.levels){
        impute.var[which(filter.var == level & is.na(impute.var))] <-
            impute.values[level]
    }
    # Check if all NAs removed.  Throw warning if not.
    if(sum(is.na(impute.var)) > 0){
        warning("Not all NAs removed")
    }
    # Return values
    return(impute.var)

},
    error = function(err) print(paste0("ImputeValues: ",err)),
    warning = function(war) print(paste0("ImputeValues: ",war))
)
}
impute.varfilter.var是从data.frame中获取的 vector (它们是Ages和Titles的 vector (例如'Mr','Mrs')impute.values是与impute.var相同类型的 vector ,但带有从filter.var提取的标签(即c('Mr' = 30, 'Mrs' = 25...)的形式)

问题

为了检查我的验证是否正常工作,我向该函数提供了NA的命名 vector ,因此:
ages <-   c(34, 22, NA, 17, 38, NA)
titles <- c("Mr", "Mr", "Mr", "Mrs", "Mrs", "Mrs")
ages.values <- c("Mr" = NA, "Mrs" = NA)

ages.new <- ImputeVariables(ages, ages.values, titles)

print(ages.new)

但是它输出以下内容:
 "ImputeValues: Error: class(impute.var) == class(impute.values) is not TRUE\n"
 "ImputeValues: Error: class(impute.var) == class(impute.values) is not TRUE\n"

这两行是由于函数打印了ages.new vector ,并且以下打印语句打印了ages.new(为什么?)

如果我注释掉验证(stopifnot函数),那么我会得到:
"ImputeValues: simpleWarning in doTryCatch(return(expr), name, parentenv, handler): Not all NAs removed\n"

我在问什么
  • 为什么tryCatch块的行为如此?
  • 我的验证和错误处理策略是否最佳(显然没有错误)?

  • 非常感谢您的宝贵时间。

    最佳答案

    谢谢奥利弗。

    现在的工作代码是:

     ImputeVariables <- function(impute.var, impute.values,
                            filter.var){
    # function to impute values.
    # impute.var = variables with NAs
    # impute.values = the missing value(s) to replace NAs, value labesl are levels
    # filter.var = the variables to filter on.
    # filter.levels = the categories of filter.var
    tryCatch({
        filter.levels <- names(impute.values)
        # Validation
        stopifnot(class(impute.var) == class(impute.values),
                 length(impute.values) > 0,
                 sum(is.na(impute.values)) == 0)
        # Impute values
        for(level in filter.levels){
            impute.var[which(filter.var == level & is.na(impute.var))] <-
                impute.values[level]
        }
        # Check if all NAs removed.  Throw warning if not.
        if(sum(is.na(impute.var)) > 0){
            warning("Not all NAs removed")
        }
        # Return values
        return(impute.var)
    
    },
        error = function(err) stop(paste0("ImputeValues: ",err)),
        warning = function(war) {
            message(paste0("ImputeValues: ",war))
            return(impute.var)}
    )
    }
    

    10-04 23:21
    查看更多