0

I am trying to check the results of IDW interpolation by leave-one-out cross validation and then get the RMSE to see the quality of the prediction.

From github Interpolation in R, I found some hints and apply it in my case as following:

I have 63 locations which is saved as a SpatialPointDataFrame, named x_full_utm_2001. For each location, there is attached precipitation data, named sumdata_2001.

idw.out<- vector(length = length(sumdata_2001$Jan))
for (i in 1:length(sumdata_2001$Jan)) {
  idw.out[i]<-idw(sumdata_2001$Jan~1, x_full_2001_utm[-i, ], x_full_2001_utm[i, ])$var1.pred
}

But I don't know why there is always error warning me as follows:

dimensions do not match: locations 124 and data 63

I'm wondering why it works out like this. How should I revise this?

soundslikeodd
  • 1,078
  • 3
  • 19
  • 32
Yanan
  • 1
  • 2
  • My guess is NA's in your data. This remains to be seen as your example is not reproducible. Also, the `gstat` package supports performing cross-validation, so there is no need to write your own loop. Finally, I recommend using 10-fold cross-validation, i.e. leave out 10% of the data and not 1 at a time. – Paul Hiemstra Jan 24 '17 at 15:27
  • Thank you for your reply! I processed my data before this step. There shouldn't be any NA value. In `gstat` package, I noticed one that could apply cross validation, called `krige.cv`. Is it also working with IDW interpolation? – Yanan Jan 24 '17 at 15:50
  • `@Paul Hiemstra` I tried this `krige.cv` using the following code: `idw.out<- krige.cv(sumdata_2001$Jan~1, x_full_2001_utm, nfold=nrow(x_full_2001_utm))`. The warning is still that `dimensions do not match: locations 124 and data 63` Is it because of some wrong settings? – Yanan Jan 24 '17 at 15:58
  • Please make your example [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), otherwise it is hard to provide feedback. In addition, if you use the correct syntax `krige.cv` will use `idw`. See the `gstat` documentation for more details. – Paul Hiemstra Jan 25 '17 at 09:06

0 Answers0