From 7dcb281f12c382a1525a7552dc22070538005793 Mon Sep 17 00:00:00 2001 From: BERENZ Date: Sat, 8 Mar 2025 23:07:54 +0100 Subject: [PATCH] final version of the summary and print.summary method --- R/prints.R | 23 ++++++++++++---------- R/summary.R | 44 +++++++++++++++++++++++++++++++++++++++--- man/summary.nonprob.Rd | 43 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 96 insertions(+), 14 deletions(-) diff --git a/R/prints.R b/R/prints.R index 09ad099..2b6d0c6 100644 --- a/R/prints.R +++ b/R/prints.R @@ -134,7 +134,7 @@ print.nonprob_summary <- function(x, digits, as.numeric(x$ps_scores_nonprob["Median"]), digits, as.numeric(x$ps_scores_nonprob["Max."]))) - if (!is.null(x$ps_scores_prob)) { + if (!x$no_prob) { cat(" - distribution of IPW probabilities (prob sample):\n") cat(sprintf( " - min: %.*f; mean: %.*f; median: %.*f; max: %.*f\n", @@ -181,16 +181,19 @@ print.nonprob_summary <- function(x, digits, as.numeric(x$ys_nons_pred[[y]]["Max."]))) } - cat(" - distribution of outcome predictions (prob sample):\n") - for (y in 1:length(x$ys_rand_pred)) { - cat(sprintf( - " - %s: min: %.*f; mean: %.*f; median: %.*f; max: %.*f\n", - names(x$ys_rand_pred)[y], - digits, as.numeric(x$ys_rand_pred[[y]]["Min."]), - digits, as.numeric(x$ys_rand_pred[[y]]["Mean"]), - digits, as.numeric(x$ys_rand_pred[[y]]["Median"]), - digits, as.numeric(x$ys_rand_pred[[y]]["Max."]))) + if (!x$no_prob) { + cat(" - distribution of outcome predictions (prob sample):\n") + for (y in 1:length(x$ys_rand_pred)) { + cat(sprintf( + " - %s: min: %.*f; mean: %.*f; median: %.*f; max: %.*f\n", + names(x$ys_rand_pred)[y], + digits, as.numeric(x$ys_rand_pred[[y]]["Min."]), + digits, as.numeric(x$ys_rand_pred[[y]]["Mean"]), + digits, as.numeric(x$ys_rand_pred[[y]]["Median"]), + digits, as.numeric(x$ys_rand_pred[[y]]["Max."]))) + } } + } else { cat(" - distribution of outcome predictions:\n") cat(" too many variables (more than 5). Details are stored in the `ys_nons_pred` and `ys_rand_pred` elements.") diff --git a/R/summary.R b/R/summary.R index 1b9507e..d575a54 100644 --- a/R/summary.R +++ b/R/summary.R @@ -10,9 +10,48 @@ #' #' @return An object of \code{nonprob_summary} class containing: #' \itemize{ -#' \item \code{call} +#' \item {\code{call} call} +#' \item {\code{estimator} type of estimator} +#' \item {\code{control} list of controls} +#' \item {\code{ipw_weights} estimated IPW weights} +#' \item {\code{ipw_weights_total} estimated IPW total (sum)} +#' \item {\code{ps_scores_nonprob} estimated propensity scores for non-probability sample} +#' \item {\code{ps_scores_prob} estimated propensity scores for probability sample} +#' \item {\code{case_weights} case weights} +#' \item {\code{output} estimated means and standard errors} +#' \item {\code{SE} estimated standard errors of V1 and V2} +#' \item {\code{confidence_interval} confidence intervals} +#' \item {\code{nonprob_size} size of the non-probability sample} +#' \item {\code{prob_size} size of the probability sample} +#' \item {\code{pop_size} population size} +#' \item {\code{pop_size_fixed} whether the population size is treated as fixed} +#' \item {\code{no_prob} whether probability sample was provided} +#' \item {\code{outcome} model details} +#' \item {\code{selection} selection details} +#' \item {\code{estimator_method} estimator method} +#' \item {\code{selection_formula} selection formula} +#' \item {\code{outcome_formula} outcome formula} +#' \item {\code{vars_selection} whether variable selection algorithm was applied} +#' \item {\code{vars_outcome} variables of the outcome models} +#' \item {\code{ys_rand_pred} predicted values for the random sample (if applies)} +#' \item {\code{ys_nons_pred} predicted values for the non-probability sample} +#' \item {\code{ys_resid} residuals for the non-probability sample} #' } #' +#' @examples +#' +#' data(admin) +#' data(jvs) +#' +#' jvs_svy <- svydesign(ids = ~ 1, weights = ~ weight, +#' strata = ~ size + nace + region, data = jvs) +#' +#' ipw_est1 <- nonprob(selection = ~ region + private + nace + size, +#' target = ~ single_shift, +#' svydesign = jvs_svy, +#' data = admin, method_selection = "logit" +#' ) +#' summary(ipw_est1) #' #' @method summary nonprob #' @exportS3Method @@ -46,13 +85,12 @@ summary.nonprob <- function(object, ...) { prob_size = object$prob_size, pop_size = object$pop_size, pop_size_fixed = object$pop_size_fixed, + no_prob = is.null(object$svydesign), outcome = object$outcome, selection = object$selection, estimator_method = object$estimator_method, selection_formula = object$selection_formula, outcome_formula = object$outcome_formula, - outcome = object$outcome, - selection = object$selection, vars_selection = names(object$selection$coefficients), vars_outcome = lapply(object$outcome, function(x) names(x$coefficients)), ys_rand_pred = summary_ys_rand_pred, diff --git a/man/summary.nonprob.Rd b/man/summary.nonprob.Rd index 22ab4da..7510b9d 100644 --- a/man/summary.nonprob.Rd +++ b/man/summary.nonprob.Rd @@ -14,10 +14,51 @@ \value{ An object of \code{nonprob_summary} class containing: \itemize{ -\item \code{call} +\item {\code{call} call} +\item {\code{estimator} type of estimator} +\item {\code{control} list of controls} +\item {\code{ipw_weights} estimated IPW weights} +\item {\code{ipw_weights_total} estimated IPW total (sum)} +\item {\code{ps_scores_nonprob} estimated propensity scores for non-probability sample} +\item {\code{ps_scores_prob} estimated propensity scores for probability sample} +\item {\code{case_weights} case weights} +\item {\code{output} estimated means and standard errors} +\item {\code{SE} estimated standard errors of V1 and V2} +\item {\code{confidence_interval} confidence intervals} +\item {\code{nonprob_size} size of the non-probability sample} +\item {\code{prob_size} size of the probability sample} +\item {\code{pop_size} population size} +\item {\code{pop_size_fixed} whether the population size is treated as fixed} +\item {\code{no_prob} whether probability sample was provided} +\item {\code{outcome} model details} +\item {\code{selection} selection details} +\item {\code{estimator_method} estimator method} +\item {\code{selection_formula} selection formula} +\item {\code{outcome_formula} outcome formula} +\item {\code{vars_selection} whether variable selection algorithm was applied} +\item {\code{vars_outcome} variables of the outcome models} +\item {\code{ys_rand_pred} predicted values for the random sample (if applies)} +\item {\code{ys_nons_pred} predicted values for the non-probability sample} +\item {\code{ys_resid} residuals for the non-probability sample} } } \description{ Summarises the \code{nonprob} class object. The summary depends on the type of the estimator (i.e. IPW, MI, DR) } +\examples{ + +data(admin) +data(jvs) + +jvs_svy <- svydesign(ids = ~ 1, weights = ~ weight, +strata = ~ size + nace + region, data = jvs) + +ipw_est1 <- nonprob(selection = ~ region + private + nace + size, +target = ~ single_shift, +svydesign = jvs_svy, +data = admin, method_selection = "logit" +) +summary(ipw_est1) + +}