Enjoy!
$this->bbcode_second_pass_code('', '####################################################################
##
## Script that illustrate bootstrapping techniques applied
## to the Hubbert linearization of the total world production.
##
## see the thread http://www.peakoil.com/fortopic16349.html for more details
##
## Author: Khebab (January, 2006)
## version 1.0
##
####################################################################
rm(list=ls(all=TRUE)) # clean up
##
## Load matlab emulator package and gremisc (to install)
## in the menu bar: packages/install pacakage(s)
##
library(matlab)
library(gregmisc)
library(boot)
library(MASS)
library(car)
####################################################################
##
## Main
##
####################################################################
#
# World oil production since 1901 up to 2005 (from BP) in Gb (all liquids)
#
temp <- scan()
2.0000000e-001 1.6740000e-001 1.8180000e-001 1.9490000e-001 2.1800000e-001 2.1510000e-001
2.1330000e-001 2.6420000e-001 2.8560000e-001 2.9870000e-001 3.2780000e-001 3.4440000e-001
3.5240000e-001 3.8530000e-001 4.0750000e-001 4.3200000e-001 4.5750000e-001 5.0290000e-001
5.0350000e-001 5.5590000e-001 6.8890000e-001 7.6600000e-001 8.5890000e-001 1.0157000e+000
1.0143000e+000 1.0679000e+000 1.0968000e+000 1.2630000e+000 1.3250000e+000 1.4860000e+000
1.4100000e+000 1.3730000e+000 1.3100000e+000 1.4420000e+000 1.5220000e+000 1.6540000e+000
1.7920000e+000 2.0390000e+000 1.9880000e+000 2.0860000e+000 2.1500000e+000 2.2210000e+000
2.0930000e+000 2.2570000e+000 2.5930000e+000 2.5950000e+000 2.7450000e+000 3.0220000e+000
3.4330000e+000 3.4040000e+000 3.8030000e+000 4.2830000e+000 4.5190000e+000 4.7980000e+000
5.0180000e+000 5.6260000e+000 6.1250000e+000 6.4390000e+000 6.6080000e+000 7.1340000e+000
7.6613500e+000 8.1942500e+000 8.8877500e+000 9.5374500e+000 1.0285700e+001 1.1070450e+001
1.2620000e+001 1.3550000e+001 1.4760000e+001 1.5930000e+001 1.7540000e+001 1.8560000e+001
1.9590000e+001 2.1340000e+001 2.1400000e+001 2.0380000e+001 2.2050000e+001 2.2890000e+001
2.3120000e+001 2.4110000e+001 2.2980000e+001 2.1730000e+001 2.0910000e+001 2.0660000e+001
2.1050000e+001 2.0980000e+001 2.2070000e+001 2.2190000e+001 2.3050000e+001 2.3380000e+001
2.3900000e+001 2.3830000e+001 2.4010000e+001 2.4110000e+001 2.4500000e+001 2.4860000e+001
2.5510000e+001 2.6340000e+001 2.6860000e+001 2.6400000e+001 2.7360000e+001 2.7310000e+001
2.7170000e+001 2.8120000e+001
vWorldProduction <- matrix(temp, ncol=1 , byrow= F)
mProductionData <- cbind(cbind(c(1901:2004), vWorldProduction), cumsum(vWorldProduction));
mProductionData <- cbind(mProductionData, mProductionData[,2]/mProductionData[,3])
colnames(mProductionData , do.NULL = FALSE)
colnames(mProductionData )<- c("Year","Prod","CumProd","PQ")
frProductionData <- data.frame(year= mProductionData[, 1], prod = mProductionData[,2], cumprod= mProductionData[,3], pq= mProductionData[,4])
head(frProductionData)
#
# Function that apply the Hubbert linearization technique
#
boot.RLS.twoparam <- function(data, indices, maxit=20) {
data <- data[indices,]
mod <- rlm(pq ~ cumprod, data=data, maxit=maxit, method= "MM")
v <- coefficients(mod)
c(-v[1]/v[2], v[1])
}
matplot(x= frProductionData$cumprod, y= frProductionData$pq, pch = 1:4, type = "l", add= F, ylab= "P/q", xlab= "Q")
years <- c(1940,1945,1950,1955,1960,1965,1970,1975,1980,1985)
#years <- c(1940:1985)
Results.confidenceInterval <- data.frame(year= NA,
URRMin1= NA, URRMax1= NA, KMin1= NA, KMax1= NA,
URRMin2= NA, URRMax2= NA, KMin2= NA, KMax2= NA,
URRMin3= NA, URRMax3= NA, KMin3= NA, KMax3= NA)
m <- 1
#
# The bootstrapping technique is applied for different year intervals
# from startingYear to 2004.
#
for(startingYear in years ) {
idx <- find(frProductionData$year == startingYear)
idx2 <- c(idx[1]:104)
frSubProductionData <- data.frame(year= mProductionData[idx2 , 1], prod = mProductionData[idx2 ,2],
cumprod= mProductionData[idx2 ,3], pq= mProductionData[idx2 ,4]);
# bootstrapping application
Results.boot <- boot(frSubProductionData , boot.RLS.twoparam , 2000, maxit= 200)
# confidence intervals calculation
tempURR <- boot.ci(Results.boot, conf= 0.5, index= 1, type= c("perc","bca"))
tempK <- boot.ci(Results.boot, conf= 0.5, index= 2, type= c("perc","bca"))
tempURR2 <- boot.ci(Results.boot, conf= 0.9, index= 1, type= c("perc","bca"))
tempK2 <- boot.ci(Results.boot, conf= 0.9, index= 2, type= c("perc","bca"))
tempURR3 <- boot.ci(Results.boot, conf= 0.95, index= 1, type= c("perc","bca"))
tempK3 <- boot.ci(Results.boot, conf= 0.95, index= 2, type= c("perc","bca"))
Results.confidenceInterval[m,]= c(startingYear, tempURR$bca[4:5], tempK$bca[4:5]
, tempURR2$bca[4:5], tempK2$bca[4:5], tempURR3$bca[4:5], tempK3$bca[4:5])
plot(Results.boot, index=1)
#jack.after.boot(Results.boot, index=1, main='URR')
m <- m + 1
}
Results.confidenceInterval
write(t(Results.confidenceInterval), file="ConfidenceIntervals.txt")')