Nothing
#' Linearization of the total below a quantile
#'
#' Computes the linearized variable of the total in the lower tail of
#' the distribution of a variable.
#'
#' @param formula a formula specifying the income variable
#' @param design a design object of class \code{survey.design} or class \code{svyrep.design} from the \code{survey} library.
#' @param alpha the order of the quantile
#' @param quantile return the upper bound of the lower tail
#' @param na.rm Should cases with missing values be dropped?
#' @param ... arguments passed on to `survey::oldsvyquantile`
#'
#' @return Object of class "\code{cvystat}", which are vectors with a "\code{var}" attribute giving the variance and a "\code{statistic}" attribute giving the name of the statistic.
#'
#' @details you must run the \code{convey_prep} function on your survey design object immediately after creating it with the \code{svydesign} or \code{svrepdesign} function.
#'
#' @author Djalma Pessoa and Anthony Damico
#'
#' @seealso \code{\link{svyarpr}}
#'
#' @references Guillaume Osier (2009). Variance estimation for complex indicators
#' of poverty and inequality. \emph{Journal of the European Survey Research
#' Association}, Vol.3, No.3, pp. 167-195,
#' ISSN 1864-3361, URL \url{https://ojs.ub.uni-konstanz.de/srm/article/view/369}.
#'
#' Jean-Claude Deville (1999). Variance estimation for complex statistics and estimators:
#' linearization and residual techniques. Survey Methodology, 25, 193-203,
#' URL \url{https://www150.statcan.gc.ca/n1/en/catalogue/12-001-X19990024882}.
#'
#' @keywords survey
#'
#' @examples
#' library(laeken)
#' data(eusilc) ; names( eusilc ) <- tolower( names( eusilc ) )
#' library(survey)
#' des_eusilc <- svydesign(ids = ~rb030, strata =~db040, weights = ~rb050, data = eusilc)
#' des_eusilc <- convey_prep(des_eusilc)
#' svyisq(~eqincome, design=des_eusilc,.20 , quantile = TRUE)
#'
#' # replicate-weighted design
#' des_eusilc_rep <- as.svrepdesign( des_eusilc , type = "bootstrap" )
#' des_eusilc_rep <- convey_prep(des_eusilc_rep)
#'
#' svyisq( ~eqincome , design = des_eusilc_rep, .20 , quantile = TRUE )
#'
#' \dontrun{
#'
#' # linearized design using a variable with missings
#' svyisq( ~ py010n , design = des_eusilc, .20 )
#' svyisq( ~ py010n , design = des_eusilc , .20, na.rm = TRUE )
#' # replicate-weighted design using a variable with missings
#' svyisq( ~ py010n , design = des_eusilc_rep, .20 )
#' svyisq( ~ py010n , design = des_eusilc_rep , .20, na.rm = TRUE )
#'
#' # database-backed design
#' library(RSQLite)
#' library(DBI)
#' dbfile <- tempfile()
#' conn <- dbConnect( RSQLite::SQLite() , dbfile )
#' dbWriteTable( conn , 'eusilc' , eusilc )
#'
#' dbd_eusilc <-
#' svydesign(
#' ids = ~rb030 ,
#' strata = ~db040 ,
#' weights = ~rb050 ,
#' data="eusilc",
#' dbname=dbfile,
#' dbtype="SQLite"
#' )
#'
#' dbd_eusilc <- convey_prep( dbd_eusilc )
#'
#' svyisq( ~ eqincome , design = dbd_eusilc, .20 )
#'
#' dbRemoveTable( conn , 'eusilc' )
#'
#' dbDisconnect( conn , shutdown = TRUE )
#'
#' }
#'
#' @export
svyisq <-
function(formula, design, ...) {
if( length( attr( terms.formula( formula ) , "term.labels" ) ) > 1 ) stop( "convey package functions currently only support one variable in the `formula=` argument" )
UseMethod("svyisq", design)
}
#' @rdname svyisq
#' @export
svyisq.survey.design <-
function(formula, design, alpha, quantile = FALSE, na.rm = FALSE,...) {
if (is.null(attr(design, "full_design"))) stop("you must run the ?convey_prep function on your linearized survey design object immediately after creating it with the svydesign() function.")
incvar <- model.frame(formula, design$variables, na.action = na.pass)[[1]]
if(na.rm){
nas<-is.na(incvar)
design<-design[!nas,]
if (length(nas) > length(design$prob)) incvar <- incvar[!nas] else incvar[nas] <- 0
}
ind <- names(design$prob)
w <- 1/design$prob
N <- sum(w)
h <- h_fun(incvar, w)
q_alpha <- survey::oldsvyquantile(x = formula, design = design, quantiles = alpha, method = "constant", na.rm = na.rm,...)
q_alpha <- as.vector(q_alpha)
Fprime0 <- densfun(formula = formula, design = design, q_alpha, h=h, FUN = "F", na.rm=na.rm)
Fprime1 <- densfun(formula = formula, design = design, q_alpha, FUN = "big_s", na.rm = na.rm)
rval <- sum((incvar<=q_alpha)*incvar * w)
iq <- -( 1 / ( N * Fprime0 ) ) * ( ( incvar <= q_alpha ) - alpha )
isqalpha1 <- incvar * (incvar <= q_alpha)
isqalpha <- isqalpha1 + Fprime1 * iq
variance <- survey::svyrecvar(isqalpha/design$prob, design$cluster, design$strata, design$fpc, postStrata = design$postStrata)
colnames( variance ) <- rownames( variance ) <- names( rval ) <- strsplit( as.character( formula )[[2]] , ' \\+ ' )[[1]]
class(rval) <- c( "cvystat" , "svystat" )
attr(rval, "var") <- variance
attr(rval, "statistic") <- "isq"
attr(rval, "lin") <- isqalpha
if(quantile) attr(rval, "quantile") <- q_alpha
rval
}
#' @rdname svyisq
#' @export
svyisq.svyrep.design <-
function(formula, design, alpha,quantile = FALSE, na.rm = FALSE,...){
if (is.null(attr(design, "full_design"))) stop("you must run the ?convey_prep function on your replicate-weighted survey design object immediately after creating it with the svrepdesign() function.")
incvar <- model.frame(formula, design$variables, na.action = na.pass)[[1]]
if(na.rm){
nas<-is.na(incvar)
design<-design[!nas,]
if (length(nas) > length(design$prob)) incvar <- incvar[!nas] else incvar[nas] <- 0
}
compute_isq <-
function(x, w, alpha){
q_alpha <- computeQuantiles( x , w , alpha )
c( q_alpha , sum( x * ( x <= q_alpha ) * w ) )
}
rval_isq <- compute_isq(incvar, alpha = alpha, w = weights(design, "sampling"))
rval <- rval_isq[2]
ww <- weights(design, "analysis")
qq <- apply(ww, 2, function(wi) compute_isq(incvar, wi, alpha = alpha)[2])
if(anyNA(qq))variance <- NA
else variance <- survey::svrVar(qq, design$scale, design$rscales, mse = design$mse, coef = rval)
variance <- as.matrix( variance )
colnames( variance ) <- rownames( variance ) <- names( rval ) <- strsplit( as.character( formula )[[2]] , ' \\+ ' )[[1]]
class(rval) <- c( "cvystat" , "svrepstat" )
attr(rval, "var") <- variance
attr(rval, "statistic") <- "isq"
attr(rval, "lin") <- NA
if(quantile)attr(rval, "quantile") <- rval_isq[1]
rval
}
#' @rdname svyisq
#' @export
svyisq.DBIsvydesign <-
function (formula, design, ...){
if (!( "logical" %in% class(attr(design, "full_design"))) ){
full_design <- attr( design , "full_design" )
full_design$variables <-
getvars(
formula,
attr( design , "full_design" )$db$connection,
attr( design , "full_design" )$db$tablename,
updates = attr( design , "full_design" )$updates,
subset = attr( design , "full_design" )$subset
)
attr( design , "full_design" ) <- full_design
rm( full_design )
}
design$variables <-
getvars(
formula,
design$db$connection,
design$db$tablename,
updates = design$updates,
subset = design$subset
)
NextMethod("svyisq", design)
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.