Вы находитесь на странице: 1из 2

# This file contains R function implementing Empirical Bayes estimation procedure

# described in the paper: Kubacki, J. (2006): Remarks on the Polish LFS


# and Population Census Data for Unemployment Estimation by County,
# Statistics in Transition, March 2006, Vol. 7, No 4, 901-916
# and described in Bracha, Cz., Lednicki, B., Wieczorkowski, R. (2004)
# Wykorzystanie z�o�onych metod estymacji do dezagregacji danych
# z Badania Aktywno�ci Ekonomicznej Ludno�ci w roku 2003, GUS, Warszawa,
# seria "Z prac Zak�adu Bada� Statystyczno-Ekonomicznych", z. 299
# and theory described in
# Rao, J.N.K. (2003): Small Area Estimation,
# Wiley Interscience, Hoboken, New Jersey
# actualization date: 28.11.2017
#
# To Do list
# 1. verification whether source data is sufficient for
# linear regresion model (lm) and does not contain missing values
# 2. assessment whether appropriate memory for compution is available
# 3. consideration of partial assesment whether regression model is
# sufficient with repsect to Gauss-Markov theorem
# as - for example for logistic or qualitative values
# and whether estimation assumptions given in Rao (2003)
# for naive EB estimation model is fullfilled

Est_EB <- function(formula, vardir, data, PRECISION=0.0001)


{

#
# Function for naive EB estimator based on theory presented in
# Rao, J.N.K. (2003): Small Area Estimation,
# Wiley Interscience, Hoboken, New Jersey
#
# Arguments:
# formula - model formula (similar to that descibed in lm R-function)
# vardir - vector of direct estimation error
# data - set of data
# PRECISION - parameter used in solve
#
# Values:
# estim - values of EB estimator
# precision - values of naive EB estimator
#

formuladata <- model.frame(formula, na.action = NULL, data=data)

ydir <- formuladata[,1]

Est_EB <- Est_SEB <- fitted_val <- A <- nomin <- denomin <-
matrix(1,length(ydir),1)

yvar <- vardir

X1 <- model.matrix(formula, data=data)

model <- lm(formula, data)


mod_smry <- summary(model)
sigma_2 <- (mod_smry$sigma)*(mod_smry$sigma)
# fitted_val <- model$fitted.values
coefcnts <- mod_smry$coefficients
r_squared <- mod_smry$r.squared
adj_r_sqr <- mod_smry$adj.r.squared
fstatistic <- mod_smry$fstatistic[1]

InvGram <- solve(t(X1)%*%X1)


for (j in (1:length(ydir))) {
A[j,1] <- t(X1[j,])%*%InvGram%*%X1[j,]
fitted_val[j,1] <- model$fitted.values[j]
denomin[j,1] <- vardir[j]+(sigma_2*A[j,1])
nomin[j,1] <- vardir[j]*(sigma_2*A[j,1])
Est_EB[j,1] <- ((ydir[j]*sigma_2*A[j,1])+
(fitted_val[j,1]*yvar[j]))/denomin[j,1]
Est_SEB[j,1] <- sqrt(nomin[j,1]/denomin[j,1])
}

return(list(estim = Est_EB, precision = Est_SEB))


}

Вам также может понравиться