diff --git a/DESCRIPTION b/DESCRIPTION index 088a0d5..fe2965b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -39,7 +39,6 @@ Imports: GGally, ggplot2, glue, - kableExtra, lm.beta, lubridate, magrittr, @@ -53,7 +52,6 @@ Imports: tibble, tidyr, tidyselect, - flextable, sjlabelled, gt Suggests: diff --git a/NAMESPACE b/NAMESPACE index bf02e1b..43038e3 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -41,7 +41,6 @@ export(test_icr) export(to_correlation_matrix) export(unianova) export(visualize) -export(visualize_regress_sbci) export(z_scale) import(pillar) import(rlang) diff --git a/R/knit_frequencies.R b/R/knit_frequencies.R index a809ec5..1298414 100644 --- a/R/knit_frequencies.R +++ b/R/knit_frequencies.R @@ -1,3 +1,22 @@ +#' knit frequencies table variables +#' +#' Knits table for one ore more variables +#' +#' @param data a [tibble][tibble::tibble-package] +#' @param weight Weighting variable. Default = NULL, means all weights are 1. +#' @param ... List of variables. +#' @param num_decimals Decimal places of the numeric columns. Default is 0. +#' @param percent_decimals Decimal places of the percent columns. Default is 0. +#' +#' @return a gt-table +#' +#' @examples +#' WoJ |> knit_frequencies(reach, employment) +#' mtcars |> knit_frequencies(mpg, num_decimal = 1, percent_decimal = 1, cums = FALSE) +#' +#' @family categorical +#' + #' @export knit_frequencies <- function(data, ..., @@ -25,14 +44,14 @@ knit_frequencies <- function(data, n_valid <- dt %>% dplyr::filter(!is.na(!!var_sym)) %>% dplyr::summarise(n_valid = sum(.temp_weight, na.rm = TRUE)) %>% - pull(n_valid) + dplyr::pull(n_valid) t <- dt %>% - group_by(!!var_sym) %>% + dplyr::group_by(!!var_sym) %>% dplyr::summarise(n = sum(.temp_weight, na.rm = TRUE), .groups = "drop") %>% dplyr::mutate( percent = n / n_total, - valid_percent = if_else(is.na(!!var_sym), 0, n / n_valid)) + valid_percent = dplyr::if_else(is.na(!!var_sym), 0, n / n_valid)) if(cums == TRUE){ t <- t |> @@ -42,7 +61,7 @@ knit_frequencies <- function(data, gt <- t |> sjlabelled::copy_labels(dt) |> - dplyr::mutate(!!var_sym := coalesce(sjlabelled::as_character(!!var_sym), as.character(!!var_sym))) |> + dplyr::mutate(!!var_sym := dplyr::coalesce(sjlabelled::as_character(!!var_sym), as.character(!!var_sym))) |> gt::gt() |> gt::fmt_number(columns = n, decimals = num_decimal, use_seps = FALSE)|> gt::fmt_percent(columns = c(percent, valid_percent), decimals = percent_decimal, use_seps = FALSE) |> @@ -52,7 +71,7 @@ knit_frequencies <- function(data, ) |> gt::text_transform( locations = gt::cells_body( - columns = c(cum_valid_percent, valid_percent), + columns = c(valid_percent), rows = is.na(!!var_sym) ), fn = function(x) "---" @@ -65,6 +84,13 @@ knit_frequencies <- function(data, gt::cols_label( n_cum = "cum n", cum_valid_percent = "cum %" + )|> + gt::text_transform( + locations = gt::cells_body( + columns = c(cum_valid_percent), + rows = is.na(!!var_sym) + ), + fn = function(x) "---" ) } diff --git a/R/knit_regression.R b/R/knit_regression.R index 13bfa54..b150326 100644 --- a/R/knit_regression.R +++ b/R/knit_regression.R @@ -1,12 +1,23 @@ - -# Visualize as html- or pdf-table with linear model --- -# -# @param x a [tdcmm] model -# -# @return html-, pdf- or word-table -# -# @family tdcmm visualize -# +#' Compute linear regression +#' +#' Computes linear regression for all independent variables on the specified +#' dependent variable. Linear modeling of multiple independent variables uses +#' stepwise regression modeling. If specified, preconditions for +#' (multi-)collinearity and for homoscedasticity are checked. +#' +#' @param x a [tdcmm] model +#' @param digits the decimal digits +#' @param CIs show the Confidence intervals or not. Default to TRUE. +#' @param cap Set the caption. Default is NULL, because in Quarto its better to set the tbl-cap in the chunk options +#' +#' @return a gt-table +#' +#' @examples +#' WoJ |> regress(autonomy_selection, ethics_1) |> +#' knit_regress_table() +#' WoJ %>% regress(autonomy_selection, work_experience, trust_government) |> +#' knit_regress_table(digits = 3, CIs = FALSE, cap = "Regression on Autonomy Selection") +#' #' @export knit_regress_table <- function(x, digits = 2, @@ -141,6 +152,7 @@ knit_regress_table <- function(x, return(x) } } + ## Visualize swimming BETA confidence intervals ## Usefull for comparing BETAs and BETAs vs points like 0 ## @@ -151,7 +163,6 @@ knit_regress_table <- function(x, ## @family tdcmm visualize # ## @keywords internal -#' @export visualize_regress_sbci <- function(x, .design = NULL, title = NULL){ diff --git a/man/crosstab.Rd b/man/crosstab.Rd index 96c61b4..d0a3c15 100644 --- a/man/crosstab.Rd +++ b/man/crosstab.Rd @@ -43,6 +43,7 @@ WoJ \%>\% crosstab(reach, employment, add_total = TRUE, percentages = TRUE, chi_ } \seealso{ Other categorical: +\code{\link{knit_frequencies}()}, \code{\link{tab_frequencies}()} } \concept{categorical} diff --git a/man/knit_frequencies.Rd b/man/knit_frequencies.Rd new file mode 100644 index 0000000..e020005 --- /dev/null +++ b/man/knit_frequencies.Rd @@ -0,0 +1,43 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/knit_frequencies.R +\name{knit_frequencies} +\alias{knit_frequencies} +\title{knit frequencies table variables} +\usage{ +knit_frequencies( + data, + ..., + weight = NULL, + num_decimal = 0, + percent_decimal = 0, + cums = TRUE +) +} +\arguments{ +\item{data}{a \link[tibble:tibble-package]{tibble}} + +\item{...}{List of variables.} + +\item{weight}{Weighting variable. Default = NULL, means all weights are 1.} + +\item{num_decimals}{Decimal places of the numeric columns. Default is 0.} + +\item{percent_decimals}{Decimal places of the percent columns. Default is 0.} +} +\value{ +a gt-table +} +\description{ +Knits table for one ore more variables +} +\examples{ +WoJ |> knit_frequencies(reach, employment) +mtcars |> knit_frequencies(mpg, num_decimal = 1, percent_decimal = 1, cums = FALSE) + +} +\seealso{ +Other categorical: +\code{\link{crosstab}()}, +\code{\link{tab_frequencies}()} +} +\concept{categorical} diff --git a/man/knit_regress_table.Rd b/man/knit_regress_table.Rd new file mode 100644 index 0000000..ef8d6b4 --- /dev/null +++ b/man/knit_regress_table.Rd @@ -0,0 +1,33 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/knit_regression.R +\name{knit_regress_table} +\alias{knit_regress_table} +\title{Compute linear regression} +\usage{ +knit_regress_table(x, digits = 2, CIs = TRUE, cap = NULL) +} +\arguments{ +\item{x}{a \link{tdcmm} model} + +\item{digits}{the decimal digits} + +\item{CIs}{show the Confidence intervals or not. Default to TRUE.} + +\item{cap}{Set the caption. Default is NULL, because in Quarto its better to set the tbl-cap in the chunk options} +} +\value{ +a gt-table +} +\description{ +Computes linear regression for all independent variables on the specified +dependent variable. Linear modeling of multiple independent variables uses +stepwise regression modeling. If specified, preconditions for +(multi-)collinearity and for homoscedasticity are checked. +} +\examples{ +WoJ |> regress(autonomy_selection, ethics_1) |> + knit_regress_table() +WoJ \%>\% regress(autonomy_selection, work_experience, trust_government) |> + knit_regress_table(digits = 3, CIs = FALSE, cap = "Regression on Autonomy Selection") + +} diff --git a/man/tab_frequencies.Rd b/man/tab_frequencies.Rd index 38633ff..40f6504 100644 --- a/man/tab_frequencies.Rd +++ b/man/tab_frequencies.Rd @@ -25,6 +25,7 @@ WoJ \%>\% tab_frequencies(employment, country) } \seealso{ Other categorical: -\code{\link{crosstab}()} +\code{\link{crosstab}()}, +\code{\link{knit_frequencies}()} } \concept{categorical}