Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing all NAs with smoothing spline

Tags:

r

zoo

spline

Below is the sample data (out of approximately 8000 rows of data). How can I replace all NAs with values from a smoothing spline fit to the rest of the data?

Date            Max Min Rain    RHM RHE
4/24/1981   35.9    24.7    0.0 71  37
4/25/1981   36.8    22.8    0.0 62  40
4/26/1981   36.0    22.6    0.0 47  37
4/27/1981   35.1    24.2    0.0 51  39
4/28/1981   35.4    23.8    0.0 61  47
4/29/1981   35.4    25.1    0.0 67  43
4/30/1981   37.4    24.8    0.0 72  34
5/1/1981      NA      NA     NA NA  NA
5/2/1981    39.0    25.3     NA NA  55
5/3/1981    35.9    23.0    0.0 68  66
5/4/1981    28.4    22.4    0.7 70  30
5/5/1981    35.5    24.6    0.0 47  31
5/6/1981    37.4    25.5    0.0 51  31
like image 356
Mudit Avatar asked Sep 09 '13 09:09

Mudit


People also ask

What are smoothing splines used for?

Smoothing Splines as a Blending Method Smoothing splines are useful in smoothing noisy time series. The degree of smoothing is controlled by a single parameter, which plays a role much like the K value in Bühlmann credibility.

Are cubic splines smooth?

The most common case considered is k = 3, i.e., that of cubic splines. These are piecewise cubic functions that are continuous, and have continuous first, and second derivatives. Note that the continuity in all of their lower order derivatives makes splines very smooth.


1 Answers

One thing to check out might be the na.spline function in the zoo package. It appears custom designed for this purpose.

Missing values (NAs) are replaced by linear interpolation via approx or cubic spline interpolation via spline, respectively.

like image 95
ozjimbob Avatar answered Sep 28 '22 16:09

ozjimbob