# NOT RUN {
# Descriptive Statistics
summary(WVwells)
# Record means of predictors:
# These are used BOTH to eliminate the intercept and to recover predictions later.
mean.logGas<-mean(WVwells$logGProd2012);mean.logGas
mean.logElevation<-mean(WVwells$logElevation);mean.logElevation
mean.RockPres<-mean(WVwells$RockPres);mean.RockPres
# Outcome Variable, De-Meaned
WVwells$logGas <- WVwells$logGProd2012-mean.logGas
# Explanatory Variable: DE-MEANED PREDICTORS AND NO CONSTANT TERM
# Because we deducted the mean from all predictors and the outcome,
# it is valid to do regression through the origin.
WVwells$LogElevation <- WVwells$logElevation-mean.logElevation
WVwells$RockPressure <- WVwells$RockPres-mean.RockPres
# OLS Model
fracking.ols<-lm(logGas~LogElevation+RockPressure-1, data = WVwells)
summary(fracking.ols)
intercept.mod<-lm(logGProd2012~ logElevation+RockPres,data=WVwells)
summary(intercept.mod)
# Set Number of Iterations:
# WARNING: 100 iterations is intensive on many machines.
# This example was tuned on Amazon Web Services (EC2) over many hours
# with 5,000 iterations--unsuitable in 2020 for most desktop machines.
#M<-5000
M<-20
set.seed(1000, kind="Mersenne-Twister")#SET SEED FOR CONSISTENCY
# Trial Run, Linear Model of Ideology with Geospatial Errors Using Metropolis-Hastings:
wv.fit <- metropolis.krige(logGas~LogElevation+RockPressure-1, coords = c("UTMESrf", "UTMNSrf"),
data = WVwells, n.iter=M, powered.exp=0.5, spatial.share=0.60,
range.share=0.31, beta.var=1000, range.tol=0.1, b.tune=1,
nugget.tune=1, psill.tune=30)
# Discard first 20% of Iterations as Burn-In (User Discretion Advised).
wv.fit <- burnin(wv.fit, M/5)
# Summarize Results
summary(wv.fit)
# Convergence Diagnostics
# geweke(wv.fit) Not applicable due to few iterations.
heidel.welch(wv.fit)
# Draw Semivariogram
semivariogram(wv.fit)
# Predictive Data for Two Wells Tapped in 2013
well.1<-c(log(1110)-mean.logElevation,1020-mean.RockPres)
well.2<-c(log(643)-mean.logElevation,630-mean.RockPres)
site.1<-c(557306.0, 4345265)
site.2<-c(434515.7, 4258449)
well.newdata <- as.data.frame(cbind(rbind(well.1,well.2),rbind(site.1,site.2)))
colnames(well.newdata)<-c("LogElevation", "RockPressure", "UTMESrf","UTMNSrf")
# Make predictions from median parameter values:
(median.pred <- predict(wv.fit, newdata = well.newdata))
# Prediction in thousands of cubic feet (MCF):
round(exp(median.pred+mean.logGas))
# Make predictions with 90\<!-- % credible intervals: -->
(cred.pred <- predict(wv.fit, newdata = well.newdata, credible = 0.9))
# Prediction in thousands of cubic feet (MCF) and the true yield in 2013:
Actual.Yield<-c(471171, 7211)
round(cbind(exp(cred.pred+mean.logGas),Actual.Yield))
# }
Run the code above in your browser using DataLab