diff --git a/DESCRIPTION b/DESCRIPTION index 0131ecb693016b9524f511de2181ce73c0f69efd..a1a17178a0df44ad807f35e6393c2a68888ea52f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: leo Type: Package Title: Useful functions for LEO surveys -Version: 0.2.6 +Version: 0.2.7 Author: Gregor Dutz Maintainer: Gregor Dutz <gregor.dutz@uni-hamburg.de> Description: Implements simple functions to handle data from two German surveys @@ -15,9 +15,11 @@ RoxygenNote: 7.1.1 Imports: dplyr, labelled, + mitml, magrittr, mitools, purrr, rlang, survey, - tidyselect + tidyselect, + tibble diff --git a/NAMESPACE b/NAMESPACE index 1f0c739d6ab34956b1f695568760e50f776fc567..cf67032006fdf4ce0373c6607e2960415c1294a3 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,4 +1,5 @@ # Generated by roxygen2: do not edit by hand +export(leo_regresult) export(leo_svydesign) importFrom(rlang,.data) diff --git a/R/leo_regresult.R b/R/leo_regresult.R new file mode 100644 index 0000000000000000000000000000000000000000..c34e1127b7632e7ee2547e9f5d0fbc1a5e95ca06 --- /dev/null +++ b/R/leo_regresult.R @@ -0,0 +1,44 @@ +#' Creates Tiny Data from regression results from svyglm/mitools. +#' +#' @param model A list of model estimates (fitted with with.imputationList). +#' @param conf.int A logical flag indicating if the confidence intervall should be calculated. Default is `FALSE`. +#' @param conf.level The confidence level. Default is `0.95`. +#' @param exponentiate A logical flag indicating if coefficients should be exponentiated (only useful for logistic regression). Default is `FALSE`. +#' @return A tibble with regression results as Tiny Data. +#' +#' @importFrom rlang .data +#' @export +leo_regresult <- function(model, conf.int = FALSE, conf.level = 0.95, exponentiate = FALSE) { + + ret <- tibble::as_tibble(mitml::testEstimates(model)$estimates, rownames = "term") + ret <- dplyr::select(ret, -.data$df, -.data$RIV, -.data$FMI) + colnames(ret) <- c("term", "estimate", "std.error", "statistic", "p.value") + + if (conf.int) { + ci <- tibble::as_tibble(mitml::confint.mitml.testEstimates(mitml::testEstimates(model, level = conf.level)), rownames = "term") + colnames(ci) <- c("term", "conf.low", "conf.high") + ret <- dplyr::left_join(ret, ci, by = "term") + } + + if (exponentiate) + ret <- leo_exponentiate(ret) + + ret +} + +#' Exponentiates regression results from logistic regressions +#' +#' @param model A list of model estimates as Tiny Data. +#' @return A tibble with expontiated regression results as Tiny Data. +#' +#' @importFrom rlang .data +#' @keywords internal +leo_exponentiate <- function(model) { + + model <- model |> dplyr::mutate(estimate = exp(.data$estimate)) # exponentiate coefficients + + if ("conf.low" %in% colnames(model)) # exponentiate confidence interval (if present) + model <- model |> dplyr::mutate(dplyr::across(c(.data$conf.low, .data$conf.high), exp)) + + model +}