You are on page 1of 2

# This file contains R function implementing the Horvitz-Thompson estimator

# described in the paper: Jan Kubacki (2000) Some Small Area Estimation Methods
# for Polish Labour Force Survey in One Region of Poland
# Statistics In Transition, 4, No 5, 769-778
# and bootstrap method partially described in
# Popi�ski, W. (2006): Development of the Polish Labour Force Survey,
# Statistics in Transition, Vol. 7, No. 5, 1009-1030 and
# Rao, J.N.K., Wu, C.F.J. (1988): Resampling Inference with Complex Survey Data,
# Journal of the American Statistical Association, 83, 231-241
# actualization date: 28.11.2017
#
# To Do list
# 1. verification whether target and weights data has numerical vector form
# does not contain missing values, and has the same length
# 2. verification whether auxiliary data has numeric value
# 3. assessment whether appropriate memory for compution is available
# 4. cosideration of more proper seed intial value

Est_HT <- function(target, weight, variance=FALSE, history=FALSE, repli=500,


seed=123)

#
# Function for simple Horvitz-Thompson estimator together with
# simple bootstrap variance estimator based on theory presented in
# Rao, J.N.K., Wu, C.F.J. (1988): Resampling Inference with Complex Survey Data,
# Journal of the American Statistical Association, 83, 231-241
#
# Arguments:
# target - vector of target variable
# weight - vector of survey weights
# variance - logical switch decive whether variance calculations to be determined
# history - logical switch decive whether bootstrap history to be saved
# repli - number of bootstrap replications
# seed - random generator seed
#
# Values:
# estim - values of HT estimator
# precision - values of simple bootstrap estimator (if variance==TRUE)
# history - values of simple bootstrap estimator history (if variance==TRUE &&
history==TRUE)
#

{
Est_HT <- sum(target*weight)
BstWgt <- weight
numCorr <- length(target)/(length(target)-1)
set.seed(seed)
if (variance)
{
target_Bst <- vector('numeric',repli)
for (j in 1:repli) {
rndNum <- as.integer(runif(length(target)-1,1,length(target)+1))
WgtCor <- BstWgt*numCorr
target_Bst[j] <- sum(target[rndNum]*WgtCor[rndNum])
}
Est_Bst <- 0
target_diff <- 0
for (j in 1:repli) {
Est_Bst <- Est_Bst + target_Bst[j]
}
Est_Bst_Avg <- Est_Bst / repli
for (j in 1:repli) {
target_diff <- target_diff + ((target_Bst[j] - Est_Bst_Avg) * (target_Bst[j]
- Est_Bst_Avg))
}
target_var <- target_diff / (repli-1)
}
if (variance) {
if (history) {
return(list(estim = Est_Bst_Avg, precision = target_var, boots_history =
target_Bst))
}
else
{
return(list(estim = Est_Bst_Avg, precision = target_var))
}
}
else
{
return(list(estim = Est_HT))

}
}

You might also like