Skip to contents

This function can be used to compute a given number of replicates of a specified tree-sequence statistical expression

Usage

ts_replicate(n, expr, parallel = NULL)

Arguments

n

Number of replicates to compute

expr

A tree-sequence expression to compute (see Details)

parallel

How many cores should be utilized for the computation?

Value

A data frame object

Examples

init_env()
p1 <- population("p1", time = 4000, N = 1000)
p2 <- population("p2", time = 3000, N = 1000, parent = p1)
p3 <- population("p3", time = 2000, N = 1000, parent = p2)

model <- compile_model(list(p1, p2, p3), generation_time = 1)
schedule <- schedule_recording(model, times = 0, list(p1, 10), list(p2, 10), list(p3, 10))
samples <- ts_names(model, split = TRUE, schedule = schedule)

# this is how we can compute a single tree-sequence statistic with slendr:
# 1. first we simulate a tree sequence from a model
ts <- msprime(model, sequence_length = 1e6, recombination_rate = 1e-8, schedule = schedule)
# 2. then we run a desired tskit-wrapper function
ts_f3(ts, A = samples["p1"], B = samples["p2"], C = samples["p3"], mode = "branch")
#> # A tibble: 1 × 5
#>   A     B     C     statistic       f3
#>   <chr> <chr> <chr> <chr>        <dbl>
#> 1 p1    p2    p3    f3(p1;p2,p3) 5163.

# this is how we can compute the same function across multiple replicates at once
ts_replicate(
  n = 10,
  {
    ts <- msprime(model, sequence_length = 1e6, recombination_rate = 1e-8, schedule = schedule)
    ts_f3(ts, A = samples["p1"], B = samples["p2"], C = samples["p3"], mode = "branch")
  }
)
#> # A tibble: 10 × 6
#>    A     B     C     statistic       f3   rep
#>    <chr> <chr> <chr> <chr>        <dbl> <int>
#>  1 p1    p2    p3    f3(p1;p2,p3) 4241.     1
#>  2 p1    p2    p3    f3(p1;p2,p3) 3995.     2
#>  3 p1    p2    p3    f3(p1;p2,p3) 4123.     3
#>  4 p1    p2    p3    f3(p1;p2,p3) 3949.     4
#>  5 p1    p2    p3    f3(p1;p2,p3) 4539.     5
#>  6 p1    p2    p3    f3(p1;p2,p3) 3357.     6
#>  7 p1    p2    p3    f3(p1;p2,p3) 2981.     7
#>  8 p1    p2    p3    f3(p1;p2,p3) 3884.     8
#>  9 p1    p2    p3    f3(p1;p2,p3) 3702.     9
#> 10 p1    p2    p3    f3(p1;p2,p3) 4307.    10