Search code examples
rpredictloess

Why isn't the predict function predicting NA values?


I'm running into trouble here with something that should be easy, and I'm not sure why it isn't working. I have a dataframe modis.raw2, which has one column, day with all of the dates in the timeseries I am looking at. The other column, sr.og has values, some of which are NA.

I am trying to use lowess to smooth my data and fill in the NA values, but I keep getting an error:

Error in `$<-.data.frame`(`*tmp*`, sr.smooth, value = c(0.631508837997043,  : 
  replacement has 50 rows, data has 128

I'm really not sure what is going on. Any ideas? Here is a sample dataset and my code.

modis.raw2 = structure(list(day = c(86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 
96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 
109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 
122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 
135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 
148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 
161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 
174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 
187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 
200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 
213), sr.og = c(0.636095833333333, NA, NA, NA, NA, 0.665508333333333, 
NA, 0.67995, NA, 0.616720833333333, 0.695445833333333, 0.70355, 
0.687008333333333, NA, NA, 0.6801125, 0.662941666666667, 0.688133333333333, 
NA, NA, NA, NA, 0.441654166666667, 0.708595833333333, 0.698195833333333, 
0.696716666666667, 0.668525, 0.622370833333333, 0.632875, 0.617458333333333, 
NA, NA, NA, NA, NA, 0.64325, NA, 0.5615375, 0.547720833333333, 
NA, 0.551554166666667, 0.544858333333333, 0.539941666666667, 
NA, NA, 0.469941666666667, NA, 0.478479166666667, NA, 0.336470833333333, 
0.3274375, 0.350295833333333, 0.288670833333333, 0.131925, 0.2291625, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.0913333333333333, 
NA, 0.1071125, 0.1076, 0.100704166666667, 0.1160625, NA, 0.101416666666667, 
0.152929166666667, 0.1267625, NA, NA, NA, NA, NA, NA, NA, 0.1174125, 
0.217633333333333, NA, NA, NA, NA, 0.225133333333333, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.149670833333333, 0.154854166666667, 
0.239479166666667, NA, NA, NA, NA, NA, NA, 0.153670833333333, 
NA, 0.079125, NA, NA, NA, 0.208341666666667, NA, 0.102675, NA, 
NA, NA, NA, NA, NA, NA)), .Names = c("day", "sr.og"), row.names = c(NA, 
-128L), class = c("tbl_df", "tbl", "data.frame"))

sr.loess = loess(sr.og ~ day, data=modis.raw2, span=0.3)
complete <- tibble(day=modis.raw2$day)
complete$sr.smooth = stats::predict(sr.loess, data=complete)

Solution

  • The predict() function uses a parameter named newdata=, not data=. You were just extracting the fitted values here. Try

    complete$sr.smooth = stats::predict(sr.loess, newdata=complete)