University of California, Los Angeles Department of ... · Gomez-Rubio, Applied Spatial Data...

16
University of California, Los Angeles Department of Statistics Statistical Consulting Center Mini-Course Instructor: Jessica Jaynes Introduction to Spatial Data Analysis Using R Outline What is spatial data? Why spatial data? History Sources of spatial data Analysis of spatial data Inverse Distance Weighted Interpolation Trend surface analysis Examples using CA ozone data set Maps package Constructing a raster map Acknowledgments 1

Transcript of University of California, Los Angeles Department of ... · Gomez-Rubio, Applied Spatial Data...

Page 1: University of California, Los Angeles Department of ... · Gomez-Rubio, Applied Spatial Data Analysis with R, Use R!, Springer 2008. The goal of spatial statistics is: 1. In theory,

University of California, Los AngelesDepartment of Statistics

Statistical Consulting Center

Mini-Course Instructor: Jessica Jaynes

Introduction to Spatial Data Analysis Using R

Outline

• What is spatial data?

– Why spatial data?

– History

– Sources of spatial data

• Analysis of spatial data

– Inverse Distance Weighted Interpolation

– Trend surface analysis

• Examples using CA ozone data set

– Maps package

– Constructing a raster map

• Acknowledgments

1

Page 2: University of California, Los Angeles Department of ... · Gomez-Rubio, Applied Spatial Data Analysis with R, Use R!, Springer 2008. The goal of spatial statistics is: 1. In theory,

What is spatial data?The spatial locations are: s1, s2, . . . , sn. The spatial data collected at these locations willbe denoted by Z(s1), Z(s2), . . . , Z(sn). Note that the spatial locations are determined bytheir coordinates (x, y) and for the purpose of this course we will focus on two-dimensionalspace data. For example, we can measure the amount of ozone and/or another pollutant ata specific location.

Why spatial data?“A major pleasure in working with spatial data is their visualization. Maps are amongstthe most compelling graphics, because the space they map is the space we think we live in,and maps can show things we cannot see otherwise.” Roger Bivand, Edzer Pebesma, VirgilioGomez-Rubio, Applied Spatial Data Analysis with R, Use R!, Springer 2008.

The goal of spatial statistics is:

1. In theory, the goal of spatial statistics is to explain a particular phenomenon and/ormodel.

– For example lets look at the Air Quality Index: www.airnow.govWe can make inference that this map was created using some sort of spatialprediction. We can also see how ozone is distributed over a space and time. If weclick on “AQI animation” we can see the temporal changes of ozone over time,this is known as spatio-temporal models.

2. In practice, the goal is often to make predictions at unsampled locations. For example,people want to know the level of ozone concentration throughout California.

Types of spatial data:

• Environmental data

• Air pollution

• Water quality

• Agriculture

• Soil science

• Mining

• Ecology

• Forestry

• Weather

2

Page 3: University of California, Los Angeles Department of ... · Gomez-Rubio, Applied Spatial Data Analysis with R, Use R!, Springer 2008. The goal of spatial statistics is: 1. In theory,

Sources of spatial data:

• http://www.arb.ca.gov

• http://www.noaa.gov

• http://wwwcimis.water.ca.gov

• http://wiki.stat.ucla.edu/socr/index.php/SOCR Data

History of spatial statistics:Geostatistics: The term geostatistics was coined by Georges Matheron in 1962. Matheronused this term in prediction for problems in the mining industry. The prefix “geo” concernsdata related to earth.

In 1963 Matheron developed a comprehensive theory mainly for spatial prediction for theestimation of ore reserves. Geostatistical data includes observations at a collection of loca-tions. The locations themselves may or may not be random, and it is what is observed atthese locations that is of particular interest. Note, that Geostatistics can be applied to datasets not only pertaining to the earth sciences.

Two popular tools for analyzing spatial data:

1. Kriging: Kriging (Matheron 1963) owes its name to D.G. Krige a South Africanmining engineer and it was first applied in mining data. Kriging assumes a randomfield expressed through a variogram or covariance function. It is a very popular methodto solve spatial prediction problems. Kriging estimates the value at a location basedon other data points.

2. Variogram: The cornerstone of Geostatistics is the variogram, which describes spatialcorrelation and spatial dependence.

However, both of these topics are too advanced for an introductory course.

But, there are easier ways to achieve our goal of making predictions at unsampled locations:

1. Inverse Distance Weighted Interpolation (IDW): IDW is easier to understandand more intuitive; however, the downfall is that it does NOT provide standard errors.

2. Trend Surface Analysis: Trend surface analysis is regression on the coordinates.For example, we can regress ozone on the coordinates (x, y) and we can see, or maysee, how ozone concentration changes from North-South or from East-West.

3

Page 4: University of California, Los Angeles Department of ... · Gomez-Rubio, Applied Spatial Data Analysis with R, Use R!, Springer 2008. The goal of spatial statistics is: 1. In theory,

Spatial statistics - prediction

• Spatial prediction:

a. One of our goals in geostatistics is to predict values at unsampled loacations. Thiswill create a “raster map” of the area under consideration.

b. Idea: The value at an unsampled location is related to the values at sampledlocations. If not, then the predicted value can be simply the average of thesampled locations.

c. There is some relationship between the point being estimated and the samplesaround it based on its location and the distance between the points.

d. Di!erent methods for spatial prediction. All methods assume that nearby samplesare more important (they have more weight) in predicting the unknown value.

• Some simple methods of spatial prediction:

1. Method of polygons.

2. Triangulation.

3. Weighted average based on triangulation.

4. Inverse distance method.

Inverse distance method:Suppose we want to estimate data point • from the observed points ! as shown in the figurebelow:

4

Page 5: University of California, Los Angeles Department of ... · Gomez-Rubio, Applied Spatial Data Analysis with R, Use R!, Springer 2008. The goal of spatial statistics is: 1. In theory,

Each observed data point has an impact on the point being predicted with the closest pointshaving larger impact. We can consider as a predictor the following linear combination(weighted average):

z = w1z1 + w2z2 + · · · + wnzn =n!

i=1

wizi

The weights wi, i = 1, · · · , n are inversely related to the distance of point i to the point beingestimated. For example, wi = 1

di. This makes sense since we give more weight to the closest

data points. Of course other functions can be used, such as, 1d2

i, 1

d3i, 1!

di, e"di , etc.

Example:Suppose we want to predict point zo from 12 surrounding points with distances and valuesas follows:

di 150 145 153 100 120 125 130 135 136 110 115 103zi 40 37 44 42 50 55 38 41 39 47 51 58

The distances are in feet and the values of z are in %.What is the problem here?We need to generate weights that are unit-free numbers. One way to do this is to divideeach weight with the sum of the weights. Our inverse distance weighted estimator is equalto:

z =

"ni=1 wizi"ni=1 wi

= z1w1"ni=1 wi

+ z2w2"ni=1 wi

+ · · · + znwn"ni=1 wi

.

where, wi = 1di

. The sum of the new weights is equal to 1, and this will make the predictorunbiased.

A more general expression is the following based on some power we choose for the inversedistance:

z =

"ni=1 wp

i zi"ni=1 wp

i

= z1wp

1"ni=1 wp

i

+ z2wp

2"ni=1 wp

i

+ · · · + znwp

n"ni=1 wp

i

.

where, wpi = 1

dpi.

If p is large then only the points closer to the point being estimated are important. If p = 0then z = z. The default value for p in gstat is p = 2. With the method of inverse distancethe weights are guaranteed to be between 0 and 1.

5

Page 6: University of California, Los Angeles Department of ... · Gomez-Rubio, Applied Spatial Data Analysis with R, Use R!, Springer 2008. The goal of spatial statistics is: 1. In theory,

Spatial prediction - Trend surface analysis

We can predict spatial data using a regression model where the predictors are the coordinatesor functions of the coordinates. For example, the following models can be used:

Zi = !0 + !1xi + !2yi + "i

Or

Zi = !0 + !1xi + !2yi + !3x2i + !4y

2i + !5xiyi + "i

We can use the function lm as follows:

a <- read.table("http://www.stat.ucla.edu/~nchristo/statistics12/wolfcamp.txt", header=TRUE)

# Create the grid:x.range <- as.integer(range(a[,1]))x.rangey.range <- as.integer(range(a[,2]))y.rangegrd <- expand.grid(x=seq(from=x.range[1], to=x.range[2], by=2),

y=seq(from=y.range[1], to=y.range[2], by=2))

Here we regress the dependent variable level on the coordinates x and y.

q1 <- lm(level ~ x+y, data=a)q1_pred <- predict(q1, se.fit=TRUE, grd)names(q1_pred)[1] "fit" "se.fit" "df" "residual.scale"

To display the fitted values you can type q1 pred$fit. Below we display the first 6 rows.

q1_pred$fit[1:6]1 2 3 4 5 6

3516.448 3502.945 3489.442 3475.939 3462.437 3448.934

6

Page 7: University of California, Los Angeles Department of ... · Gomez-Rubio, Applied Spatial Data Analysis with R, Use R!, Springer 2008. The goal of spatial statistics is: 1. In theory,

An example using the package

Data on ozone and other pollutants are collected on a regular basis. The data set for thisexample concerns 175 locations for ozone (ppm) in California on 08 August 2005. You canread more about smog-causing pollutants at

http://www.nytimes.com/2010/01/08/science/earth/08smog.html?th&emc=th

The data can be accessed here:

a <- read.table("http://www.stat.ucla.edu/~nchristo/statistics12/ozone.txt", header=T)

The package maps in R can be loaded as follows:

library(maps)

We can display the data points and the map using the following commands:

plot(a$x,a$y, xlim=c(-125,-114),ylim=c(32,43), xlab="Longitude",ylab="Latitude", main="Ozone locations in California")

map("county", "ca",add=TRUE)

Here is the plot:

!

!!

!

!

!!

!!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

! !!!

!

!

!

!

!

!

!

!

!

!

!

!

!!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!!

!

!!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!!

!

!

!

!

!

!

!

!

!!

!

!

!

!

!

!

!

!

!

!

!

!

!!

!

!

!

!

!

!

!

!

!!

!

!!

!

!

!

!

!

!

!!

!

!

!

!

!

!

!

!

!

!

!!!

!

!

!

!

!

!

!!

!

!

!

!

!124 !122 !120 !118 !116 !114

32

34

36

38

40

42

Ozone locations in California

Longitude

Latitude

7

Page 8: University of California, Los Angeles Department of ... · Gomez-Rubio, Applied Spatial Data Analysis with R, Use R!, Springer 2008. The goal of spatial statistics is: 1. In theory,

We can also plot the points relative to their value (larger values will be displayed with largercircles). Here are the commands:

plot(a$x,a$y, xlim=c(-125,-114),ylim=c(32,43), xlab="Longitude",ylab="Latitude", main="Ozone locations in California", "n")

map("county", "ca",add=TRUE)

points(a$x, a$y, cex=a$o3/mean(a$o3))

Here is the plot:

!124 !122 !120 !118 !116 !114

32

34

36

38

40

42

Ozone locations in California

Longitude

Latitude

!

!!

!

!

!!

!!

!

!

!

!

!

!

!!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!!

!

!!!!!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!!

!

!

!

!!

!

!!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!!

!

!

!

!

!

!

!

!

!!

!

!!

!

!

!

!

!

!

!!

!

!

!

!

!

!

!

!

!

!

!!!

!

!

!

!

!

!

!!

!

!

!

!

8

Page 9: University of California, Los Angeles Department of ... · Gomez-Rubio, Applied Spatial Data Analysis with R, Use R!, Springer 2008. The goal of spatial statistics is: 1. In theory,

The following chart illustrates the health-related interpretation of the Ozone data in termsof the particulate (particles per million, ppm) recordings, according to the National Oceanicand Atmospheric Administration’s (NOAA) Air Quality Index (AQI).

http://www.noaa.gov/

What is next?

9

Page 10: University of California, Los Angeles Department of ... · Gomez-Rubio, Applied Spatial Data Analysis with R, Use R!, Springer 2008. The goal of spatial statistics is: 1. In theory,

Try to match data location with the Air Quality Index:

AQI_colors <- c("green", "yellow", "orange", "dark orange", "red")AQI_levels <- cut(a$o3, c(0, 0.06, 0.075, 0.104, 0.115, 0.374))

as.numeric(AQI_levels)

plot(a$x,a$y, xlim=c(-125,-114),ylim=c(32,43), xlab="Longitude",ylab="Latitude", main="Ozone locations in California", "n")

map("county", "ca",add=TRUE)points(a$x,a$y, cex=a$o3/mean(a$o3),

col=AQI_colors[as.numeric(AQI_levels)], pch=19)

Here is the plot:

!124 !122 !120 !118 !116 !114

32

34

36

38

40

42

Ozone locations in California

Longitude

Latitude

!

!!

!

!

!!

!!

!

!

!

!

!

!

!!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!!

!

!!!!!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!!

!

!

!

!!

!

!!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!!

!

!

!

!

!

!

!

!

!!

!

!!

!

!

!

!

!

!

!!

!

!

!

!

!

!

!

!

!

!

!!!

!

!

!

!

!

!

!!

!

!

!

!

10

Page 11: University of California, Los Angeles Department of ... · Gomez-Rubio, Applied Spatial Data Analysis with R, Use R!, Springer 2008. The goal of spatial statistics is: 1. In theory,

Spatial prediction using IDW - constructing a raster map

Consider the California ozone data:

a <- read.table("http://www.stat.ucla.edu/~nchristo/statistics12/ozone.txt", header=TRUE)

First save the original image function image.orig <- image.We will use the following packages:

library(gstat)library(maps)

A simple plot of the data points on the map:

plot(a$x,a$y, xlim=c(-125,-114),ylim=c(32,43), xlab="Longitude",ylab="Latitude", main="Ozone locations in California")

map("county", "ca",add=TRUE)

!

!!

!

!

!!

!!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

! !!!

!

!

!

!

!

!

!

!

!

!

!

!

!!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!!

!

!!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!!

!

!

!

!

!

!

!

!

!!

!

!

!

!

!

!

!

!

!

!

!

!

!!

!

!

!

!

!

!

!

!

!!

!

!!

!

!

!

!

!

!

!!

!

!

!

!

!

!

!

!

!

!

!!!

!

!

!

!

!

!

!!

!

!

!

!

!124 !122 !120 !118 !116 !114

32

34

36

38

40

42

Ozone locations in California

Longitude

Latitude

We will create a dense grid for ozone predictions and then construct a raster map.

11

Page 12: University of California, Los Angeles Department of ... · Gomez-Rubio, Applied Spatial Data Analysis with R, Use R!, Springer 2008. The goal of spatial statistics is: 1. In theory,

The values of xlim and ylim above were determined using the range command:

range(a$x)[1] -123.3497 -114.6005range(a$y)[1] 32.3533 41.7267

So we used a wider range to make sure that the entire map of California is covered.We will construct now the grid for spatial predictions (inverse distance weighted interpola-tion):

x.range <- c(-125,-114)y.range <- c(32, 43)

grd <- expand.grid(x=seq(from=x.range[1], to=x.range[2], by=0.1),y=seq(from=y.range[1], to=y.range[2], by=0.1))

Plot the grid, the map of California, and the data points:

plot(grd)map("county", "ca",add=TRUE, lwd=3)points(a, pch=19)

Here is the plot:

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!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!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

!124 !122 !120 !118 !116 !114

32

34

36

38

40

42

x

y

!

!!

!

!

!!

!!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

! !!!

!

!

!

!

!

!

!

!

!

!

!

!

!!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!!

!

!!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!!

!

!

!

!

!

!

!

!

!!

!

!

!

!

!

!

!

!

!

!

!

!

!!

!

!

!

!

!

!

!

!

!!

!

!!

!

!

!

!

!

!

!!

!

!

!

!

!

!

!

!

!

!

!!!

!

!

!

!

!

!

!!

!

!

!

!

We will use now the idw function of the package gstat to predict the value of ozone at eachone of the location on the grid above:

idw.out <- idw(o3~1, locations=~x+y, a, grd)

12

Page 13: University of California, Los Angeles Department of ... · Gomez-Rubio, Applied Spatial Data Analysis with R, Use R!, Springer 2008. The goal of spatial statistics is: 1. In theory,

Here are the first 6 rows of the object idw.iout:

> idw.out[1:6,]coordinates var1.pred var1.var

1 (-125, 32) 0.06087947 NA2 (-124.9, 32) 0.06088707 NA3 (-124.8, 32) 0.06089609 NA4 (-124.7, 32) 0.06090659 NA5 (-124.6, 32) 0.06091861 NA6 (-124.5, 32) 0.06093221 NA

The next step is to collapse the vector of the predicted values into a matrix:

qqq <- matrix(idw.out$var1.pred, length(seq(from=x.range[1],to=x.range[2], by=0.1)), length(seq(from=y.range[1],to=y.range[2], by=0.1)))

The following commands will construct the raster map of the predicted values, place thedata points on the map, and add the map of California.

image.orig(seq(from=x.range[1],to=x.range[2],by=0.1),seq(from=y.range[1],to=y.range[2], by=0.1),qqq,xlab="West to East",ylab="South to North", main="Predicted values")

points(a)

map("county", "ca",add=TRUE)

!124 !122 !120 !118 !116 !114

32

34

36

38

40

42

Raster map of the predicted values

West to East

So

uth

to

No

rth

!

!!

!

!

!!

!!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

! !!!

!

!

!

!

!

!

!

!

!

!

!

!

!!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!!

!

!!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!!

!

!

!

!

!

!

!

!

!!

!

!!

!

!

!

!

!

!

!!

!

!

!

!

!

!

!

!

!

!

!!!

!

!

!

!

!

!

!!

!

!

!

!

What is next?

13

Page 14: University of California, Los Angeles Department of ... · Gomez-Rubio, Applied Spatial Data Analysis with R, Use R!, Springer 2008. The goal of spatial statistics is: 1. In theory,

We want now to assign “NA” values to the grid values outside the boundaries of the state ofCalifornia. This can be done easily with the map.where function. To understand how thisfunction works, here is a simple example:

> map.where(database="state", -119, 37)[1] "california"> map.where(database="state", -121, 43)[1] "oregon"> map.where(database="state", -125, 37)[1] NA

We can now identify the location of each point on the grid.

#Identify the location of each point in the grid:in.what.state <- map.where(database="state", x=grd$x, y=grd$y)

The next step is to find the points of the grid that belongs to California. We can do thisusing the which function. To understand how it works we do first a simple example:

xx <- c("california", NA, "oregon", "california", "nevada")which(xx=="california")

#The answer is:[1] 1 4

We apply the which function for the entire grid:

#Find the points of the grid that belong to California:in.ca <- which(in.what.state=="california")

#Assign NA values to all the points outside California:idw.out$var1.pred[-in.ca] <- NA

The command above did the following: It assigned the value NA to all of the 12321 rows ofthe vector idw.out$var1.pred except to the points that were identified above as points inCalifornia. Now the vector idw.out$var1.pred contains the California points plus the NAvalues outside California. This vector will be collapsed into a matrix below:

qqq.ca <- matrix(idw.out$var1.pred,length(seq(from=x.range[1], to=x.range[2], by=0.1)),length(seq(from=y.range[1], to=y.range[2], by=0.1)))

We can now construct the new raster map:

image.orig(seq(from=x.range[1],to=x.range[2],by=0.1),seq(from=y.range[1],to=y.range[2], by=0.1),qqq.ca,xlab="West to East", ylab="South to North",main="Raster map of the predicted values")

14

Page 15: University of California, Los Angeles Department of ... · Gomez-Rubio, Applied Spatial Data Analysis with R, Use R!, Springer 2008. The goal of spatial statistics is: 1. In theory,

Here is the raster map:

!124 !122 !120 !118 !116 !114

32

34

36

38

40

42

Raster map of the predicted values

West to East

So

uth

to

No

rth

And we can add the data points and contours to the previous plot:

contour(seq(from=x.range[1], to=x.range[2], by=0.1),seq(from=y.range[1],to=y.range[2], by=0.1), qqq.ca, add=TRUE,col="black", labcex=1, levels=seq(0.05, 0.09, 0.005))

points(a, cex=a$o3/mean(a$o3), pch=19)

The final raster map is shown below:

!124 !122 !120 !118 !116 !114

32

34

36

38

40

42

Raster map of the predicted values

West to East

So

uth

to

No

rth

0.05

0.05

0.0

55

0.06

0.06

0.065

0.07

0.07

0.07

0.07

0.075

0.08

0.08

0.08 !

!!

!

!

!!

!!

!

!

!

!

!

!

!!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!!

!

! !!!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!!

!

!!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!

!!

!

!

!

!

!

!

!

!

!!

!

!!

!

!

!

!

!

!

!!

!

!

!

!

!

!

!

!

!

!

!!!

!

!

!

!

!

!

!!

!

!

!

!

15

Page 16: University of California, Los Angeles Department of ... · Gomez-Rubio, Applied Spatial Data Analysis with R, Use R!, Springer 2008. The goal of spatial statistics is: 1. In theory,

Acknowledgments

All material and the layout of this presentation should be accredited to Professor NicolasChristou. I would like to thank Professor Nicolas Christou for all of his help in creating thispresentation. If you would like to know more about spatial statistics please consider thefollowing classes:

Statistics 12: Intro. to Statistical Methods for Geography and Environmental StudiesStatistics M222: Spatial StatisticsStatistics C173/273: Applied Geostatistics

16