@@ -31,7 +31,7 @@ LEAVcore <- function(data, names,
3131 fqout <- prop * size.count
3232 fq_overall <- summary(data [, x ])
3333 fqout <- ifelse(fqout > fq_overall , fq_overall , fqout )
34- fqout <- round.to.target (fqout )
34+ fqout <- round_to_target (fqout )
3535 return (fqout )
3636 })
3737 names(freq1 ) <- qualitative
@@ -99,7 +99,7 @@ LEAVcore <- function(data, names,
9999 freq <- lapply(qualitative , function (x ) {
100100 prop <- prop.adj(data [, x ], method = prop.adj )
101101 fqout <- prop * nrow(data )
102- # fqout <- round.to.target (fqout)
102+ # fqout <- round_to_target (fqout)
103103 return (fqout )
104104 })
105105 names(freq ) <- qualitative
@@ -117,7 +117,8 @@ LEAVcore <- function(data, names,
117117
118118 # # Estimate LEAV ----
119119 LEAVdf <- LEAV(data = data , names = names ,
120- quantitative = quantitative , qualitative = qualitative ,
120+ quantitative = quantitative ,
121+ qualitative = qualitative ,
121122 adj = FALSE ,
122123 freq = freq , mean = mean , sd = sd , e = e )
123124
@@ -139,7 +140,7 @@ LEAVcore <- function(data, names,
139140
140141}
141142
142- round.to.target <- function (x , target = round(sum(x ))) {
143+ round_to_target <- function (x , target = round(sum(x ))) {
143144
144145 while (sum(round(x )) - target > 0 ) {
145146 i <- which.min(ifelse(x %% 1 < 0.5 , 1 , x %% 1 ))
@@ -151,3 +152,17 @@ round.to.target <- function(x, target = round(sum(x))) {
151152 }
152153 round(x )
153154}
155+
156+ # Hamilton rounding
157+ # largest remainder method
158+ round_preserve_sum <- function (x , target = round(sum(x ))) {
159+ y <- floor(x )
160+ deficit <- target - sum(y )
161+
162+ if (deficit > 0 ) {
163+ idx <- order(x - y , decreasing = TRUE )[1 : deficit ]
164+ y [idx ] <- y [idx ] + 1
165+ }
166+
167+ y
168+ }
0 commit comments