R version 2.15.2 (2012-10-26) -- "Trick or Treat" Copyright (C) 2012 The R Foundation for Statistical Computing ISBN 3-900051-07-0 Platform: x86_64-pc-linux-gnu (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > > assignInNamespace(".help.ESS", help, ns=asNamespace("base")) > options(STERM='iESS', str.dendrogram.last="'", editor='emacsclient', show.error.locations=TRUE) > ## Paul Johnson > ## 2012-12-19 > > ## Concentrating on the amsweep function in Amelia. > > ## Results below indicate that amsweep.pj2 generates overall Amelia runs that are > ## between 5 and 10 percent shorter. Note I'm not comparing against the > ## troubled implementation of amsweep in Amelia-1.6.3 as on CRAN. If I did > ## that, I'd have a 30% faster run. > > ## Larger scale testing will be done if more test cases can be provided. > > datM <- if (length(list.files(path=".", pattern="missingDat.csv"))){ + read.csv("missingDat.csv") + } else { + read.csv("http://pj.freefaculty.org/scraps/profile/missingDat.csv") + } > > > library(Amelia) Loading required package: foreign ## ## Amelia II: Multiple Imputation ## (Version 1.6.3, built: 2012-06-21) ## Copyright (C) 2005-2012 James Honaker, Gary King and Matthew Blackwell ## Refer to http://gking.harvard.edu/amelia/ for more information ## > > > ##pj Fix La_chol() problem in Amelia-1.6.3. > ##pj This is the ORIGINAL amsweep function, as in Amelia source code > ##pj With only one correction, don't use mp.inv()--just uncomment > ##pj old usage of solve. > ## Sweep function (NOTE does not take sign as sign to reverse sweep) > amsweep <- function(g,m,reverse=FALSE){ + if (identical(m,vector(mode='logical',length=length(m)))) # This is check for sweeping on no elements + {return(g)} else { + p<-nrow(g) + rowsm<-sum(m) + + # Add Checks of Inputs About Here + + if (rowsm==p){ + h<-solve(g) + h<-(-h) + } else { + kseq<-1:p + k<-kseq[m] + kcompl<-kseq[-k] # we could do everything with m and !m, but only with small numbers of variables + g11<-g[k,k,drop=FALSE] # can not subset matricies with long logical vectors + g12<-g[k,kcompl, drop=FALSE] + g21<-t(g12) + g22<-g[kcompl,kcompl , drop=FALSE] + + ## this doesn't actually save us much time. + ##pj h11a <- try(am.inv(a = g11), silent = TRUE) + ##pj uncomment following + h11a <- try(solve(g11), silent=TRUE) + if (inherits(h11a,"try-error")) { + h11a<-mpinv(g11) # This is where significant time is spent! + # About as much time as in the rest of the EM + # step + } + h11<-as.matrix((-h11a)) + if (reverse) {sgn2<- -1} else {sgn2<- 1} + h12<-as.matrix(sgn2 * (h11a %*% g12)) + h21<-as.matrix(t(h12)) + h22<-g22-(g21 %*% h11a %*% g12) + + hwo<-rbind(cbind(h11,h12),cbind(h21,h22)) + xordering<-c(k,kcompl) + h<-matrix(0,p,p) + h[xordering,xordering]<-hwo + } + return(h) + } + } > > amsweep.orig <- Amelia:::amsweep > > assignInNamespace(x = "amsweep", value = amsweep, ns = "Amelia") > > > ## maybe: double-check time with ps2=0 > set.seed(12345) > system.time( + impA <- amelia(datM, m = 5, idvars="group", p2s = 1) + ) -- Imputation 1 -- 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 -- Imputation 2 -- 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 -- Imputation 3 -- 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 -- Imputation 4 -- 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 -- Imputation 5 -- 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 user system elapsed 59.235 0.288 59.291 > > ## bad case, use empri to accelerate. > set.seed(12345) > system.time( + impA <- amelia(datM, m = 10, idvars="group", p2s = 0, empri = 0.10*nrow(datM)) + ) user system elapsed 21.386 0.036 21.327 > > > > > ##pj my first try, just tidying up > ## amsweep.pj <-function(g, m, reverse=FALSE){ > ## # This is check for sweeping on no elements > ## if (identical(m,vector(mode='logical',length=length(m)))){return(g)} > > ## ##PJ else { > ## p <- nrow(g) > ## rowsm <- sum(m) > > ## ## Add Checks of Inputs About Here > ## if (rowsm == p){ > ## h <- solve(g) > ## h <- (-h) > ## } else { > ## kseq <- 1:p > ## k <- kseq[m] > ## kcompl <- kseq[-k] # we could do everything with m and !m, but only with small numbers of variables > ## g11 <- g[k, k, drop=FALSE] # can not subset matricies with long logical vectors > ## g12 <- g[k, kcompl, drop=FALSE] > ## g21 <- t(g12) > ## g22 <- g[kcompl,kcompl , drop=FALSE] > > ## ## this doesn't actually save us much time. > ## ##PJ h11a <- try(am.inv(a = g11), silent = TRUE) > > ## h11a <- try(solve(g11),silent=TRUE) > ## if (inherits(h11a,"try-error")) { > ## h11a <- Amelia:::mpinv(g11) # This is where significant time is spent! > ## } > ## ## About as much time as in the rest of the EM > ## ## step > ## ##pj } > ## ##pj h11<-as.matrix((-h11a)) > > ## h11 <- -1 * h11a > ## if (reverse) {sgn2 <- -1} else {sgn2 <- 1} > ## ##pj h12<-as.matrix(sgn2 * (h11a %*% g12)) > ## h12 <- sgn2 * (h11a %*% g12) > ## ##pj h21<-as.matrix(t(h12)) > ## h21 <- t(h12) > ## ##pj h22 <- g22-(g21 %*% h11a %*% g12) > ## h22 <- g22-(g21 %*% (h11a %*% g12)) > ## hwo <- rbind(cbind(h11,h12), cbind(h21,h22)) > ## ##pj please verify following: hwo <- c(h11, h21, h12, h22) > ## ##pj dim(hwo) <- c(length(h11)+length(h21)), length(h12) + length(h22)) > ## xordering <- c(k,kcompl) > ## h <- matrix(0,p,p) > ## h[xordering,xordering] <- hwo > ## } > ## h > ## } > > > > ## Sweep function (NOTE does not take sign as sign to reverse sweep) > amsweep.pj2 <-function(g, m, reverse=FALSE){ + ## This is check for sweeping on no elements + if (identical(m,vector(mode='logical',length=length(m)))){return(g)} + + ##PJ else { + p <- nrow(g) + rowsm <- sum(m) + + ## Add Checks of Inputs About Here + if (rowsm == p){ + h <- solve(g) + h <- (-h) + } else { + kseq <- 1:p + k <- kseq[m] + kcompl <- kseq[-k] # we could do everything with m and !m, but only with small numbers of variables + g11 <- g[k, k, drop=FALSE] # can not subset matricies with long logical vectors + g12 <- g[k, kcompl, drop=FALSE] + ##pj: avoid allocation g21 <- t(g12) + g22 <- g[kcompl,kcompl , drop=FALSE] + + ## this doesn't actually save us much time. + ##PJ h11a <- try(am.inv(a = g11), silent = TRUE) + + h11a <- try(solve(g11),silent=TRUE) + if (inherits(h11a,"try-error")) { + h11a <- Amelia:::mpinv(g11) # This is where significant time is spent! + } + ## About as much time as in the rest of the EM + ## step + ##pj } + ##pj h11<-as.matrix((-h11a)) + + h11 <- -1 * h11a + if (reverse) {sgn2 <- -1} else {sgn2 <- 1} + ##pj h12<-as.matrix(sgn2 * (h11a %*% g12)) + h12 <- sgn2 * (h11a %*% g12) + ##pj h21<-as.matrix(t(h12)) + ##pj avoid allocatin h21 <- t(h12) + ##h22 <- g22-(g21 %*% h11a %*% g12) + h22 <- g22 - (t(g12) %*% (sgn2 * h12)) + hwo <- rbind(cbind(h11, h12), cbind(t(h12), h22)) + ##pj previous looks time consuming. Test other approaches. + ##pj how about following: + ##pj hwo <- c(h11, h21, h12, h22) + ##pj dim(hwo) <- c(length(h11)+length(h21)), length(h12) + length(h22)) + xordering <- c(k,kcompl) + h <- matrix(0,p,p) + h[xordering,xordering] <- hwo + } + h + } > > > > ## Replace that into Amelia namespace > assignInNamespace(x = "amsweep", value = amsweep.pj2, ns = "Amelia") > > set.seed(12345) > system.time( + impA <- amelia(datM, m = 5, idvars="group", p2s = 1) + ) -- Imputation 1 -- 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 -- Imputation 2 -- 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 -- Imputation 3 -- 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 -- Imputation 4 -- 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 -- Imputation 5 -- 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 user system elapsed 53.527 0.456 53.778 > > > set.seed(12345) > system.time( + impA <- amelia(datM, m = 10, idvars="group", p2s = 0, empri = 0.10*nrow(datM)) + ) user system elapsed 19.393 0.048 19.358 > q() Save workspace image? [y/n/c]: n Process R finished at Wed Dec 19 03:07:51 2012