-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathE2.dev.v2
More file actions
61 lines (46 loc) · 1.29 KB
/
E2.dev.v2
File metadata and controls
61 lines (46 loc) · 1.29 KB
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
load("E2.RData")
dim(Y1)
cor(Y1, use = "pairwise.complete.obs")
N <- nrow(Y1)
p = 6
x <- scale(Y1)
Y <-rowSums(x,na.rm=TRUE)
stacks_dat <- list(Y=Y,x=x,p = 6, N = 365)
all_data <- cbind(Y,x)
colnames(all_data) <- c("Y","X1","X2","X3","X4","X5","X6")
pairs(all_data)
mlr_model <- function(){
# Likelihood:
for(i in 1:N){
Y[i] ~ dnorm(mu[i],tau)
mu[i] <- intercept + inprod(x[i,],beta[])
}
# Prior for likelihood parameters:
tau~dgamma(0.1,0.1)
intercept~dnorm(0,0.01)
for(j in 1:p){
beta[j]~dnorm(0,0.01)
}
# Missing data model for x
for(i in 1:N){
x[i,1:p]~dmnorm(x_mn[],x_prec[,])
}
# Priors for missing-data model parameters
for(j in 1:p){
x_mn[j]~dnorm(0,0.01)
}
x_prec[1:p,1:p]~dwish(R[,],k)
x_cov[1:p,1:p]<-inverse(x_prec[,])
k<-p+0.1
for(j1 in 1:p){for(j2 in 1:p){R[j1,j2]<-0.1*equals(j1,j2)}} #R is diagonal
}
library(R2OpenBUGS)
mlr_inits <- function() {
list(intercept = mean(stacks_dat$Y,na.rm=T), beta = rnorm(stacks_dat$p), tau = 0.01)
}
samps <- bugs(data = stacks_dat,
inits = mlr_inits,
parameters.to.save = c("intercept", "beta", "x_mn","x_cov"),
model.file = mlr_model,
n.chains = 1, n.burnin=100, n.iter = 200, n.thin=10, DIC=F)
plot(samps)