# NOT RUN {
##
## Example 1: Fit Linear trend model with semiparametric nuisance time series to temperature data
##
# Use this variable to set the autoregressive model order
ar.order <- 2
# Initialize the linear trend model
nuisanceModel <- nuisanceModel_linearTrend()
data <- as.numeric(nhtemp)
n <- length(data)
# If you run the example be aware that this may take several minutes
print("example may take some time to run")
Ntotal <- 50000; burnin <- 20000; thin <- 4; Neffective <- (Ntotal-burnin)/thin
mcmc <- gibbs_NPC_nuisance(data, nuisanceModel, Ntotal=Ntotal,
burnin=burnin, thin=thin, ar.order=ar.order, eta=F)
# Reconstruct linear trend lines from posterior traces of theta=c(mu,b)
trend_lines <- array(NA, dim=c(n,Neffective))
for (j in 1:Neffective) {
mu_j <- mcmc$theta[1,j]
b_j <- mcmc$theta[2,j]
trend_lines[,j] <- mu_j + b_j * (1:n)
}
trend.median <- apply(trend_lines, 1, median)
trend.p05 <- apply(trend_lines, 1, quantile, 0.05) # 90% CI
trend.p95 <- apply(trend_lines, 1, quantile, 0.95) # "
trend.p025 <- apply(trend_lines, 1, quantile, 0.025) # 95% CI
trend.p975 <- apply(trend_lines, 1, quantile, 0.975) #
# Plot confidence bands for linear trend curve
par(mfcol=c(2,1),mar=c(4,4,2,2))
data_timePeriod <- start(nhtemp)[1]:end(nhtemp)[1]
plot(x=data_timePeriod, y=data,
main=paste0("New Haven temperature w/ NPC (p=", ar.order,
") linear trend estimate"),
col="gray", type="l", xlab="Year", ylab="Avg temp. (deg. F)")
lines(x=data_timePeriod, y=trend.median)
lines(x=data_timePeriod, y=trend.p05, lty=2)
lines(x=data_timePeriod, y=trend.p95, lty=2)
lines(x=data_timePeriod, y=trend.p025, lty=3)
lines(x=data_timePeriod, y=trend.p975, lty=3)
legend(x="topleft", legend=c("data", "posterior median",
"posterior 90% CI", "posterior 95% CI"),
lty=c(1,1,2,3), col=c("gray", 1, 1, 1),
cex=.75, x.intersp=.5, y.intersp=.5)
# Plot spectral estimate
N <- length(mcmc$psd.median)
pdgrm <- (abs(fft(data))^2 / (2*pi*length(data)))[1:N]
plot.ts((pdgrm[-1]), col="gray",
main=paste0("New Haven temperature NPC (p=",
ar.order, ") spectral estimate"))
lines((mcmc$psd.median[-1]))
lines((mcmc$psd.p05[-1]),lty=2)
lines((mcmc$psd.p95[-1]),lty=2)
lines((mcmc$psd.u05[-1]),lty=3)
lines((mcmc$psd.u95[-1]),lty=3)
legend(x="top", legend=c("periodogram", "pointwise median",
"pointwise CI", "uniform CI"),
lty=c(1,1,2,3), col=c("gray", 1, 1, 1),
cex=.75, x.intersp=.5, y.intersp=.5)
par(mfcol=c(1,1))
##
## Example 2: Fit mean model (with semiparametric nuisance time series) to AR(1) data
##
# Use this variable to set the autoregressive model order
ar.order <- 1
n <- 256
mu_true <- 3
data <- arima.sim(n=n, model=list(ar=0.75))
data <- data + mu_true
psd_true <- psd_arma(pi*omegaFreq(n), ar=0.95, ma=numeric(0), sigma2=1)
# Initialize the mean model
nuisanceModel <- nuisanceModel_mean()
# If you run the example be aware that this may take several minutes
print("example may take some time to run")
Ntotal <- 50000; burnin <- 20000; thin <- 4; Neffective <- (Ntotal-burnin)/thin
mcmc <- gibbs_NPC_nuisance(data, nuisanceModel, Ntotal=Ntotal,
burnin=burnin, thin=thin, ar.order=ar.order)
# Plot posterior trace of mu
par(mfcol=c(2,1),mar=c(4,2,2,2))
plot.ts(mcmc$theta[1,], main="Posterior trace of mu", xlab="Iteration")
abline(h=mu_true, col=2, lty=2)
abline(h=mean(data), col=3, lty=2)
legend(x="topright", legend=c("true mean", "empirical mean of data"), lty=c(2,2), col=c(2,3))
# Plot spectral estimate
N <- length(mcmc$psd.median)
pdgrm <- (abs(fft(data))^2 / (2*pi*length(data)))[1:N]
plot.ts((pdgrm[-1]), col="gray",
main=paste0("AR(1) NPC (p=", ar.order, ") spectral estimate"))
lines((mcmc$psd.median[-1]))
lines((mcmc$psd.p05[-1]),lty=2)
lines((mcmc$psd.p95[-1]),lty=2)
lines((mcmc$psd.u05[-1]),lty=3)
lines((mcmc$psd.u95[-1]),lty=3)
legend(x="top", legend=c("periodogram", "pointwise median",
"pointwise CI", "uniform CI"),
lty=c(1,1,2,3), col=c("gray", 1, 1, 1))
par(mfcol=c(1,1))
# }
Run the code above in your browser using DataLab