I need to smooth some simulated data, but occasionally run into problems when the simulated ordinates to be smoothed are mostly the same value. 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 analogue in MATLAB produce the expected results without error on this example. I am using loess.smooth here because I need the estimates evaluated at a set number of points. According to the documentation, I believe loess.smooth and loess are using the same estimation functions, but the former is an “auxiliary function” to handle the evaluation points. The error seems to come from a 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 appears to be different arguments. Of course, if you vary enough of the y values to be nonzero, loess.smooth runs without error, but I need the program to run in even the most extreme case.
Hopefully, someone can help me with one and/or all of the following:
- Understand why only
loess.smooth, and not the other functions, produces this error and find a solution for this problem. - Find a work-around using
loessbut still evaluating the estimate at a specified number of points that can differ from the vector x. For example, I might want to use onlyx <- seq(0,50,10)in the smoothing, but evaluate the estimate atx <- 0:50. As far as I know, usingpredictwith a new data frame will not properly handle this situation, but please let me know if I am missing something there. - Handle the error in a way that doesn’t stop the program from moving onto the next simulated data set.
Thanks in advance for any help on this problem.
For part 1:
This took a bit of tracking down, but if you do:
loess.smooth(x, y, family = "guassian")the model will fit. This arises due to the different defaults of
loess.smoothandloess; the former hasfamily = c("symmetric", "gaussian")whilst the latter has it reversed. If you trawl through the code forloessandloess.smooth, you’ll see that whenfamily = "gaussian"iterationsis set to1. Otherwise it takes the valueloess.control()$iterations. If you do iterations insimpleLoess, the following function call returns a vector ofNaN:Which causes the next function call to throw the error you saw:
This all relates to robust fitting in Loess (the method). If you don’t want/need a robust fit, use
family = "gaussian"in yourloess.smoothcall.Also, note that the defaults for
loess.smoothdiffer from those ofloess, e.g. for'span'and'degree'. So carefully check out what models you want to fit and adjust the relevant function’s defaults.For part 2:
Which gives:
The default won’t extrapolate if that was what you meant. I don’t see what the problem with using
predicthere is at all, in fact.For part 3:
Look at
?tryand?tryCatchwhich you can wrap round the loess fitting function (loess.smoothsay), which will allow computations to continue if an error inloess.smoothis encountered.You will need to handle the output of
tryortryCatchby including something like (if you are doing this in a loop:I would probably combine
tryortryCatchwith fitting vialoessand usingpredictfor such a problem.