Skip to content

Commit

Permalink
Add analysis pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
danielvartan committed Nov 26, 2024
1 parent 6045691 commit c2dd103
Show file tree
Hide file tree
Showing 48 changed files with 9,638 additions and 1,315 deletions.
2 changes: 1 addition & 1 deletion R/filter_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ filter_data <- function(data) {
!test_outlier(age, method = "iqr", iqr_mult = 1.5),
!test_outlier(transform_time(msf_sc), method = "iqr", iqr_mult = 1.5)
) |>
tidyr::drop_na(sex, age, state, latitude, longitude)
tidyr::drop_na(msf_sc, sex, age, state, latitude, longitude)
}
22 changes: 17 additions & 5 deletions R/normality_summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,24 @@

source(here::here("R/test_normality.R"))

normality_summary <- function(x, round = FALSE, digits = 5, ...) {
prettycheck:::assert_numeric(x)
normality_summary <- function(
data,
col,
round = FALSE,
digits = 5,
...
) {
prettycheck:::assert_tibble(data)
prettycheck:::assert_choice(col, names(data))
prettycheck:::assert_numeric(data[[col]])
prettycheck:::assert_flag(round)
prettycheck:::assert_number(digits)

stats <- test_normality(x, print = FALSE, ...)
x <- data |> dplyr::pull(col)

stats <-
data |>
test_normality(col = col, print = FALSE, ...)

out <- dplyr::tibble(
test = c(
Expand Down Expand Up @@ -64,8 +76,8 @@ normality_summary <- function(x, round = FALSE, digits = 5, ...) {
ifelse(is.null(stats$shapiro), NA, stats$shapiro$p.value),
ifelse(is.null(stats$sf), NA, stats$sf$p.value)
)
) |>
dplyr::select(test, p_value)
)
# dplyr::select(test, p_value)

if (isTRUE(round)) {
out |>
Expand Down
139 changes: 139 additions & 0 deletions R/panel_tabset_coef_dfbetas.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# library(dplyr)
# library(ggplot2)
# library(glue)
# library(here)
# library(latex2exp)
library(magrittr)
# library(parsnip)
# library(prettycheck) # github.com/danielvartan/prettycheck
# library(readr)
# library(rutils) # github.com/danielvartan/rutils
# library(stats)
# library(utils)

source(here::here("R", "utils.R"))

panel_tabset_coef_dfbetas <- function(
fit_engine,
coef = names(stats::coef(fit_engine)),
coef_labels = coef,
source = rep("Created by the author.", length(coef)),
heading = "###",
fit_engine_name = "fit_engine",
suffix = ""
) {
prettycheck:::assert_class(fit_engine, "lm")

prettycheck:::assert_character(
coef,
len = length(stats::coef(fit_engine)),
any.missing = FALSE
)

prettycheck:::assert_subset(coef, names(stats::coef(fit_engine)))
prettycheck:::assert_character(coef_labels, len = length(coef))
prettycheck:::assert_character(source, len = length(coef))
prettycheck:::assert_string(heading, pattern = "^#*")
prettycheck:::assert_string(fit_engine_name)
prettycheck:::assert_string(suffix)

if (!file.exists(here::here("qmd"))) dir.create(here::here("qmd"))

file <- here::here(
"qmd",
glue::glue("_panel-tabset-coef-dfbetas-{suffix}.qmd")
)

libraries <-
c("ggplot2", "olsrr", "stats") |>
sort() %>%
paste0("library(", ., ")", collapse = "\n")

scripts <-
c("utils.R") |>
sort() %>%
paste0("source(here::here('R', '", ., "'))", collapse = "\n")

out <- glue::glue(
"
:::: {{.panel-tabset}}
```{{r}}
#| code-fold: true
plots <- {fit_engine_name} |> olsrr::ols_plot_dfbetas(print_plot = FALSE)
coef_names <- stats::coef({fit_engine_name}) |> names()
```
\n
"
)

coef_labels <- coef_labels |> stringr::str_remove_all("\\(|\\)")

# Source: {source[i]}

for (i in seq_along(coef)) {
if (i == length(coef)) {
end <- ""
} else {
end <- "\n\n"
}

coef_fix_1 <-
coef[i] |>
stringr::str_to_lower() |>
stringr::str_remove_all("\\(|\\)") |>
stringr::str_replace_all("_", "-")

coef_fix_2 <-
coef[i] |>
stringr::str_remove_all("\\(|\\)")

out <- c(
out,
glue::glue(
"
{heading} {coef_labels[i]}
::: {{#tbl-{suffix}-diag-influence-{coef_fix_1}}}
```{{r}}
#| code-fold: true
plots$plots[[{i}]] +
ggplot2::labs(title = '{coef_labels[i]} coefficient')
```
Standardized DFBETAS values for each observation concerning the
`{coef_fix_2}` coefficient.
:::{end}
"
)
)
}

out <-
c(out, "\n::::") |>
paste0(collapse = "") |>
readr::write_lines(file)

include_string <- glue::glue(
"{{{{< include {to_relative_path(file)} >}}}}"
)

cli::cli_alert_info(glue:::glue(
"Use `{{include_string}}` to include ",
"the panel in the file (Copied to clipboard).",
"\n\n",
"Also, don't forget call the libraries and to source the scripts ",
"below in the file.",
"\n\n",
libraries,
"\n\n",
scripts,
wrap = TRUE
))

utils::writeClipboard(include_string)

invisible()
}
106 changes: 0 additions & 106 deletions R/panel_tabset_var_dist.R

This file was deleted.

Loading

0 comments on commit c2dd103

Please sign in to comment.