[R-sig-eco] Extract values from 'tripGrid' output for Mantel test?

Michael Sumner mdsumner at gmail.com
Fri Oct 5 01:35:39 CEST 2012


Hello, tripGrid returns a SpatialGridDataFrame (package sp) and so
from that step you only need to know about that class.

I'll use a data set in sp to create an object like yours:

library(sp)
data(meuse.grid)
coordinates(meuse.grid) <- ~x+y
gridded(meuse.grid) <- TRUE
fullgrid(meuse.grid) <- TRUE
class(meuse.grid)
tpc.for <- meuse.grid

class(tpc.for)
[1] "SpatialGridDataFrame"
attr(,"package")
[1] "sp"


Notice that this object can contain multiple attributes, and this
example data does:

names(tpc.for)
[1] "part.a" "part.b" "dist"   "soil"   "ffreq"

but tripGrid just returns a grid with one attribute named "z". You can index the
layers by name or number e.g. tpc.for[1] or tpc.for["part.a"], and plotting will
assume just using the first.

So to get data as an R matrix, you can use either of these, keeping in mind the
different orientation conventions. I prefer the image() based one
simply because I'm
used to how it works, but they are just the transpose of each other:

ff1 <- as.matrix(tpc.for[1])
ff2 <- as.image.SpatialGridDataFrame(tpc.for[1])$z

(as.image... uses the same x,y,z name convention as image() which is
partly why tripGrid
unhelpfully uses that as well).

Compare these which show that one is the column-reversed version of
the other, and that
image()'s orientation is not quite obvious:

par(mfrow = c(2,2))
image(ff1, main = "ff1 <- as.matrix(x)")
image(ff2, main = "ff2 <- as.image.SpatialGridDataFrame(x)$z")
image(ff2[,ncol(ff2):1], main = "ff2[,ncol(ff2):1]")

I hope that helps, it probably does not matter which one you use but
it's important
to know the difference.

Cheers, Mike.





On Thu, Oct 4, 2012 at 9:51 PM, Kylie Scales <klscales10 at gmail.com> wrote:
> Hi,
>
> I am testing levels of association between an environmental variable,
> extracted from a raster, and a time spent per unit area grid, created using
> the 'trip' package to determine whether the habtiat variable influences the
> amount of time spent in that grid cell by an animal.  Both grids have been
> set to the same resolution.  I would like to use a Mantel test to assess
> levels of 2D correlation.  I have created a dissimilarity matrix using
> values extracted from the raster and now need a comparable matrix
> describing the 'time spent' data.
>
> These 'time spent' data are arranged as a slot in a SpatialGridDataFrame,
> known here as tpc.for at data$z.  When I try to extract these data as a matrix:
>> ff <- as.matrix(tpc.for at data$z)
>
> the resultant object is only one-dimensional and then when I try to apply
> the function 'distance' from package 'ecodist', to calculate dissimilarity
> matrix, I get the following error:
>
>> gg <- distance(ff, "bray-curtis")
> Error in numeric(N * N * P) : vector size cannot be NA
> In addition: Warning message:
> In N * N : NAs produced by integer overflow
>
> Any ideas on how I can extract the time spent values from the 'trip' object
> as a matrix, the dimensions of which will match the raster dimensions?
> Using 'image' plots the values at the same resolution.
>
> Any help would be very much appreciated.
>
> Kylie Scales
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-ecology mailing list
> R-sig-ecology at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology



-- 
Michael Sumner
Hobart, Australia
e-mail: mdsumner at gmail.com



More information about the R-sig-ecology mailing list