Skip to content

Commit

Permalink
man and Error corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Fretwurst committed Oct 29, 2024
1 parent c118c36 commit 0344ea2
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 19 deletions.
2 changes: 0 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ Imports:
GGally,
ggplot2,
glue,
kableExtra,
lm.beta,
lubridate,
magrittr,
Expand All @@ -53,7 +52,6 @@ Imports:
tibble,
tidyr,
tidyselect,
flextable,
sjlabelled,
gt
Suggests:
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
36 changes: 31 additions & 5 deletions R/knit_frequencies.R
Original file line number Diff line number Diff line change
@@ -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,
...,
Expand Down Expand Up @@ -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 |>
Expand All @@ -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) |>
Expand All @@ -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) "---"
Expand All @@ -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) "---"
)
}

Expand Down
31 changes: 21 additions & 10 deletions R/knit_regression.R
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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
##
Expand All @@ -151,7 +163,6 @@ knit_regress_table <- function(x,
## @family tdcmm visualize
#
## @keywords internal
#' @export
visualize_regress_sbci <- function(x,
.design = NULL,
title = NULL){
Expand Down
1 change: 1 addition & 0 deletions man/crosstab.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions man/knit_frequencies.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions man/knit_regress_table.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/tab_frequencies.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0344ea2

Please sign in to comment.