Ordinary Kriging in R
library(geoR);library(fields);library(maps)
#You will need to change the directory to load these files:
source("...\\plot.field.points.R")
load("...\\PM25.RData")
#Combine the spatial coordinates in a 84x2 matrix
s<-cbind(long,lat)
#Plot the data
plot.field.points(s,PM,map.border="county",cex=1.5)
X11()
#Estimate parameters by maximum likelihood:
ml <- likfit(data=PM,coords=s,
fix.nugget=F,cov.model="exponential",
ini = c(30, 5),nugget=5)
summary(ml)
#Create grid of prediction points:
sp1<-seq(min(s[,1]),max(s[,1]),length=100)
sp2<-seq(min(s[,2]),max(s[,2]),length=100)
sp<-expand.grid(sp1,sp2)
inCA<-map.where("state",x=sp[,1],y=sp[,2])
inCA[is.na(inCA)]<-"NA"
inCA<-inCA=="california"
#Perform ordinary Kriging (value of cov.pars and nugget are copied from mle output):
pred<-krige.conv(data=PM,coords=s,locations=sp,
krige=krige.control(cov.model="exponential",
cov.pars=c(14.73,6.144),
nugget=4.299))
pred$predict[!inCA]<-NA
pred$krige.var[!inCA]<-NA
#Plot the predicted values:
image.plot(sp1,sp2,matrix(pred$predict,100,100),zlim=range(PM))
map("county",add=T)
#Plot the standard errors:
image.plot(sp1,sp2,matrix(sqrt(pred$krige.var),100,100),zlim=c(2,3.5))
map("county",add=T)
points(s)
Output
> summary(ml)
Parameters of the mean component (trend):
beta
6.6346
40
Raw data:
16
Parameters of the spatial component:
correlation function: exponential
(estimated) partial sill = 14.73
(estimated) range parameter = 6.144
38
14
locs[, 2]
12
10
36
Parameter of the error component:
(estimated) nugget = 4.299
8
34
6
4
-124
-122
-120
-118
-116
locs[, 1]
Standard deviations
40
40
Predicted values
3.4
16
3.2
38
38
14
3.0
36
8
2.8
sp2
10
36
sp2
12
2.6
2.4
4
34
34
6
2.2
2.0
-124
-122
-120
sp1
-118
-116
-124
-122
-120
sp1
-118
-116
© Copyright 2026 Paperzz