0% found this document useful (0 votes)
3 views3 pages

HW4

OK

Uploaded by

77627381
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

HW4

OK

Uploaded by

77627381
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

rm(list=ls())

library(classInt)
library(fields)
library(maps)
library(sp)
library(gstat)
library(geoR)
library(mvtnorm)
library(MCMCpack)
library(coda)

#############################
## California temperatures ##
#############################

load("CAtemps.RData")

ploteqc <- function(spobj, z, breaks, ...){


pal <- tim.colors(length(breaks)-1)
fb <- classIntervals(z, n = length(pal),
style = "fixed", fixedBreaks = breaks)
col <- findColours(fb, pal)
plot(spobj, col = col, ...)
image.plot(legend.only = TRUE, zlim = range(breaks), col = pal)
}

## Plotting

range(CAtemp$avgtemp)
breaks <- seq(40, 75, by = 5)
ploteqc(CAtemp, CAtemp$avgtemp, breaks, pch = 19)
map("county", region = "california", add = TRUE)
title(main = "Average Annual Temperatures, 1961-1990, Degrees F")

range(CAgrid$elevation)
breaks <- seq(-100, 3600, by = 100)
ploteqc(CAgrid, CAgrid$elevation, breaks, pch = 19)
map("county", region = "california", add = TRUE)
title(main = "Elevations at prediction locations, m")

## Prior parameters
linmod <- lm(avgtemp~lon+lat+elevation, data = CAtemp)
summary(linmod)

m.beta <- rep(0, 4); V.beta <- 1000 * diag(4)


a.s2 <- 0.001; b.s2 <- 0.001
a.t2 <- 0.001; b.t2 <- 0.001

rhoseq <- seq(0.01, 300, length = 100)


a.rho <- 2; b.rho <- 50
plot(rhoseq, dgamma(rhoseq, shape = a.rho, scale = b.rho), type = "l") # prior for
rho

## Setup, storage, and starting values

y <- CAtemp$avgtemp
n <- nrow(CAtemp); m <- nrow(CAgrid)
d <- rdist.earth(coordinates(CAtemp))
X <- cbind(rep(1, n), CAtemp$lon, CAtemp$lat, CAtemp$elevation)
Xpred <- cbind(rep(1, m), CAgrid$lon, CAgrid$lat, CAgrid$elevation)

B <- 1000

beta.samps <- matrix(NA, nrow = 4, ncol = B)


beta.samps[,1] <- coef(linmod)

s2.samps <- t2.samps <- rho.samps <- rep(NA, B)


s2.samps[1] <- 5
rho.samps[1] <- 100
t2.samps[1] <- 2

eta.obs.samps <- matrix(NA, nrow = n, ncol = B)

v.prop <- 100^2

## MCMC sampler

Gamma <- exp(-d/rho.samps[1]) # initalize Gamma matrix


Ginv <- solve(Gamma)

for(i in 2:B){

if(i%%100==0) print(i)

## eta_obs | Rest
V <- ??
m <- ??
eta.obs.samps[,i] <- rmvnorm(1, mean = m, sigma = V, method = "svd")

## beta | Rest
V <- ??
m <- ??
beta.samps[,i] <- rmvnorm(1, mean = m, sigma = V, method = "svd")

## s2 | Rest
a <- ??
b <- ??
s2.samps[i] <- rinvgamma(1, a, b)

## t2 | Rest
a <- ??
b <- ??
t2.samps[i] <- rinvgamma(1, a, b)

## rho | Rest
# MH update

## Prediction

dcross <- rdist.earth(coordinates(CAtemp), coordinates(CAgrid))


dpred <- rdist.earth(coordinates(CAgrid))

eta.pred <- matrix(NA, nrow = nrow(CAgrid), ncol = B)


for(j in 1:B){
print(j)

m <- ??
V <- ??
eta.pred[,j] <- rmvnorm(1, m, V, method = "svd")
}

You might also like