# Filling in missing values in raster using R

I am looking for a robust way to fill in missing values in some rasters. They all have a single layer. Missing values consist of single pixels to medium sized patches. Rasters are around 1000 x 1000 pixels in size and the largest patches are like 20x20 pixels.

I'm tempted to use aregImpute in the Hmisc R package.

Has anyone used it for this purpose?

this approach looks seems very cool but I think it is only meant to produce aesthetically pleasing corrections.

Detailed explanation of this:

All the rasters (I have 36 in total) share the same extent, they overlap and are aligned. Each raster is a different variable, I gathered variable from various sources (remote sensing, topographic and climatological). The original rasters come in various resolutions. The smallest being 30m. From there up they get as high as 1km. I resampled everything using cubic convolution (all the variables are continuos) to 1km. I have another 1km raster where I have data of a variable of interest for some sampled points. So I trained a model using those points and the other rasters as covariates to be able to generate a full raster of that variable. Unfortunately most covariate rasters have some missing values in them, actually not much but I would want to eliminate the problem entirely.

I would like to use R for this.

• What is the reason for the missingness and why are you filling in the values? (Both of these things matter in the selection of an appropriate solution.) What exactly do you mean by "robust"? (It has a technical statistical sense but it's not apparent yet how that would apply here.) Commented Jun 26, 2013 at 5:00
• I am using the layers as covariates for a predictive model. The model I am using does not handle missing values, so It simply does not calculate the pixels with a missing value in any of the rasters, leaving holes in my "predicted layer". Maybe the word robust was poorly used, I apologize. What I would be looking for is that the imputation conserves the underlying relation between my covariates and my objective variable. I'm not sure how to call this, the manifold assumption? Commented Jun 26, 2013 at 5:11
• Depending on the variable, the missigness is caused by sensor faultiness or measurement error replaced by a missing value. Commented Jun 26, 2013 at 5:25
• Do your rasters overlap or not? If they do not overlap, or if the typical amount of overlap is only two or three rasters at any one point, then it would be difficult to get much value from `aregImpute`. Otherwise, that is a promising approach that would be even more attractive if you included spatial correlation terms in the model. Commented Jun 26, 2013 at 12:44
• Data missing due to sensing issues are always spatially correlated. I suspect that any reasonable method that accounts for this correlation, no matter how simple otherwise, would perform better than even the most sophisticated methods that neglect that correlation. The resampling could be an issue, but it is unclear what you have done. A more detailed explanation in your question would be welcome. (A good general principle is to perform your statistical analyses with original data rather than resampled data if you possibly can, to avoid artifacts of the resampling.) Commented Jun 26, 2013 at 15:59

I am the author of the R package `gapfill`, which is a flexible tool to predict missing values in spatio-temporal remote sensing data sets. https://CRAN.R-project.org/package=gapfill It could be helpful in your case.