[R-sig-Geo] how to export results of predict.randomForest

Anne Axel axelanne at msu.edu
Thu Dec 25 19:01:43 CET 2008


Hi Robert and list,

Thanks so much for the Christmas message!  I tried your great 
suggestion and I got the following error:

 > spgrid <- SpatialGridDataFrame(bands, output)
Error in rbind(grid at cellcentre.offset, grid at cellcentre.offset + 
(grid at cells.dim -  :
   no slot of name "cellcentre.offset" for this object of class 
"SpatialGridDataFrame"

So, I examined the structure of both files (see below). In addition, 
there are no NAs in "imgtabla" or "bands", but there are NAs in the 
rf.prediction ("output").

I tried to  writeGDAL(bands, "test2.tif") and this worked without any 
errors.  Could it have something to do with the fact that my output 
is of class factor?

Again, thanks Robert,  for setting me on the right path.  Any other 
suggestions out there to clear this last hurdle?

Best,
Anne Axel

 > str(bands)
Formal class 'SpatialGridDataFrame' [package "sp"] with 6 slots
   ..@ data       :'data.frame': 163982 obs. of  55 variables:
   .. ..$ band1 : num [1:163982] 0 0 0 0 0 0 0 0 0 0 ...
   .. ..
   ..@ grid       :Formal class 'GridTopology' [package "sp"] with 3 slots
   .. .. ..@ cellcentre.offset: Named num [1:2] 0.5 0.5
   .. .. .. ..- attr(*, "names")= chr [1:2] "x" "y"
   .. .. ..@ cellsize         : num [1:2] 1 1
   .. .. ..@ cells.dim        : int [1:2] 442 371
   ..@ grid.index : int(0)
   ..@ coords     : num [1:2, 1:2]   0.5 441.5   0.5 370.5
   .. ..- attr(*, "dimnames")=List of 2
   .. .. ..$ : NULL
   .. .. ..$ : chr [1:2] "x" "y"
   ..@ bbox       : num [1:2, 1:2] 0 0 442 371
   .. ..- attr(*, "dimnames")=List of 2
   .. .. ..$ : chr [1:2] "x" "y"
   .. .. ..$ : chr [1:2] "min" "max"
   ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots
   .. .. ..@ projargs: chr " +proj=utm +zone=38 +south +a=6378137 
+datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0"

 > str(output)
  Factor w/ 4 levels "dry","gallery",..: 3 3 3 3 3 3 3 3 3 3 ...


 > class(bands)
[1] "SpatialGridDataFrame"
attr(,"package")
[1] "sp"


 > class(output)
[1] "factor"

 > summary(output)
       dry   gallery notforest     spiny      NA's
     33009      5878    103772     21315         8









At 12/24/2008 09:02 PM, you wrote:
>Hi Anne,
>
>The first argument of writeGDAL is "an object of class 
>SpatialGridDataFrame-class or SpatialPixelsDataFrame-class"
>So you need to create a new SpatialGridDataFrame with the spatial 
>properties of "multibands3.img" and with the RandomForest 
>predictions ("output"). Note that there are some additional 
>complications when there are NAs in your predictors (i.e. in 
>"imgtabla"), but if there are none, I think that something like this 
>might work:
>
>spgrid <- SpatialGridDataFrame(bands, output)
>writeGDAL(spgrid, "rf_results.tif")
>
>Robert
>
>On Thu, Dec 25, 2008 at 4:58 AM, Anne Axel 
><<mailto:axelanne at msu.edu>axelanne at msu.edu> wrote:
>Hi List,
>
>I am requesting help with exporting results from the 
>predict.randomForest function.  This is my first attempt at working 
>with spatial data in R--I was able to import the data and run the 
>randomForest model, but I cannot figure out how to export the data 
>for use in ArcGIS or ENVI.
>
>I've pasted below the code I used for randomForest and exporting of 
>results.  If anyone has suggestions, I would be eternally 
>grateful!  Thanks, and Happy Holidays.
>
>Anne Axel
>
>
>Department of Fisheries and Wildlife
>Michigan State University
>20 Natural Resources Building
>East Lansing, MI 48824 USA
>517-930-2248
><mailto:axelanne at msu.edu>axelanne at msu.edu
>
>
>####Read in *img file with 55 bands
>bands=readGDAL("multibands3.img")
>imgtabla <- as(bands, "data.frame")
>
>######Read in training data and run randomForest
>data=read.csv("bandnumsOutput_cond2.csv", header=TRUE)
>data2=data[,1:58]
>data.rf=randomForest(COVERTYPE~ ., data=data2, mtry=16, importance = 
>TRUE, do.trace=100)
>print(data.rf)
>output=predict(data.rf, imgtabla)
>
>#### I've tried numerous ways of exporting the data, but I don't 
>seem to have the data in a format that's acceptable to the 
>functions. In addition, I've tried to coerce data into other 
>formats, but nothing has worked.  Any suggestions?
>
>
> > writeGDAL(bands, output, drivername = "GTiff", type = "Float32", 
> mvFlag = NA, options=NULL)
>Error in .local(.Object, ...) :
>  STRING_ELT() can only be applied to a 'character vector', not a 'integer'
>In addition: Warning messages:
>1: In if (nchar(fname) == 0) stop("empty file name") :
>  the condition has length > 1 and only the first element will be used
>2: In if (nchar(filename) == 0) stop("empty file name") :
>  the condition has length > 1 and only the first element will be used
>
> > writeGDAL(output, bands, drivername = "GTiff", type = "Float32", 
> mvFlag = NA, options=NULL)
>Error in nchar(fname) : cannot coerce type S4 to character vector
>
> > write.ENVI(output)
>Error in dim(X) = c(nRow, nCol, nBand) : length-0 dimension vector is invalid
>
> > write.ascii.grid(output, "output", header = NULL, write.header = TRUE)
>Error: is.matrix(data) is not TRUE
>
>
>
>
>
>Anne C. Axel
>Department of Fisheries and Wildlife
>Michigan State University
>20 Natural Resources Building
>East Lansing, MI 48824 USA
>517-930-2248
><mailto:axelanne at msu.edu>axelanne at msu.edu
>
>_______________________________________________
>R-sig-Geo mailing list
><mailto:R-sig-Geo at stat.math.ethz.ch>R-sig-Geo at stat.math.ethz.ch
>https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

" Know your own bone. Gnaw at it, bury it, unearth it, and gnaw it still."
         -Henry David Thoreau

"Outside of a dog, a book is a man's best friend. Inside of a dog, 
it's too dark to read."
         - Groucho Marx

Anne C. Axel
Department of Fisheries and Wildlife
Michigan State University
20 Natural Resources Building
East Lansing, MI 48824 USA
517-930-2248
axelanne at msu.edu




More information about the R-sig-Geo mailing list