How to suppress warnings from stats:::regularize.values?

use*_*250 5 warnings r

In newer versions of R (I have 3.6 and previously had 3.2), the stats::regularize.values function has been changed to have a default value of warn.collapsing as TRUE. This function is used in splinefun and several other interpolation functions in R. In a microsimulation model, I am using splinefun to smooth a large amount (n > 100,000) of data points of the form (x, f(x)). Here, x is a simulated vector of positive-valued scalers, and f(x) is some function of (x). With an n that large, there are often some replications of pseudo-randomly generated values (i.e., not all values of x are unique). My understanding is that splinefun gets rid of ties in the x values. That is not a problem for me, but, because of the new default, I get a warning message printed each time (below)

“在regularize.values(x,y,关系,missing(ties))中:折叠为唯一的'x'值”

有没有办法将stats::regularize.values函数的warn.collapsing参数的默认值改回F?还是我可以以某种方式禁止该警告?这很重要,因为它嵌入了很长的微仿真代码中,当我对其进行更新时,我经常会遇到错误。因此,我不能只是忽略警告消息。

我尝试使用形式化功能。我能够获取默认的print参数stats::regularize.values,但是当我尝试使用该alist函数分配新值时,它说没有对象'stats'。

小智 7

我也遇到了这个问题,并通过添加ties=min到 的参数列表来修复它splinefun()。的值missing(ties)现在被传递warn.collapsingregularize.values()

https://svn.r-project.org/R/trunk/src/library/stats/R/splinefun.R
https://svn.r-project.org/R/trunk/src/library/stats/R /约.R

另请参阅: https ://cran.r-project.org/doc/manuals/r-release/NEWS.html 并搜索regularize.values().

  • 类似的策略适用于“approx”(明确指定“ties”参数) (3认同)

Mak*_*212 3

参考这篇文章

regularize.values像这样包装你的电话:

withCallingHandlers(regularize.values(x), warning = function(w){
  if (grepl("collapsing to unique 'x' values", w$message))
   invokeRestart("muffleWarning")
})
Run Code Online (Sandbox Code Playgroud)

工作示例(改编自上面的链接以调用函数):

f1 <- function(){
  x <- 1:10
  x + 1:3
}

f1()

# if we just call f1() we get a warning
Warning in x + 1:3 :
  longer object length is not a multiple of shorter object length
 [1]  2  4  6  5  7  9  8 10 12 11


withCallingHandlers(f1(), warning=function(w){invokeRestart("muffleWarning")})
 [1]  2  4  6  5  7  9  8 10 12 11
Run Code Online (Sandbox Code Playgroud)

  • 感谢您的回答。所以,我实际上并没有调用regularize.values函数。它是我使用的 splinefun 函数的一部分。splinefun 函数的第一行是使用regularize.values。不过,你的建议确实给了我一个想法。我最终做的是使用 fix 函数来更改 splinefun 函数代码中的regularize.values 调用中的最后一个参数(从“missing(ties)”到“F”)。然而,在这样的函数中调整代码让我感到紧张。 (2认同)