Skip to contents

Write a pipeline to perform some calculation whose result can be coerced into one line of a data frame. Add trials(times=3) to the end of the pipeline in order to repeat the calculation multiple times. Typically, each trial involves some random component, but another (or an additional) capability is to parameterize the pipeline expression by including some unbound variable in it, e.g. lambda. Then call trials(lambda=c(10,20)) to repeat the calculation for each of the elements of the named parameter.

Usage

trials(.ex, times = 1, ...)

Arguments

.ex

(Not user-facing.) The left side of the pipeline.

times

The number of times to run the trial.

...

Values for any unbound parameter in the left side of the pipeline. If a vector of length > 1, the trials will be run separately for each element of the vector.

Value

a dataframe with one row for each trial. (But see the ... argument.)

Details

This is intended as a pipeline friendly replacement for mosaic::do().

Examples

mean(rnorm(10)) |> trials(times=3)
#> New names:
#>  `` -> `...2`
#> New names:
#>  `` -> `...2`
#> New names:
#>  `` -> `...2`
#>   .trial         ...2
#> 1      1  0.195557925
#> 2      2 -0.358358647
#> 3      3  0.008935456
mean(rnorm(lambda)) |> trials(lambda=c(1, 100, 10000))
#> New names:
#>  `` -> `...3`
#> New names:
#>  `` -> `...3`
#> New names:
#>  `` -> `...3`
#>   .trial lambda         ...3
#> 1      1      1  1.007865750
#> 2      1    100 -0.066747849
#> 3      1  10000  0.006162709
mean(rnorm(lambda)) |> trials(times=5, lambda=c(1, 100, 10000))
#> New names:
#>  `` -> `...3`
#> New names:
#>  `` -> `...3`
#> New names:
#>  `` -> `...3`
#> New names:
#>  `` -> `...3`
#> New names:
#>  `` -> `...3`
#> New names:
#>  `` -> `...3`
#> New names:
#>  `` -> `...3`
#> New names:
#>  `` -> `...3`
#> New names:
#>  `` -> `...3`
#> New names:
#>  `` -> `...3`
#> New names:
#>  `` -> `...3`
#> New names:
#>  `` -> `...3`
#> New names:
#>  `` -> `...3`
#> New names:
#>  `` -> `...3`
#> New names:
#>  `` -> `...3`
#>    .trial lambda         ...3
#> 1       1      1  0.056373446
#> 2       2      1 -1.246264472
#> 3       3      1  0.188988127
#> 4       4      1 -0.752384639
#> 5       5      1  0.661741495
#> 6       1    100  0.178970252
#> 7       2    100  0.051135235
#> 8       3    100  0.013606658
#> 9       4    100  0.020965758
#> 10      5    100  0.040333309
#> 11      1  10000  0.002462381
#> 12      2  10000  0.007751799
#> 13      3  10000  0.007810626
#> 14      4  10000 -0.009780755
#> 15      5  10000  0.018391817
sample(mtcars, n=lambda, replace=TRUE) |> select(mpg, hp) |>
  model_train(mpg ~ resample(hp)) |>
  regression_summary() |> trials(times=3, lambda=c(10, 20, 40)) |>
  filter(term == "resample(hp)")
#> Warning: The `tidy()` method for objects of class `model_object` is not maintained by the broom team, and is only supported through the `lm` tidier method. Please be cautious in interpreting and reporting broom output.
#> 
#> This warning is displayed once per session.
#>   .trial lambda         term     estimate  std.error  statistic    p.value
#> 1      1     10 resample(hp)  0.012597995 0.01795724  0.7015554 0.50285102
#> 2      2     10 resample(hp)  0.030261467 0.02511576  1.2048796 0.26268019
#> 3      3     10 resample(hp) -0.041730533 0.03982042 -1.0479682 0.32527933
#> 4      1     20 resample(hp)  0.012090463 0.02078032  0.5818228 0.56790213
#> 5      2     20 resample(hp)  0.003021334 0.01859688  0.1624645 0.87274999
#> 6      3     20 resample(hp) -0.013214974 0.02288540 -0.5774413 0.57079438
#> 7      1     40 resample(hp)  0.034207337 0.01353217  2.5278528 0.01575414
#> 8      2     40 resample(hp) -0.002350105 0.01204203 -0.1951586 0.84630856
#> 9      3     40 resample(hp) -0.030200397 0.01217559 -2.4804048 0.01766836