r语言 - 稳健回归不起作用 - "对象'味精。未找到 UCV"



我正在尝试使用 R 中的 robust 包运行稳健的线性回归。但它不起作用,并且失败了一个相当神秘的错误。

以下是重现此分析的设置详细信息和数据-

# setup
set.seed(123)
library(tidyverse)
library(robust)
#> Loading required package: fit.models
# data
df <- structure(list(value = c(0.833333333333333, 0, 0, 0, 0, 0.166666666666667, 
                         0.166666666666667, 0, 0, 0.166666666666667, 0, 0, 0, 0.333333333333333, 
                         0.333333333333333, 0, 0, 0, 0.5, 0, 0, 0.166666666666667, 0, 
                         0, 0.166666666666667, 0, 0, 0, 0, 0.166666666666667, 0, 0, 0.5, 
                         0.166666666666667, 0, 0, 0, 0.333333333333333, 0, 0.5, 0.333333333333333, 
                         0.5, 0, 0.166666666666667, 0.333333333333333, 0.166666666666667, 
                         0, 0, 0.166666666666667, 0, 0.833333333333333, 0, 0, 0.333333333333333, 
                         0, 0, 0.166666666666667, 0, 0.166666666666667, 0.5, 0, 0, 0, 
                         0.166666666666667, 0, 0, 0.166666666666667, 0.166666666666667, 
                         0, 0, 0, 0, 0, 0, 0, 0, 0.166666666666667, 0.166666666666667, 
                         0.166666666666667, 0.666666666666667, 0, 0, 0, 0.166666666666667, 
                         0.166666666666667, 0, 0.166666666666667, 0, 0, 0, 0.166666666666667, 
                         0, 0, 0, 0.166666666666667, 0, 0.333333333333333, 0.166666666666667, 
                         0.166666666666667, 0, 0.166666666666667, 0.166666666666667, 0, 
                         0, 0, 0, 0, 0.166666666666667, 0.166666666666667, 0, 0, 0, 0.333333333333333, 
                         0, 0, 0, 0, 0.333333333333333, 0, 0, 0, 0.166666666666667, 0.166666666666667, 
                         0.166666666666667, 0.166666666666667, 0, 0.166666666666667, 0.166666666666667, 
                         0.166666666666667, 0.166666666666667, 0, 0.666666666666667, 0.333333333333333, 
                         0.166666666666667, 0, 0.166666666666667, 0.333333333333333, 0.333333333333333, 
                         0, 0.166666666666667, 0, 0.166666666666667, 0.333333333333333, 
                         0.333333333333333, 0, 0.166666666666667, 0.166666666666667, 0.666666666666667, 
                         0.5, 0, 0, 0, 0, 0.666666666666667, 0.166666666666667, 0, 0.333333333333333, 
                         0.166666666666667), parameter = c(-0.26053156261281, 0.294881565797054, 
                                                           0.294881565797054, -0.260531562612809, -1.37135781943254, 0.294881565797054, 
                                                           0.850294694206917, -1.37135781943254, -0.260531562612809, -1.37135781943254, 
                                                           -0.26053156261281, 1.40570782261678, -0.26053156261281, 0.850294694206917, 
                                                           1.96112095102664, -0.260531562612809, -1.9267709478424, 0.850294694206917, 
                                                           1.40570782261678, 0.850294694206917, -1.37135781943254, -0.26053156261281, 
                                                           0.294881565797054, -2.48218407625226, 0.850294694206917, 1.40570782261678, 
                                                           -0.260531562612809, 0.294881565797053, 0.294881565797054, -0.260531562612809, 
                                                           -0.260531562612809, -1.37135781943254, 0.294881565797054, 0.294881565797054, 
                                                           0.294881565797054, 0.850294694206917, 1.96112095102664, 0.850294694206917, 
                                                           0.294881565797054, 0.294881565797054, -0.815944691022672, 0.294881565797054, 
                                                           0.850294694206917, -0.260531562612809, 0.850294694206917, -1.37135781943254, 
                                                           -1.37135781943254, -0.815944691022673, -1.37135781943254, -1.9267709478424, 
                                                           1.40570782261678, -1.37135781943254, 0.294881565797054, -0.815944691022673, 
                                                           0.850294694206917, 0.850294694206917, -0.260531562612809, 0.850294694206917, 
                                                           -0.26053156261281, 0.850294694206917, -1.37135781943254, -1.9267709478424, 
                                                           1.40570782261678, 1.96112095102664, 0.850294694206917, -0.815944691022672, 
                                                           -0.260531562612809, 0.850294694206917, 0.850294694206917, -0.815944691022673, 
                                                           0.294881565797053, -0.260531562612809, -0.260531562612809, -1.9267709478424, 
                                                           -0.815944691022673, -1.9267709478424, -1.9267709478424, 0.294881565797054, 
                                                           0.294881565797054, -1.9267709478424, 0.850294694206917, 0.850294694206917, 
                                                           -0.815944691022672, -0.815944691022672, -1.9267709478424, 1.40570782261678, 
                                                           -0.815944691022673, 0.294881565797054, 0.850294694206917, -1.37135781943254, 
                                                           0.850294694206917, 0.294881565797054, -0.260531562612809, -1.9267709478424, 
                                                           1.40570782261678, -0.815944691022673, -0.260531562612809, 1.40570782261678, 
                                                           0.294881565797054, 1.40570782261678, -0.815944691022672, -0.815944691022673, 
                                                           -0.815944691022673, 0.850294694206917, -1.37135781943254, 0.294881565797054, 
                                                           1.96112095102664, -0.260531562612809, -0.26053156261281, 0.294881565797054, 
                                                           0.850294694206917, 0.850294694206917, -0.260531562612809, 0.294881565797054, 
                                                           1.96112095102664, -0.260531562612809, 1.40570782261678, 1.96112095102664, 
                                                           -0.815944691022672, 0.294881565797054, -0.815944691022672, -0.815944691022672, 
                                                           -0.815944691022673, 0.850294694206917, -1.9267709478424, -1.37135781943254, 
                                                           -1.37135781943254, -0.815944691022672, 1.96112095102664, 0.850294694206917, 
                                                           -0.26053156261281, -0.815944691022672, 0.850294694206917, 0.294881565797054, 
                                                           0.294881565797053, -0.815944691022672, 0.850294694206917, -0.815944691022673, 
                                                           1.40570782261678, 1.96112095102664, -0.815944691022673, 0.294881565797054, 
                                                           0.850294694206917, -0.260531562612809, 0.294881565797054, 1.40570782261678, 
                                                           0.850294694206917, 1.96112095102664, -0.260531562612809, 2.51653407943651, 
                                                           0.294881565797054, 0.294881565797054, 0.294881565797053, -0.815944691022672, 
                                                           -1.37135781943254, -1.37135781943254, -1.37135781943254, 0.294881565797054
                         )), row.names = c(NA, -158L), class = c("tbl_df", "tbl", "data.frame"
                         ))

这是我得到的确切错误-

# running robust linear model
# using robust
robust::lmRob(
  data = df,
  formula = value ~ parameter,
  control = robust::lmRob.control(
    tlo = 1e-100,
    tua = 1.5e-100,
    tl = 1e-100,
    mxr = 10000,
    mxf = 10000,
    mxs = 10000,
    final.alg = "Adaptive"
  ),
  nrep = 100000
)
#> Error in paste(msg.UCV, " when refining initial estimates."): object 'msg.UCV' not found

另一方面,如果我使用robustbase包,它会给我一个可能的原因——

# using robustbase
robustbase::lmrob(data = df,
                  formula = value ~ parameter,
                  method = "MM")
#> Warning in lmrob.S(x, y, control = control): S-estimated scale == 0:
#> Probably exact fit; check your data
#> 
#> Call:
#> robustbase::lmrob(formula = value ~ parameter, data = df, method = "MM")
#> 
#> Exact fit detected
#> 
#> Coefficients:
#> (Intercept)    parameter  
#>           0            0

所以我想知道是否有任何方法可以用我拥有的数据类似地获得robust::lmRob函数。

这是因为我在分组变量的所有级别上运行相同的分析,并且该函数仅在一个级别上失败,因此整个分析失败。我更喜欢一种更故障安全的方法。

它看起来像一个错误。请参阅此处,其中警告出现在第 547 行中,但msg.UCV未在任何地方定义(实际上,仅在第 419 行中未达到(。鉴于变量在该阶段只是未定义,解决方法是在全局环境中定义它:

msg.UCV <- "A WORKAROUND"
robust::lmRob(
  data = df,
  formula = value ~ parameter,
  control = robust::lmRob.control(
    tlo = 1e-100,
    tua = 1.5e-100,
    tl = 1e-100,
    mxr = 10000,
    mxf = 10000,
    mxs = 10000,
    final.alg = "Adaptive"
  ),
  nrep = 100000
)
# Call:
# robust::lmRob(formula = value ~ parameter, data = df, nrep = 1e+05, 
#     control = robust::lmRob.control(tlo = 1e-100, tua = 1.5e-100, 
#         tl = 1e-100, mxr = 10000, mxf = 10000, mxs = 10000, final.alg = "Adaptive"))
# 
# Coefficients:
# (Intercept)    parameter  
#           0            0  
#
# Warning message:
# In lmRob.fit.compute(x, y, x1.idx = x1.idx, nrep = nrep, robust.control =  robust.control,  :
#   A WORKAROUND  when refining initial estimates.

相关内容

最新更新