r - How to predict new raster using model generated by cforest -
i use randomforest model predict class memberships. 'x' consists of 10 classes use train 'training_predictors' values extracted large rasterstack/brick. specific line of codes is:
r_tree<-randomforest(x ~. , data=training_predictors, ...)
then run 'predict' using model 'r_tree' apply rasterstack 'predictor_data', follow:
predictions<-predict(predictor_data, r_tree, filename=outraster, fun=predict na.rm=true, format="pcdisk", overwrite=true, progress="text", type="response").
the output raster use thematic map.
i use conditional inference trees mode 'cforest' instead of randomforest achieve same goals.
i understand 'predict' can used cforest, yet, have not been able generate raster files, such randomforest illustrated above.
it should run fine, may need add argument oob=true, , identify factors if there any.
example data
p <- matrix(c(48, 48, 48, 53, 50, 46, 54, 70, 84, 85, 74, 84, 95, 85, 66, 42, 26, 4, 19, 17, 7, 14, 26, 29, 39, 45, 51, 56, 46, 38, 31, 22, 34, 60, 70, 73, 63, 46, 43, 28), ncol=2) <- matrix(c(22, 33, 64, 85, 92, 94, 59, 27, 30, 64, 60, 33, 31, 9, 99, 67, 15, 5, 4, 30, 8, 37, 42, 27, 19, 69, 60, 73, 3, 5, 21, 37, 52, 70, 74, 9, 13, 4, 17, 47), ncol=2) # extract values points xy <- rbind(cbind(1, p), cbind(0, a)) v <- data.frame(cbind(xy[,1], extract(logo, xy[,2:3]))) colnames(v)[1] <- 'pa'
basic model
library(party) m1 <- cforest(pa~., control=cforest_unbiased(mtry=3), data=v) pc1 <- predict(logo, m1, oob=true) plot(pc1)
model factors
v$red <- as.factor(round(v$red/100)) logo$red <- round(logo[[1]]/100) m2 <- cforest(pa~., control=cforest_unbiased(mtry=3), data=v) f <- list(levels(v$red)) names(f) <- 'red' pc2 <- predict(logo, m2, oob=true, factors=f) plot(pc2)
by way, comes straight out of file of raster::predict
Comments
Post a Comment