#Program 6
#
# This performs the efficient apportionment of sample sizes when converting an approximate design
with design weights in deswts to an exact design with N observations.
#See Pukelsheim (1993, Chapter 12).
#For those values of N for which there is more than one efficient apportionment, this program
#selects one at random. To see more than one such apportionment, run the program several times.
#The required input consists of the value of N and the value of deswts (the s design weights).
N <- 18
deswts <- c(0.246,0.301,0.109,0.125,0.219)
s <- length(deswts)
svec <- 1:s
nu <- N - s/2
apportion <- nu*deswts
apportion <- ceiling(apportion)
indic1 <- 0
while(sum(apportion) != N)
{
if(sum(apportion) < N) {ratio <- apportion/deswts
minratio <- min(ratio)
indic2 <- ratio == minratio
if (sum(indic2) > 1) indic1 <- 1
points <- svec[indic2]
change <- sample(points,1)
apportion[change] <- apportion[change] + 1}
else {ratio <- (apportion - 1)/deswts
maxratio <- max(ratio)
indic2 <- ratio == maxratio
if (sum(indic2) > 1) indic1 <- 1
points <- svec[indic2]
change <- sample(points,1)
apportion[change] <- apportion[change] - 1}
}
if(indic1 > 0) cat("There is more than one efficient apportionment \n")
apportion