R/optiallo.R

Defines functions optiallo

Documented in optiallo

optiallo <-
function(n,x,stratum=NULL,...) {
  if (is.null(stratum)) {stratum<- stratify(x,...)}
  SxUh   <- as.vector(by(x,stratum,sd))
  SxUh[is.na(SxUh)]  <- sum(SxUh,na.rm=TRUE)
  if (min(SxUh)==0&max(SxUh)==0) {SxUh<- SxUh+1}
  Nh     <- as.vector(by(x,stratum,length))
  nh     <- n*Nh*SxUh/sum(Nh*SxUh)
  indice <- (1:length(nh))
  SxUh2  <- SxUh
  
  while (sum(nh>Nh)>0) {
    indice<- ifelse(nh<=Nh,indice,NA)
    SxUh2[is.na(indice)]<- NA
    Nhpro <- Nh[is.na(indice)]
    ncor  <- n-sum(Nhpro)
    nhcor <- ncor*Nh*SxUh2/sum(Nh*SxUh2,na.rm=TRUE)
    nh    <- ifelse(is.na(nhcor),Nh,nhcor)
  }
  stratum2<- names(table(stratum))
  nh<- nh[match(stratum,stratum2)]
  list(stratum=stratum,nh=nh)
}

Try the optimStrat package in your browser

Any scripts or data that you put into this service are public.

optimStrat documentation built on Aug. 24, 2023, 9:09 a.m.