rregressionrobust

robust regression not working - "object 'msg.UCV' not found"


I am trying to run robust linear regression using the robust package in R. But it doesn't work and fails with a rather cryptic error.

Here are the setup details and data to reproduce this analysis-

# 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"
                         ))

This is the exact error I get-

# 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

On the other hand, if I use robustbase package, it gives me a possible reason as to why-

# 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

So I wanted to know if there is any way I can similarly get the robust::lmRob function with the data I have.

This is because I am running the same analysis across all levels of a grouping variable and the function fails for only one level and, as a result, the entire analysis fails. I would much prefer a more failsafe approach.


Solution

  • It looks like a bug. See here, where the warning appears in line 547, but msg.UCV is not defined anywhere around (in fact, only in line 419, which is not reached). Given that a variable is simply undefined at that stage, a workaround would be to define it in the global environment:

    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.