Commit
·
1bb4cbf
1
Parent(s):
aa8ed31
add 36
Browse files- 36/paper.pdf +3 -0
- 36/replication_package/README.txt +3 -0
- 36/replication_package/categorized_studies.pdf +3 -0
- 36/replication_package/cces_code.Rmd +0 -0
- 36/replication_package/cces_code.html +0 -0
- 36/replication_package/cces_matched_data.rds +3 -0
- 36/replication_package/cces_unmatched_data.rds +3 -0
- 36/replication_package/codebook.pdf +3 -0
- 36/replication_package/lucid_code.Rmd +0 -0
- 36/replication_package/lucid_code.html +0 -0
- 36/replication_package/lucid_data.csv +3 -0
- 36/replication_package/post.R +493 -0
- 36/replication_package/postSim.R +173 -0
- 36/should_reproduce.txt +3 -0
36/paper.pdf
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:ae326c505eaed43328dd88b15847f631880b1e6ffcd1fe45226d5e3400d5d563
|
3 |
+
size 923599
|
36/replication_package/README.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:778ec95c61e38a20b546da5eb725cb4d9e3607e0f9199eeae3a5e7358c5e12fb
|
3 |
+
size 6762
|
36/replication_package/categorized_studies.pdf
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:bfd1e876427672061ffd0b763bd74a42a513343fb343dc872e6448e1ec518db1
|
3 |
+
size 150770
|
36/replication_package/cces_code.Rmd
ADDED
The diff for this file is too large to render.
See raw diff
|
|
36/replication_package/cces_code.html
ADDED
The diff for this file is too large to render.
See raw diff
|
|
36/replication_package/cces_matched_data.rds
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:5e457a36519c2a2141b98e23a66e89dd697c2623d29c4b7bfe6a3673fab0017a
|
3 |
+
size 19492
|
36/replication_package/cces_unmatched_data.rds
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:527c134f2f9f40b52f4f3677a3f8edb3c552fe5583cb9d44739583c9ba7f9a8f
|
3 |
+
size 28969
|
36/replication_package/codebook.pdf
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:1fa15149248ecf8c6d985b8f953f3d274f12f43164942ef45f4f35c921e23743
|
3 |
+
size 316909
|
36/replication_package/lucid_code.Rmd
ADDED
The diff for this file is too large to render.
See raw diff
|
|
36/replication_package/lucid_code.html
ADDED
The diff for this file is too large to render.
See raw diff
|
|
36/replication_package/lucid_data.csv
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:e908953ac6a81862b2170a41d0fdf442d9f52f547526787f6cf4f82842d7c992
|
3 |
+
size 734289
|
36/replication_package/post.R
ADDED
@@ -0,0 +1,493 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
####### post #######
|
2 |
+
|
3 |
+
setGeneric("post",
|
4 |
+
function(model,x1name=NULL,x1vals=NULL,x2name=NULL,x2vals=NULL,holds=NULL,
|
5 |
+
n.sims=1000,cut=NULL,quantiles=c(.025,.975),did=NULL,weights=NULL, digits=2){
|
6 |
+
standardGeneric("post")
|
7 |
+
}
|
8 |
+
)
|
9 |
+
|
10 |
+
setClassUnion("arrayORNULL", c("array","NULL"))
|
11 |
+
setClassUnion("listORcharacter", c("list","character"))
|
12 |
+
|
13 |
+
setClass("post",
|
14 |
+
slots = c(est = "array",
|
15 |
+
did = "arrayORNULL",
|
16 |
+
sims = "array",
|
17 |
+
model = "character",
|
18 |
+
link = "listORcharacter",
|
19 |
+
quantiles = "numeric",
|
20 |
+
call = "call")
|
21 |
+
)
|
22 |
+
|
23 |
+
|
24 |
+
post.glm <- function(model,x1name=NULL,x1vals=NULL,x2name=NULL,x2vals=NULL,holds=NULL,
|
25 |
+
n.sims=1000,cut=NULL,quantiles=c(.025,.975),did=NULL,weights=NULL, digits=2){
|
26 |
+
|
27 |
+
call <- match.call()
|
28 |
+
|
29 |
+
sims <- postSim(model, n.sims=n.sims)
|
30 |
+
|
31 |
+
if (family(model)[2]=="identity"){link <- identity}
|
32 |
+
else if (family(model)[2]=="probit"){link <- pnorm}
|
33 |
+
else if (family(model)[2]=="logit"){link <- plogis}
|
34 |
+
else if (family(model)[2]=="log"){link <- exp}
|
35 |
+
else if (family(model)[2]=="cloglog"){link <- function(x){1-exp(-exp(x))}}
|
36 |
+
else {stop("Link function is not supported")}
|
37 |
+
|
38 |
+
if (is.null(weights)){wi <- c(rep(1, length(model$model[,1])))} else{wi <- weights}
|
39 |
+
n.obs <- length(model.matrix(model)[,1])
|
40 |
+
k <- length(model.matrix(model)[1,])
|
41 |
+
n.q <- length(quantiles)
|
42 |
+
|
43 |
+
if (is.null(x1name)){
|
44 |
+
X <- array(NA, c(n.obs,k))
|
45 |
+
newdata <- data.frame(model$model)
|
46 |
+
if (!is.null(holds)){
|
47 |
+
for (i in 1:length(holds)){
|
48 |
+
newdata[ ,names(holds)[i]] <- as.numeric(holds[i])
|
49 |
+
}
|
50 |
+
}
|
51 |
+
X <- aperm(model.matrix(lm(formula(model), data=newdata)))
|
52 |
+
l1 <- array(NA, c(nrow(sims@coef),1))
|
53 |
+
l1[,1] <- apply(link(sims@coef %*% X), 1, function(x) weighted.mean(x, wi))
|
54 |
+
l2 <- array(NA, c(1,n.q+1))
|
55 |
+
l2[1,1] <- mean(l1)
|
56 |
+
l2[1,2:(n.q+1)] <- quantile(l1, probs=quantiles)
|
57 |
+
colnames(l2) <- c("mean",quantiles)
|
58 |
+
|
59 |
+
ans <- new("post",
|
60 |
+
est=round(l2, digits=digits),
|
61 |
+
did=NULL,
|
62 |
+
sims=l1,
|
63 |
+
model=class(model),
|
64 |
+
link=family(model)[2],
|
65 |
+
quantiles=quantiles,
|
66 |
+
call=call)
|
67 |
+
return(ans)
|
68 |
+
}
|
69 |
+
|
70 |
+
else if (is.null(x2name)){
|
71 |
+
|
72 |
+
n.x1 <- length(x1vals)
|
73 |
+
X <- array(NA, c(n.obs,k,n.x1))
|
74 |
+
|
75 |
+
for (i in 1:(n.x1)){
|
76 |
+
newdata <- data.frame(model$model)
|
77 |
+
if (!is.null(holds)){
|
78 |
+
for (j in 1:length(holds)){
|
79 |
+
newdata[ ,names(holds)[j]] <- as.numeric(holds[j])
|
80 |
+
}
|
81 |
+
}
|
82 |
+
newdata[ ,x1name] <- x1vals[i]
|
83 |
+
X[ , ,i] <- model.matrix(lm(formula(model), data=newdata))
|
84 |
+
}
|
85 |
+
|
86 |
+
X <- aperm(X, c(2,1,3))
|
87 |
+
l1 <- apply(apply(X, c(2,3), function(x) link(sims@coef %*% x)), c(1,3), function(x) weighted.mean(x, wi))
|
88 |
+
l2 <- array(NA, c(n.x1+1,n.q+1))
|
89 |
+
l2[1:n.x1,1] <- apply(l1, 2, mean)
|
90 |
+
l2[1:n.x1,2:(n.q+1)] <- aperm(apply(l1, 2, function(x) quantile(x, probs=quantiles)))
|
91 |
+
|
92 |
+
l2[nrow(l2),1] <- mean(l1[ ,n.x1] - l1[ ,1])
|
93 |
+
l2[nrow(l2),2:(n.q+1)] <- quantile(l1[ ,n.x1] - l1[ ,1], probs=quantiles)
|
94 |
+
rownames(l2) <- c(paste(c(rep(paste(x1name,"="),n.x1),
|
95 |
+
paste("\u0394","(",x1vals[1],",",x1vals[length(x1vals)],")")),
|
96 |
+
c(x1vals,"")))
|
97 |
+
colnames(l2) <- c("mean",quantiles)
|
98 |
+
|
99 |
+
ans <- new("post",
|
100 |
+
est=round(l2, digits=digits),
|
101 |
+
did=NULL,
|
102 |
+
sims=l1,
|
103 |
+
model=class(model),
|
104 |
+
link=family(model)[2],
|
105 |
+
quantiles=quantiles,
|
106 |
+
call=call)
|
107 |
+
return(ans)
|
108 |
+
}
|
109 |
+
|
110 |
+
else{
|
111 |
+
|
112 |
+
n.x1 <- length(x1vals)
|
113 |
+
n.x2 <- length(x2vals)
|
114 |
+
X <- array(NA, c(n.obs,k,n.x1,n.x2))
|
115 |
+
|
116 |
+
for (j in 1:n.x2){
|
117 |
+
for (i in 1:n.x1){
|
118 |
+
newdata <- data.frame(model$model)
|
119 |
+
if (!is.null(holds)){
|
120 |
+
for (k in 1:length(holds)){
|
121 |
+
newdata[ ,names(holds)[k]] <- as.numeric(holds[k])
|
122 |
+
}
|
123 |
+
}
|
124 |
+
newdata[ ,x1name] <- x1vals[i]
|
125 |
+
newdata[ ,x2name] <- x2vals[j]
|
126 |
+
X[ , ,i,j] <- model.matrix(lm(formula(model), data=newdata))
|
127 |
+
}
|
128 |
+
}
|
129 |
+
|
130 |
+
X <- aperm(X, c(2,1,3,4))
|
131 |
+
l1 <- apply(apply(X, c(2,3,4), function(x) link(sims@coef %*% x)), c(1,3,4), function(x) weighted.mean(x, wi))
|
132 |
+
l2 <- array(NA, c(n.x1+1,n.q+1,n.x2))
|
133 |
+
l2[1:n.x1,1,1:n.x2] <- apply(l1,c(2,3),mean)
|
134 |
+
l2[1:n.x1,2:(n.q+1),1:n.x2] <- aperm(apply(l1, c(2,3), function(x) quantile(x, probs=quantiles)), c(2,1,3))
|
135 |
+
l2[nrow(l2),1,1:n.x2] <- apply(l1[ ,n.x1,1:n.x2] - l1[ ,1,1:n.x2], 2, mean)
|
136 |
+
l2[nrow(l2),2:(n.q+1),1:n.x2] <- apply(l1[ ,n.x1,1:n.x2] - l1[ ,1,1:n.x2], 2, function(x) quantile(x, probs=quantiles))
|
137 |
+
dimnames(l2) <- list(paste(c(rep(paste(x1name,"="),n.x1),paste("\u0394","(",x1vals[1],",",x1vals[length(x1vals)],")")),c(x1vals,"")),
|
138 |
+
c("mean",quantiles),
|
139 |
+
paste(c(rep(paste(x2name,"="),n.x2)),
|
140 |
+
c(x2vals)))
|
141 |
+
|
142 |
+
if (is.null(did)){did <- c(x2vals[1],x2vals[n.x2])} else{did <- did}
|
143 |
+
l3 <- array(NA, c(1,n.q+1))
|
144 |
+
l3[1,1] <- mean((l1[ ,n.x1,match(did[2],x2vals)] - l1[ ,1,match(did[2],x2vals)]) - (l1[ ,n.x1,match(did[1],x2vals)] - l1[ ,1,match(did[1],x2vals)]))
|
145 |
+
l3[1,2:(n.q+1)] <- quantile((l1[ ,n.x1,match(did[2],x2vals)] - l1[ ,1,match(did[2],x2vals)]) - (l1[ ,n.x1,match(did[1],x2vals)] - l1[ ,1,match(did[1],x2vals)]), probs=quantiles)
|
146 |
+
dimnames(l3) <- list("did",c("mean",quantiles))
|
147 |
+
|
148 |
+
ans <- new("post",
|
149 |
+
est=round(l2, digits=digits),
|
150 |
+
did=round(l3, digits=digits),
|
151 |
+
sims=l1,
|
152 |
+
model=class(model),
|
153 |
+
link=family(model)[2],
|
154 |
+
quantiles=quantiles,
|
155 |
+
call=call)
|
156 |
+
return(ans)
|
157 |
+
}
|
158 |
+
}
|
159 |
+
|
160 |
+
|
161 |
+
post.polr <- function(model,x1name=NULL,x1vals=NULL,x2name=NULL,x2vals=NULL,holds=NULL,
|
162 |
+
n.sims=1000,cut=NULL,quantiles=c(.025,.975),did=NULL,weights=NULL, digits=2){
|
163 |
+
|
164 |
+
call <- match.call()
|
165 |
+
|
166 |
+
sims <- suppressMessages(postSim(model, n.sims=n.sims))
|
167 |
+
|
168 |
+
if (is.null(weights)){wi <- c(rep(1, length(model$model[,1])))} else{wi <- weights}
|
169 |
+
|
170 |
+
if (model$method=="probit"){link <- pnorm}
|
171 |
+
else if (model$method=="logistic"){link <- plogis}
|
172 |
+
else if (model$method=="cloglog"){link <- function(x){1-exp(-exp(x))}}
|
173 |
+
else {stop("Link function is not supported")}
|
174 |
+
|
175 |
+
n.obs <- length(model$model[,1])
|
176 |
+
k <- length(model.matrix(polr(getCall(model)$formula, model$model))[1,])
|
177 |
+
n.q <- length(quantiles)
|
178 |
+
n.y <- length(levels(model$model[,1]))
|
179 |
+
n.z <- length(model$zeta)
|
180 |
+
tau <- array(NA, c(n.sims,n.z+2))
|
181 |
+
tau[,1] <- -Inf
|
182 |
+
tau[,2:(ncol(tau)-1)] <- sims@zeta[,1:n.z]
|
183 |
+
tau[,ncol(tau)] <- Inf
|
184 |
+
beta <- sims@coef
|
185 |
+
|
186 |
+
if (is.null(cut)){
|
187 |
+
|
188 |
+
if (is.null(x1name)){
|
189 |
+
|
190 |
+
X_temp <- array(NA, c(n.obs,k))
|
191 |
+
X <- array(NA, c(n.obs,k-1))
|
192 |
+
|
193 |
+
newdata <- data.frame(model$model)
|
194 |
+
if (!is.null(holds)){
|
195 |
+
for (j in 1:length(holds)){
|
196 |
+
newdata[ ,names(holds)[j]] <- as.numeric(holds[j])
|
197 |
+
}
|
198 |
+
}
|
199 |
+
X_temp[ , ] <- suppressWarnings(model.matrix(polr(getCall(model)$formula, data=newdata)))
|
200 |
+
X[ , ] <- X_temp[,-1]
|
201 |
+
X <- aperm(X)
|
202 |
+
|
203 |
+
l1 <- array(NA, c(n.sims, n.obs, n.y))
|
204 |
+
for (z in 1:n.y){
|
205 |
+
l1[,,z] <- link(tau[,z+1] - beta %*% X) - link(tau[,z] - beta %*% X)
|
206 |
+
}
|
207 |
+
|
208 |
+
l2 <- apply(l1, c(1,3), function(x) weighted.mean(x, wi))
|
209 |
+
l3 <- array(NA, c(n.y,n.q+1))
|
210 |
+
for (i in 1:n.y){
|
211 |
+
l3[i,1] <- mean(l2[,i])
|
212 |
+
l3[i,2:(n.q+1)] <- quantile(l2[,i], probs=quantiles)
|
213 |
+
}
|
214 |
+
rownames(l3) <- paste(c(rep("Y =",n.y)), c(1:n.y))
|
215 |
+
colnames(l3) <- c("mean",quantiles)
|
216 |
+
|
217 |
+
ans <- new("post",
|
218 |
+
est=round(l3, digits=digits),
|
219 |
+
did=NULL,
|
220 |
+
sims=l2,
|
221 |
+
model=class(model),
|
222 |
+
link=model$method,
|
223 |
+
quantiles=quantiles,
|
224 |
+
call=call)
|
225 |
+
return(ans)
|
226 |
+
}
|
227 |
+
|
228 |
+
else if (is.null(x2name)){
|
229 |
+
|
230 |
+
n.x1 <- length(x1vals)
|
231 |
+
|
232 |
+
X_temp <- array(NA, c(n.obs,k,n.x1))
|
233 |
+
X <- array(NA, c(n.obs,k-1,n.x1))
|
234 |
+
|
235 |
+
for (i in 1:(n.x1)){
|
236 |
+
newdata <- data.frame(model$model)
|
237 |
+
if (!is.null(holds)){
|
238 |
+
for (j in 1:length(holds)){
|
239 |
+
newdata[ ,names(holds)[j]] <- as.numeric(holds[j])
|
240 |
+
}
|
241 |
+
}
|
242 |
+
newdata[ ,x1name] <- x1vals[i]
|
243 |
+
X_temp[ , ,i] <- suppressWarnings(model.matrix(polr(getCall(model)$formula, data=newdata)))
|
244 |
+
X[ , ,i] <- X_temp[,-1,i]
|
245 |
+
}
|
246 |
+
|
247 |
+
l1 <- array(NA, c(n.sims, n.obs, n.x1, n.y))
|
248 |
+
X <- aperm(X, c(2,1,3))
|
249 |
+
for (z in 1:n.y){
|
250 |
+
l1[,,,z] <- apply(X, c(2,3), function(x) (link(tau[,z+1] - beta %*% x) - link(tau[,z] - beta %*% x)))
|
251 |
+
}
|
252 |
+
|
253 |
+
l2 <- apply(l1, c(1,3,4), function(x) weighted.mean(x, wi))
|
254 |
+
l3 <- array(NA, c(n.x1+1, n.q+1, n.y))
|
255 |
+
for (j in 1:n.y){
|
256 |
+
for (i in 1:n.x1){
|
257 |
+
l3[i,1,j] <- mean(l2[,i,j])
|
258 |
+
l3[i,2:(n.q+1),j] <- quantile(l2[,i,j], probs=quantiles)
|
259 |
+
}
|
260 |
+
l3[nrow(l3),1,j] <- mean(l2[ ,n.x1,j] - l2[ ,1,j])
|
261 |
+
l3[nrow(l3),2:(n.q+1),j] <- quantile(l2[ ,n.x1,j] - l2[ ,1,j], probs=quantiles)
|
262 |
+
}
|
263 |
+
dimnames(l3) <- list(paste(c(rep(paste(x1name,"="),n.x1),paste("\u0394","(",x1vals[1],",",x1vals[length(x1vals)],")")),c(x1vals,"")),
|
264 |
+
c("mean",quantiles),
|
265 |
+
paste(c(rep("Y =",length(levels(model$model[,1])))),
|
266 |
+
c(1:length(levels(model$model[,1])))))
|
267 |
+
|
268 |
+
ans <- new("post",
|
269 |
+
est=round(l3, digits=digits),
|
270 |
+
did=NULL,
|
271 |
+
sims=l2,
|
272 |
+
model=class(model),
|
273 |
+
link=model$method,
|
274 |
+
quantiles=quantiles,
|
275 |
+
call=call)
|
276 |
+
return(ans)
|
277 |
+
}
|
278 |
+
|
279 |
+
else{
|
280 |
+
|
281 |
+
n.x1 <- length(x1vals)
|
282 |
+
n.x2 <- length(x2vals)
|
283 |
+
|
284 |
+
X_temp <- array(NA, c(n.obs,k,n.x1,n.x2))
|
285 |
+
X <- array(NA, c(n.obs,k-1,n.x1,n.x2))
|
286 |
+
|
287 |
+
for (j in 1:n.x2){
|
288 |
+
for (i in 1:(n.x1)){
|
289 |
+
newdata <- data.frame(model$model)
|
290 |
+
if (!is.null(holds)){
|
291 |
+
for (k in 1:length(holds)){
|
292 |
+
newdata[ ,names(holds)[k]] <- as.numeric(holds[k])
|
293 |
+
}
|
294 |
+
}
|
295 |
+
newdata[ ,x1name] <- x1vals[i]
|
296 |
+
newdata[ ,x2name] <- x2vals[j]
|
297 |
+
X_temp[ , ,i,j] <- suppressWarnings(model.matrix(polr(getCall(model)$formula, data=newdata)))
|
298 |
+
X[ , ,i,j] <- X_temp[,-1,i,j]
|
299 |
+
}
|
300 |
+
}
|
301 |
+
|
302 |
+
X <- aperm(X, c(2,1,3,4))
|
303 |
+
|
304 |
+
l1 <- array(NA, c(n.sims, n.obs, n.x1, n.x2, n.y))
|
305 |
+
for (z in 1:n.y){
|
306 |
+
l1[,,,,z] <- apply(X, c(2,3,4), function(x) (link(tau[,z+1] - beta %*% x) - link(tau[,z] - beta %*% x)))
|
307 |
+
}
|
308 |
+
|
309 |
+
l2 <- apply(l1, c(1,3,4,5), function(x) weighted.mean(x, wi))
|
310 |
+
l3 <- array(NA, c(n.x1+1, n.q+1, n.x2, n.y))
|
311 |
+
for (k in 1:n.y){
|
312 |
+
for (j in 1:n.x2){
|
313 |
+
for (i in 1:n.x1){
|
314 |
+
l3[i,1,j,k] <- mean(l2[,i,j,k])
|
315 |
+
l3[i,2:(n.q+1),j,k] <- quantile(l2[,i,j,k], probs=quantiles)
|
316 |
+
}
|
317 |
+
l3[n.x1+1,1,j,k] <- mean(l2[,n.x1,j,k] - l2[,1,j,k])
|
318 |
+
l3[n.x1+1,2:(n.q+1),j,k] <- quantile(l2[,n.x1,j,k] - l2[,1,j,k], probs=quantiles)
|
319 |
+
}
|
320 |
+
}
|
321 |
+
dimnames(l3) <- list(paste(c(rep(paste(x1name," ="),n.x1),paste("\u0394","(",x1vals[1],",",x1vals[length(x1vals)],")")),c(x1vals,"")),
|
322 |
+
c("mean",quantiles),
|
323 |
+
paste(c(rep(paste(x2name,"="),n.x2)),x2vals),
|
324 |
+
paste(c(rep("Y =",n.y)), c(1:n.y)))
|
325 |
+
|
326 |
+
if (is.null(did)){did <- c(x2vals[1],x2vals[n.x2])} else{did <- did}
|
327 |
+
l4 <- array(NA, c(n.y,n.q+1))
|
328 |
+
for (i in 1:n.y){
|
329 |
+
l4[i,1] <- mean((l2[ ,n.x1,match(did[2],x2vals),i] - l2[ ,1,match(did[2],x2vals),i]) - (l2[ ,n.x1,match(did[1],x2vals),i] - l2[ ,1,match(did[1],x2vals),i]))
|
330 |
+
l4[i,2:(n.q+1)] <- quantile((l2[ ,n.x1,match(did[2],x2vals),i] - l2[ ,1,match(did[2],x2vals),i]) - (l2[ ,n.x1,match(did[1],x2vals),i] - l2[ ,1,match(did[1],x2vals),i]), probs=quantiles)
|
331 |
+
}
|
332 |
+
yvals <- 1:n.y
|
333 |
+
dimnames(l4) <- list(paste(c(rep(paste("Y","="),n.y)),yvals),c("mean",quantiles))
|
334 |
+
|
335 |
+
ans <- new("post",
|
336 |
+
est=round(l3, digits=digits),
|
337 |
+
did=round(l4, digits=digits),
|
338 |
+
sims=l2,
|
339 |
+
model=class(model),
|
340 |
+
link=model$method,
|
341 |
+
quantiles=quantiles,
|
342 |
+
call=call)
|
343 |
+
return(ans)
|
344 |
+
}
|
345 |
+
}
|
346 |
+
|
347 |
+
else{
|
348 |
+
|
349 |
+
if (is.null(x1name)){
|
350 |
+
|
351 |
+
X_temp <- array(NA, c(n.obs,k))
|
352 |
+
X <- array(NA, c(n.obs,k-1))
|
353 |
+
|
354 |
+
newdata <- data.frame(model$model)
|
355 |
+
if (!is.null(holds)){
|
356 |
+
for (j in 1:length(holds)){
|
357 |
+
newdata[ ,names(holds)[j]] <- as.numeric(holds[j])
|
358 |
+
}
|
359 |
+
}
|
360 |
+
X_temp[ , ] <- suppressWarnings(model.matrix(polr(getCall(model)$formula, data=newdata)))
|
361 |
+
X[ , ] <- X_temp[,-1]
|
362 |
+
X <- aperm(X)
|
363 |
+
|
364 |
+
l1 <- apply(link(-tau[,cut+1] + beta %*% X), 1, function(x) weighted.mean(x, wi))
|
365 |
+
l2 <- array(NA, c(1,n.q+1))
|
366 |
+
l2[1,1] <- mean(l1)
|
367 |
+
l2[1,2:(n.q+1)] <- quantile(l1, probs=quantiles)
|
368 |
+
colnames(l2) <- c("mean",quantiles)
|
369 |
+
|
370 |
+
ans <- new("post",
|
371 |
+
est=round(l2, digits=digits),
|
372 |
+
did=NULL,
|
373 |
+
sims=l1,
|
374 |
+
model=class(model),
|
375 |
+
link=model$method,
|
376 |
+
quantiles=quantiles,
|
377 |
+
call=call)
|
378 |
+
return(ans)
|
379 |
+
}
|
380 |
+
|
381 |
+
|
382 |
+
else if (is.null(x2name)){
|
383 |
+
|
384 |
+
n.x1 <- length(x1vals)
|
385 |
+
|
386 |
+
X_temp <- array(NA, c(n.obs,k,n.x1))
|
387 |
+
X <- array(NA, c(n.obs,k-1,n.x1))
|
388 |
+
|
389 |
+
for (i in 1:(n.x1)){
|
390 |
+
newdata <- data.frame(model$model)
|
391 |
+
if (!is.null(holds)){
|
392 |
+
for (j in 1:length(holds)){
|
393 |
+
newdata[ ,names(holds)[j]] <- as.numeric(holds[j])
|
394 |
+
}
|
395 |
+
}
|
396 |
+
newdata[ ,x1name] <- x1vals[i]
|
397 |
+
X_temp[ , ,i] <- suppressWarnings(model.matrix(polr(getCall(model)$formula, data=newdata)))
|
398 |
+
X[ , ,i] <- X_temp[,-1,i]
|
399 |
+
}
|
400 |
+
|
401 |
+
X <- aperm(X, c(2,1,3))
|
402 |
+
l1 <- apply(apply(X, c(2,3), function(x) link(-tau[,cut+1] + beta %*% x)),
|
403 |
+
c(1,3), function(x) weighted.mean(x, wi))
|
404 |
+
l2 <- array(NA, c(n.x1+1,n.q+1))
|
405 |
+
for (i in 1:n.x1){
|
406 |
+
l2[i,1] <- mean(l1[,i])
|
407 |
+
l2[i,2:(n.q+1)] <- quantile(l1[,i], probs=quantiles)
|
408 |
+
}
|
409 |
+
l2[nrow(l2),1] <- mean(l1[ ,ncol(l1)] - l1[ ,1])
|
410 |
+
l2[nrow(l2),2:(n.q+1)] <- quantile(l1[ ,ncol(l1)] - l1[ ,1], probs=quantiles)
|
411 |
+
rownames(l2) <- c(paste(c(rep(paste(x1name,"="),n.x1),
|
412 |
+
paste("\u0394","(",x1vals[1],",",x1vals[length(x1vals)],")")),
|
413 |
+
c(x1vals,"")))
|
414 |
+
colnames(l2) <- c("mean",quantiles)
|
415 |
+
|
416 |
+
ans <- new("post",
|
417 |
+
est=round(l2, digits=digits),
|
418 |
+
did=NULL,
|
419 |
+
sims=l1,
|
420 |
+
model=class(model),
|
421 |
+
link=model$method,
|
422 |
+
quantiles=quantiles,
|
423 |
+
call=call)
|
424 |
+
return(ans)
|
425 |
+
}
|
426 |
+
|
427 |
+
else{
|
428 |
+
|
429 |
+
n.x1 <- length(x1vals)
|
430 |
+
n.x2 <- length(x2vals)
|
431 |
+
|
432 |
+
X_temp <- array(NA, c(n.obs,k,n.x1,n.x2))
|
433 |
+
X <- array(NA, c(n.obs,k-1,n.x1,n.x2))
|
434 |
+
|
435 |
+
for (j in 1:n.x2){
|
436 |
+
for (i in 1:n.x1){
|
437 |
+
newdata <- data.frame(model$model)
|
438 |
+
if (!is.null(holds)){
|
439 |
+
for (k in 1:length(holds)){
|
440 |
+
newdata[ ,names(holds)[k]] <- as.numeric(holds[k])
|
441 |
+
}
|
442 |
+
}
|
443 |
+
newdata[ ,x1name] <- x1vals[i]
|
444 |
+
newdata[ ,x2name] <- x2vals[j]
|
445 |
+
X_temp[ , ,i,j] <- suppressWarnings(model.matrix(polr(getCall(model)$formula, data=newdata)))
|
446 |
+
X[ , ,i,j] <- X_temp[,-1,i,j]
|
447 |
+
}
|
448 |
+
}
|
449 |
+
|
450 |
+
X <- aperm(X, c(2,1,3,4))
|
451 |
+
l1 <- apply(apply(X, c(2,3,4), function(x) link(-tau[,cut+1] + beta %*% x)), c(1,3,4), function(x) weighted.mean(x, wi))
|
452 |
+
l2 <- array(NA, c(n.x1+1,n.q+1,n.x2))
|
453 |
+
for (j in 1:n.x2){
|
454 |
+
for (i in 1:n.x1){
|
455 |
+
l2[i,1,j] <- mean(l1[,i,j])
|
456 |
+
l2[i,2:(n.q+1),j] <- quantile(l1[,i,j], probs=quantiles)
|
457 |
+
}
|
458 |
+
l2[nrow(l2),1,j] <- mean(l1[ ,n.x1,j] - l1[ ,1,j])
|
459 |
+
l2[nrow(l2),2:(n.q+1),j] <- quantile(l1[ ,n.x1,j] - l1[ ,1,j], probs=quantiles)
|
460 |
+
}
|
461 |
+
dimnames(l2) <- list(paste(c(rep(paste(x1name," ="),n.x1),paste("\u0394","(",x1vals[1],",",x1vals[length(x1vals)],")")),c(x1vals,"")),
|
462 |
+
c("mean",quantiles),
|
463 |
+
paste(c(rep(paste(x2name," ="),n.x2)),
|
464 |
+
c(x2vals)))
|
465 |
+
|
466 |
+
if (is.null(did)){did <- c(x2vals[1],x2vals[n.x2])} else{did <- did}
|
467 |
+
l3 <- array(NA, c(1,n.q+1))
|
468 |
+
l3[1,1] <- mean((l1[ ,n.x1,match(did[2],x2vals)] - l1[ ,1,match(did[2],x2vals)]) - (l1[ ,n.x1,match(did[1],x2vals)] - l1[ ,1,match(did[1],x2vals)]))
|
469 |
+
l3[1,2:(n.q+1)] <- quantile((l1[ ,n.x1,match(did[2],x2vals)] - l1[ ,1,match(did[2],x2vals)]) - (l1[ ,n.x1,match(did[1],x2vals)] - l1[ ,1,match(did[1],x2vals)]), probs=quantiles)
|
470 |
+
dimnames(l3) <- list("did",c("mean",quantiles))
|
471 |
+
|
472 |
+
ans <- new("post",
|
473 |
+
est=round(l2, digits=digits),
|
474 |
+
did=round(l3, digits=digits),
|
475 |
+
sims=l1,
|
476 |
+
model=class(model),
|
477 |
+
link=model$method,
|
478 |
+
quantiles=quantiles,
|
479 |
+
call=call)
|
480 |
+
return(ans)
|
481 |
+
}
|
482 |
+
}
|
483 |
+
}
|
484 |
+
|
485 |
+
setMethod("post", signature(model = "lm"), post.glm)
|
486 |
+
setMethod("post", signature(model = "glm"), post.glm)
|
487 |
+
setMethod("post", signature(model = "svyglm"), post.glm)
|
488 |
+
setMethod("post", signature(model = "polr"), post.polr)
|
489 |
+
|
490 |
+
|
491 |
+
|
492 |
+
|
493 |
+
|
36/replication_package/postSim.R
ADDED
@@ -0,0 +1,173 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
###### postSim ######
|
2 |
+
|
3 |
+
setGeneric("postSim",
|
4 |
+
function(object, n.sims=1000){
|
5 |
+
standardGeneric("postSim")
|
6 |
+
}
|
7 |
+
)
|
8 |
+
|
9 |
+
setClass("postSim",
|
10 |
+
slots = c(coef = "matrix",
|
11 |
+
sigma = "numeric")
|
12 |
+
)
|
13 |
+
|
14 |
+
setClass("postSim.polr",
|
15 |
+
slots = c(coef = "matrix",
|
16 |
+
zeta = "matrix")
|
17 |
+
)
|
18 |
+
|
19 |
+
setMethod("postSim", signature(object = "lm"),
|
20 |
+
function(object, n.sims=1000)
|
21 |
+
{
|
22 |
+
object.class <- class(object)[[1]]
|
23 |
+
summ <- summary (object)
|
24 |
+
coef <- summ$coef[,1:2,drop=FALSE]
|
25 |
+
dimnames(coef)[[2]] <- c("coef.est","coef.sd")
|
26 |
+
sigma.hat <- summ$sigma
|
27 |
+
beta.hat <- coef[,1,drop = FALSE]
|
28 |
+
V.beta <- summ$cov.unscaled
|
29 |
+
n <- summ$df[1] + summ$df[2]
|
30 |
+
k <- summ$df[1]
|
31 |
+
sigma <- rep (NA, n.sims)
|
32 |
+
beta <- array (NA, c(n.sims,k))
|
33 |
+
dimnames(beta) <- list (NULL, rownames(beta.hat))
|
34 |
+
for (s in 1:n.sims){
|
35 |
+
sigma[s] <- sigma.hat*sqrt((n-k)/rchisq(1,n-k))
|
36 |
+
beta[s,] <- MASS::mvrnorm (1, beta.hat, V.beta*sigma[s]^2)
|
37 |
+
}
|
38 |
+
|
39 |
+
ans <- new("postSim",
|
40 |
+
coef = beta,
|
41 |
+
sigma = sigma)
|
42 |
+
return (ans)
|
43 |
+
}
|
44 |
+
)
|
45 |
+
|
46 |
+
|
47 |
+
setMethod("postSim", signature(object = "glm"),
|
48 |
+
function(object, n.sims=1000)
|
49 |
+
{
|
50 |
+
object.class <- class(object)[[1]]
|
51 |
+
summ <- summary (object, correlation=TRUE, dispersion = object$dispersion)
|
52 |
+
coef <- summ$coef[,1:2,drop=FALSE]
|
53 |
+
dimnames(coef)[[2]] <- c("coef.est","coef.sd")
|
54 |
+
beta.hat <- coef[,1,drop=FALSE]
|
55 |
+
sd.beta <- coef[,2,drop=FALSE]
|
56 |
+
corr.beta <- summ$corr
|
57 |
+
n <- summ$df[1] + summ$df[2]
|
58 |
+
k <- summ$df[1]
|
59 |
+
V.beta <- corr.beta * array(sd.beta,c(k,k)) * t(array(sd.beta,c(k,k)))
|
60 |
+
beta <- array (NA, c(n.sims,k))
|
61 |
+
dimnames(beta) <- list (NULL, dimnames(beta.hat)[[1]])
|
62 |
+
for (s in 1:n.sims){
|
63 |
+
beta[s,] <- MASS::mvrnorm (1, beta.hat, V.beta)
|
64 |
+
}
|
65 |
+
# Added by Masanao
|
66 |
+
beta2 <- array (0, c(n.sims,length(coefficients(object))))
|
67 |
+
dimnames(beta2) <- list (NULL, names(coefficients(object)))
|
68 |
+
beta2[,dimnames(beta2)[[2]]%in%dimnames(beta)[[2]]] <- beta
|
69 |
+
# Added by Masanao
|
70 |
+
sigma <- rep (sqrt(summ$dispersion), n.sims)
|
71 |
+
|
72 |
+
ans <- new("postSim",
|
73 |
+
coef = beta2,
|
74 |
+
sigma = sigma)
|
75 |
+
return(ans)
|
76 |
+
}
|
77 |
+
)
|
78 |
+
|
79 |
+
|
80 |
+
setMethod("postSim", signature(object = "polr"),
|
81 |
+
function(object, n.sims=1000){
|
82 |
+
x <- as.matrix(model.matrix(object))
|
83 |
+
coefs <- coef(object)
|
84 |
+
k <- length(coefs)
|
85 |
+
zeta <- object$zeta
|
86 |
+
Sigma <- vcov(object)
|
87 |
+
|
88 |
+
if(n.sims==1){
|
89 |
+
parameters <- t(MASS::mvrnorm(n.sims, c(coefs, zeta), Sigma))
|
90 |
+
}else{
|
91 |
+
parameters <- MASS::mvrnorm(n.sims, c(coefs, zeta), Sigma)
|
92 |
+
}
|
93 |
+
ans <- new("postSim.polr",
|
94 |
+
coef = parameters[,1:k,drop=FALSE],
|
95 |
+
zeta = parameters[,-(1:k),drop=FALSE])
|
96 |
+
return(ans)
|
97 |
+
}
|
98 |
+
)
|
99 |
+
|
100 |
+
|
101 |
+
setMethod("postSim", signature(object = "svyglm"),
|
102 |
+
function(object, n.sims=1000)
|
103 |
+
{
|
104 |
+
object.class <- class(object)[[2]]
|
105 |
+
summ <- summary (object, correlation=TRUE, dispersion = object$dispersion)
|
106 |
+
coef <- summ$coef[,1:2,drop=FALSE]
|
107 |
+
dimnames(coef)[[2]] <- c("coef.est","coef.sd")
|
108 |
+
beta.hat <- coef[,1,drop=FALSE]
|
109 |
+
sd.beta <- coef[,2,drop=FALSE]
|
110 |
+
corr.beta <- summ$corr
|
111 |
+
n <- summ$df[1] + summ$df[2]
|
112 |
+
k <- summ$df[1]
|
113 |
+
V.beta <- corr.beta * array(sd.beta,c(k,k)) * t(array(sd.beta,c(k,k)))
|
114 |
+
beta <- array (NA, c(n.sims,k))
|
115 |
+
dimnames(beta) <- list (NULL, dimnames(beta.hat)[[1]])
|
116 |
+
for (s in 1:n.sims){
|
117 |
+
beta[s,] <- MASS::mvrnorm (1, beta.hat, V.beta)
|
118 |
+
}
|
119 |
+
beta2 <- array (0, c(n.sims,length(coefficients(object))))
|
120 |
+
dimnames(beta2) <- list (NULL, names(coefficients(object)))
|
121 |
+
beta2[,dimnames(beta2)[[2]]%in%dimnames(beta)[[2]]] <- beta
|
122 |
+
sigma <- rep (sqrt(summ$dispersion), n.sims)
|
123 |
+
|
124 |
+
ans <- new("postSim",
|
125 |
+
coef = beta2,
|
126 |
+
sigma = sigma)
|
127 |
+
return(ans)
|
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 |
+
|
36/should_reproduce.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:e5e35976e408ddac58129b747106217d47cd00a01cac05276e73bdb6794b4c0c
|
3 |
+
size 46
|