-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path5.6 The example (p.95).txt
64 lines (44 loc) · 3.38 KB
/
5.6 The example (p.95).txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#5.6 The example (p.95)
library(LearnBayes)
data(cancermortality)
attach(cancermortality)
fit=laplace(betabinexch, c(-7,6), cancermortality)
fit
#laplace's output has four components
#mode gives the posterior mode (theta hat)
#var gives the associated variance-covariance matrix V
#int is the approximation to the logarithm of the prior predictive density
#converge indicates if agorithm converged
#use mycontour AND lbinorm (log bivariate normal density)
#to display contours of the approximate bivariate normal density
npar=list(m=fit$mode, v=fit$var)
mycontour(lbinorm, c(-8,-4.5,3,16.5), npar, xlab="logit eta", ylab="log K")
#construct 90% probability intervals
#use the diagonal elements of the variance-covariance matrix
se=sqrt(diag(fit$var))
fit$mode-1.645*se
fit$mode+1.645*se
#90% interval estimate for logit(η) is (−7.28,−6.36)
#90% interval estimate for logK is (5.67, 9.49)
########
#another run
#Approximations based on posterior modes
#5.6 the example (p.95-97)
#Based on our contour plot, we start the Nelder-Mead method with
#initial guess(logit(eta), logK) = (-7,6)
library(LearnBayes)
fit=laplace(betabinexch, c(-7,6), cancermortality)
fit
#Results:
#$mode is posterior mode
#$var is variance-covariance matrix
#we use these to make a "new" contour plot
npar=list(m=fit$mode, v=fit$var)
mycontour(lbinorm, c(-8, -4.5, 3, 16.5), npar, xlab="logit eta", ylab = "log K")
#se = sqrt of diagonal elements of "var"
#we use these to construct the 90% probability intervals for the parameters
se=sqrt(diag(fit$var))
CI = c(fit$mode-1.645*se, fit$mode+1.645*se)
CI
#first 2 values are lower bounds
#last 2 values are upper bounds