##################### ###THIS IS SCRIPT TO PRODUCE SIMULATIONS, FIGURES AND STATISTICS FOR # XXXXXX # Hockey Sticks, Principal Components and Spurious Significance # Stephen McIntyre and Ross McKitrick # XXXXXXXXX, # doi: XXXXXXXXXXXXXXXXXXXX, 2004 ################################################# #PRE REQUISITES: see README # PACKAGE waveslim ############################################### #REQUIRED DIRECTORY IDENTIFICATIONS # A defined system variable can override mbhdatatree. url.source<-"http://www.climate2003.com/data/MM04c" source( file.path(url.source,"functions.txt")) temp.directory<-"c:/temp" pcs.collation.directory<-file.path(temp.directory,"pcs") createsubdirs(pcs.collation.directory) library(waveslim) #RED NOISE SIMULATIONS #LOAD NOAMER NETWORK, EXTEND BY PERSISTENCE AND TRUNCATE TO 1980 tree<-read.table(file.path(url.source,"NOAMER.1400.txt"),sep="\t",header=TRUE) #collated AD1400 network tree<-ts(tree[,2:ncol(tree)],start=tree[1,1],end=tree[nrow(tree),1]) dates<-tsp(tree)# [1] 1400 1992 1 end0<-min(1980,dates[2]) tree<-ts( tree[(1400-tsp(tree)[1]+1):(end0-dates[1]+1),] ,start=1400,end=end0) tree<-extend.persist(tree) #this has start date for oldest tree at this stage AD441 for SOAMER dim(tree) #581 70 #NOW DO SIMULATION #I USED NN=20 and NM=500 to do 10000 simulations without space limitations on my computer #IT's RESET HERE FOR 100 simulations SINCE THEY TAKE A WHILE NN<-1 NM<-100 for (nn in 1:NN) { Eigen0<-localfunction(tree,"arfima",NM) save(Eigen0,file=file.path(temp.directory,paste("arfima.sim",nn,"tab",sep="."))) } #################### #CALCULATE HOCKEY-STICKNESS ON MANNOMATIC AND ON PRINCOMP stat2<-rep(NA,NN*NM) stat3<-rep(NA,NN*NM) stat4<-rep(NA,NN*NM) index<-1:NM for (nn in 1:NN) { load(file.path(temp.directory,paste("arfima.sim",nn,"tab",sep="."))) stat2[NM*(nn-1)+index]<- apply(Eigen0[[3]],2,hockeystat) stat4[NM*(nn-1)+ index]<- apply(Eigen0[[4]],2,hockeystat) } #nn-iteration par(mfrow=c(1,1),mar=c(3,2,2,2)) #FIGURE 2 hist(stat4,freq=FALSE,main="",xlab="") #princomp this has no scouring of center #FIGURE 3 hist(stat2,freq=FALSE,main="",xlab="") #mannomatic #this shows scouring of center #HOCKEY STICK FIGURES CITED IN PAPER #MANNOMATIC #simulations are not hard-wired and results will differ slightly in each run #the values shown here are for a run of 10,000 and results for run of 100 need to be multiplied temp<-(stat2>1)|(stat2< -1); sum(temp) #[1] 9929 temp<-(stat2>1.5)|(stat2< -1.5) ; sum(temp) #[1] 7252 temp<-(stat2>1.75)|(stat2< -1.75); sum(temp) #[1]1912 temp<-(stat2>2)|(stat2< -2); sum(temp) #[1] 21 #HOCKEY STICK FIGURES CITED IN PAPER #PRINCOMP #again these results are not hard-wired temp<-(stat4>1)|(stat4< -1); sum(temp) # #1527 princomp temp<-(stat4>1.5)|(stat4< -1.5); sum(temp) # #123 princomp temp<-(stat4>1.75)|(stat4< -1.75); sum(temp) ##13 princomp temp<-(stat4>2)|(stat4< -2); sum(temp) #[1] 0 princomp ############################## ##FIRST EIGENVALUE LOADING localf<-function(x) {localf<-sum(x^2);localf} stat5<-rep(NA,NN*NM) stat6<-rep(NA,NN*NM) index<-1:NM for (nn in 1:NN) { load(file.path(temp.directory,paste("sim",nn,"tab",sep="."))) stat3[NM*(nn-1)+ index]<-Eigen0[[1]][1,] stat4[NM*(nn-1)+ index]<-Eigen0[[2]][1,] stat5[NM*(nn-1)+ index]<-apply(Eigen0[[1]],2,localf) stat6[NM*(nn-1)+ index]<-apply(Eigen0[[2]],2,localf) } #nn iteration stat7<-stat3^2/stat5 quantile(stat7,c(0.5,0.95,0.99)) # these may differ slightly since simulations not hard-wired # 50% 95% 99% #0.1301041 0.1967865 0.2336457 stat8<-stat4^2/stat6 quantile(stat8,c(.5,0.95,0.99)) # simulations are not hard-wired #50% 95% 99% #0.04108163 0.04943022 0.05533654 ############################### #FIGURE 4 - PC CALCULATIONS z<-princomp(tree) MM.PC1<-ts(-z$scores[,1],start=1400,end=1980) #SIGN-ADJUSTED MM<-norm(MM.PC1,1902,1980) #NOAMER PC1 FOR AD1400 STEP MBH.PC1<-read.table("c:/climate/data/new/TREE/ITRDB/NOAMER/BACKTO_1400/pc01.out") MBH.PC1<- ts( MBH.PC1[,2], start=MBH.PC1[1,1], end=MBH.PC1[nrow(MBH.PC1),1]) MBH<-norm(MBH.PC1,1902,1980) par(mfrow=c(2,1),mar=c(3,3,2,2)) combine<-ts.union(MBH,MM) plot.ts(combine,xlab="",main="") #CALCULATE EXPLAINED VARIANCE MBH.eigen<-read.table("c:/climate/data/new/TREE/ITRDB/NOAMER/BACKTO_1400/eigen.out") MBH.eigen[1,] # 1 0.3818 0.3818 #shows more than 38% z$sdev[1]^2/sum(z$sdev^2) # 0.1818731 ########################################## #NOW DO STATISTICAL CALCS #use "sparse" NH temperature dataset for verification following MBH98 url<-"ftp://ftp.ngdc.noaa.gov/paleo/paleocean/by_contributor/mann1998/nhem-sparse.dat" use0<-"pairwise.complete.obs" sparse<-read.table(url,skip=1) sparse<-ts(sparse[,2:3],start=sparse[1,1],end=sparse[nrow(sparse),1]) dimnames(sparse)[[2]]<-c("instr","recon") T<-sparse[,"instr"] T<-ts(T[1:(1980-1853)],start=1854,end=1980) calibration<-c(1902,1980) verification<-c(1854,1901) stat<-array (rep (NA,NN*NM*7),dim=c(NN*NM,7)) dimnames(stat)[[2]]<-c( "RE.cal","RE.ver","R2.cal","R2.ver","CE","sign.test","prod.test") for (nn in 1:NN) { load(file.path(temp.directory,paste("arfima.sim",nn,"tab",sep="."))) for (i in 1:NM) { test<-ts(Eigen0[[3]][,i],start=1400,end=1980) z<-ts.union(T,test) N<-tsp(z)[1]-1 z<-data.frame(z) fm<-lm(T~test,data=z[(1902-N):(1980-N),]) predict0<-predict(fm,newdata=z[(1854-N):(1901-N),]) estimator<-ts( c(predict0,fm$fitted.values),start=1854,end=1980) stat[NM*(nn-1)+i,]<- unlist( verification.stats(estimator,T,calibration,verification)) } #i iteration } #nn-iteration quantile (stat[,"RE.ver"],c(.9,.95,.99)) #0.5071820 0.5399537 0.5872039 quantile(stat[,"R2.ver"],c(.9,.95,.99)) #0.06528128 0.09042410 0.14941991 quantile(stat[,"CE"],c(.9,.95,.99)) #-0.15522552 -0.07840460 0.03235564 quantile(stat[,"sign.test"],c(.9,.95,.99)) # 28 30 32 quantile(stat[,"prod.test"],c(.9,.95,.99)) # 1.674515 2.065928 2.736887 ############## ##FIGURE 1 #this graphs a high-RE hockey stick url<-"ftp://ftp.ngdc.noaa.gov/paleo/paleocean/by_contributor/mann1998/mannnhem.dat" g<-read.table(url,skip=1) NH.MBH<-ts(g[1:581,2],start=g[1,1],end=g[581,1]) par(mfrow=c(2,1),mar=c(3,4,1,2)) plot.ts(Eigen0[[3]][,(stat2==max(stat2))] ,ylab="(a)", main="",axes=FALSE ) plot.ts(NH.MBH,ylab="(b)",main="",axes=FALSE) axis(side=1, at=seq(1400,2000,by=100), labels=c(1400,"",1600,"",1800,"",2000)) ################################# ############################################ #TRY THIS WITH ARIMA - METHOD NN<-1 NM<-100 for (nn in 1:NN) { Eigen0<-localfunction(tree,"arima",NM) save(Eigen0,file=file.path(temp.directory,paste("arima.sim",nn,"tab",sep="."))) } #repeat above steps mutatis mutandi