|
| 1 | +library(tidyverse) |
| 2 | +proto_gen_comp_var_ind <- function(var_type_abb, |
| 3 | + summary_tbl = NULL, # TODO: Should be passed in, no NULLS |
| 4 | + cov_mat = NULL, |
| 5 | + B = NULL, m = NULL, replace = NULL) { |
| 6 | + |
| 7 | + # Define the emoji lookup table. |
| 8 | + # Ensure that output is a {glue} string from consistency |
| 9 | + # TODO: We might delete this, if we are happy with just constructing |
| 10 | + # a glue string directly, as per code below, rather than filtering |
| 11 | + # this tibble. For discussion |
| 12 | + var_emoji_tbl <- |
| 13 | + tibble::tribble( |
| 14 | + ~"var_abb", ~"var_emoji", |
| 15 | + "lm", "\U1F4C9\U1F4C8", |
| 16 | + "sand", "\U1F969\U1F35E", |
| 17 | + "emp", "\U1F9EE\U1F45F", |
| 18 | + "res", "\U2696\U1F45F", |
| 19 | + "mul", "\U274C\U1F45F" |
| 20 | + ) %>% |
| 21 | + dplyr::mutate(dplyr::across(dplyr::everything(), glue::as_glue)) |
| 22 | + |
| 23 | + # Define the Title lookup table. |
| 24 | + # Ensure that output is a {glue} string from consistency |
| 25 | + # TODO: We might delete this, if we are happy with just constructing |
| 26 | + # a glue string directly, as per code below, rather than filtering |
| 27 | + # this tibble. For discussion |
| 28 | + var_title_tbl <- |
| 29 | + tibble::tribble( |
| 30 | + ~"var_abb", ~"var_title", |
| 31 | + "lm", "Well Specified Model", |
| 32 | + "sand", "Sandwich", |
| 33 | + "emp", "Empirical Bootstrap", |
| 34 | + "res", "Residual Bootstrap", |
| 35 | + "mul", "Multiplier Bootstrap" |
| 36 | + ) %>% |
| 37 | + dplyr::mutate(dplyr::across(dplyr::everything(), glue::as_glue)) |
| 38 | + |
| 39 | + # Define the Emoji Title lookup table. |
| 40 | + # Ensure that output is a {glue} string from consistency |
| 41 | + # TODO: We might delete this, if we are happy with just constructing |
| 42 | + # a glue string directly, as per code below, rather than filtering |
| 43 | + # this tibble. For discussion |
| 44 | + var_emoji_title_tbl <- var_emoji_tbl %>% |
| 45 | + dplyr::left_join(x = ., y = var_title_tbl, by = "var_abb") %>% |
| 46 | + dplyr::mutate( |
| 47 | + .data = ., |
| 48 | + var_emoji_title = glue::glue("{var_emoji}: {var_title}:") |
| 49 | + ) |
| 50 | + |
| 51 | + # Define the Emoji Title lookup table. Second version |
| 52 | + # This one only requires the var_emoji_tbl to be created |
| 53 | + # Ensure that output is a {glue} string from consistency |
| 54 | + # TODO: We might delete this, if we are happy with just constructing |
| 55 | + # a glue string directly, as per code below, rather than filtering |
| 56 | + # this tibble. For discussion |
| 57 | + var_emoji_title_tbl2 <- |
| 58 | + tibble::tribble( |
| 59 | + ~"var_abb", ~"var_emoji", |
| 60 | + "lm", "\U1F4C9\U1F4C8", |
| 61 | + "sand", "\U1F969\U1F35E", |
| 62 | + "emp", "\U1F9EE\U1F45F", |
| 63 | + "res", "\U2696\U1F45F", |
| 64 | + "mul", "\U274C\U1F45F" |
| 65 | + ) %>% |
| 66 | + dplyr::mutate( |
| 67 | + .data = ., |
| 68 | + var_title = dplyr::case_when( |
| 69 | + var_abb == "lm" ~ "Well Specified Model", |
| 70 | + var_abb == "sand" ~ "Sandwich", |
| 71 | + var_abb == "emp" ~ "Empirical Bootstrap", |
| 72 | + var_abb == "res" ~ "Residual Bootstrap", |
| 73 | + var_abb == "mul" ~ "Multiplier Bootstrap" |
| 74 | + ), |
| 75 | + var_emoji_title = glue::glue("{var_emoji}: {var_title}:") |
| 76 | + ) %>% |
| 77 | + dplyr::mutate(dplyr::across(dplyr::everything(), glue::as_glue)) |
| 78 | + |
| 79 | + # Get the specific type of individual variance variable |
| 80 | + out <- switch(var_type_abb, |
| 81 | + "lm" = { |
| 82 | + var_emoji <- glue::glue("\U1F4C9\U1F4C8") |
| 83 | + var_title <- glue::glue("Well Specified Model") |
| 84 | + out_list <- list( |
| 85 | + "var_type" = glue::glue("well_specified"), |
| 86 | + "var_type_abb" = glue::glue("{var_type_abb}"), |
| 87 | + var_emoji = var_emoji, |
| 88 | + var_title = var_title, |
| 89 | + "var_emoji_title" = glue::glue("{var_emoji}: {var_title}:"), |
| 90 | + "var_summary" = summary_tbl, |
| 91 | + "var_assumptions" = tibble::tribble( |
| 92 | + ~"var_abb", ~"var_assumption_type", ~"var_assumption_val", |
| 93 | + glue::glue("{var_type_abb}"), "text", "Observations are assumed to be independent", |
| 94 | + glue::glue("{var_type_abb}"), "text", "Residuals are assumed to be homoscedastic", |
| 95 | + glue::glue("{var_type_abb}"), "text", "Linearity of the conditional expectation is assumed" |
| 96 | + ) %>% |
| 97 | + dplyr::mutate(dplyr::across(dplyr::everything(), glue::as_glue)), |
| 98 | + "cov_mat" = cov_mat |
| 99 | + ) |
| 100 | + }, |
| 101 | + "sand" = { |
| 102 | + var_emoji <- glue::glue("\U1F969\U1F35E") |
| 103 | + var_title <- glue::glue("Sandwich") |
| 104 | + out_list <- list( |
| 105 | + "var_type" = glue::glue("sand"), |
| 106 | + "var_type_abb" = glue::glue("{var_type_abb}"), |
| 107 | + var_emoji = var_emoji, |
| 108 | + var_title = var_title, |
| 109 | + "var_emoji_title" = glue::glue("{var_emoji}: {var_title}:"), |
| 110 | + "var_summary" = summary_tbl, |
| 111 | + "var_assumptions" = tibble::tribble( |
| 112 | + ~"var_abb", ~"var_assumption_type", ~"var_assumption_val", |
| 113 | + glue::glue("{var_type_abb}"), "text", "Observations are assumed to be independent" |
| 114 | + ) %>% |
| 115 | + dplyr::mutate(dplyr::across(dplyr::everything(), glue::as_glue)), |
| 116 | + "cov_mat" = cov_mat |
| 117 | + ) |
| 118 | + }, |
| 119 | + "emp" = { |
| 120 | + var_emoji <- glue::glue("\U1F9EE\U1F45F") |
| 121 | + var_title <- glue::glue("Empirical Bootstrap") |
| 122 | + out_list <- list( |
| 123 | + "var_type" = glue::glue("boot_emp"), |
| 124 | + "var_type_abb" = glue::glue("{var_type_abb}"), |
| 125 | + var_emoji = var_emoji, |
| 126 | + var_title = var_title, |
| 127 | + "var_emoji_title" = glue::glue("{var_emoji}: {var_title}:"), |
| 128 | + "var_summary" = summary_tbl, |
| 129 | + "var_assumptions" = tibble::tribble( |
| 130 | + ~"var_abb", ~"var_assumption_type", ~"var_assumption_val", |
| 131 | + glue::glue("{var_type_abb}"), "text", "Observations are assumed to be independent", |
| 132 | + glue::glue("{var_type_abb}"), "text", "Linearity of the conditional expectation is assumed", |
| 133 | + glue::glue("{var_type_abb}"), "B", glue::glue("{B}"), |
| 134 | + glue::glue("{var_type_abb}"), "m", glue::glue("{m}"), |
| 135 | + glue::glue("{var_type_abb}"), "replace", glue::glue("{replace}") |
| 136 | + ) %>% |
| 137 | + dplyr::mutate(dplyr::across(dplyr::everything(), glue::as_glue)), |
| 138 | + "cov_mat" = cov_mat |
| 139 | + ) |
| 140 | + }, |
| 141 | + "res" = { |
| 142 | + var_emoji <- glue::glue("\U2696\U1F45F") |
| 143 | + var_title <- glue::glue("Residual Bootstrap") |
| 144 | + out_list <- list( |
| 145 | + "var_type" = glue::glue("boot_res"), |
| 146 | + "var_type_abb" = glue::glue("{var_type_abb}"), |
| 147 | + var_emoji = var_emoji, |
| 148 | + var_title = var_title, |
| 149 | + # "var_emoji" = dplyr::filter(.data = var_emoji_tbl, |
| 150 | + # var_abb == var_type_abb), |
| 151 | + # "var_title" = dplyr::filter(.data = var_title_tbl, |
| 152 | + # var_abb == var_type_abb), |
| 153 | + "var_emoji_title" = glue::glue("{var_emoji}: {var_title}:"), |
| 154 | + "var_summary" = summary_tbl, |
| 155 | + "var_assumptions" = tibble::tribble( |
| 156 | + ~"var_abb", ~"var_assumption_type", ~"var_assumption_val", |
| 157 | + glue::glue("{var_type_abb}"), "text", "Observations are assumed to be independent", |
| 158 | + glue::glue("{var_type_abb}"), "text", "Residuals are assumed to be homoscedastic", |
| 159 | + glue::glue("{var_type_abb}"), "text", "Linearity of the conditional expectation is assumed" |
| 160 | + ) %>% |
| 161 | + dplyr::mutate(dplyr::across(dplyr::everything(), glue::as_glue)), |
| 162 | + "cov_mat" = cov_mat |
| 163 | + ) |
| 164 | + }, |
| 165 | + "mul" = { |
| 166 | + var_emoji <- glue::glue("\U274C\U1F45F") |
| 167 | + var_title <- glue::glue("Multiplier Bootstrap") |
| 168 | + out_list <- list( |
| 169 | + "var_type" = glue::glue("boot_mul"), |
| 170 | + "var_type_abb" = glue::glue("{var_type_abb}"), |
| 171 | + var_emoji = var_emoji, |
| 172 | + var_title = var_title, |
| 173 | + "var_emoji_title" = glue::glue("{var_emoji}: {var_title}:"), |
| 174 | + "var_summary" = summary_tbl, |
| 175 | + "var_assumptions" = tibble::tribble( |
| 176 | + ~"var_abb", ~"var_assumption_type", ~"var_assumption_val", |
| 177 | + glue::glue("{var_type_abb}"), "text", "Observations are assumed to be independent", |
| 178 | + glue::glue("{var_type_abb}"), "text", "Linearity of the conditional expectation is assumed", |
| 179 | + glue::glue("{var_type_abb}"), "B", glue::glue("{B}") |
| 180 | + ) %>% |
| 181 | + dplyr::mutate(dplyr::across(dplyr::everything(), glue::as_glue)), |
| 182 | + "cov_mat" = cov_mat |
| 183 | + ) |
| 184 | + } |
| 185 | + ) |
| 186 | + return(out) |
| 187 | +} |
| 188 | + |
| 189 | + |
| 190 | +# Variables as they would be passed in from individual functions |
| 191 | +B <- 150 |
| 192 | +m <- 60 |
| 193 | +replace <- TRUE |
| 194 | +cov_mat <- diag(3) |
| 195 | + |
| 196 | +# Run the get_assumptions |
| 197 | +proto_gen_comp_var_ind( |
| 198 | + var_type_abb = "res", |
| 199 | + summary_tbl = NULL, # TODO: Should be passed in, no NULLS |
| 200 | + cov_mat = cov_mat, |
| 201 | + B = B, |
| 202 | + m = m, |
| 203 | + replace = replace |
| 204 | +) |
0 commit comments