Archive for July 2012
Wettest June on record?
On monday 2 July the UK Met office announced that June 2012 was the wettest on record.
How does the month really compare with previous years?
Here is an analysis in R that helps to show the patterns.
A few useful R tricks
As a result of running an introductory course on R I received queries on the following matters.
- How to import files with additional header lines.
- How to remove ALL non numeric characters from a column in one pass.
- How to add the position of the mean to boxplots.
- How to save multiple images in a file.
- How to use reshape to form tables with multiple measured variables.
- How to paste numeric output into plot titles.
The code has been published here as my first test of the great new Knit html feature in Rstudio.
http://rpubs.com/dgolicher/727
The climate data downloaded from
http://www.metoffice.gov.uk/climate/uk/stationdata/
Here is a PDF showing some of the tricks.
https://dl.dropbox.com/u/2703650/R/NewRCourse/SomeRTricks.pdf
Or as an HTML page.
https://dl.dropbox.com/u/2703650/R/NewRCourse/Rtricks.html
Here are some useful functions
### R code from vignette source ‘SomeRTricks.Rnw’
### Encoding: UTF-8
source(url(“http://tinyurl.com/Rexample/IntroFunctions.R”))
###################################################
### code chunk number 1: SomeRTricks.Rnw:92-93
###################################################
options(prompt=” “,continue=” “)
###################################################
### code chunk number 2: SomeRTricks.Rnw:96-97
###################################################
datasource<-”http://www.metoffice.gov.uk/climate/uk/stationdata/hurndata.txt”
###################################################
### code chunk number 3: SomeRTricks.Rnw:100-101 (eval = FALSE)
###################################################
## d<-read.table(datasource,skip=7)
###################################################
### code chunk number 4: SomeRTricks.Rnw:121-123
###################################################
d<-read.table(datasource,skip=7,fill=T,col.names=c(“Yr”,”Mon”,”tmax”,”tmin”,”af”,”rain”,”sun”,”comments”))
head(d)
###################################################
### code chunk number 5: SomeRTricks.Rnw:140-144
###################################################
clean<-function(x){
x<-as.character(x)
x<-gsub(“[^0-9,/.]“,”",x)
as.numeric(x)}
###################################################
### code chunk number 6: SomeRTricks.Rnw:150-152
###################################################
x<-”##%Meas126.34cm”
clean(x)
###################################################
### code chunk number 7: SomeRTricks.Rnw:159-161
###################################################
x<-”123.4cm^2″
clean(x)
###################################################
### code chunk number 8: SomeRTricks.Rnw:171-172
###################################################
d$Mon<-clean(d$Mon)
###################################################
### code chunk number 9: SomeRTricks.Rnw:179-182
###################################################
cleandf<-function(x){
x<-data.frame(lapply(x,clean))
x}
###################################################
### code chunk number 10: SomeRTricks.Rnw:185-186
###################################################
d<-cleandf(d)
###################################################
### code chunk number 11: SomeRTricks.Rnw:204-208
###################################################
library(car)
library(gplots)
Boxplot(rain~Mon,labels=Yr,data=d,col=”grey”)
plotmeans(rain~Mon,connect=F,add=T,data=d,n.label=F,barcol=”red”,col=”red”,pch=21)
###################################################
### code chunk number 12: SomeRTricks.Rnw:214-216
###################################################
Boxplot(tmax~Mon,labels=Yr,data=d,col=”grey”)
plotmeans(tmax~Mon,connect=F,add=T,data=d,n.label=F,barcol=”red”,col=”red”,pch=21)
###################################################
### code chunk number 13: SomeRTricks.Rnw:228-234
###################################################
library(mgcv)
Boxplot(rain~Mon,labels=Yr,data=d,col=”grey”)
mod<-gam(rain~s(Mon),data=d)
attach(d)
ablines2(mod,lwd=2,col=2,lty=2)
detach(d)
###################################################
### code chunk number 14: SomeRTricks.Rnw:243-244
###################################################
d<-d[d$Yr<2012,]
###################################################
### code chunk number 15: SomeRTricks.Rnw:250-255
###################################################
library(reshape)
d1<-melt(d,id=c(“Yr”,”Mon”),meas=c(“tmax”,”tmin”,”rain”))
d2<-data.frame(cast(d1,Yr~variable,mean))
head(d2)
str(d2)
###################################################
### code chunk number 16: SomeRTricks.Rnw:261-266
###################################################
plot(rain~tmin,data=d2)
mod<-lm(rain~tmin,data=d2)
attach(d2)
ablines(mod)
anova(mod)
###################################################
### code chunk number 17: SomeRTricks.Rnw:272-276
###################################################
plot(rain~tmax,data=d2)
mod<-lm(rain~tmax,data=d2)
ablines(mod)
anova(mod)
###################################################
### code chunk number 18: SomeRTricks.Rnw:283-288
###################################################
plot(rain~Yr,data=d2)
mod<-lm(rain~Yr,data=d2)
ablines(mod)
anova(mod)
confint(mod)
###################################################
### code chunk number 19: SomeRTricks.Rnw:294-299
###################################################
plot(tmin~Yr,data=d2)
mod<-lm(tmin~Yr,data=d2)
ablines(mod)
anova(mod)
confint(mod)
###################################################
### code chunk number 20: SomeRTricks.Rnw:303-308
###################################################
plot(tmax~Yr,data=d2)
mod<-lm(tmax~Yr,data=d2)
ablines(mod)
anova(mod)
confint(mod)
###################################################
### code chunk number 21: SomeRTricks.Rnw:316-320
###################################################
plot(acf(residuals(mod)))
library(lmtest)
dwtest(tmin~Yr,data=d2)
###################################################
### code chunk number 22: SomeRTricks.Rnw:329-331
###################################################
plot(acf(tmin))
detach(d2)
###################################################
### code chunk number 23: SomeRTricks.Rnw:340-346
###################################################
getstatdata<-function(x=”hurn”){
x1<-paste(“http://www.metoffice.gov.uk/climate/uk/stationdata/”,x,”data.txt”,sep=”")
d<-read.table(x1,skip=7,fill=T,col.names=c(“Yr”,”Mon”,”tmax”,”tmin”,”af”,”rain”,”sun”,”comments”))
d<-cleandf(d)
d<-data.frame(station=x,d)
d}
###################################################
### code chunk number 24: SomeRTricks.Rnw:353-356
###################################################
a<-c(“hurn”,”southampton”,”yeovilton”,”chivenor”,”camborne”,
“oxford”,”heathrow”,”eastbourne”,”manston”,”rossonwye”,”cardiff”,”cambridge”)
d<-lapply(a,getstatdata)
###################################################
### code chunk number 25: SomeRTricks.Rnw:361-364
###################################################
d<-do.call(“rbind”,d)
d<-d[!is.na(d$rain),]
d<-d[d$Yr<2012,]
###################################################
### code chunk number 26: SomeRTricks.Rnw:367-368 (eval = FALSE)
###################################################
## write.csv(d,file=”/home/duncan/climate.csv”,row.names=F)
###################################################
### code chunk number 27: SomeRTricks.Rnw:379-403
###################################################
rainmodels<-function(d)
{
d<-droplevels(d)
nm<-toupper(levels(d$station))
nm<-paste(nm,” Mean annual rain =”,round(mean(tapply(d$rain,d$Yr,sum)),0))
attach(d)
Boxplot(rain~Mon,labels=Yr,data=d,col=”grey”,main=nm)
mod<-gam(rain~s(Mon),data=d)
ablines2(mod,lwd=2,col=2,lty=2)
detach(d)
####################################
d1<-melt(d,id=c(“Yr”,”Mon”),meas=c(“rain”))
d2<-data.frame(cast(d1,Yr~variable,sum))
attach(d2)
####################################################
mod<-lm(rain~Yr)
an<-anova(mod)
pval<-round(an[5][1,],2)
nm<-paste(nm,”\n”,”P value=”,pval)
plot(rain~Yr,pch=21,bg=2,main=nm)
ablines(mod)
detach(d2)
}
###################################################
### code chunk number 28: SomeRTricks.Rnw:413-416
###################################################
pdf(“/home/duncan/rplots.pdf”)
lapply(split(d,d$station),rainmodels)
dev.off()
###################################################
### code chunk number 29: SomeRTricks.Rnw:420-427
###################################################
for(i in 1:22){
file=”/home/duncan/rplots.pdf”
cat(“\\includegraphics[page=",i,"]{“, file, “}”, sep=”")
if(i%%2==0){
cat(“\n\n”)
}
}
###################################################
### code chunk number 30: SomeRTricks.Rnw:439-464
###################################################
tminmodels<-function(d)
{
d<-droplevels(d)
nm<-toupper(levels(d$station))
nm<-paste(nm,” Mean annual minimum temp =”,round(mean(tapply(d$tmin,d$Yr,sum)),1))
attach(d)
Boxplot(tmin~Mon,labels=Yr,data=d,col=”grey”,main=nm)
mod<-gam(tmin~s(Mon),data=d)
ablines2(mod,lwd=2,col=2,lty=2)
detach(d)
####################################
d1<-melt(d,id=c(“Yr”,”Mon”),meas=c(“tmin”))
d2<-data.frame(cast(d1,Yr~variable,mean))
attach(d2)
####################################################
mod<-lm(tmin~Yr)
an<-anova(mod)
pval<-round(an[5][1,],4)
nm<-paste(nm,”\n”,”P value=”,pval)
nm<-paste(nm,”\n Increase per decade =”,10*round(coef(mod)[2],3))
plot(tmin~Yr,pch=21,bg=2,main=nm)
ablines(mod)
detach(d2)
}
###################################################
### code chunk number 31: SomeRTricks.Rnw:467-471
###################################################
pdf(“/home/duncan/rplots2.pdf”)
d<-d[!is.na(d$tmin),]
lapply(split(d,d$station),tminmodels)
dev.off()
###################################################
### code chunk number 32: SomeRTricks.Rnw:475-482
###################################################
for(i in 1:22){
file=”/home/duncan/rplots2.pdf”
cat(“\\includegraphics[page=",i,"]{“, file, “}”, sep=”")
if(i%%2==0){
cat(“\n\n”)
}
}





