if (FALSE) {
## generate some data
set.seed(111)
n <- 500
## regressors
dat <- data.frame(x = runif(n, -3, 3), z = runif(n, -3, 3),
w = runif(n, 0, 6), fac = factor(rep(1:10, n/10)))
## response
dat$y <- with(dat, 1.5 + sin(x) + cos(z) * sin(w) +
c(2.67, 5, 6, 3, 4, 2, 6, 7, 9, 7.5)[fac] + rnorm(n, sd = 0.6))
## estimate model
b1 <- bayesx(y ~ sx(x) + sx(z, w, bs = "te") + fac,
data = dat, method = "MCMC")
## plot p-spline term
plot(b1, term = 1)
## same with
plot(b1, term = "sx(x)")
## with residuals
plot(b1, term = "sx(x)", residuals = TRUE)
## plot tensor term
plot(b1, term = "sx(z,w)")
## use other palette
plot(b1, term = "sx(z,w)", col.surface = heat.colors)
## swap colors
plot(b1, term = "sx(z,w)", col.surface = heat.colors, swap = TRUE)
## plot tensor term with residuals
plot(b1, term = "sx(z,w)", residuals = TRUE)
## plot image and contour
plot(b1, term = "sx(z,w)", image = TRUE)
plot(b1, term = "sx(z,w)", image = TRUE, contour = TRUE)
## increase the grid
plot(b1, term = "sx(z,w)", image = TRUE, contour = TRUE, grid = 100)
## plot factor term
plot(b1, term = "fac")
## plot factor term with residuals
plot(b1, term = "fac", resid = TRUE, cex = 0.5)
## plot residual dignostics
plot(b1, which = 5:8)
## plot variance sampling path of term sx(x)
plot(b1, term = 1, which = "var-samples")
## plot coefficients sampling paths of term sx(x)
plot(b1, term = 1, which = "coef-samples")
## plot the sampling path of the intercept
par(mfrow = c(1, 1))
plot(b1, which = "intcpt-samples")
## plot the autcorrelation function
## of the sampled intercept
plot(b1, which = "intcpt-samples",
acf = TRUE, lag.max = 50)
## increase lags
plot(b1, which = "intcpt-samples",
acf = TRUE, lag.max = 200)
## plot maximum autocorrelation
## of all sampled parameters in b1
plot(b1, which = "max-acf")
## plot maximum autocorrelation of
## all sampled coefficients of term sx(x)
plot(b1, term = "sx(x)", which = "coef-samples",
max.acf = TRUE, lag.max = 100)
## now a spatial example
set.seed(333)
## simulate some geographical data
data("MunichBnd")
N <- length(MunichBnd); names(MunichBnd) <- 1:N
n <- N*5
## regressors
dat <- data.frame(id = rep(1:N, n/N), x1 = runif(n, -3, 3))
dat$sp <- with(dat, sort(runif(N, -2, 2), decreasing = TRUE)[id])
dat$re <- with(dat, rnorm(N, sd = 0.6)[id])
## response
dat$y <- with(dat, 1.5 + sin(x1) + sp + re + rnorm(n, sd = 0.6))
## estimate model
b2 <- bayesx(y ~ sx(x1) + sx(id, bs = "mrf", map = MunichBnd) +
sx(id, bs = "re"), method = "MCMC", data = dat)
## summary statistics
summary(b2)
## plot structured spatial effect
plot(b2, term = "sx(id)", map = MunichBnd)
## plot unstructured spatial effect
plot(b2, term = "sx(id):re", map = MunichBnd)
## now without map
## generates a kernel density plot
## of the effects
plot(b2, term = "sx(id):mrf", map = FALSE)
plot(b2, term = "sx(id):re", map = FALSE)
## with approximate quantiles of the
## kernel density estimate
plot(b2, term = "sx(id):re", map = FALSE,
kde.quantiles = TRUE, probs = c(0.025, 0.5, 0.975))
## plot the total spatial effect
plot(b2, term = "sx(id):total")
plot(b2, term = "sx(id):total", map = MunichBnd)
## combine model objects
b <- c(b1, b2)
## plot first term of second model
plot(b, model = 2, term = 1)
plot(b, model = "b2", term = "sx(x1)")
## plot second term of both models
plot(b, term = 2, map = MunichBnd)
## plot everything
plot(b)
}
Run the code above in your browser using DataLab