data( Tphap)
# Find the annual minimum of daily minimum temperatures at Phoenix Sky Harbor airport.
MinT <- aggregate( Tphap$MinT, by=list(Tphap$Year), min, na.rm=TRUE)$x
# Make two vectors of the years, one (Yr) for human readability, and one (yr) for including as a covariate
# in the lcoation parameter of a GEV df.
Yr <- 1900 + unique( Tphap$Year)
yr <- 1:length(Yr)
ycov <- matrix( yr, ncol=1)
# Fit a GEV( mu(t) = mu0 + mu1*t, t=1,2,... (years), sigma, xi) to the negative annual min. temperatures.
fit <- gev.fit( -MinT, ydat=ycov, mul=1)
# Make a function representing the location parameter model.
hold <- function(x, ...) {
a <- list(...)
out <- a$mu0 + a$mu1*x$yr
return( out)
} # end of 'hold' function.
# Make a list to pass to 'gev.effective.rl'.
hold <- list( location=hold)
# Find the effective medians for this GEV model.
look <- gev.effective.rl( models=hold, data=data.frame( yr=yr), return.period=2,
mu0=fit$mle[1], mu1=fit$mle[2], sigma=fit$mle[3], xi=fit$mle[4])
# Note the medians are all negative because the model is for the negative annual minima.
# Make a nicer looking plot.
plot( Yr, MinT, type="l", lwd=2, col="darkblue", xlab="Year", ylab="Minimum temperature (deg. F)",
main="Phoenix summer minimum temperature",
cex.main=1.5, cex.lab=1.5, cex.axis=1.5,
col.lab="darkblue", col.axis="darkblue", col.main="darkblue")
lines( Yr, -look, col="red", lty=2, lwd=2)
##
## Now an example using the GP df.
##
data(FtCoPrec)
prec <- FtCoPrec[,"Prec"]
ind <- prec > 0.395
t1 <- sin(2*pi*(1:length(prec))/365.25)
t2 <- cos(2*pi*(1:length(prec))/365.25)
ycov <- cbind( t1, t2)
## Fit annual cycle in Poisson rate parameter (orthogonal approach).
lamfit <- glm( ind~t1+t2, family=poisson())
lamcoeffs <- summary( lamfit)$coefficients[,1]
## Fit a GP df to the data with the 't1' and 't2' harmonics as covariates.
## Note that model is log sigma(t1,t2) = sig0 + sig1*sin( 2pi t/T) + sig2*cos( 2pi t/T).
GPfit <- gpd.fit( prec, 0.395, ydat=ycov, sigl=c(1,2), siglink=exp)
scale.fun <- function( x, ...) {
a <- list(...)
out <- exp( a$sig0 + a$sig1*x$t1 + a$sig2*x$t2)
return( out)
} # end of 'scale.fun' internal function.
rate.fun <- function( x, ...) {
a <- list(...)
out <- exp( a$lam0 + a$lam1*x$t1 + a$lam2*x$t2)
return( out)
} # end of 'rate.fun' internal function.
hold <- list( scale=scale.fun, lambda=rate.fun)
# Find effective 100-year return levels for each value of the harmonics used in the fit.
look <- gpd.effective.rl( models=hold, data=data.frame( t1=t1, t2=t2), return.period=100*365.25,
plot=FALSE,
sig0=GPfit$mle[1], sig1=GPfit$mle[2], sig2=GPfit$mle[3],
xi=GPfit$mle[4], u=0.395, lam0=lamcoeffs[1], lam1=lamcoeffs[2],
lam2=lamcoeffs[3])
# Look at 1945.
id <- FtCoPrec[,"year"] == 1945
n <- sum( id)
plot( 1:n, look[ id], pch="+", xlab="time (days)",
main="Fort Collins precipitation (year 1945, inches)",
ylab="100-year Effective Return Level", col="blue")
lines( 1:n, look[id], lty=3, col="blue")
Run the code above in your browser using DataLab