I do spatial modelling of variable T (temperature). I use what is commonly used in literature - perform regression (using variables like altitude etc.) and then spatially interpolate the residuals using IDW. R package gstat seems to have this option:
interpolated <- idw(T ~ altitude, stations, grid, idp=6)
spplot(interpolated["var1.pred"])
But in the documentation of idw()
they write:
Function idw performs [...] . Don't use with predictors in the formula.
And actually, the result looks exactly like if only regression was performed, without spatial interpolation of the residuals. I know I can do it manually:
m1 <- lm(T ~ altitude, data = data.frame(stations))
Tres <- resid(m1)
res.int <- idw(Tres ~ 1, stations, grid, idp=6)
Tpred <- predict.lm(m1, grid)
spplot(SpatialGridDataFrame(grid, data.frame(T = Tpred + data.frame(res.int)['var1.pred'])))
But this have many drawbacks - the model is not in one object, so you cannot directly do summary, check for deviance, residuals and most importantly, do crossvalidation... everything will have to be done manually. So,
Is there a way how to do regression and IDW in one model in R?
Note that I don't want to use different method of spatial interpolation, because IDW is used in this area of modelling and was well tested for these purposes.