R statistics: Difference between revisions
(+testing) |
|||
Line 80: | Line 80: | ||
* - | * - | ||
=== Running === | |||
The ''R'' interface for GRASS 5.4 was provided by a CRAN package called ''grass''. Changes going forward to the current GRASS 6 release meant that the interface had to be rewritten, and this provided the opportunity to adapt it to the ''sp'' CRAN package classes. Because GRASS provides the same kinds of data as ''sp'' classes handle, and relies on much of the same open source infrastructure (PROJ.4, GDAL, OGR), this step seemed sensible. Wherever possible ''spgrass6'' tries to respect the current region in GRASS to avoid handling raster data with different resolutions or extents. Like the GRASS 5 interface, R is assumed to be running within GRASS: | |||
* Start GRASS. At the GRASS command line start ''R''. | |||
: ''In this example we will use the sample Spearfish dataset.'' | |||
* Load the ''spgrass6'' library: | |||
> library(spgrass6) | |||
> G <- gmeta6() | |||
> spear <- rast.get6(c("geology", "elevation.dem"), | |||
+ cat = c(TRUE, FALSE), ignore.stderr = TRUE) | |||
The metadata are accessed and available, but are not (yet?) used to structure the ''sp'' class objects, here a SpatialGridDataFrame object filled with data from two Spearfish layers. Here is a plot of the elevation data: | |||
> image(spear, attr = 2, col = terrain.colors(20)) | |||
> title("Spearfish elevation") | |||
=== Getting Help === | === Getting Help === |
Revision as of 14:21, 10 March 2010
Q: How do I enjoy high quality statistic analysis in GRASS?
A: Well, GRASS has got an interface to the most powerful statistics analysis package around: R (http://www.r-project.org)
- The spgrass6 R addon toolbox provides the R ←→ GRASS interface.
Quick start
For the impatient just start it:
> R #and install packages directly from the net pkgs <- c('akima', 'spgrass6', 'RODBC', 'VR', 'gstat') install.packages(pkgs, dependencies=TRUE, type='source')
Once you have R in your system, take a look at http://grass.osgeo.org/statsgrass/grass6_r_install.html .
Installation
First of all you need to add R to your system, the R version must be >= 1.9.1.
R and many of its addon packages are pre-built and distributed through the CRAN network of mirrors. In addition many Linux distributions prepackage R and a number of the most popular addon toolboxes.
All the necessary functions for the GRASS 6 interface are now in packages on CRAN, so that on Linux/Unix (or Mac OSX) installing rgdal from source with PROJ4 and GDAL installed, or Windows installing from binary, the required packages are: sp; maptools (now includes spmaptools); rgdal (now includes spGDAL, spproj); spgrass6 - now all on CRAN.
Source packages
From the R console first pick a local mirror:
chooseCRANmirror()
you can then see what it picked with
options("repos")
To permanently save the mirror site add it to ~/.Rprofile. For example:
options(repos=c(CRAN="http://cran.stat.auckland.ac.nz"))
and then run install.packages() as in the Quick Start section above.
For more information see http://cran.r-project.org/doc/manuals/R-admin.html
Linux
Debian and Ubuntu
R and a number of pre-build cran packages are already present in the main repositories. Start with:
# apt-get install r-base r-cran-vr r-cran-rodbc r-cran-xml
Once those are installed start "R" at the command prompt and install the non-pre-packaged libraries:
install.packages("sp") install.packages("gstat")
Debian/Lenny ships with R 2.7.1 which is too old for the modern rgdal package. So we have to fetch one from the archive and build it from the Linux command line:
$ wget http://cran.r-project.org/src/contrib/Archive/rgdal/rgdal_0.6-24.tar.gz $ R CMD INSTALL -l /usr/local/lib/R/site-library rgdal_0.6-24.tar.gz
And finally, back inside the R session:
install.packages("spgrass6")
You can also use the CRAN Debian package repository: (pick one; adjust distribution as needed [here "Debian/testing"])
deb http://debian.cran.r-project.org/cran2deb/debian-i386 testing/ deb http://debian.cran.r-project.org/cran2deb/debian-amd64 testing/
RPM based
- RedHat, Suse, Mandrake and similar distros: take the latest R RPM and install it
Mac OSX
- for install.packages() you might have to rely on building packages from source code. try:
install.packages("spgrass6", type="source", dependencies = TRUE)
MS Windows
- -
Running
The R interface for GRASS 5.4 was provided by a CRAN package called grass. Changes going forward to the current GRASS 6 release meant that the interface had to be rewritten, and this provided the opportunity to adapt it to the sp CRAN package classes. Because GRASS provides the same kinds of data as sp classes handle, and relies on much of the same open source infrastructure (PROJ.4, GDAL, OGR), this step seemed sensible. Wherever possible spgrass6 tries to respect the current region in GRASS to avoid handling raster data with different resolutions or extents. Like the GRASS 5 interface, R is assumed to be running within GRASS:
- Start GRASS. At the GRASS command line start R.
- In this example we will use the sample Spearfish dataset.
- Load the spgrass6 library:
> library(spgrass6)
> G <- gmeta6()
> spear <- rast.get6(c("geology", "elevation.dem"), + cat = c(TRUE, FALSE), ignore.stderr = TRUE)
The metadata are accessed and available, but are not (yet?) used to structure the sp class objects, here a SpatialGridDataFrame object filled with data from two Spearfish layers. Here is a plot of the elevation data:
> image(spear, attr = 2, col = terrain.colors(20))
> title("Spearfish elevation")
Getting Help
- Primary support for R + GRASS and the spgrass6 package is through the grass-stats mailing list.
GRASS Modules
- v.krige: Perform kriging operations in the GRASS environment, using R software functions in background.
- Special requirements: python-rpy2 (not python-rpy!)
(merge info from man page here)
See also
- Using GRASS and R: http://grass.osgeo.org/statsgrass/grass6_r_interface.html
- Connecting R to RDBMS: http://grass.osgeo.org/statsgrass/r_and_dbms.html
- Neural Networks with GRASS and R (posted by Markus Neteler on the grass-user mailing list) http://www.uam.es/proyectosinv/Mclim/pdf/MBenito_EcoMod.pdf
- A detailed example on the use of GRASS and R, with spearfish data: http://casoilresource.lawr.ucdavis.edu/drupal/node/438
- Using R and GRASS with cygwin: It is possible to use Rterm inside the GRASS shell in cygwin, just as in Unix/Linux or OSX. You should not, however, start Rterm from a cygwin xterm, because Rterm is not expecting to be run in an xterm under Windows, and loses its input. If you use the regular cygwin bash shell, but need to start display windows, start X from within GRASS with startx &, and then start Rterm in the same cygwin shell, not in the xterm.
- Spatial data in R (
sp
) is a R library that provides classes and methods for spatial data (points, lines, polygons, grids), and to new or existing spatial statistics R packages that use sp, depend on sp, or will become dependent onsp
, such asmaptools
,rgdal
,splancs
,spgrass6
,gstat
,spgwr
and many others.
- RPy - Python interface to the R Programming Language
Articles
- GRASS News vol.3, June 2005 (R. Bivand. Interfacing GRASS 6 and R. GRASS Newsletter, 3:11-16, June 2005. ISSN 1614-8746).
- OSGeo Journal vol. 1 May 2007 (R. Bivand. Using the R— GRASS interface. OSGeo Journal, 1:31-33, May 2007. ISSN 1614-8746).
- GRASS Book, last chapter