## Not run:
# ## Example 1: mean ----------------------------------
# ## define a predict function
# PredictMean <- function (a, i) mean(a, na.rm = TRUE)
#
# out1 <- Gapfill(data = ndvi, fnPredict = PredictMean)
# Image(out1$fill)
#
# ## start with a smaller subset
# args(Subset)
# out2 <- Gapfill(data = ndvi, fnPredict = PredictMean,
# initialSize = c(0, 0, 1, 6))
# Image(out2$fill)
#
# ## require at least "nNotNA" non-NA values
# ## return predicted value and number of iterations i
# PredictMean2 <- function (a, i, nNotNA) {
# if (sum(!is.na(a)) < nNotNA)
# return (c(NA, NA))
# c(mean(a, na.rm = TRUE), i)
# }
# out3 <- Gapfill(data = ndvi, fnPredict = PredictMean2, nPredict = 2,
# initialSize = c(0, 0, 1, 6), nNotNA = 0)
# stopifnot(identical(c(out2$fill), c(out3$fill[,,,,1])))
# Image(out3$fill[,,,,2]) # number of used iterations i
#
# out4 <- Gapfill(data = ndvi, fnPredict = PredictMean2, nPredict = 2,
# initialSize = c(0, 0, 1, 6), nNotNA = 50)
# Image(out4$fill[,,,,1]) # fill values
# Image(out4$fill[,,,,2]) # number of used iterations i
#
#
# ## Example 2: Score() and lm() ----------------------
# PredictLm <- function (a, i, nNotNA = 50, minScores = 2){
# if (sum(!is.na(a)) < nNotNA)
# return (NA)
# am <- Array2Matrix(a)
# sx <- Score(t(am))
# lsx <- length(sx)
# if (lsx < minScores)
# return (NA)
# sy <- Score(am)
# lsy <- unique(length(sy))
# if (lsy < minScores)
# return (NA)
# df <- data.frame(z = c(am),
# sx = rep(sx, ncol(am)),
# sy = rep(sy, each = nrow(am)))
# newdata <- df[IndexTwoOne(attr(am, "mp"), dim(am)),]
# m <- lm(z ~ sx * sy, data = df)
# predict(m, newdata = newdata)
# }
#
# ## test PredictLm() by running it
# ## manually for one missing value
# mp <- IndexOneFour(which(is.na(ndvi))[1], dim(ndvi))
# a <- Subset(data = ndvi, mp = mp, i = 0)
# PredictLm(a = a, i = 0)
#
# ## run PredictLm() on ndvi data
# out5 <- Gapfill(data = ndvi, fnPredict = PredictLm,
# nNotNA = 50)
# Image(out5$fill)
# ## End(Not run)
Run the code above in your browser using DataLab