Error using `loess.smooth`, but not` loess` or `lowess`

I need to smooth out some of the simulated data, but sometimes I run into problems when the simulated ordinates that need to be smoothed out basically have the same meaning. Here is a small reproducible example of the simplest case.

> x <- 0:50 > y <- rep(0,51) > loess.smooth(x,y) Error in simpleLoess(y, x, w, span, degree, FALSE, FALSE, normalize = FALSE, : NA/NaN/Inf in foreign function call (arg 1) 

loess(y~x) , lowess(x,y) , and their counterpart in MATLAB gives the expected results without errors in this example. I use loess.smooth here because I need grades rated with a given number of points. According to the documentation, I believe that loess.smooth and loess use the same evaluation functions, but the former is a “helper function” for processing the evaluation points. The error seems to come from the C function:

 > traceback() 3: .C(R_loess_raw, as.double(pseudovalues), as.double(x), as.double(weights), as.double(weights), as.integer(D), as.integer(N), as.double(span), as.integer(degree), as.integer(nonparametric), as.integer(order.drop.sqr), as.integer(sum.drop.sqr), as.double(span * cell), as.character(surf.stat), temp = double(N), parameter = integer(7), a = integer(max.kd), xi = double(max.kd), vert = double(2 * D), vval = double((D + 1) * max.kd), diagonal = double(N), trL = double(1), delta1 = double(1), delta2 = double(1), as.integer(0L)) 2: simpleLoess(y, x, w, span, degree, FALSE, FALSE, normalize = FALSE, "none", "interpolate", control$cell, iterations, control$trace.hat) 1: loess.smooth(x, y) 

loess also calls simpleLoess , but with what seems like a different argument. Of course, if you change enough y values ​​to be non-zero, loess.smooth works without errors, but I need a program to work even in the most extreme case.

Hope someone can help me with one and / or all of the following:

  • Understand why only loess.smooth , and not other functions, throws this error and finds a solution to this problem.
  • Find a job using loess , but still evaluating the score at a certain number of points that may differ from the vector x. For example, I can only use x <- seq(0,50,10) in smoothing, but evaluate the score at x <- 0:50 . As far as I know, using predict with a new data frame will not handle this situation correctly, but please let me know if I'm missing something there.
  • Handle the error so that the program does not go to the next simulated data set.

Thank you in advance for your help in resolving this issue.

+7
source share
2 answers

For part 1: It took a bit of tracking, but if you do this:

loess.smooth(x, y, family = "guassian")

the model will do. This is due to the different defaults of loess.smooth and loess ; the former has family = c("symmetric", "gaussian") , while the latter has the opposite side. If you go through the code for loess and loess.smooth , you will see that when family = "gaussian" iterations set to 1 . Otherwise, it takes on the value loess.control()$iterations . If you iterate over simpleLoess , the following function call returns the NaN vector:

 pseudovalues <- .Fortran(R_lowesp, as.integer(N), as.double(y), as.double(z$fitted.values), as.double(weights), as.double(robust), integer(N), pseudovalues = double(N))$pseudovalues 

Which causes the following function call to throw the error you saw:

 zz <- .C(R_loess_raw, as.double(pseudovalues), as.double(x), as.double(weights), as.double(weights), as.integer(D), as.integer(N), as.double(span), as.integer(degree), as.integer(nonparametric), as.integer(order.drop.sqr), as.integer(sum.drop.sqr), as.double(span * cell), as.character(surf.stat), temp = double(N), parameter = integer(7), a = integer(max.kd), xi = double(max.kd), vert = double(2 * D), vval = double((D + 1) * max.kd), diagonal = double(N), trL = double(1), delta1 = double(1), delta2 = double(1), as.integer(0L)) 

All this relates to a reliable installation in Loess (method). If you don't want / need a reliable fit, use family = "gaussian" in your loess.smooth call.

Also note that the default values ​​for loess.smooth are different from loess values, for example. for 'span' and 'degree' . Therefore, carefully check which models you want to fit and configure the appropriate default values.

For part 2:

 DF <- data.frame(x = 0:50, y = rep(0,51)) mod <- loess(y ~ x, data = DF) pred <- predict(mod, newdata = data.frame(x = c(-1, 10, 15, 55))) mod2 <- loess(y ~ x, data = DF, control = loess.control(surface = "direct")) pred2 <- predict(mod2, newdata = data.frame(x = c(-1, 10, 15, 55))) 

What gives:

 > pred 1 2 3 4 NA 0 0 NA > pred2 1 2 3 4 0 0 0 0 

The default value will not be extrapolated if that was what you had in mind. I do not see that there is a problem with using predict at all.

For part 3: Look at ?try Try and ?tryCatch , which you can wrap around the loess selection function ( loess.smooth say), which will allow you to continue the calculation if an error occurs in loess.smooth .

You need to handle the output of try or tryCatch by including something like this (if you do this in a loop:

 mod <- try(loess.smooth(x, y)) if(inherits(mod, "try-error")) next ## if here, model work, do something with `mod` 

I would probably combine try or tryCatch with installation via loess and using predict for such a problem.

+7
source

This is the first time I've come across these functions, so I can't help you, but could it have something to do with variance 0 in y values? Now you are trying to evaluate a smooth line from data that is already as smooth as it is, and this works:

 x <- 0:50 y <- c(rep(0,25),rep(1,26)) loess.smooth(x,y) 
0
source

All Articles