Used internally by quake()
to generate new x values. Different flavours (shake_*
) exist
depending the type of uncertainties you have. *_within
variants allow to share new values
within a level.
Usage
shake_uniform(x, min, max)
shake_uniform_within(x, min, max, within)
shake_gaussian(x, mean, sd)
shake_gaussian_within(x, mean, sd, within)
Arguments
- x
tibble()
- min, max
colnames to pass to
stats::runif()
(shake_uniform_*
)- within
colname to define which levels share new x values (
*_within
variants)- mean, sd
colnames to pass to
stats::rnorm()
(shake_gaussian_*
)
Details
uniform
considers that the actual x can be anytime between two temporal bounds.
This is typically the case for ante quem
data.
gaussian
considers that the actual x is centered on a value but with an associated error.
You can think of C^14 dating which is provided as and estimate and a standard deviation to expect
around it.
For shake_uniform_*
, min
/max
correspond to tpq
/taq
respectively.
For shake_gaussian_*
, mean
/sd
correspond to the single (best) x and its standard deviation.
Functions
shake_uniform
: generate new x values using uniform distributionshake_uniform_within
: shake_uniform within a groupshake_gaussian
: generate new x values using gaussian distributionshake_gaussian_within
: shake_gaussian within a group
Examples
set.seed(2329) # replicability
# we will use builtin dummy df_u and df_g
# show uniform shaking
df_u # 'unshaken' data
#> # A tibble: 8 × 5
#> tpq taq species site mes
#> <dbl> <dbl> <chr> <chr> <dbl>
#> 1 34 43 fox a 1.22
#> 2 -90 -46 fox a 1.90
#> 3 87 98 fox b 2.94
#> 4 5 32 fox b 4.38
#> 5 -95 -85 hound a 4.35
#> 6 42 69 hound a 2.91
#> 7 10 50 hound b 1.58
#> 8 27 46 hound b 1.45
df_u %>% shake_uniform(tpq, taq) # new x values bounded between each tpq/taq
#> # A tibble: 8 × 6
#> tpq taq species site mes x_new
#> <dbl> <dbl> <chr> <chr> <dbl> <dbl>
#> 1 34 43 fox a 1.22 40.0
#> 2 -90 -46 fox a 1.90 -87.8
#> 3 87 98 fox b 2.94 97.3
#> 4 5 32 fox b 4.38 19.2
#> 5 -95 -85 hound a 4.35 -94.8
#> 6 42 69 hound a 2.91 61.2
#> 7 10 50 hound b 1.58 32.0
#> 8 27 46 hound b 1.45 39.1
df_u %>% shake_uniform(tpq, taq) # same idea, different values
#> # A tibble: 8 × 6
#> tpq taq species site mes x_new
#> <dbl> <dbl> <chr> <chr> <dbl> <dbl>
#> 1 34 43 fox a 1.22 34.7
#> 2 -90 -46 fox a 1.90 -52.2
#> 3 87 98 fox b 2.94 88.6
#> 4 5 32 fox b 4.38 17.9
#> 5 -95 -85 hound a 4.35 -93.8
#> 6 42 69 hound a 2.91 55.3
#> 7 10 50 hound b 1.58 41.0
#> 8 27 46 hound b 1.45 32.8
# you can decide that new values must be stratified per site
# we create a df_u variant with equal tpq and taq per site (otherwise makes no sense)
df_u %>%
dplyr::group_by(site) %>%
dplyr::mutate(tpq=tpq[1], taq=taq[1]) %>% # first value defines all others
dplyr::ungroup() -> df_u_site
# now compare
df_u_site %>% shake_uniform(tpq, taq)
#> # A tibble: 8 × 6
#> tpq taq species site mes x_new
#> <dbl> <dbl> <chr> <chr> <dbl> <dbl>
#> 1 34 43 fox a 1.22 40.5
#> 2 34 43 fox a 1.90 37.6
#> 3 87 98 fox b 2.94 91.8
#> 4 87 98 fox b 4.38 96.6
#> 5 34 43 hound a 4.35 41.6
#> 6 34 43 hound a 2.91 37.7
#> 7 87 98 hound b 1.58 87.8
#> 8 87 98 hound b 1.45 97.5
df_u_site %>% shake_uniform_within(tpq, taq, within=site) # equal x_new within site
#> # A tibble: 8 × 6
#> tpq taq species site mes x_new
#> <dbl> <dbl> <chr> <chr> <dbl> <dbl>
#> 1 34 43 fox a 1.22 35.8
#> 2 34 43 fox a 1.90 35.8
#> 3 87 98 fox b 2.94 87.5
#> 4 87 98 fox b 4.38 87.5
#> 5 34 43 hound a 4.35 35.8
#> 6 34 43 hound a 2.91 35.8
#> 7 87 98 hound b 1.58 87.5
#> 8 87 98 hound b 1.45 87.5
# gaussian shaking now
df_g # unshaken data
#> # A tibble: 8 × 5
#> c14 sd1 species site mes
#> <dbl> <dbl> <chr> <chr> <dbl>
#> 1 34 6 fox a 1.22
#> 2 -90 18 fox a 1.90
#> 3 87 7 fox b 2.94
#> 4 5 12 fox b 4.38
#> 5 -95 7 hound a 4.35
#> 6 42 12 hound a 2.91
#> 7 10 17 hound b 1.58
#> 8 27 10 hound b 1.45
df_g %>% shake_gaussian(c14, sd1) # x_new is centered on c14 with gaussian noise (sd=sd1)
#> # A tibble: 8 × 6
#> c14 sd1 species site mes x_new
#> <dbl> <dbl> <chr> <chr> <dbl> <dbl>
#> 1 34 6 fox a 1.22 24.1
#> 2 -90 18 fox a 1.90 -69.1
#> 3 87 7 fox b 2.94 77.1
#> 4 5 12 fox b 4.38 20.5
#> 5 -95 7 hound a 4.35 -94.4
#> 6 42 12 hound a 2.91 58.2
#> 7 10 17 hound b 1.58 -6.83
#> 8 27 10 hound b 1.45 30.6
df_g %>% shake_gaussian(c14, sd1) # same idea, different values
#> # A tibble: 8 × 6
#> c14 sd1 species site mes x_new
#> <dbl> <dbl> <chr> <chr> <dbl> <dbl>
#> 1 34 6 fox a 1.22 31.9
#> 2 -90 18 fox a 1.90 -86.5
#> 3 87 7 fox b 2.94 92.4
#> 4 5 12 fox b 4.38 2.09
#> 5 -95 7 hound a 4.35 -95.5
#> 6 42 12 hound a 2.91 40.3
#> 7 10 17 hound b 1.58 29.5
#> 8 27 10 hound b 1.45 39.9
# stratified version
# same approach as above, inherit from 1st value,
# to make a tibble that makes sense for *_within variant
df_g %>%
dplyr::group_by(site) %>%
dplyr::mutate(c14=c14[1], sd1=sd1[1]) %>%
dplyr::ungroup() -> df_g_site
df_g_site %>% shake_gaussian_within(c14, sd1, site)
#> # A tibble: 8 × 6
#> c14 sd1 species site mes x_new
#> <dbl> <dbl> <chr> <chr> <dbl> <dbl>
#> 1 34 6 fox a 1.22 35.8
#> 2 34 6 fox a 1.90 35.8
#> 3 87 7 fox b 2.94 82.3
#> 4 87 7 fox b 4.38 82.3
#> 5 34 6 hound a 4.35 35.8
#> 6 34 6 hound a 2.91 35.8
#> 7 87 7 hound b 1.58 82.3
#> 8 87 7 hound b 1.45 82.3