r for macroecology

25
R for Macroecology Spatial data continued

Upload: urban

Post on 23-Feb-2016

40 views

Category:

Documents


0 download

DESCRIPTION

R for Macroecology. Spatial data continued. Projections. Cylindrical projections. Lambert CEA. Behrmann EA. Latitude of true scale = 30. Choosing a projection. What properties are important? Angles (conformal) Area (equal area) Distance from a point (equidistant) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: R for  Macroecology

R for MacroecologySpatial data continued

Page 2: R for  Macroecology

Projections Cylindrical projections

Lambert CEA

Page 3: R for  Macroecology

Behrmann EA Latitude of true scale = 30

Page 4: R for  Macroecology

Choosing a projection What properties are important?

Angles (conformal) Area (equal area) Distance from a point (equidistant) Directions should be strait lines (gnomonic)

Minimize distortion Cylindrical, conic, azimuthal

http://www.geo.hunter.cuny.edu/~jochen/gtech201/lectures/lec6concepts/map%20coordinate%20systems/how%20to%20choose%20a%20projection.htm

Page 5: R for  Macroecology

Projecting points project() function in the proj4 package is

good

Can also use project() from rgdal

spTransform() (in rgdal) works for SpatialPoints, SpatialLines, SpatialPolygons . . . Can also handle transformations from one datum

to another

Page 6: R for  Macroecology

Projecting points> lat = rep(seq(-90,90,by = 5),(72+1))> long = rep(seq(-180,180,by = 5),each = (36+1))> xy = project(cbind(long,lat),"+proj=cea +datum=WGS84 +lat_ts=30")> par(mfrow = c(1,2))> plot(long,lat)> plot(xy)

Page 7: R for  Macroecology

Projecting a grid> mat = raster("MAT.tif")> mat = aggregate(mat,10)> bea = projectExtent(mat,"+proj=cea +datum=WGS84 +lat_ts=30")> matclass : RasterLayer dimensions : 289, 288, 83232 (nrow, ncol, ncell)resolution : 0.04166667, 0.04166667 (x, y)extent : 0, 12, 47.96667, 60.00833 (xmin, xmax, ymin, ymax)projection : +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0 values : in memorymin value : -22.88 max value : 113.56

> beaclass : RasterLayer dimensions : 289, 288, 83232 (nrow, ncol, ncell)resolution : 4016.896, 3137.077 (x, y)extent : 0, 1156866, 5450663, 6357279 (xmin, xmax, ymin, ymax)projection : +proj=cea +datum=WGS84 +lat_ts=30 +ellps=WGS84 +towgs84=0,0,0 values : none

Page 8: R for  Macroecology

Projecting a grid> bea = projectExtent(mat,"+proj=cea +datum=WGS84 +lat_ts=30")> res(bea) = xres(bea)> matBEA = projectRaster(mat,bea)> matclass : RasterLayer dimensions : 289, 288, 83232 (nrow, ncol, ncell)resolution : 0.04166667, 0.04166667 (x, y)extent : 0, 12, 47.96667, 60.00833 (xmin, xmax, ymin, ymax)projection : +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0 values : in memorymin value : -22.88 max value : 113.56

> matBEAclass : RasterLayer dimensions : 169, 288, 48672 (nrow, ncol, ncell)resolution : 4638.312, 4638.312 (x, y)extent : 0, 1335834, 4721690, 5505565 (xmin, xmax, ymin, ymax)projection : +proj=cea +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 +lat_ts=30values : in memorymin value : -21.65266 max value : 113.3013

Page 9: R for  Macroecology

How does it look?

Page 10: R for  Macroecology

What happened?x = xFromCell(bea,1:ncell(bea))y = yFromCell(bea,1:ncell(bea))plot(x,y,pch = ".")

xyLL = project(cbind(x,y), "+proj=cea +datum=WGS84 +latts=30”,inverse = T)plot(xyLL,pch = ".")

Page 11: R for  Macroecology

What happened Grid of points in lat-long (where each point

corresponds with a BEA grid cell) Sample original raster at those points (with

interpolation)

Identical spacing in x direction

Diffe

rent

spac

ing

in

y di

rect

ion

Page 12: R for  Macroecology

What are the units?> matBEAclass : RasterLayer dimensions : 169, 288, 48672 (nrow, ncol, ncell)resolution : 4638.312, 4638.312 (x, y)extent : 0, 1335834, 4721690, 5505565 (xmin, xmax, ymin, ymax)projection : +proj=cea +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 +lat_ts=30values : in memorymin value : -21.65266 max value : 113.3013

Meters, along the latitude of true scale (30N and 30S)

Page 13: R for  Macroecology

A break to try things out Projections

Page 14: R for  Macroecology

Spatially structured data Tobler’s first law of geography

“Everything is related to everything else, but near things are more related than distant things.” Waldo Tobler

Nearby data points often have similar conditions

Understanding these patterns can provide insights into the data, and are critical for statistical tests

Page 15: R for  Macroecology

Visualizing spatial structure The correlogram

Often based on Moran’s I, a measure of spatial correlation

Positive autocorrelation

Negative autocorrelation

Page 16: R for  Macroecology

Making Correlograms First goal – get x, y and z vectors

x = xFromCell(mat,1:ncell(mat))y = yFromCell(mat,1:ncell(mat))z = getValues(mat)

assignColors = function(z) { z = (z-min(z,na.rm=T))/(max(z,na.rm=T)-min(z,na.rm=T)) color = rep(NA,length(z)) index = which(!is.na(z)) color[index] = rgb(z[index],0,(1-z[index])) return(color) }

plot(x,y,col = assignColors(z),pch = 15, cex = 0.2)

Page 17: R for  Macroecology

Pairwise distance matrix Making a correlogram starts with a pairwise

distance matrix! (N data points requires ~ N2 calculations) Big data sets need to be subsetted down

> co = correlog(x,y,z,increment = 10,resamp = 0, latlon = T,na.rm=T)Error in outer(zscal, zscal) : allocMatrix: too many elements specified

Page 18: R for  Macroecology

Pairwise distance matrix Making a correlogram starts with a pairwise

distance matrix! (N data points requires ~ N2 calculations) Big data sets need to be subsetted down

sample() can help us do this

> co = correlog(x,y,z,increment = 100,resamp = 0, latlon = T,na.rm=T)Error in outer(zscal, zscal) : allocMatrix: too many elements specified> length(x)[1] 83232> length(x)^2[1] 6927565824

Page 19: R for  Macroecology

Quick comments on sample() sample() takes a vector and draws elements

out of it> v = c("a","c","f","g","r")> sample(v,3)[1] "r" "f" "c" > sample(v,3,replace = T)[1] "c" "a" "a"> sample(v,6,replace = F)Error in sample(v, 6, replace = F) : cannot take a sample larger than the population when

'replace = FALSE‘> sample(1:20,20)[1] 12 14 2 8 6 5 10 4 7 9 18 16 1 17 19 15 20 3 13 11

Page 20: R for  Macroecology

Sampling What’s wrong with this? co = correlog(x[sample(1:length(x),1000,replace = F)], y[sample(1:length(y),1000,replace = F)], z[sample(1:length(z),1000,replace = F)], increment = 10, resamp = 0, latlon = T,na.rm=T)

Page 21: R for  Macroecology

Sampling, the right way> index = sample(1:length(x),1000,replace = F)> co = correlog(x[index],y[index],z[index],increment = 100,resamp = 0, latlon = T,na.rm=T)> plot(co)

Page 22: R for  Macroecology

Autocorrelation significance Assessed through random permutation tests

Reassign z values randomly to x and y coordinates Calculate correlogram Repeat many times Does the observed correlogram differ from

random?> index = sample(1:length(x),1000,replace = F)> co = correlog(x[index],y[index],z[index],increment = 100,resamp = 100, latlon = T,na.rm=T)> plot(co)

Downside – this is slow!

Page 23: R for  Macroecology

Significant deviation from random

Page 24: R for  Macroecology

Why is spatial dependence important? Classic statistical tests assume that data

points are independent Can bias parameter estimates Leads to incorrect P value estimates

A couple of methods to deal with this (next week!) Simultaneous autoregressive models Spatial filters

Page 25: R for  Macroecology

Just for fun – 3d surfaces R can render cool 3d surfaces Demonstrate live rgl.surface() (package rgl)