<div>Thanks to all that replied.</div>
<div> </div>
<div>everything works well now. THank you all for such an elegant way to model!!!</div>
<div>Much more fun to play with GRASS and a stats package at the same time.</div>
<div> </div>
<div>I have just received my copy of Open Source GIS A GRASS GIS Approach", 3rd edition and at a first glance much of the issues are covered in Chapter 10.</div>
<div> </div>
<div>Once again thanks.</div>
<div> </div>
<div>Andy</div>
<div> </div>
<div> </div>
<div><br><br> </div>
<div><span class="gmail_quote">On 12/20/07, <b class="gmail_sendername">Roger Bivand</b> <<a href="mailto:Roger.Bivand@nhh.no">Roger.Bivand@nhh.no</a>> wrote:</span>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">On Wed, 19 Dec 2007, Dylan Beaudette wrote:<br><br>> On Wednesday 19 December 2007, Roger Bivand wrote:
<br>>> On Wed, 19 Dec 2007, Daniel McInerney wrote:<br>>><br>>> The original questioner should have written to the grass-stats list in the<br>>><br>>> first place - thanks for CC-ing. See below for inline comments:
<br>>>> Hi Andy,<br>>>><br>>>>> I am unsure how to then move the 'outmap' back across to grass.<br>>>>> How can i convert the df to a spatial grid object.<br>>>>
<br>>>> AFAIK, 'predict' won't create a dataframe object.<br>>>> R should return FALSE for is.data.frame(outmap) and<br>>>> TRUE for is.numeric(outmap)<br>>>><br>>>> You can slot the model output to the AttributeList of
<br>>><br>>> It hasn't been an AttributeList for a long time - the data slot *is* a<br>>> data frame. We *always* need the output of sessionInfo() to help -<br>>> update.packages() is most often very helpful too.
<br>>><br>>>> one of the SpatialGridDataFrames that you created when<br>>>> you read in a GRASS raster and then use writeRAST6 to<br>>>> write it back to GRASS.<br>>>><br>>>>
e.g. using 'anmax' from your example<br>>>><br>>>> anmax$anmax <- outmap<br>>>> #if that doesn't work, you might try<br>>>> anmax$anmax <- as.numeric(outmap)<br>>>> writeRAST6(anmax, "NameOfNewGRASSRaster", "anmax")
<br>>>><br>>>> Regards,<br>>>> Daniel.<br>>>><br>>>> cc: <a href="mailto:grass-stats@lists.osgeo.org">grass-stats@lists.osgeo.org</a><br>>>><br>>>> andrew haywood wrote:
<br>>>>> Dear List,<br>>>>><br>>>>> I am having some problems analysing some ecoligical models in grass<br>>>>> using the spgrass package through R.<br>>>>><br>
>>>> I have 130 plot locations where i have observed presence/absence of a<br>>>>> species. I have followed a similar framework to the BUGSITE modelling<br>>>>> example from Markus's 2003 grass gis handouts (Grass 5) I have no
<br>>>>> problems constructing the model based on the 130 plots and the<br>>>>> environmental layers from grass.<br>>><br>>> See the OSGeo tutorial September 2006:<br>>><br>>>
<a href="http://www.foss4g2006.org/contributionDisplay.py?contribId=46&sessionId=59&">http://www.foss4g2006.org/contributionDisplay.py?contribId=46&sessionId=59&</a><br>>> confId=1<br>>><br>>> and the OSGeo Journal note:
<br>>><br>>> <a href="http://www.osgeo.org/files/journal/final_pdfs/OSGeo_vol1_GRASS-R.pdf">http://www.osgeo.org/files/journal/final_pdfs/OSGeo_vol1_GRASS-R.pdf</a><br>>><br>>> for more up-to-date information.
<br>>><br>>>>> However, I am having problems bringing all the maps through into R so I<br>>>>> can make a prediction map.<br>>>>> The region isnt too large 1600 by 800 cells at 10m resolution
<br>>>>> I can bring all the environmental layers through to R using readRAST6()<br>>>>> which doesnt take too much time at all.<br>>>>><br>>>>> However i assume I must convert the spatial grid objects into
<br>>>>> dataframes to apply the predicted model function.<br>>><br>>> No, usually not at all, since the objects have a data.frame in the data<br>>> slot, and have the standard access methods.
<br>>><br>>>>> So I then coerce them into dataframes using as.dataframe (this takes<br>>>>> ages) I then merge all the dataframes into a single dataframe. (this<br>>>>> takes ages)
<br>>>>><br>>>>> I then apply the model predict to the new data frame.<br>>>>><br>>>>> I am unsure how to then move the 'outmap' back across to grass.<br>>>>> How can i convert the df to a spatial grid object.
<br>>>>><br>>>>> Im thinking i must be doing something wrong. As it quite quick to pull<br>>>>> through the layers . But seems to take quite a lot of processing to get<br>>>>> the layers into a datframe appropppriate for applying the predictions.
<br>>>>><br>>>>> Any help would be greatly appreciated.<br>>>>><br>>>>> Andy<br>>>>><br>>>>><br>>>>> # pull through environmental layers
<br>>>>> # FAST<br>>>>> anmax <- readRAST6("anmax", ignore.stderr=TRUE)<br>>>>> anmin <- readRAST6("anmin", ignore.stderr=TRUE)<br>>>>> aspect <- readRAST6("aspect",
ignore.stderr=TRUE)<br>>>>> dem10_lidar <- readRAST6("dem10_lidar", ignore.stderr=TRUE)<br>>><br>>> Wrong, do:<br>>><br>>> mydata <- readRAST6(c("anmax", "anmin", "aspect", "dem10_lidar"),
<br>>> ignore.stderr=TRUE)<br>>><br>>> then the data slot of the object is a data frame. Look at<br>>><br>>> summary(mydata)<br>>><br>>> for a sanity check.<br>>><br>>>>> # coerce to dataframe
<br>>>>> # SLOW<br>>>>> mypred_anmaxDF<-as.data.frame(anmax)<br>>>>> mypred_anminDF<-as.data.frame(anmin)<br>>>>> mypred_aspectDF<-as.data.frame(aspect)<br>>>>> mypred_dem10_lidarDF<-
as.data.frame(dem10_lidar)<br>>>>><br>>>>> # merge into single dataframe<br>>>>> # VERY SLOW<br>>>>> merge_tmp<-merge(mypred_anmaxDF,mypred_anminDF)<br>>>>> rm(mypred_anmaxDF,mypred_anminDF)
<br>>>>> merge_tmp1<-merge(merge_tmp,mypred_aspectDF)<br>>>>> rm(merge_tmp,mypred_aspectDF)<br>>>>> mypredDF<-merge(merge_tmp1,mypred_dem10_lidarDF)<br>>>>><br>>>>> #apply model
<br>>><br>>> What is tree? You may need to do extra steps depending on what class(tree)<br>>> says - if you have used rpart() or some such, you may find that<br>>><br>>> outmap <- predict(tree,newdata=mydata, type="class")
<br>>><br>>> works,<br>>><br>>> or<br>>><br>>> outmap <- predict(tree,newdata=as(mydata, "data.frame"), type="class")<br>>><br>>> Maybe just assign into mydata straight away:
<br>>><br>>> mydata$outmap <- predict(tree,newdata=as(mydata, "data.frame"),<br>>> type="class")<br>>><br>>> given ?predict.rpart saying:<br>>><br>>> "If 'type="class"': (for a classification tree) a factor of
<br>>> classifications based on the responses."<br>>><br>>> which looks like a vector for a vector response in the formula to rpart().<br>>> But do check what happens if there are NA in the newdata, because the
<br>>> default predict() behaviour may be to drop those observations. Look at<br>>> summary(mydata).<br>>><br>>> Some formula-using model fitting functions just work, like lm() and<br>>> the predict() method for lm objects.
<br>>><br>>>> From there, as Daniel wrote:<br>>><br>>> writeRAST6(mydata, "rpartpred", "outmap")<br>>><br>>> Hope this helps,<br>>><br>>> Roger<br>>>
<br>>>>> outmap <- predict(tree,newdata=mypredDF, type="class")<br>>>>><br>>>>><br>><br>><br>> An article on this in the OSGeo newsletter might be a nice way to document
<br>> simple modeling examples with GRASS and R<br><br>Perhaps, and then there is the section in chapter 10 in "Open Source GIS<br>A GRASS GIS Approach", 3rd edition (my copy is still on its way, but from<br>
the ToC, it looks as though pages 353-363 should be very helpful).<br><br>In fact, your site is a convenient collection of resources, I ought to<br>have mentioned it in my reply!<br><br>Roger<br><br>><br>> D<br>>
<br>><br>><br><br>--<br>Roger Bivand<br>Economic Geography Section, Department of Economics, Norwegian School of<br>Economics and Business Administration, Helleveien 30, N-5045 Bergen,<br>Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
<br>e-mail: <a href="mailto:Roger.Bivand@nhh.no">Roger.Bivand@nhh.no</a><br></blockquote></div><br>