I am trying to use function "lowess" in base package to smooth a vector. The question is that the vector only has one NA value, but after smoothing by "lowess", 4 more NAs appear. I searched and some one suggest using "lowess" in package "gplots". I tried but got same results.
x1 <- c(NA, 19.93621, 17.64789, 17.78488, 17.11141, 18.4648, 19.62629, 17.5737, 17.48582, 18.76946, 19.57138, 19.62812, 2.982385, -0.1320513)
x2 <- lowess(x1)
x2
$x
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14
$y
[1] NA NA NA NA NA 18.988279 18.563642 18.407768 18.496699 17.922510 14.419999 10.861535 7.179754 3.145612
One way is to delete the NA in x1 so there is no NA after smoothing in x2$y.
x2 <- lowess(x1[-1])
x2
$x
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13
$y
[1] 18.7079309 18.4481273 18.2491632 18.0847709 18.0946245 18.1282192 18.1497617 17.9298278 14.6441882 10.9465210 7.2635244 3.5247529 -0.3021372
But I just wonder why more NAs appear and is there an easier way? Thank you!
lowess
doesn't have any built-inNA
handlingProbably the easiest thing to do long term is learn to use
loess
(whose options and settings are slightly different), which does deal withNA
values (it has anna.action
argument, likelm
does, which by default should work much the way you want - however, in terms of syntax,loess
works much more likelm
than it does likelowess
).you can use
na.omit
directly on the arguments tolowess
; that way you don't need to specifically identify which observations are omitted.