-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_bayesian_poisson.R
101 lines (75 loc) · 2.47 KB
/
my_bayesian_poisson.R
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
mcmc.poisson <- function(x, y, init, n.sample = 10000, step = 0.3){
post.beta <- matrix(0, n.sample, p)
ac.ratio <- rep(0, n.sample)
prior.m <- 0
prior.s <- 1000 # for vague prior
# intialize
post.beta[1,] <- beta <- init
eta <- x %*% beta
mu <- exp(eta)
log.prior <- sum(dnorm(beta, prior.m, prior.s, log = T))
log.like <- sum(-mu + y * log(mu))
iter <- 2
for (iter in 1:n.sample){
# candidate
beta.new <- beta + rnorm(p, 0, step)
eta.new <- x %*% beta.new
mu.new <- exp(eta.new)
# prior
log.prior.new <- sum(dnorm(beta.new, prior.m, prior.s, log = T))
# liklihood
log.like.new <- sum(-mu.new + y * log(mu.new))
# ratio
temp <- exp((log.like.new + log.prior.new) - (log.like + log.prior))
rho <- min(1, temp)
if (runif(1) < rho) {
ac.ratio[iter] <- 1
beta <- beta.new
log.prior <- log.prior.new
log.like <- log.like.new
eta <- x %*% beta
mu <- exp(eta)
}
post.beta[iter,] <- beta
}
obj <- list(posterior = post.beta, acpt.ratio = mean(ac.ratio))
return(obj)
}
# Bayesian Logistic Regression
set.seed(1)
n <- 100 # sample size
p <- 1 # predictor dimension
x <- matrix(runif(n*p), n, p) # generate predictor
true.beta <- 0 # true beta
true.mu <- exp(true.beta * x)
y <- rpois(n, true.mu) # generate reponse
init <- rep(5, p)
# mle
obj.mle <- glm(y ~ x - 1, family = "poisson")
mle <- coef(obj.mle)
cat("ML estimate =", mle, "\n")
print("CI based on MLE:")
print(confint(obj.mle))
# MH for Bayesian Poisson
obj <- mcmc.poisson(x, y, init = init, n.sample = 10000, step = 0.3)
posterior <- obj$posterior[-(1:2000),]
est <- mean(posterior)
cat("accptance ratio =",obj$acpt.ratio, "\n")
cat("Bayesian estimate =", est, "\n")
cr <- t(apply(as.matrix(posterior), 2, quantile, c(0.025, 0.975))) # Credible Region
print("CR for posteiors:")
print(cr)
#comparison
library(MCMCpack)
obj2 <- summary(MCMCpoisson(y~x-1))
obj2$statistics
obj2$quantiles
# trace plot
par(mfrow = c(1,1))
for (k in 1:p){
plot(obj$posterior[,k], type = "l", col = "gray", ylab = "posterior", xlab = "", main = "trace plot")
abline(h = est[k], col = "blue", lwd = 2)
abline(h = mle[k], col = "green", lty = 3, lwd = 2)
abline(h = true.beta[k], col = "red", lty = 2, lwd = 2)
legend("topright", c("mle", "Bayes", "True"), col = c(3,4,2), lty = c(3, 1, 2))
}