Skip to content
Snippets Groups Projects
Commit a567587e authored by gdutz's avatar gdutz
Browse files

Add leo_regresult()

parent e4198f28
No related branches found
No related tags found
No related merge requests found
Package: leo Package: leo
Type: Package Type: Package
Title: Useful functions for LEO surveys Title: Useful functions for LEO surveys
Version: 0.2.6 Version: 0.2.7
Author: Gregor Dutz Author: Gregor Dutz
Maintainer: Gregor Dutz <gregor.dutz@uni-hamburg.de> Maintainer: Gregor Dutz <gregor.dutz@uni-hamburg.de>
Description: Implements simple functions to handle data from two German surveys Description: Implements simple functions to handle data from two German surveys
...@@ -15,9 +15,11 @@ RoxygenNote: 7.1.1 ...@@ -15,9 +15,11 @@ RoxygenNote: 7.1.1
Imports: Imports:
dplyr, dplyr,
labelled, labelled,
mitml,
magrittr, magrittr,
mitools, mitools,
purrr, purrr,
rlang, rlang,
survey, survey,
tidyselect tidyselect,
tibble
# Generated by roxygen2: do not edit by hand # Generated by roxygen2: do not edit by hand
export(leo_regresult)
export(leo_svydesign) export(leo_svydesign)
importFrom(rlang,.data) importFrom(rlang,.data)
#' 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
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment