|
| 1 | +# 06_workshop_extra.R |
| 2 | + |
| 3 | + |
| 4 | +# EXERCISE 1 - Writing a Function #####################3 |
| 5 | +f = function(Eu, El, sd){ |
| 6 | + cp = (Eu - El) / (6 * sd) |
| 7 | + return(cp) |
| 8 | +} |
| 9 | + |
| 10 | +# roxygen2 style commenting for functions |
| 11 | + |
| 12 | +#' @name f |
| 13 | +#' @title Cp Process Control Index Function |
| 14 | +#' @description |
| 15 | +#' Calculate any Cp statistic with just 3 inputs! Yay! |
| 16 | +#' @param Eu:int Upper Specification Limit |
| 17 | +#' @param El:int Lower Specification Limit |
| 18 | +#' @param sd:int Sigma-short (average within group standard deviation) |
| 19 | +#' @importFrom dplyr `%>%` filter (for example) |
| 20 | +f = function(Eu, El, sd){ |
| 21 | + cp = (Eu - El) / (6 * sd) |
| 22 | + return(cp) |
| 23 | +} |
| 24 | + |
| 25 | + |
| 26 | +f(Eu = 10, El = 2, sd = 5) |
| 27 | + |
| 28 | +# EXERCISE 2 ################################### |
| 29 | +library(dplyr) |
| 30 | +library(readr) |
| 31 | +water = read_csv("workshops/onsen.csv", show_col_types = FALSE) |
| 32 | +water %>% head() %>% glimpse() |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +# EXERCISE 3 ##################################### |
| 40 | + |
| 41 | + |
| 42 | +# What's bootstrapping? |
| 43 | +# Take the existing data |
| 44 | +# Sample with Replacement for the entire data |
| 45 | +# Calculate a Stat |
| 46 | +# Get a stat that has been slightly influenced by random chance |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +library(dplyr) |
| 52 | +library(readr) |
| 53 | +water = read_csv("workshops/onsen.csv", show_col_types = FALSE) |
| 54 | + |
| 55 | +water %>% |
| 56 | + summarize(sigma_t = sd(temp)) |
| 57 | + |
| 58 | + |
| 59 | +# We need 1000 standard deviations |
| 60 | +# We need 1000 resampled water datasets |
| 61 | +# We need 1000 water datasets --- x |
| 62 | +# We need a dataframe of 1000 ids -- x |
| 63 | + |
| 64 | +boot = tibble(rep = 1:1000) %>% |
| 65 | + group_by(rep) %>% |
| 66 | + reframe(water) %>% |
| 67 | + group_by(rep) %>% |
| 68 | + sample_n(size = n(), replace = TRUE) |
| 69 | + |
| 70 | +stat = boot %>% |
| 71 | + group_by(rep) %>% |
| 72 | + summarize(sigma_t = sd(temp)) |
| 73 | + |
| 74 | +stat$sigma_t %>% hist() |
| 75 | + |
| 76 | +stat %>% |
| 77 | + ungroup() %>% |
| 78 | + summarize( |
| 79 | + estimate = mean(sigma_t), |
| 80 | + se = sd(sigma_t), |
| 81 | + upper = estimate + se*qnorm(0.975), |
| 82 | + lower = estimate - se*qnorm(0.975) |
| 83 | + ) |
0 commit comments