Skip to content

Commit b2c37b7

Browse files
committed
Add chapter 3 and several changes
1 parent fa498ef commit b2c37b7

113 files changed

Lines changed: 7762 additions & 6355 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

R/_dev.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@
5353
# --cache-refresh
5454
# quarto render
5555
# quarto render --profile gfm
56-
# quarto render --profile pdf
57-
# quarto render --profile html
58-
# quarto render --profile revealjs
56+
# quarto render --profile pdf # Source pre-render first.
57+
# quarto render --profile html # Source pre-render first.
58+
# quarto render --profile revealjs # Source pre-render first.
5959

6060
# # LaTeX
6161
#

R/_post-render-html.R

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
# library(rutils) # github.com/danielvartan/rutils
44
# library(yaml)
55

6-
# Post-render begin ----------
6+
# Post-render begin -----
77

88
source(here::here("R", "_post-render-begin.R"))
99

10-
# Copy PDF (if exists) to `output_dir_html` folder ----------
10+
# Copy PDF (if exists) to `output_dir_html` folder -----
1111

1212
pdf_file <- list.files(output_dir_pdf, full.names = TRUE, pattern = ".pdf$")
1313

@@ -18,7 +18,7 @@ if (length(pdf_file) == 1) {
1818
)
1919
}
2020

21-
# Create robots.txt file ----------
21+
# Create robots.txt file -----
2222

2323
robots_file <- file.path(output_dir_html, "robots.txt")
2424

@@ -43,6 +43,17 @@ writeLines(
4343
con = robots_file
4444
)
4545

46+
# Copy favicon.png file to the `docs` folder -----
47+
48+
favicon_file <- here::here("images", "favicon.png")
49+
50+
if (prettycheck:::test_file_exists(favicon_file)) {
51+
rutils:::copy_file(
52+
from = favicon_file,
53+
to = file.path(output_docs, "favicon.png")
54+
)
55+
}
56+
4657
# Post-render end ----------
4758

4859
source(here::here("R", "_post-render-end.R"))

R/_pre-render-begin.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ quarto_yml_pdf_vars <- yaml::read_yaml(quarto_yml_pdf_path)
116116
# This function should work with any version of BetterBibTeX (BBT) for Zotero.
117117
# Verify if @wmoldham PR was merged in the `rbbt` package (see issue #47
118118
# <https://github.com/paleolimbot/rbbt/issues/47>). If not, install `rbbt`
119-
# from @wmoldham fork `remotes::install_github("wmoldham/rbbt")`.
119+
# from @wmoldham fork `renv::install("wmoldham/rbbt")`.
120120

121121
if (isTRUE(quarto_yml_pdf_vars$format$`abnt-pdf`$zotero)) {
122122
rutils:::bbt_write_quarto_bib(

R/_pre-render-vars.R

Lines changed: 91 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,133 @@
1+
# Load packages -----
2+
13
# library(dplyr)
24
# library(lubritime)
35
library(magrittr)
46
library(targets)
57

8+
# Load functions -----
9+
610
source(here::here("R", "utils.R"))
711

12+
# Load variables -----
13+
14+
env_vars <- yaml::read_yaml(here::here("_variables.yml"))
15+
res_vars <- yaml::read_yaml(here::here("_results.yml"))
16+
17+
# Load data -----
18+
819
# targets::tar_make(script = here::here("_targets.R"))
920

1021
raw_data <- targets::tar_read(
1122
"raw_data",
1223
store = here::here("_targets")
1324
)
1425

26+
tidy_data <- targets::tar_read(
27+
"tidy_data",
28+
store = here::here("_targets")
29+
)
30+
1531
weighted_data <- targets::tar_read(
1632
"weighted_data",
1733
store = here::here("_targets")
1834
)
1935

20-
# Chapter 6 -----
36+
# Chapter 5 -----
2137

22-
analysis_sample_per_nrow_2017_10_15 <-
38+
pr_analysis_sample_msf_sc_mean <-
2339
weighted_data |>
24-
dplyr::filter(lubridate::date(timestamp) == as.Date("2017-10-15")) |>
40+
dplyr::pull(msf_sc) |>
41+
lubritime:::link_to_timeline(threshold = hms::parse_hms("12:00:00")) |>
42+
mean(na.rm = TRUE) |>
43+
hms::as_hms() |>
44+
lubritime::round_time() |>
45+
as.character()
46+
47+
pr_analysis_sample_msf_sc_sd <-
48+
weighted_data |>
49+
dplyr::pull(msf_sc) |>
50+
lubritime:::link_to_timeline(threshold = hms::parse_hms("12:00:00")) |>
51+
stats::sd(na.rm = TRUE) |>
52+
hms::as_hms() |>
53+
lubritime::round_time() |>
54+
as.character()
55+
56+
pr_tidy_data_per_nrow_2017_10_15_21 <-
57+
tidy_data |>
58+
dplyr::filter(
59+
lubridate::date(timestamp) >= as.Date("2017-10-15"),
60+
lubridate::date(timestamp) <= as.Date("2017-10-21")
61+
) |>
2562
nrow() |>
26-
magrittr::divide_by(weighted_data |> nrow()) |>
63+
magrittr::divide_by(tidy_data |> nrow()) |>
2764
magrittr::multiply_by(100)
2865

29-
# Supplemental Material 1 -----
30-
31-
# Supplemental Material 2 -----
32-
33-
# Supplemental Material 3 -----
34-
35-
# Supplemental Material 4 -----
36-
37-
# Supplemental Material 5 -----
38-
39-
# Supplemental Material 6 -----
66+
data_sex_per <-
67+
weighted_data |>
68+
dplyr::summarise(
69+
n = dplyr::n(),
70+
.by = sex
71+
) |>
72+
dplyr::mutate(n_per = (n / sum(n)) * 100)
73+
74+
pr_weighted_data_male_per <-
75+
data_sex_per |>
76+
dplyr::filter(sex == "Male") |>
77+
dplyr::pull(n_per)
78+
79+
pr_weighted_data_female_per <-
80+
data_sex_per |>
81+
dplyr::filter(sex == "Female") |>
82+
dplyr::pull(n_per)
4083

41-
# Supplemental Material 7 -----
84+
# Chapter 6 -----
4285

43-
# Supplemental Material 8 -----
86+
pr_analysis_sample_per_nrow_2017_10_15 <-
87+
weighted_data |>
88+
dplyr::filter(lubridate::date(timestamp) == as.Date("2017-10-15")) |>
89+
nrow() |>
90+
magrittr::divide_by(weighted_data |> nrow()) |>
91+
magrittr::multiply_by(100)
4492

45-
# Supplemental Material 9 -----
93+
# Others -----
4694

47-
# Supplemental Material 10 -----
95+
if (res_vars$hta_effect_size$f_squared >
96+
res_vars$htb_effect_size$f_squared) {
97+
final_effect_size <- res_vars$hta_effect_size
98+
} else {
99+
final_effect_size <- res_vars$htb_effect_size
100+
}
48101

49102
# Write in `results.yml` -----
50103

51104
write_in_results_yml(
52105
list(
53106
pr_raw_data_nrow = raw_data |> nrow(),
54-
pr_analysis_sample_per_nrow_2017_10_15 = analysis_sample_per_nrow_2017_10_15
107+
pr_analysis_sample_msf_sc_mean = pr_analysis_sample_msf_sc_mean,
108+
pr_analysis_sample_msf_sc_sd = pr_analysis_sample_msf_sc_sd,
109+
pr_tidy_data_per_nrow_2017_10_15_21 = pr_tidy_data_per_nrow_2017_10_15_21,
110+
pr_weighted_data_male_per = pr_weighted_data_male_per,
111+
pr_weighted_data_female_per = pr_weighted_data_female_per,
112+
pr_analysis_sample_per_nrow_2017_10_15 = pr_analysis_sample_per_nrow_2017_10_15,
113+
final_effect_size = final_effect_size
55114
)
56115
)
57116

117+
# Clean environment -----
118+
58119
rm(
59120
raw_data,
60121
weighted_data,
61-
analysis_sample_per_nrow_2017_10_15
122+
pr_analysis_sample_msf_sc_mean,
123+
pr_analysis_sample_msf_sc_sd,
124+
pr_tidy_data_per_nrow_2017_10_15_21,
125+
pr_weighted_data_male_per,
126+
pr_weighted_data_female_per,
127+
pr_analysis_sample_per_nrow_2017_10_15,
128+
final_effect_size
62129
)
63130

64-
results_vars <- yaml::read_yaml(here::here("_results.yml"))
131+
# Reload `result_vars` -----
132+
133+
res_vars <- yaml::read_yaml(here::here("_results.yml"))

R/_render-common.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ if (!prettycheck:::test_file_exists(env_vars_file_path)) {
5050

5151
# Create `_results.yml` if it doesn't exist -----
5252

53-
results_vars_file_path <- here::here("_results.yml")
53+
res_vars_file_path <- here::here("_results.yml")
5454

55-
if (!prettycheck:::test_file_exists(results_vars_file_path)) {
56-
rutils:::create_file(results_vars_file_path)
55+
if (!prettycheck:::test_file_exists(res_vars_file_path)) {
56+
rutils:::create_file(res_vars_file_path)
5757
}
5858

5959
# Run the data pipeline if the `_targets` directory doesn't exist -----

R/_setup.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Based on <https://github.com/hadley/r4ds/blob/main/_common.R>.
22

3-
# Load libraries -----
3+
# Load packages -----
44

55
library(downlit)
66
library(ggplot2)
@@ -41,7 +41,7 @@ options(
4141
set.seed(2025)
4242

4343
env_vars <- yaml::read_yaml(here::here("_variables.yml"))
44-
results_vars <- yaml::read_yaml(here::here("_results.yml"))
44+
res_vars <- yaml::read_yaml(here::here("_results.yml"))
4545

4646
if (env_vars$format == "html") {
4747
base_size <- 11

R/cohens_f_squared.R

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ cohens_f_squared <- function(base_r_squared, new_r_squared = NULL) {
2525
# library(prettycheck) # github.com/danielvartan/prettycheck
2626

2727
cohens_f_squared_effect_size <- function(f_squared) {
28-
prettycheck:::assert_number(f_squared, lower = - 1, upper = 1)
28+
prettycheck:::assert_number(f_squared, lower = 0)
2929

3030
dplyr::case_when(
31-
abs(f_squared) >= 0.35 ~ "Large",
32-
abs(f_squared) >= 0.15 ~ "Medium",
33-
abs(f_squared) >= 0.02 ~ "Small",
31+
f_squared >= 0.35 ~ "Large",
32+
f_squared >= 0.15 ~ "Medium",
33+
f_squared >= 0.02 ~ "Small",
3434
TRUE ~ "Negligible"
3535
)
3636
}
@@ -41,17 +41,42 @@ cohens_f_squared_effect_size <- function(f_squared) {
4141
cohens_f_squared_summary <- function(
4242
base_r_squared,
4343
new_r_squared = NULL
44-
) {
45-
prettycheck:::assert_number(base_r_squared, lower = 0, upper = 1)
46-
prettycheck:::assert_number(
47-
new_r_squared, lower = 0, upper = 1, null.ok = TRUE
48-
)
44+
) {
45+
if (is.atomic(base_r_squared)) {
46+
prettycheck:::assert_number(base_r_squared, lower = 0, upper = 1)
47+
prettycheck:::assert_number(
48+
new_r_squared, lower = 0, upper = 1, null.ok = TRUE
49+
)
4950

50-
f_squared <- cohens_f_squared(base_r_squared, new_r_squared)
51-
category <- cohens_f_squared_effect_size(f_squared)
51+
f_squared <- cohens_f_squared(base_r_squared, new_r_squared)
5252

53-
dplyr::tibble(
54-
name = c("f_squared", "effect_size"),
55-
value = c(f_squared, category)
56-
)
53+
list(
54+
f_squared = f_squared,
55+
effect_size = cohens_f_squared_effect_size(f_squared)
56+
)
57+
} else {
58+
col_check <- c("Rsq", "SErsq", "LCL", "UCL") # psychometric::CI.Rsq()
59+
60+
prettycheck:::assert_data_frame(base_r_squared)
61+
prettycheck:::assert_set_equal(names(base_r_squared), col_check)
62+
prettycheck:::assert_data_frame(new_r_squared)
63+
prettycheck:::assert_set_equal(names(new_r_squared), col_check)
64+
65+
f_values <- c(
66+
cohens_f_squared(base_r_squared$UCL, new_r_squared$UCL),
67+
cohens_f_squared(base_r_squared$UCL, new_r_squared$LCL),
68+
cohens_f_squared(base_r_squared$LCL, new_r_squared$UCL),
69+
cohens_f_squared(base_r_squared$LCL, new_r_squared$LCL)
70+
)
71+
72+
min_f <- ifelse(min(f_values) < 0, 0, min(f_values))
73+
max_f <- ifelse(max(f_values) < 0, 0, max(f_values))
74+
75+
list(
76+
f_squared = cohens_f_squared(base_r_squared$Rsq, new_r_squared$Rsq),
77+
lower_ci_limit = min_f,
78+
upper_ci_limit = max_f,
79+
effect_size = cohens_f_squared_effect_size(min_f)
80+
)
81+
}
5782
}

0 commit comments

Comments
 (0)