File size: 18,390 Bytes
b1a2e61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
####### post #######

setGeneric("post", 
           function(model,x1name=NULL,x1vals=NULL,x2name=NULL,x2vals=NULL,holds=NULL,
                    n.sims=1000,cut=NULL,quantiles=c(.025,.975),did=NULL,weights=NULL, digits=2){
             standardGeneric("post")
           }
)

setClassUnion("arrayORNULL", c("array","NULL"))
setClassUnion("listORcharacter", c("list","character"))

setClass("post", 
         slots = c(est = "array", 
                   did = "arrayORNULL", 
                   sims = "array", 
                   model = "character", 
                   link = "listORcharacter", 
                   quantiles = "numeric", 
                   call = "call")
)


post.glm <- function(model,x1name=NULL,x1vals=NULL,x2name=NULL,x2vals=NULL,holds=NULL,
                     n.sims=1000,cut=NULL,quantiles=c(.025,.975),did=NULL,weights=NULL, digits=2){
  
  call <- match.call()
  
  sims <- postSim(model, n.sims=n.sims)
  
  if (family(model)[2]=="identity"){link <- identity} 
  else if (family(model)[2]=="probit"){link <- pnorm}
  else if (family(model)[2]=="logit"){link <- plogis}
  else if (family(model)[2]=="log"){link <- exp}
  else if (family(model)[2]=="cloglog"){link <- function(x){1-exp(-exp(x))}}
  else {stop("Link function is not supported")}
  
  if (is.null(weights)){wi <- c(rep(1, length(model$model[,1])))} else{wi <- weights}
  n.obs <- length(model.matrix(model)[,1])
  k <- length(model.matrix(model)[1,])
  n.q <- length(quantiles)
  
  if (is.null(x1name)){
    X <- array(NA, c(n.obs,k))
    newdata <- data.frame(model$model)
    if (!is.null(holds)){
      for (i in 1:length(holds)){
        newdata[ ,names(holds)[i]] <- as.numeric(holds[i])
      }
    }
    X <- aperm(model.matrix(lm(formula(model), data=newdata)))
    l1 <- array(NA, c(nrow(sims@coef),1))
    l1[,1] <- apply(link(sims@coef %*% X), 1, function(x) weighted.mean(x, wi))
    l2 <- array(NA, c(1,n.q+1))
    l2[1,1] <- mean(l1)
    l2[1,2:(n.q+1)] <- quantile(l1, probs=quantiles)
    colnames(l2) <- c("mean",quantiles)
    
    ans <- new("post", 
               est=round(l2, digits=digits),
               did=NULL,
               sims=l1, 
               model=class(model), 
               link=family(model)[2], 
               quantiles=quantiles, 
               call=call)
    return(ans)
  }
  
  else if (is.null(x2name)){
    
    n.x1 <- length(x1vals)
    X <- array(NA, c(n.obs,k,n.x1))
    
    for (i in 1:(n.x1)){
      newdata <- data.frame(model$model)
      if (!is.null(holds)){
        for (j in 1:length(holds)){
          newdata[ ,names(holds)[j]] <- as.numeric(holds[j])
        }
      }
      newdata[ ,x1name] <- x1vals[i]
      X[ , ,i] <- model.matrix(lm(formula(model), data=newdata))
    }
    
    X <- aperm(X, c(2,1,3))
    l1 <- apply(apply(X, c(2,3), function(x) link(sims@coef %*% x)), c(1,3), function(x) weighted.mean(x, wi))
    l2 <- array(NA, c(n.x1+1,n.q+1))
    l2[1:n.x1,1] <- apply(l1, 2, mean)
    l2[1:n.x1,2:(n.q+1)] <- aperm(apply(l1, 2, function(x) quantile(x, probs=quantiles)))
    
    l2[nrow(l2),1] <- mean(l1[ ,n.x1] - l1[ ,1])
    l2[nrow(l2),2:(n.q+1)] <- quantile(l1[ ,n.x1] - l1[ ,1], probs=quantiles)
    rownames(l2) <- c(paste(c(rep(paste(x1name,"="),n.x1),
                              paste("\u0394","(",x1vals[1],",",x1vals[length(x1vals)],")")),
                            c(x1vals,"")))  
    colnames(l2) <- c("mean",quantiles)
    
    ans <- new("post", 
               est=round(l2, digits=digits), 
               did=NULL, 
               sims=l1, 
               model=class(model), 
               link=family(model)[2], 
               quantiles=quantiles, 
               call=call)
    return(ans)
  } 
  
  else{
    
    n.x1 <- length(x1vals)
    n.x2 <- length(x2vals)
    X <- array(NA, c(n.obs,k,n.x1,n.x2))
    
    for (j in 1:n.x2){
      for (i in 1:n.x1){
        newdata <- data.frame(model$model)
        if (!is.null(holds)){
          for (k in 1:length(holds)){
            newdata[ ,names(holds)[k]] <- as.numeric(holds[k])
          }
        }
        newdata[ ,x1name] <- x1vals[i]
        newdata[ ,x2name] <- x2vals[j]
        X[ , ,i,j] <- model.matrix(lm(formula(model), data=newdata))
      }
    }
    
    X <- aperm(X, c(2,1,3,4))
    l1 <- apply(apply(X, c(2,3,4), function(x) link(sims@coef %*% x)), c(1,3,4), function(x) weighted.mean(x, wi))
    l2 <- array(NA, c(n.x1+1,n.q+1,n.x2))
    l2[1:n.x1,1,1:n.x2] <- apply(l1,c(2,3),mean)
    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))
    l2[nrow(l2),1,1:n.x2] <- apply(l1[ ,n.x1,1:n.x2] - l1[ ,1,1:n.x2], 2, mean)
    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))
    dimnames(l2) <- list(paste(c(rep(paste(x1name,"="),n.x1),paste("\u0394","(",x1vals[1],",",x1vals[length(x1vals)],")")),c(x1vals,"")),
                         c("mean",quantiles),
                         paste(c(rep(paste(x2name,"="),n.x2)),
                               c(x2vals)))
    
    if (is.null(did)){did <- c(x2vals[1],x2vals[n.x2])} else{did <- did}
    l3 <- array(NA, c(1,n.q+1))
    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)]))
    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)
    dimnames(l3) <- list("did",c("mean",quantiles)) 
    
    ans <- new("post", 
               est=round(l2, digits=digits), 
               did=round(l3, digits=digits), 
               sims=l1, 
               model=class(model), 
               link=family(model)[2], 
               quantiles=quantiles, 
               call=call)
    return(ans)
  }
}


post.polr <- function(model,x1name=NULL,x1vals=NULL,x2name=NULL,x2vals=NULL,holds=NULL,
         n.sims=1000,cut=NULL,quantiles=c(.025,.975),did=NULL,weights=NULL, digits=2){
  
  call <- match.call()
  
  sims <- suppressMessages(postSim(model, n.sims=n.sims))
  
  if (is.null(weights)){wi <- c(rep(1, length(model$model[,1])))} else{wi <- weights}
  
  if (model$method=="probit"){link <- pnorm}
  else if (model$method=="logistic"){link <- plogis}
  else if (model$method=="cloglog"){link <- function(x){1-exp(-exp(x))}}
  else {stop("Link function is not supported")}
  
  n.obs <- length(model$model[,1])
  k <- length(model.matrix(polr(getCall(model)$formula, model$model))[1,])
  n.q <- length(quantiles)
  n.y <- length(levels(model$model[,1]))
  n.z <- length(model$zeta)
  tau <- array(NA, c(n.sims,n.z+2))
  tau[,1] <- -Inf
  tau[,2:(ncol(tau)-1)] <- sims@zeta[,1:n.z]
  tau[,ncol(tau)] <- Inf
  beta <- sims@coef
  
  if (is.null(cut)){
    
    if (is.null(x1name)){
      
      X_temp <- array(NA, c(n.obs,k))
      X <- array(NA, c(n.obs,k-1))
      
      newdata <- data.frame(model$model)
      if (!is.null(holds)){
        for (j in 1:length(holds)){
          newdata[ ,names(holds)[j]] <- as.numeric(holds[j])
        }
      }
      X_temp[ , ] <- suppressWarnings(model.matrix(polr(getCall(model)$formula, data=newdata)))
      X[ , ] <- X_temp[,-1]
      X <- aperm(X)
      
      l1 <- array(NA, c(n.sims, n.obs, n.y))
      for (z in 1:n.y){
        l1[,,z] <- link(tau[,z+1] - beta %*% X) - link(tau[,z] - beta %*% X)
      }
      
      l2 <- apply(l1, c(1,3), function(x) weighted.mean(x, wi))
      l3 <- array(NA, c(n.y,n.q+1))
      for (i in 1:n.y){
        l3[i,1] <- mean(l2[,i])
        l3[i,2:(n.q+1)] <- quantile(l2[,i], probs=quantiles)
      }
      rownames(l3) <- paste(c(rep("Y =",n.y)), c(1:n.y))
      colnames(l3) <- c("mean",quantiles)
      
      ans <- new("post", 
                 est=round(l3, digits=digits), 
                 did=NULL, 
                 sims=l2, 
                 model=class(model), 
                 link=model$method, 
                 quantiles=quantiles, 
                 call=call)
      return(ans)
    }
    
    else if (is.null(x2name)){
      
      n.x1 <- length(x1vals)
      
      X_temp <- array(NA, c(n.obs,k,n.x1))
      X <- array(NA, c(n.obs,k-1,n.x1))
      
      for (i in 1:(n.x1)){
        newdata <- data.frame(model$model)
        if (!is.null(holds)){
          for (j in 1:length(holds)){
            newdata[ ,names(holds)[j]] <- as.numeric(holds[j])
          }
        }
        newdata[ ,x1name] <- x1vals[i]
        X_temp[ , ,i] <- suppressWarnings(model.matrix(polr(getCall(model)$formula, data=newdata)))
        X[ , ,i] <- X_temp[,-1,i]
      }
      
      l1 <- array(NA, c(n.sims, n.obs, n.x1, n.y))
      X <- aperm(X, c(2,1,3))
      for (z in 1:n.y){
        l1[,,,z] <- apply(X, c(2,3), function(x) (link(tau[,z+1] - beta %*% x) - link(tau[,z] - beta %*% x)))
      }
      
      l2 <- apply(l1, c(1,3,4), function(x) weighted.mean(x, wi))
      l3 <- array(NA, c(n.x1+1, n.q+1, n.y))
      for (j in 1:n.y){
        for (i in 1:n.x1){
          l3[i,1,j] <- mean(l2[,i,j])
          l3[i,2:(n.q+1),j] <- quantile(l2[,i,j], probs=quantiles)
        }
        l3[nrow(l3),1,j] <- mean(l2[ ,n.x1,j] - l2[ ,1,j])
        l3[nrow(l3),2:(n.q+1),j] <- quantile(l2[ ,n.x1,j] - l2[ ,1,j], probs=quantiles)
      }
      dimnames(l3) <- list(paste(c(rep(paste(x1name,"="),n.x1),paste("\u0394","(",x1vals[1],",",x1vals[length(x1vals)],")")),c(x1vals,"")),
                           c("mean",quantiles),
                           paste(c(rep("Y =",length(levels(model$model[,1])))),
                                 c(1:length(levels(model$model[,1]))))) 
      
      ans <- new("post", 
                 est=round(l3, digits=digits), 
                 did=NULL, 
                 sims=l2, 
                 model=class(model), 
                 link=model$method, 
                 quantiles=quantiles, 
                 call=call)
      return(ans)
    }
    
    else{
      
      n.x1 <- length(x1vals)
      n.x2 <- length(x2vals)
      
      X_temp <- array(NA, c(n.obs,k,n.x1,n.x2))
      X <- array(NA, c(n.obs,k-1,n.x1,n.x2))
      
      for (j in 1:n.x2){
        for (i in 1:(n.x1)){
          newdata <- data.frame(model$model)
          if (!is.null(holds)){
            for (k in 1:length(holds)){
              newdata[ ,names(holds)[k]] <- as.numeric(holds[k])
            }
          }
          newdata[ ,x1name] <- x1vals[i]
          newdata[ ,x2name] <- x2vals[j]
          X_temp[ , ,i,j] <- suppressWarnings(model.matrix(polr(getCall(model)$formula, data=newdata)))
          X[ , ,i,j] <- X_temp[,-1,i,j]
        }
      }
      
      X <- aperm(X, c(2,1,3,4))
      
      l1 <- array(NA, c(n.sims, n.obs, n.x1, n.x2, n.y))
      for (z in 1:n.y){
        l1[,,,,z] <- apply(X, c(2,3,4), function(x) (link(tau[,z+1] - beta %*% x) - link(tau[,z] - beta %*% x)))
      }
      
      l2 <- apply(l1, c(1,3,4,5), function(x) weighted.mean(x, wi))
      l3 <- array(NA, c(n.x1+1, n.q+1, n.x2, n.y))
      for (k in 1:n.y){
        for (j in 1:n.x2){
          for (i in 1:n.x1){
            l3[i,1,j,k] <- mean(l2[,i,j,k])
            l3[i,2:(n.q+1),j,k] <- quantile(l2[,i,j,k], probs=quantiles)
          }
          l3[n.x1+1,1,j,k] <- mean(l2[,n.x1,j,k] - l2[,1,j,k])
          l3[n.x1+1,2:(n.q+1),j,k] <- quantile(l2[,n.x1,j,k] - l2[,1,j,k], probs=quantiles)
        }
      }
      dimnames(l3) <- list(paste(c(rep(paste(x1name," ="),n.x1),paste("\u0394","(",x1vals[1],",",x1vals[length(x1vals)],")")),c(x1vals,"")),
                           c("mean",quantiles),
                           paste(c(rep(paste(x2name,"="),n.x2)),x2vals),
                           paste(c(rep("Y =",n.y)), c(1:n.y))) 
      
      if (is.null(did)){did <- c(x2vals[1],x2vals[n.x2])} else{did <- did}
      l4 <- array(NA, c(n.y,n.q+1))
      for (i in 1:n.y){
        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]))
        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)
      }
      yvals <- 1:n.y
      dimnames(l4) <- list(paste(c(rep(paste("Y","="),n.y)),yvals),c("mean",quantiles)) 
      
      ans <- new("post", 
                 est=round(l3, digits=digits), 
                 did=round(l4, digits=digits), 
                 sims=l2, 
                 model=class(model), 
                 link=model$method, 
                 quantiles=quantiles, 
                 call=call)
      return(ans)
    }
  }
  
  else{
    
    if (is.null(x1name)){
      
      X_temp <- array(NA, c(n.obs,k))
      X <- array(NA, c(n.obs,k-1))
      
      newdata <- data.frame(model$model)
      if (!is.null(holds)){
        for (j in 1:length(holds)){
          newdata[ ,names(holds)[j]] <- as.numeric(holds[j])
        }
      }
      X_temp[ , ] <- suppressWarnings(model.matrix(polr(getCall(model)$formula, data=newdata)))
      X[ , ] <- X_temp[,-1]
      X <- aperm(X)
      
      l1 <- apply(link(-tau[,cut+1] + beta %*% X), 1, function(x) weighted.mean(x, wi))
      l2 <- array(NA, c(1,n.q+1))
      l2[1,1] <- mean(l1)
      l2[1,2:(n.q+1)] <- quantile(l1, probs=quantiles)
      colnames(l2) <- c("mean",quantiles)
      
      ans <- new("post", 
                 est=round(l2, digits=digits), 
                 did=NULL, 
                 sims=l1, 
                 model=class(model), 
                 link=model$method, 
                 quantiles=quantiles, 
                 call=call)
      return(ans)
    }
    
    
    else if (is.null(x2name)){
      
      n.x1 <- length(x1vals)
      
      X_temp <- array(NA, c(n.obs,k,n.x1))
      X <- array(NA, c(n.obs,k-1,n.x1))
      
      for (i in 1:(n.x1)){
        newdata <- data.frame(model$model)
        if (!is.null(holds)){
          for (j in 1:length(holds)){
            newdata[ ,names(holds)[j]] <- as.numeric(holds[j])
          }
        }
        newdata[ ,x1name] <- x1vals[i]
        X_temp[ , ,i] <- suppressWarnings(model.matrix(polr(getCall(model)$formula, data=newdata)))
        X[ , ,i] <- X_temp[,-1,i]
      }
      
      X <- aperm(X, c(2,1,3))
      l1 <- apply(apply(X, c(2,3), function(x) link(-tau[,cut+1] + beta %*% x)), 
                  c(1,3), function(x) weighted.mean(x, wi))
      l2 <- array(NA, c(n.x1+1,n.q+1))
      for (i in 1:n.x1){
        l2[i,1] <- mean(l1[,i])
        l2[i,2:(n.q+1)] <- quantile(l1[,i], probs=quantiles)
      }
      l2[nrow(l2),1] <- mean(l1[ ,ncol(l1)] - l1[ ,1])
      l2[nrow(l2),2:(n.q+1)] <- quantile(l1[ ,ncol(l1)] - l1[ ,1], probs=quantiles)
      rownames(l2) <- c(paste(c(rep(paste(x1name,"="),n.x1),
                                paste("\u0394","(",x1vals[1],",",x1vals[length(x1vals)],")")),
                              c(x1vals,"")))  
      colnames(l2) <- c("mean",quantiles)
      
      ans <- new("post", 
                 est=round(l2, digits=digits), 
                 did=NULL, 
                 sims=l1, 
                 model=class(model), 
                 link=model$method, 
                 quantiles=quantiles, 
                 call=call)
      return(ans)
    } 
    
    else{
      
      n.x1 <- length(x1vals)
      n.x2 <- length(x2vals)
      
      X_temp <- array(NA, c(n.obs,k,n.x1,n.x2))
      X <- array(NA, c(n.obs,k-1,n.x1,n.x2))
      
      for (j in 1:n.x2){
        for (i in 1:n.x1){
          newdata <- data.frame(model$model)
          if (!is.null(holds)){
            for (k in 1:length(holds)){
              newdata[ ,names(holds)[k]] <- as.numeric(holds[k])
            }
          }
          newdata[ ,x1name] <- x1vals[i]
          newdata[ ,x2name] <- x2vals[j]
          X_temp[ , ,i,j] <- suppressWarnings(model.matrix(polr(getCall(model)$formula, data=newdata)))
          X[ , ,i,j] <- X_temp[,-1,i,j]
        }
      }
      
      X <- aperm(X, c(2,1,3,4))
      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))
      l2 <- array(NA, c(n.x1+1,n.q+1,n.x2))
      for (j in 1:n.x2){
        for (i in 1:n.x1){
          l2[i,1,j] <- mean(l1[,i,j])
          l2[i,2:(n.q+1),j] <- quantile(l1[,i,j], probs=quantiles)
        }
        l2[nrow(l2),1,j] <- mean(l1[ ,n.x1,j] - l1[ ,1,j])
        l2[nrow(l2),2:(n.q+1),j] <- quantile(l1[ ,n.x1,j] - l1[ ,1,j], probs=quantiles)
      }
      dimnames(l2) <- list(paste(c(rep(paste(x1name," ="),n.x1),paste("\u0394","(",x1vals[1],",",x1vals[length(x1vals)],")")),c(x1vals,"")),
                           c("mean",quantiles),
                           paste(c(rep(paste(x2name," ="),n.x2)),
                                 c(x2vals)))
      
      if (is.null(did)){did <- c(x2vals[1],x2vals[n.x2])} else{did <- did}
      l3 <- array(NA, c(1,n.q+1))
      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)]))
      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)
      dimnames(l3) <- list("did",c("mean",quantiles)) 
      
      ans <- new("post", 
                 est=round(l2, digits=digits), 
                 did=round(l3, digits=digits), 
                 sims=l1, 
                 model=class(model), 
                 link=model$method, 
                 quantiles=quantiles, 
                 call=call)
      return(ans)
    }
  }
}

setMethod("post", signature(model = "lm"), post.glm)
setMethod("post", signature(model = "glm"), post.glm)
setMethod("post", signature(model = "svyglm"), post.glm)  
setMethod("post", signature(model = "polr"), post.polr)