[GRASS-user] Grass to R and back again (randomForest classification)

Daniel Victoria daniel.victoria at gmail.com
Fri Jun 11 09:32:47 EDT 2010


Thanks Nikos, it worked like a charm!
I'd also like to thaks Ned Horning for some greatlly aprecciated
off-list R help!

Cheers
Daniel

On Fri, Jun 11, 2010 at 9:39 AM, Nikos Alexandris
<nikos.alexandris at felis.uni-freiburg.de> wrote:
> Daniel Victoria:
>> I'm trying to do a randomForest classification in a MODIS NDVI time
>> series. So far I've been able to generate the randomForest and get the
>> Grass NDVI images inside grass as a SpatialGridDataFrame. Then,
>> following some notes from Markus Neteler [1], I converted the
>> SpatialGridDataFrame to a DataFrame and sucessfully applied the
>> randomForest classifier. The problem is that now I'm struglling to
>> transform the DataFrame back to a grass image. What is giving me a
>> headach is that the images contains lots of null values (I need to
>> have a MASK in place) so, each NDVI image has 1023701 cells but the
>> DataFrame has only 264647 values since the conversion from SpatialGrid
>> to DataFrame skips the nulls.
>
>> So, the question is, how to convert my DataFrame back to a Grass image?
>
> Daniel,
> so far I used the following steps:
>
> # import raster data in R using "readRAST6" of course
> x.raw <- readRAST6 (SomeRasterMap) # or readRAST6 (SomeRasterMap ,  NODATA =
> -999999 )
>
> # use complete cases to deal with NA's
> x.nonas <- complete.cases ( x at data )
>
> # get values
> x <- x.raw at data[x.nonas,]
>
> # add new columns to "x.raw" that will be fed with... "newvalues"
> x.raw at data$column1 <- NA
> x.raw at data$column2 <- NA
> [etc.]
>
> # do something with your data or create new data.frame(s)
>
> # fill in the new (empty) columns of xraw
> x.raw at data$column1[x.nonas] <- new.data.frame$newslot[,"newcolumn"]
> [etc.]
>
> # write back to grass
> writeRAST6(x.raw, zcol= NumberOfNewColumn, vname="SomeNameForTheRaster",
> overwrite=TRUE)
>
> Something like that... Hope it helps,
> Nikos
>
> ---
>> [1] http://mpa.fbk.eu/markus/shortcourse/notes7.html
>
> ---
>> PS - For completion sake...
>> Using Grass 6.4.0RC6 in Ubuntu 9.10
>> R 2.11
>>
>> commands used in R:
>>
>> # open images
>> ndvi <- readRAST6(<list of 23 ndvi images>)
>>
>> # convert to dataframe
>> ndvi.df <- as.data.frame(ndvi)
>>
>> class <- predict(RFmodel, ndvi.df)
>>
>> # class contains 264647 classified pixels - how to get them back to an
>> image?
>


More information about the grass-user mailing list