[Raster-commits] r465 - pkg/raster/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Aug 5 02:46:24 CEST 2009


Author: rhijmans
Date: 2009-08-05 02:46:22 +0200 (Wed, 05 Aug 2009)
New Revision: 465

Modified:
   pkg/raster/R/predict.R
Log:


Modified: pkg/raster/R/predict.R
===================================================================
--- pkg/raster/R/predict.R	2009-06-11 06:10:46 UTC (rev 464)
+++ pkg/raster/R/predict.R	2009-08-05 00:46:22 UTC (rev 465)
@@ -5,14 +5,28 @@
 }	
 
 setMethod('predict', signature(object='RasterStack'), 
-	function(object, model, filename="", datatype='FLT4S', filetype = 'raster', overwrite=FALSE, track=-1, ...) {
+	function(object, model, filename="", datatype='FLT4S', filetype = 'raster', overwrite=FALSE, track=-1, vartype=NA, ...) {
 		predrast <- raster(object)
 		filename(predrast) <- filename
 		dataType(predrast) <- datatype
 		
+		dataclasses <- attr(model$terms, "dataClasses")
+		f <- which(dataclasses == 'factor')
+		if (length(f) > 0) { 
+			haveFactor <- TRUE 
+		} else {
+			haveFactor <- FALSE
+		}
+		
 		if (dataContent(object) == 'all') {
 			indices <- 1:ncell(predrast)
 			rowvals <- data.frame( na.omit(cbind( indices, values(object, names=TRUE)) ) )
+			if (haveFactor) {
+				for (i in 1:length(f)) {
+					rowvals[,f[i]] <- as.factor(rowvals[,f[i]])
+				}
+			}
+			
 			predv <- indices
 			predv[] <- NA
 			predv[indices] <- predict(model, rowvals[,-1], ...)
@@ -26,7 +40,14 @@
 			predv <- 1:ncol(object)
 			for (r in 1:nrow(object)) {
 				object <- readRow(object, r)
+				
 				rowvals <- na.omit( cbind(myIdVar, values(object, names=TRUE)) )
+				if (haveFactor) {
+					for (i in 1:length(f)) {
+						rowvals[,f[i]] <- as.factor(rowvals[,f[i]])
+					}
+				}
+
 				indices <- rowvals[,1]
 				predv[] <- NA
 				if (length(indices) > 0) {



More information about the Raster-commits mailing list