[GRASS-user] Fuzzy K-mean

Robert A Brown brown782 at purdue.edu
Thu Apr 11 15:52:19 PDT 2013

Hi Tom,

I am an avid R user and forgot about the spgrass6 addon. I have linked to grass through R and read in my rasters with the following script. The process worked beautifully and I appreciate you reminding me of the package and process!!! 

Script for reading the rasters


initGRASS("C:/Program Files (x86)/GRASS GIS 7.0.svn", gisDbase="C:/Users/brown782/Desktop",location="grass_data",mapset ="PERMANENT", override=T) 
rasters <- readRAST6(c("slope_pct", "wetness_indx", "nomralized_height"), cat=c(T, F, F), useGDAL=T)

 I decided to use the EMCluster package in R instead of the cmeans package.  Yesterday, I only tried to work in the R interface with the raster and cmeans("e1071") packages and read in the grids as ascii files. My issue was that when I stacked the grids and tried using cmeans it seemed that the fuzzy cluster process did not seem to understand that my stacked grids were spatial data. 

So now that I am using the EMCluster package R is giving me an error that the "rasters" object needs to be a matrix. I used the following script to convert the data to a matrix.

rasters2 = as.matrix(rasters)

It seemed to work but R gave me the below warning that the Spatial.Data.Frame

In as.matrix.SpatialGridDataFrame(rasters) :
  as.matrix.SpatialPixelsDataFrame uses first column;
 pass subset or [] for other columns

It seems that the as.matrix function somehow combined the three rasters into one because when I looked at the dimensions of the newly formed matrix it only gave me one set of dimensions. All of the rasters have the same dimensions. Where the Dimensions are below for the three rasters and the newly formed matrix.

slope_pct = raster: 
rows:     6750
columns   9152

wetness_indx = raster: 
rows:     6750
columns   9152

normalized_height = raster
rows:     6750
columns   9152

rasters2 = Matrix 
rows:     6750
columns   9152

can I convert the three individual grids to their own matrix then in run the EMCluster with the following script?

results = simple.init(slope_pct, wetness_indx, normalized_height, nclass = 5) 
results = simple.init(slope_pct + wetness_indx + normalized_height, nclass = 5) 

I tried the second above option and it failed to produce any kind of meaningful cluster it just gave me a short list of NA and NAN Errors.

Below I used the following code for the EMCluster package with the "rasters2" matrix but would like to replace it with either of the codes above? 

results = simple.init(rasters2, nclass = 5) 

This may be something I need to post in the R discussion forum.

Thanks for all of your help!

Robert Brown

----- Original Message -----
From: "Thomas Adams" <tea3rd at gmail.com>
To: "Robert A Brown" <brown782 at purdue.edu>
Cc: grass-user at lists.osgeo.org, "Markus Metz" <markus.metz.giswork at gmail.com>
Sent: Thursday, April 11, 2013 1:30:25 PM
Subject: Re: [GRASS-user] Fuzzy K-mean


I would recommend a combination of GRASS and R ( http://cran.r-project.org/ ). R has fuzzy K-mean cluster capability, using the R addon package spgrass6 to read/write to/from GRASS and R. Also, see this: http://www.r-bloggers.com/fuzzy-clustering-with-fanny/ and this: http://stats.stackexchange.com/questions/31906/fuzzy-k-means-cluster-sizes as starting points. 


On Thu, Apr 11, 2013 at 12:48 PM, Robert A Brown < brown782 at purdue.edu > wrote: 

Hello All, 
I am trying to find a way to process a fuzzy K-mean cluster using multiple rasters/grids in GRASS. Is anyone aware of a grass add-on or another simple process I can use to accomplish this? 


Robert Brown 
PhD Student 
Purdue University 
Department of Agronomy 

grass-user mailing list 
grass-user at lists.osgeo.org 

More information about the grass-user mailing list