-
Notifications
You must be signed in to change notification settings - Fork 3
/
examples_cut_offs.R
182 lines (104 loc) · 4.87 KB
/
examples_cut_offs.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
library(truncnorm)
library(mvtnorm)
library(tmvtnorm)
vec = function(M){
return(as.vector(M))
}
rmatnorm = function(M, U, V, tol = .Machine$double.eps^0.5, method = "chol"){
n = nrow(M)
p = ncol(M)
Sigma = kronecker(U, V)
vec.X = mvtnorm::rmvnorm(1, vec(M), Sigma, method = method)
X = matrix(vec.X, nrow = n, ncol = p, dimnames = list(rownames(U),colnames(V)))
return(X)
}
# Dataset
p = 5 # number of variables
K = 2 # number of levels of each variable (assumed to be equal for all)
n = 20 # number of observations
set.seed(1)
X = matrix(NA, n, p)
for(i in 1:n){
X[i,] = sample(1:K, p, replace = TRUE)
}
X # a (n,p) dataset
set.seed(1)
# construct the matrix parameter Theta collecting the (true) cutoffs
# for each variable (column of Theta) we have K+1 cutoffs
# for each variable the first and last cutoffs are fixed beforehand at -Inf and Inf respectively
# finally, we have K-1 "free" cutoffs for each variable
Theta = matrix(NA, K+1, p)
for(j in 1:p){
Theta[,j] = c(-Inf, sort(rtruncnorm(K-1, a = -Inf, b = Inf)), Inf)
}
Theta
###########################
## Dealing with cut-offs ##
###########################
source("functions_match_cut_offs.r")
## see functions_match_cut_offs.r for details on the various functions
match_x_Theta(X[1,], Theta, p) # cutoffs of individual 1 (each row corresponds to a variable)
match_x_Theta(X[2,], Theta, p)
cbind(match_x_theta_j(X[,1], Theta[,1]), X[,1]) # apply the function to variable 1
cbind(match_x_theta_j(X[,2], Theta[,2]), X[,2])
cbind(match_x_theta_j(X[,3], Theta[,3]), X[,3])
match_x_theta_j_lower(X[,1], Theta[,1]) # lower cut-offs for variable 1 (and all individuals)
match_x_theta_j_upper(X[,1], Theta[,1]) # upper cut-offs for variable 1 (and all individuals)
######################################
## Dealing with truncnorm functions ##
######################################
# To propose a candidate value for the cutoff we will need
x = rtruncnorm(1, mean = 0, sd = 1, a = 0, b = 2) # normal N(mean, sd) truncated at [a,b]
x
# To evaluate the proposal density of a cutoff
dtruncnorm(x, mean = 0, sd = 1, a = 0, b = 2) # normal N(mean, sd) truncated at [a,b]
# To evaluate the likelihood
ptruncnorm(x, mean = 0, sd = 1, a = 0, b = 2) # normal N(mean, sd) truncated at [a,b]
## Storage for the cutoffs
S = 10 # will be the number of MCMC iterations
Theta_chain = array(NA, c(S, K+1, p)) # each element is a (S, K+1) matrix collecting the S accepted cutoffs (rows) of variable j
# set initial values for the cutoffs
Theta_0 = matrix(NA, K+1, p)
for(j in 1:p){
Theta_0[,j] = c(-Inf, sort(rtruncnorm(K-1, mean = 0, sd = 2, a = -Inf, b = Inf)), Inf)
}
Theta_0
Theta_chain[1,,] = Theta_0 # store the initial value Theta_0
Theta_chain[,1,] = -Inf # set first and last cutoff equal to -Inf and Inf respectively
Theta_chain[,K+1,] = Inf
# (1) Propose the cutoffs
sigma_theta = 0.1 # sd of the proposal
# Recall that first (1) and last (K+1) cutoffs are fixed at -Inf and Inf
# Therefore there are (K-1) "free" cutoffs
s = 2 # iteration 2
j = 1 # first variable
theta_j_prop = c(-Inf,rep(NA,K-1),Inf)
for(k in 2:K){
theta_j_prop[k] = rtruncnorm(1, mean = Theta_chain[s-1,k,j], sd = sigma_theta, a = theta_j_prop[k-1], b = Theta_chain[s-1,k+1,j])
}
theta_j_prop
# (2) Evaluate the proposal
theta_prop_eval = sum(log(dtruncnorm(theta_j_prop[2:K], mean = Theta_chain[s-1,2:K,j], sd = sigma_theta,
a = Theta_chain[s-1,1:(K-1),j], b = theta_j_prop[3:(K+1)])))
theta_eval = sum(log(dtruncnorm(Theta_chain[s-1,2:K,j], mean = theta_j_prop[2:K], sd = sigma_theta,
a = theta_j_prop[1:(K-1)], b = Theta_chain[s-1,3:(K+1),j])))
# log ratio for proposal evaluation
theta_eval - theta_prop_eval
# Likelihood evaluation
theta_x_j = match_x_theta_j(x = X[,j], theta = theta_j_prop)
match_x_theta_j_lower(x = X[,j], theta = theta_j_prop)
match_x_Theta_lower(x = X[i,], Theta = Theta_chain[s-1,,], p = p)
lower_X = t(apply(X = X, MARGIN = 1, FUN = match_x_Theta_lower, Theta = Theta_chain[s-1,,], p = p))
upper_X = t(apply(X = X, MARGIN = 1, FUN = match_x_Theta_upper, Theta = Theta_chain[s-1,,], p = p))
lower_X[1:10,]
upper_X[1:10,]
match_x_theta_j_lower(x = X[,j], theta = theta_j_prop)
match_x_theta_j_upper(x = X[,j], theta = theta_j_prop)
lower_X_tmp = lower_X
lower_X_tmp[,j] = match_x_theta_j_lower(x = X[,j], theta = theta_j_prop)
upper_X_tmp = upper_X
upper_X_tmp[,j] = match_x_theta_j_upper(x = X[,j], theta = theta_j_prop)
Sigma = diag(rep(1,p))
like_prop_eval = sum(log(sapply(1:n, function(i) pmvnorm(lower = lower_X_tmp[i,], upper = upper_X_tmp[i,], sigma = Sigma)))) # likelihood evaluated at the proposed cutoffs
like_eval = sum(log(sapply(1:n, function(i) pmvnorm(lower = lower_X[i,], upper = upper_X[i,], sigma = Sigma)))) # likelihood evaluated at the current cutoffs
like_prop_eval - like_eval