Predict gam in a loop on multiple raster stacks and keeping identical layer names
Kristin, I think you can do something like this:
lst <- list()
for(i in 1:96) {
Predictors <- stack(stackchl[[i]], stacksst[[i]], stackpar[[i]], lat, lon)
names(Predictos) <- c('chl', 'sst', 'par', 'lat', 'lon')
lst[[i]] <- predict(Predictors, gammodel, na.rm=TRUE, type="response")
}
s <- stack(lst)
On Tue, May 5, 2015 at 8:54 AM, Kristin <nessnjor at gmail.com> wrote:
I have a gam model with predictor names (variables) par, chl, sst, lat and
lon. The model I called "gammodel".
Then I have predictors for 96 day period in raster stacks: "stackpar",
"stackchl", "stacksst"
-that is 3 different raster stacks - each one with 96 layers.
lat and lon are fixed in time and are each a separated raster with the
central value of each cell as a value in the raster.
All stacks and rasters have same extent, dimention, projection etc.
dim (84 840 96)
I want predict for each day for each pixel in a loop and create an output as
a raster stack of predictions for each of the 96 days
Something like this:
Stack <- stack()
lenght(Stack) <-96
for(i in 1:96) {
Predictors <- stack(stackchl[[i]],stacksst[[i]],stackpar[[i]],lat, lon)
Pred <- predict(Predictors,gammodel, na.rm=TRUE, type="response")
Stack[i] <- Pred
}
I?m not sure I?m doing the loop correct? Any suggestions of improvement?
BUT the main problem is that the layers for each predictive varaible have
names which
do not fit with the model. When I run it outside the loop for a single
layer:
Error in eval(expr, envir, enclos) : object 'par' not found
In addition: Warning message:
In predict.gam(model, blockvals, ...) :
not all required variables have been supplied in newdata!
I have tried to rename the layers in each raster stack like this:
names (par)=rep("par",96)
But the names are changed to: par.1, par.2, par.3, etc..
So they do not fit the variable name in the gam model itself.
all the best and thanks in advance
--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/Predict-gam-in-a-loop-on-multiple-raster-stacks-and-keeping-identical-layer-names-tp7588176.html
Sent from the R-sig-geo mailing list archive at Nabble.com.
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo