Hurricane Sandy Land Wind Speed and Kriging

[This article was first published on Statistical Research » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

NJ Hurricane Sandy Landfall Data

These data come from the National Climatic Data Center (NCDC).  Using the above link will download all of the data collected by the NCDC on the day of Hurricane Sandy.  The data can also be obtained directly from the source at http://cdo.ncdc.noaa.gov/qclcd/QCLCD.

The purpose of this post is not a discussion on kriging or any of its properties. The purpose is to simply provide a simple R example on kriging and how it can be applied on real data.   This R code uses Hurricane Sandy as an example and predicts the wind speed across the state of New Jersey using the measurement stations that the NCDC uses. It is easy to see how this technique can be applied to other data.

 


wd <- "C:\\nj hurricane data"
setwd(wd)

plot.field.points <- function(locs, data, zlim, xlim, ylim,empty_locs=diag(2),
projection, add=FALSE, legend=TRUE,gray=FALSE,
legend.offset=4, pch=20, cex=4, n.color=64,
xlab="",ylab="",
map.borders=NULL, parameters=NULL, ...)
{
require(fields)
require(maps)

miss<-is.na(data)
data<-data[!miss]
locs<-locs[!miss,]


if (missing(zlim)){
zlim <- range(data)
}

if (missing(xlim)){
xlim <- range(locs[ ,1])
}
if (missing(ylim)){
ylim <- range(locs[ ,2])
}
if (!missing(projection)) {
require(mapproj)
proj <- mapproject(locs[ ,1], locs[ ,2], projection=projection)
locs <- cbind(proj$x, proj$y)
}


plot.range <- zlim
# Which data points are outside the allowed range?
outside.range <- ((data < plot.range[1]) | (data > plot.range[2]))
# A scaled version of the data that's between 1 and n.color
pos.data <- data - plot.range[1]
pos.data <- pos.data / (plot.range[2]-plot.range[1]) * (n.color-1) + 1
# Put in a dummy value if the data is not in range
pos.data[outside.range] <- 1
# Assign a color to each data point
cols <- tim.colors(n.color)[pos.data]
if(gray){
grays<-gray(seq(0.99,0.01,length=n.color)^0.5)
cols <- grays[pos.data]
}

# Make the data points that are outside the range white
cols[outside.range] <- "#FFFFFF"

if (!add) {
if (legend) {
old.par <- par(no.readonly=TRUE)
par(oma=c(0,0,0,legend.offset))
if (missing(projection)) {
plot(locs[ ,1], locs[ ,2], xlab=xlab,ylab=ylab,xlim=xlim, ylim=ylim, type="n", ...)
} else {
map(xlim=xlim, ylim=ylim, projection=projection, type="n", ...)
}
points(locs[ ,1], locs[ ,2], col=cols, pch=pch, cex=cex, ...)
points(empty_locs[ ,1], empty_locs[ ,2], col=0,pch=1,cex=1, ...)
if (!is.null(map.borders)) {
if(!missing(projection)) {
map(map.borders, add=TRUE,
xlim=xlim, ylim=ylim, projection=projection, ...)
} else {
map(map.borders, add=TRUE,
xlim=xlim, ylim=ylim, ...)
}
}
par(old.par)
if(!gray){
quilt.plot(x=locs[ ,1], y=locs[ ,2], z=data, legend.only=TRUE, zlim=zlim)
}
if(gray){
quilt.plot(x=locs[ ,1], y=locs[ ,2], z=data, col=grays,legend.only=TRUE, zlim=zlim)
}
} else {
plot(locs[ ,1], locs[ ,2], xlab=xlab,ylab=ylab, col=cols, pch=pch, cex=pch, ...)
}
} else {
points(locs[ ,1], locs[ ,2], col=cols, cex=cex, pch=pch, ...)
}
}

 
my.files <- dir(wd, pattern = ".txt", full.names = TRUE, ignore.case = TRUE)

krig <- matrix(NA, nrow=length(my.files), ncol=3)
for(i in 1:length(my.files)){

raw <- read.csv(my.files[i], skip=6, header=T)
raw <- subset(raw, raw$Date=="20121029")

latlongraw <- read.csv(my.files[i], skip=3, header=F, sep="")
latlongrows <- head(latlongraw,2)[,2]
latlong <- as.numeric(levels(latlongrows)[latlongrows])

raw$WindSpeed[raw$WindSpeed=="M"] <- NA
if(is.factor(raw$WindSpeed)){
raw$WindSpeed <- as.numeric(levels(raw$WindSpeed)[raw$WindSpeed])
}
krig[i,1] <- latlong[1]
krig[i,2] <- latlong[2]
krig[i,3] <- max(raw$WindSpeed, na.rm=T)

}
lat <- krig[,1]
long <- krig[,2]
s<-cbind(long,lat)
PM <- krig[,3]
sandy$coords <- s
sandy$data <- PM

ml <- likfit(sandy,
fix.nugget=F, cov.model="exponential",
ini = c(10, 5), nugget=4)

ml
summary(ml)

grid <- map("state","new jersey", plot=FALSE)
grid.plot <- cbind(
c(min(grid$x, na.rm=TRUE),max(grid$x, na.rm=TRUE)),
c(min(grid$y, na.rm=TRUE),max(grid$y, na.rm=TRUE))
)

sp1<-seq(grid.plot[1,1]-.25,grid.plot[2,1]+.25,length=100)
sp2<-seq(grid.plot[1,2]-.25,grid.plot[2,2]+.25,length=100)
sp<-expand.grid(sp1,sp2)
inLoc<-map.where("state",x=sp[,1],y=sp[,2])
inLoc[is.na(inLoc)]<-"NA"
inLoc<-inLoc=="new jersey"
#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(type.krige="ok",cov.model="exponential",
cov.pars=c(100,3),
nugget=5))

pred$predict[!inLoc]<-NA
pred$krige.var[!inLoc]<-NA

#Plot the predicted values:
image.plot(sp1,sp2,matrix(pred$predict,100,100),zlim=range(PM), main="Sandy Maximum Wind Speed in MPH", xlab="Longitude",ylab="Latitude")
map("county",add=T)
points(s, pch=16)

To leave a comment for the author, please follow the link and comment on their blog: Statistical Research » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)