Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. library("Amelia")
  2. data(freetrade)
  3. amelia.out <- amelia(freetrade, m = 15, ts = "year", cs = "country")
  4.  
  5. library("Zelig")
  6. zelig.fit <- zelig(tariff ~ pop + gdp.pc + year + polity, data = amelia.out$imputations, model = "ls", cite = FALSE)
  7. summary(zelig.fit)
  8.  
  9. Model: ls
  10. Number of multiply imputed data sets: 15
  11.  
  12. Combined results:
  13.  
  14. Call:
  15. lm(formula = formula, weights = weights, model = F, data = data)
  16.  
  17. Coefficients:
  18. Value Std. Error t-stat p-value
  19. (Intercept) 3.18e+03 7.22e+02 4.41 6.20e-05
  20. pop 3.13e-08 5.59e-09 5.59 4.21e-08
  21. gdp.pc -2.11e-03 5.53e-04 -3.81 1.64e-04
  22. year -1.58e+00 3.63e-01 -4.37 7.11e-05
  23. polity 5.52e-01 3.16e-01 1.75 8.41e-02
  24.  
  25. For combined results from datasets i to j, use summary(x, subset = i:j).
  26. For separate results, use print(summary(x), subset = i:j).
  27.  
  28. library("mitools")
  29. imp.data <- imputationList(amelia.out$imputations)
  30. mitools.fit <- MIcombine(with(imp.data, lm(tariff ~ polity + pop + gdp.pc + year)))
  31. mitools.res <- summary(mitools.fit)
  32. mitools.res <- cbind(mitools.res, df = mitools.fit$df)
  33. mitools.res
  34.  
  35. results se (lower upper) missInfo df
  36. (Intercept) 3.18e+03 7.22e+02 1.73e+03 4.63e+03 57 % 45.9
  37. pop 3.13e-08 5.59e-09 2.03e-08 4.23e-08 19 % 392.1
  38. gdp.pc -2.11e-03 5.53e-04 -3.20e-03 -1.02e-03 21 % 329.4
  39. year -1.58e+00 3.63e-01 -2.31e+00 -8.54e-01 57 % 45.9
  40. polity 5.52e-01 3.16e-01 -7.58e-02 1.18e+00 41 % 90.8
  41.  
  42. combined.results <- merge(mitools.res, zelig.res$coefficients[, c("t-stat", "p-value")], by = "row.names", all.x = TRUE)
  43.  
  44. as.mids2 <- function(data2, .imp=1, .id=2){
  45. ini <- mice(data2[data2[, .imp] == 0, -c(.imp, .id)], m = max(as.numeric(data2[, .imp])), maxit=0)
  46. names <- names(ini$imp)
  47. if (!is.null(.id)){
  48. rownames(ini$data) <- data2[data2[, .imp] == 0, .id]
  49. }
  50. for (i in 1:length(names)){
  51. for(m in 1:(max(as.numeric(data2[, .imp])))){
  52. if(!is.null(ini$imp[[i]])){
  53. indic <- data2[, .imp] == m & is.na(data2[data2[, .imp]==0, names[i]])
  54. ini$imp[[names[i]]][m] <- data2[indic, names[i]]
  55. }
  56. }
  57. }
  58. return(ini)
  59. }
  60.  
  61. library("mice")
  62. imp.data <- do.call("rbind", amelia.out$imputations)
  63. imp.data <- rbind(freetrade, imp.data)
  64. imp.data$.imp <- as.numeric(rep(c(0:15), each = nrow(freetrade)))
  65. mice.data <- as.mids2(imp.data, .imp = ncol(imp.data), .id = NULL)
  66.  
  67. mice.fit <- with(mice.data, lm(tariff ~ polity + pop + gdp.pc + year))
  68. mice.res <- summary(pool(mice.fit, method = "rubin1987"))
  69.  
  70. est se t df Pr(>|t|) lo 95 hi 95 nmis fmi lambda
  71. (Intercept) 3.18e+03 7.22e+02 4.41 45.9 6.20e-05 1.73e+03 4.63e+03 NA 0.571 0.552
  72. pop 3.13e-08 5.59e-09 5.59 392.1 4.21e-08 2.03e-08 4.23e-08 0 0.193 0.189
  73. gdp.pc -2.11e-03 5.53e-04 -3.81 329.4 1.64e-04 -3.20e-03 -1.02e-03 0 0.211 0.206
  74. year -1.58e+00 3.63e-01 -4.37 45.9 7.11e-05 -2.31e+00 -8.54e-01 0 0.570 0.552
  75. polity 5.52e-01 3.16e-01 1.75 90.8 8.41e-02 -7.58e-02 1.18e+00 2 0.406 0.393
  76.  
  77. pool.r.squared(mice.fit)
  78.  
  79. mice.fit2 <- with(mice.data, lm(tariff ~ polity + pop + gdp.pc))
  80. pool.compare(mice.fit, mice.fit2, method = "Wald")$pvalue
  81.  
  82. lichtrubin <- function(fit){
  83. ## pools the p-values of a one-sided test according to the Licht-Rubin method
  84. ## this method pools p-values in the z-score scale, and then transforms back
  85. ## the result to the 0-1 scale
  86. ## Licht C, Rubin DB (2011) unpublished
  87. if (!is.mira(fit)) stop("Argument 'fit' is not an object of class 'mira'.")
  88. fitlist <- fit$analyses
  89. if (!inherits(fitlist[[1]], "htest")) stop("Object fit$analyses[[1]] is not an object of class 'htest'.")
  90. m <- length(fitlist)
  91. p <- rep(NA, length = m)
  92. for (i in 1:m) p[i] <- fitlist[[i]]$p.value
  93. z <- qnorm(p) # transform to z-scale
  94. num <- mean(z)
  95. den <- sqrt(1 + var(z))
  96. pnorm( num / den) # average and transform back
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement