Professional Documents
Culture Documents
Installing WinBUGS
1) Go to the website http://www.mrc-bsu.cam.ac.uk/bugs/winbugs/contents.shtml.
2) Save and run the file WinBUGS14.exe.
3) With this educational version you can analyze small data sets. However, to analysis large
spatial datasets you need to register (for free) and obtain the key to unlock the full version.
Register at the website http://www.mrc-bsu.cam.ac.uk/bugs/winbugs/register.shtml. BUGS will
email you the key.
4) Along with the key, youll receive these instructions for using the key.
To install the key for WinBUGS 1.4 please follow these
instructions:
1.
1
6
2
7
3
8
4
4
5
9
6
11
7
12
8
14
9
15
10
19
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept)
2.8667
1.3574
2.112 0.067681 .
x
1.3879
0.2188
6.344 0.000222 ***
mean sd
MC error
2.8
1.531 0.004971
1.397 0.248 8.212E-4
2.5%
-0.2888
0.9059
median
2.813
1.396
97.5%
5.831
1.896
Running WinBUGS
1) Highlight the word model in the first line of the model code.
2) Go to model/specification and click check model. model is syntactically correct should
pop up on the bottom of the WinBUGS window.
3) Highlight list in the data list and click load data. data loaded should pop up on the
bottom of the WinBUGS window.
4) Highlight list in the initial values list and click load inits. Click gen inits to have BUGS
randomly generate initial values.
5) For each variable youd like to analyze, go to inference/sample, enter the name of the
variable in the nodes box, and click set. Samples of variables you dont request to record are
automatically discarded.
6) To begin sampling, go to model/update and click update.
7) When the MCMC sampling is completed, go back to inference/sample to view summaries
of the posterior.
for(j in 1:M) {
# Make predictions at unobserved locations
height.pred[j] ~ spatial.unipred(beta, x.pred[j], y.pred[j], height[])
}
}
There are two way to do prediction in BUGS, spatial.pred and spatial.unipred
BUGS manual says Spatial interpolation or prediction at arbitrary locations can be carried out using the
spatial.pred or spatial.unipred functions, in conjunction with fitting the spatial.exp model to a set of
observed data. spatial.pred carries out joint or simultaneous prediction at a set of target locations,
whereas spatial.unipred carries out single site prediction. The difference is that the single site prediction
yields marginal prediction intervals (i.e. ignoring correlation between prediction locations) whereas joint
prediction yields simultaneous prediction intervals for the set of target locations (which will tend to be
narrower than the marginal prediction intervals). The predicted means should be the same under joint or
single site prediction. The disadvantage of joint prediction is that it is very slow (the computational time is
of order P3, where P is the number of prediction sites).
for(j in 1:M) {
# Make predictions at unobserved locations
height.pred[j] ~ spatial.unipred(beta, x.pred[j], y.pred[j], height[])
}
}
Step 2) Load the data in R
Here is R code to load the surface elevation data.
N<-52
M<-225
x<-c(0.3, 1.4, 2.4, 3.6, 5.7, 1.6, 2.9, 3.4, 3.4, 4.8, 5.3, 6.2, 0.2, 0.9, 2.3, 2.5, 3, 3.5, 4.1, 4.9, 6.3, 0.9, 1.7, 2.4, 3.7, 4.5, 5.2,
6.3, 0.3, 2, 3.8, 6.3, 0.6, 1.5, 2.1, 2.1, 3.1, 4.5, 5.5, 5.7, 6.2, 0.4, 1.4, 1.4, 2.1, 2.3, 3.1, 4.1, 5.4, 6, 5.7, 3.6)
y <- c(6.1, 6.2, 6.1, 6.2, 6.2, 5.2, 5.1, 5.3, 5.7, 5.6, 5, 5.2, 4.3, 4.2, 4.8, 4.5, 4.5, 4.5, 4.6, 4.2, 4.3, 3.2, 3.8, 3.8, 3.5, 3.2, 3.2,
3.4, 2.4, 2.7, 2.3, 2.2, 1.7, 1.8, 1.8, 1.1, 1.1, 1.8, 1.7, 1, 1, 0.5, 0.6, 0.1, 0.7, 0.3, 0, 0.8, 0.4, 0.1, 3, 6)
height <- c(870, 793, 755, 690, 800, 800, 730, 728, 710, 780, 804, 855, 830, 813, 762, 765, 740, 765, 760, 790, 820, 855,
812, 773, 812, 827, 805, 840, 890, 820, 873, 875, 873, 865, 841, 862, 908, 855, 850, 882, 910, 940, 915, 890, 880, 870,
880, 960, 890, 860, 830, 705)
x.pred<-c(0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.63, 0.63, 0.63, 0.63, 0.63,
0.63, 0.63, 0.63, 0.63, 0.63, 0.63, 0.63, 0.63, 0.63, 0.63, 1.05, 1.05, 1.05, 1.05, 1.05, 1.05, 1.05, 1.05, 1.05, 1.05, 1.05,
1.05, 1.05, 1.05, 1.05, 1.47, 1.47, 1.47, 1.47, 1.47, 1.47, 1.47, 1.47, 1.47, 1.47, 1.47, 1.47, 1.47, 1.47, 1.47, 1.89,
1.89, 1.89, 1.89, 1.89, 1.89, 1.89, 1.89, 1.89, 1.89, 1.89, 1.89, 1.89, 1.89, 1.89, 2.31, 2.31, 2.31, 2.31, 2.31, 2.31, 2.31, 2.31,
2.31, 2.31, 2.31, 2.31, 2.31, 2.31, 2.31, 2.73, 2.73, 2.73, 2.73, 2.73, 2.73, 2.73, 2.73, 2.73, 2.73, 2.73, 2.73, 2.73, 2.73, 2.73,
3.15, 3.15, 3.15, 3.15, 3.15, 3.15, 3.15, 3.15, 3.15, 3.15, 3.15, 3.15, 3.15, 3.15, 3.15, 3.57, 3.57, 3.57, 3.57, 3.57, 3.57, 3.57,
3.57, 3.57, 3.57, 3.57, 3.57, 3.57, 3.57, 3.57, 3.99, 3.99, 3.99, 3.99, 3.99, 3.99, 3.99, 3.99, 3.99, 3.99, 3.99, 3.99, 3.99, 3.99,
3.99, 4.41, 4.41, 4.41, 4.41, 4.41, 4.41, 4.41, 4.41, 4.41, 4.41, 4.41, 4.41, 4.41, 4.41, 4.41, 4.83, 4.83, 4.83, 4.83, 4.83, 4.83,
4.83, 4.83, 4.83, 4.83, 4.83, 4.83, 4.83, 4.83, 4.83, 5.25, 5.25, 5.25, 5.25, 5.25, 5.25, 5.25, 5.25, 5.25, 5.25, 5.25, 5.25, 5.25,
5.25, 5.25, 5.67, 5.67, 5.67, 5.67, 5.67, 5.67, 5.67, 5.67, 5.67, 5.67, 5.67, 5.67, 5.67, 5.67, 5.67, 6.09, 6.09, 6.09, 6.09, 6.09,
6.09, 6.09, 6.09, 6.09, 6.09, 6.09, 6.09, 6.09, 6.09, 6.09)
lat<- c(6.09, 5.67, 5.25, 4.83, 4.41, 3.99, 3.57, 3.15, 2.73, 2.31, 1.89, 1.47, 1.05,
0.63, 0.21)
y.pred<-c(rep(lat,15))
y.pred<- c(6.09, 5.67, 5.25, 4.83, 4.41, 3.99, 3.57, 3.15, 2.73, 2.31, 1.89, 1.47, 1.05, 0.63, 0.21, 6.09, 5.67, 5.25, 4.83, 4.41,
3.99, 3.57, 3.15, 2.73, 2.31, 1.89, 1.47, 1.05, 0.63, 0.21, 6.09, 5.67, 5.25, 4.83, 4.41, 3.99, 3.57, 3.15, 2.73, 2.31, 1.89, 1.47,
1.05, 0.63, 0.21, 6.09, 5.67, 5.25, 4.83, 4.41, 3.99, 3.57, 3.15, 2.73, 2.31, 1.89, 1.47, 1.05, 0.63, 0.21, 6.09, 5.67, 5.25, 4.83,
4.41, 3.99, 3.57, 3.15, 2.73, 2.31, 1.89, 1.47, 1.05, 0.63, 0.21, 6.09, 5.67, 5.25, 4.83, 4.41, 3.99, 3.57, 3.15, 2.73, 2.31, 1.89,
1.47, 1.05, 0.63, 0.21, 6.09, 5.67, 5.25, 4.83, 4.41, 3.99, 3.57, 3.15, 2.73, 2.31, 1.89, 1.47, 1.05, 0.63, 0.21, 6.09, 5.67, 5.25,
4.83, 4.41, 3.99, 3.57, 3.15, 2.73, 2.31, 1.89, 1.47, 1.05, 0.63, 0.21, 6.09, 5.67, 5.25, 4.83, 4.41, 3.99, 3.57, 3.15, 2.73, 2.31,
1.89, 1.47, 1.05, 0.63, 0.21, 6.09, 5.67, 5.25, 4.83, 4.41, 3.99, 3.57, 3.15, 2.73, 2.31, 1.89, 1.47, 1.05, 0.63, 0.21, 6.09, 5.67,
5.25, 4.83, 4.41, 3.99, 3.57, 3.15, 2.73, 2.31, 1.89, 1.47, 1.05, 0.63, 0.21, 6.09, 5.67, 5.25, 4.83, 4.41, 3.99, 3.57, 3.15, 2.73,
2.31, 1.89, 1.47, 1.05, 0.63, 0.21, 6.09, 5.67, 5.25, 4.83, 4.41, 3.99, 3.57, 3.15, 2.73, 2.31, 1.89, 1.47, 1.05, 0.63, 0.21, 6.09,
5.67, 5.25, 4.83, 4.41, 3.99, 3.57, 3.15, 2.73, 2.31, 1.89, 1.47, 1.05, 0.63, 0.21, 6.09, 5.67, 5.25, 4.83, 4.41, 3.99, 3.57, 3.15,
2.73, 2.31, 1.89, 1.47, 1.05, 0.63, 0.21)
# prior on precision
}
The initial values are
list(tau = 1, a=0, b=c(0,0,0,0,0,NA,0,NA,0,0, NA,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0))
#Here is where we tell BUGS how to set up the adjacency structure. For example, site 1 neighbors sites 19,9, and 5.
adj = c(
19, 9, 5,
10, 7,
12,
28, 20, 18,
19, 12, 1,
#Site 6 has no neighbors
17, 16, 13, 10, 2,
29, 23, 19, 17, 1,
22, 16, 7, 2,
5, 3,
19, 17, 7,
35, 32, 31,
29, 25,
29, 22, 21, 17, 10, 7,
29, 19, 16, 13, 9, 7,
56, 55, 33, 28, 20, 4,
17, 13, 9, 5, 1,
56, 18, 4,
50, 29, 16,
16, 10,
39, 34, 29, 9,
56, 55, 48, 47, 44, 31, 30, 27,
29, 26, 15,
43, 29, 25,
56, 32, 31, 24,
45, 33, 18, 4,
50, 43, 34, 26, 25, 23, 21, 17, 16, 15, 9,
55, 45, 44, 42, 38, 24,
47, 46, 35, 32, 27, 24, 14,
31, 27, 14,
55, 45, 28, 18,
54, 52, 51, 43, 42, 40, 39, 29, 23,
46, 37, 31, 14,
41, 37,
46, 41, 36, 35,
54, 51, 49, 44, 42, 30,
40, 34, 23,
52, 49, 39, 34,
53, 49, 46, 37, 36,
51, 43, 38, 34, 30,
42, 34, 29, 26,
49, 48, 38, 30, 24,
55, 33, 30, 28,
53, 47, 41, 37, 35, 31,
53, 49, 48, 46, 31, 24,
49, 47, 44, 24,
54, 53, 52, 48, 47, 44, 41, 40, 38,
29, 21,
54, 42, 38, 34,
54, 49, 40, 34,
49, 47, 46, 41,
52, 51, 49, 38, 34,
56, 45, 33, 30, 24, 18,
55, 27, 24, 20, 18
))