\[ meter~development = \frac{chainring}{cog} \times radius~ratio \] \[ radius~ratio = \frac{wheel~radius}{crank~radius} \]
library(dplyr)
library(rstanarm)
library(ggplot2)
library(bayesplot)
library(tidybayes)
library(broom)
library(knitr)
Here is an example of a set of cogs.
cogs <- c(11, 13, 15, 18, 21, 24, 28, 32, 36, 40, 46)
Here is an example of a front chainring with the popular two up front.
chain_ring <- 40
crank_length_rad_mm <- 160
Here is an example of popular wheel sizes.
wheel_dia_mm <- 685.60
wheel_rad_mm <- wheel_dia_mm/2
bike_function <- function(x, named.df = "df"){
# conversions
# 1 mm = .0394 inches
mm_to_in <- .0393701
# 1 m = 1.09361 yards
m_to_yrd <- 1.09361
# 1,760 yars in a mile
yrd_in_mile <- 1760
miles = x
df <- tibble(chain_ring, cogs) %>%
mutate(
meter_dev = (chain_ring / cogs) *
(wheel_rad_mm / crank_length_rad_mm), # radius ratio
yrd_dev = meter_dev*m_to_yrd, # conver from meters to yards
# revolutions it takes to get x miles for each cog
revolutions = (yrd_in_mile * miles / yrd_dev),
possible_steps = revolutions * 2, # get possible step count for each cog
centered_cogs = cogs - 24) # we use this centered variable in a model
assign(named.df, df, envir=.GlobalEnv)
}
bike_function(50)
kable(df) # print table
| chain_ring | cogs | meter_dev | yrd_dev | revolutions | possible_steps | centered_cogs |
|---|---|---|---|---|---|---|
| 40 | 11 | 7.790909 | 8.520216 | 10328.38 | 20656.75 | -13 |
| 40 | 13 | 6.592308 | 7.209414 | 12206.26 | 24412.53 | -11 |
| 40 | 15 | 5.713333 | 6.248158 | 14084.15 | 28168.30 | -9 |
| 40 | 18 | 4.761111 | 5.206799 | 16900.98 | 33801.96 | -6 |
| 40 | 21 | 4.080952 | 4.462970 | 19717.81 | 39435.62 | -3 |
| 40 | 24 | 3.570833 | 3.905099 | 22534.64 | 45069.28 | 0 |
| 40 | 28 | 3.060714 | 3.347228 | 26290.41 | 52580.83 | 4 |
| 40 | 32 | 2.678125 | 2.928824 | 30046.19 | 60092.37 | 8 |
| 40 | 36 | 2.380556 | 2.603399 | 33801.96 | 67603.92 | 12 |
| 40 | 40 | 2.142500 | 2.343059 | 37557.73 | 75115.47 | 16 |
| 40 | 46 | 1.863044 | 2.037443 | 43191.39 | 86382.79 | 22 |
fit <- stan_glm(possible_steps ~ centered_cogs, data=df,
family = gaussian(),
prior = normal(location=0, scale=20),
prior_intercept = normal(location=0, scale=50000),
iter = 2000,
warmup = 500,
thin = 1,
cores = 4)
summary(fit)
tidy(fit)
## # A tibble: 2 x 3
## term estimate std.error
## <chr> <dbl> <dbl>
## 1 (Intercept) 45069. 0.00147
## 2 centered_cogs 1878. 0.000134
-means from each replication is plotted as a histogram against the mean of the observed variable.
# the dark blue line represents our data.
pp_check(fit, "stat")
pp_check(fit, "stat_2d")
draws <- tidy_draws(fit, `(Intercept)`, centered_cogs)
head(draws)
## # A tibble: 6 x 12
## .chain .iteration .draw `(Intercept)` centered_cogs sigma accept_stat__
## <int> <int> <int> <dbl> <dbl> <dbl> <dbl>
## 1 1 1 1 45069. 1878. 0.0139 0.991
## 2 1 2 2 45069. 1878. 0.0143 0.996
## 3 1 3 3 45069. 1878. 0.0150 0.906
## 4 1 4 4 45069. 1878. 0.0150 0.999
## 5 1 5 5 45069. 1878. 0.0152 1.00
## 6 1 6 6 45069. 1878. 0.0151 0.949
## # … with 5 more variables: stepsize__ <dbl>, treedepth__ <dbl>,
## # n_leapfrog__ <dbl>, divergent__ <dbl>, energy__ <dbl>
draws <- tidy_draws(fit, `(Intercept)`, centered_cogs)
head(draws)
## # A tibble: 6 x 12
## .chain .iteration .draw `(Intercept)` centered_cogs sigma accept_stat__
## <int> <int> <int> <dbl> <dbl> <dbl> <dbl>
## 1 1 1 1 45069. 1878. 0.0139 0.991
## 2 1 2 2 45069. 1878. 0.0143 0.996
## 3 1 3 3 45069. 1878. 0.0150 0.906
## 4 1 4 4 45069. 1878. 0.0150 0.999
## 5 1 5 5 45069. 1878. 0.0152 1.00
## 6 1 6 6 45069. 1878. 0.0151 0.949
## # … with 5 more variables: stepsize__ <dbl>, treedepth__ <dbl>,
## # n_leapfrog__ <dbl>, divergent__ <dbl>, energy__ <dbl>
mean(draws$`(Intercept)`)
## [1] 45069.28
fit$coefficients[1]
## (Intercept)
## 45069.28