[GRASS-user] grass/stats mapping/prediction question

Daniel McInerney daniel.mcinerney at ucd.ie
Wed Dec 19 06:36:53 EST 2007


Hi Andy,

 > I am unsure how to then move the 'outmap' back across to grass.
 > How can i convert the df to a spatial grid object.

AFAIK, 'predict' won't create a dataframe object.
R should return FALSE for is.data.frame(outmap) and
TRUE for is.numeric(outmap)

You can slot the model output to the AttributeList of
one of the SpatialGridDataFrames that you created when
you read in a GRASS raster and then use writeRAST6 to
write it back to GRASS.

e.g. using 'anmax' from your example

anmax$anmax <- outmap
#if that doesn't work, you might try
anmax$anmax <- as.numeric(outmap)
writeRAST6(anmax, "NameOfNewGRASSRaster", "anmax")

Regards,
Daniel.

cc: grass-stats at lists.osgeo.org

andrew haywood wrote:
> Dear List,
>  
> I am having some problems analysing some ecoligical models in grass 
> using the spgrass package through R.
>  
> I have 130 plot locations where i have observed presence/absence  of a 
> species.
> I have followed a similar framework to the BUGSITE modelling 
> example from Markus's 2003 grass gis handouts (Grass 5)
> I have no problems constructing the model based on the 130 plots and the 
> environmental layers from grass.
>  
> However, I am having problems bringing all the maps through into R so I 
> can make a prediction map.
> The region isnt too large 1600 by 800 cells at 10m resolution
> I can bring all the environmental layers through to R using readRAST6() 
> which doesnt take too much time at all.
>  
> However i assume I must convert the spatial grid objects into dataframes 
> to apply the predicted model function.
> So I then coerce them into dataframes using as.dataframe (this takes ages)
> I then merge all the dataframes into a single dataframe. (this takes ages)
>  
> I then apply the model predict to the new data frame.
>  
> I am unsure how to then move the 'outmap' back across to grass.
> How can i convert the df to a spatial grid object.
>  
> Im thinking i must be doing something wrong. As it quite quick to pull 
> through the layers . But seems to take quite a lot of processing to get 
> the layers into a datframe appropppriate for applying the predictions.
>  
> Any help would be greatly appreciated.
>  
> Andy
>  
>  
> #pull through environmental layers
> #FAST
> anmax <- readRAST6("anmax", ignore.stderr=TRUE)
> anmin <- readRAST6("anmin", ignore.stderr=TRUE)
> aspect <- readRAST6("aspect", ignore.stderr=TRUE)
> dem10_lidar <- readRAST6("dem10_lidar", ignore.stderr=TRUE)
>  
> #coerce to dataframe
> #SLOW
> mypred_anmaxDF<-as.data.frame(anmax)
> mypred_anminDF<-as.data.frame(anmin)
> mypred_aspectDF<-as.data.frame(aspect)
> mypred_dem10_lidarDF<-as.data.frame(dem10_lidar)
>  
> #merge into single dataframe
> #VERY SLOW
> merge_tmp<-merge(mypred_anmaxDF,mypred_anminDF)
> rm(mypred_anmaxDF,mypred_anminDF)
> merge_tmp1<-merge(merge_tmp,mypred_aspectDF)
> rm(merge_tmp,mypred_aspectDF)
> mypredDF<-merge(merge_tmp1,mypred_dem10_lidarDF)
>  
> #apply model
> outmap   <- predict(tree,newdata=mypredDF, type="class")
>  
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> grass-user mailing list
> grass-user at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/grass-user


More information about the grass-user mailing list