Performs a permutation test comparing the difference in means of a numeric variable between two groups. The null distribution is generated by randomly shuffling group labels.

permutation_test(
  df,
  groups,
  vals,
  n = 1000,
  type = "two-tailed",
  seed = 123,
  ci = 0.95
)

Arguments

df

A data.frame or tibble containing the data.

groups

A string specifying the grouping variable (must have exactly two levels).

vals

A string specifying the numeric response variable.

n

Number of permutations to perform. Default is 1000.

type

Type of test: either `"two-tailed"` (default) or `"one-tailed"`.

seed

Integer to set random seed for reproducibility. Default is 123.

ci

Confidence level for the null distribution (default 0.95).

Value

A list with components:

permutations

Vector of permuted test statistics.

observed_statistic

Observed difference in group means.

confidence

Confidence interval for the null distribution.

p_value

Permutation p-value.

Examples

set.seed(123)
df <- data.frame(
  group = rep(c("A", "B"), each = 10),
  value = c(rnorm(10, 0), rnorm(10, 1))
)
permutation_test(df, groups = "group", vals = "value", n = 500)
#> $permutations
#>   [1]  0.058301964  0.849944051  1.104491926 -0.465953531 -0.205368417
#>   [6]  0.002674168  0.253550318 -0.675171880  0.361120414  0.925571756
#>  [11] -0.641589253 -0.648173315 -0.264323793  0.153334094  0.144422057
#>  [16] -0.511871431 -0.396147291 -0.288873514 -0.229674621 -0.162525614
#>  [21]  0.207163690 -0.103078396 -0.311210517 -0.058309483  0.814609139
#>  [26]  1.246727614  0.268239564 -0.058150589  1.276517477  0.746655881
#>  [31] -0.258448015  1.276730353  0.240050463  0.032951862  0.300494184
#>  [36] -0.083559257  1.091508588 -0.990411120  0.150461965  0.217819092
#>  [41]  0.663992761  0.240160937 -0.352704722 -0.262539298 -0.246987554
#>  [46]  0.255632764  0.117487994 -0.396590903  0.100770499  0.815959643
#>  [51] -0.097587600 -0.394990528  0.734720565 -0.001421284 -0.688000823
#>  [56]  0.332514265  0.249782519 -0.286840707  1.276927583 -0.476003743
#>  [61] -0.327763999 -0.897129080 -0.998452241 -0.233357015  0.956032150
#>  [66]  0.258217610  0.675054536 -0.290164159  0.313347944 -0.469036272
#>  [71]  0.123617563 -0.458167995 -0.416885793 -0.576280839  0.555064804
#>  [76] -0.411901395  0.646314351 -0.444924782 -0.396653682  1.151163483
#>  [81] -0.327223115  0.183833629  0.071997961 -0.081702601 -0.289296210
#>  [86] -0.476576061  0.786508433  0.167101344 -0.009439577 -0.035708229
#>  [91]  0.068564727  0.385716188 -0.149262114 -0.250082479 -0.315385431
#>  [96] -0.337238234 -0.275397516 -0.283925224  0.479186798  0.152423761
#> [101]  0.260923769  1.077945129  1.079982793 -0.157848113 -0.223321179
#> [106]  0.399675070  0.042333774  0.182746719 -0.055697730  0.467344978
#> [111] -0.725864858  0.042638095  0.019320014  0.594701670 -0.063310078
#> [116]  0.410938517  0.424575285  0.597326354  0.146728550 -0.227719997
#> [121]  0.967026931 -0.025953593 -0.446141710  0.098538859  0.018277575
#> [126] -0.609497921 -0.140002369  0.417359348 -0.300843101 -0.348985424
#> [131]  0.718100902  0.642929612 -0.701441598  0.865598283  0.685929233
#> [136] -0.607795723 -0.217755049  0.037707624  0.098408114 -0.600951619
#> [141]  0.929891996  0.599010530 -0.317493525 -0.179221131  0.185918708
#> [146] -0.584930108 -0.548858532  0.451772722 -0.582156408 -0.431878406
#> [151]  1.031350839  0.291744686 -0.758571750 -0.491472682 -0.506491346
#> [156]  0.256771041 -0.758318759 -0.714684869  0.079851237  0.147328618
#> [161] -0.639714003  0.554336357  0.206127075  0.770747935  0.441317912
#> [166]  0.315673079 -0.040312200 -0.150529542 -0.714923138 -0.719447481
#> [171] -0.293456491  0.074615768  0.006854834  0.222311680 -0.055862396
#> [176] -0.463613948  0.578368029 -0.233808306 -0.051670108  0.490068449
#> [181] -0.271065981  0.230344382 -0.026578443  0.146328146  0.481327879
#> [186] -0.151912011 -0.786917035  0.601664111 -0.561967373 -0.500260443
#> [191]  0.679408245  0.136959953  0.830508925  0.540539840 -0.643275618
#> [196] -0.387414909 -0.261801309 -0.340810853  0.228533298  0.023844357
#> [201]  0.873879998  0.276571346 -0.079091846  0.667480394 -0.159684172
#> [206]  0.344033926  0.806286004 -0.215684975 -1.191014613  1.017124085
#> [211]  0.485884306  0.667297989 -0.247287923 -0.289904643  0.082753882
#> [216] -0.013634454 -0.779576611 -1.002280912 -0.486595990  0.176541975
#> [221] -0.864846659 -0.402220465 -0.013525672  0.465429732 -0.066703233
#> [226]  0.407051708 -0.377006025  0.208403001 -0.441625827 -0.347964291
#> [231]  0.172274770  0.068036486 -0.070060249  0.453526462  0.115962757
#> [236] -0.757321102 -0.003929012 -0.331711633 -0.255946130  0.098613054
#> [241]  0.283811530  0.784109276 -0.472225215  0.962955188  0.173019107
#> [246]  0.095104379 -0.234159646 -0.031449189 -0.462121670 -0.002573545
#> [251]  0.469640900 -0.088634966  0.014582052 -0.360272484 -0.610764763
#> [256] -0.128340939  0.006765273  0.587634474  0.183764973  0.969005034
#> [261]  0.781692548 -0.462209988 -0.744134573 -0.085757920  0.416462462
#> [266] -0.719114820  0.688399167  0.091590203  0.373032786  0.622859619
#> [271] -0.076304307 -0.814477529  0.472693405  0.289917541 -0.499645902
#> [276]  0.118572982  0.927410780 -0.151777980 -1.095680781  0.116947442
#> [281]  0.271483938  0.493832134 -0.483879351 -0.697832472 -0.883967878
#> [286] -0.023263005  0.112186930  0.784698011 -0.225066311 -0.023954890
#> [291] -0.216910777 -0.048451831  0.463983411  0.249995395  0.269378179
#> [296]  0.390252056 -0.371933812  0.330726733 -0.316593401 -0.341032935
#> [301] -0.137698774 -0.178789688 -0.605043162 -0.323849059  0.322697224
#> [306] -0.956715977  0.261931755  0.171293446 -0.101010543  0.422848989
#> [311] -0.087548056 -1.103020840  0.185454428  0.880921557  0.017235897
#> [316] -0.087546399  0.774652645 -0.486305770 -0.310799623  0.661579375
#> [321] -0.556488054 -0.678201275 -0.642302344 -0.373790275 -0.183275307
#> [326]  0.292434280 -0.190832889  0.118131037  0.315701214 -0.011860521
#> [331]  0.399492772  0.725756688  0.211359197  1.387293609 -0.316906301
#> [336] -0.044035461 -0.640270087  0.107317309 -0.325897309 -0.013438813
#> [341]  0.186344851 -0.440962964  0.226626495 -0.061598928 -0.397814188
#> [346]  0.096538773  0.167796123 -0.442221818  0.368122790  0.369008097
#> [351]  0.675937981  0.796935727 -0.357530667  0.296257274  0.540127843
#> [356]  0.070199141  0.028731496 -0.294154236 -0.355605281 -0.653132246
#> [361]  0.449121019 -0.932288760  0.571890157  0.227858669 -0.473658106
#> [366]  0.219539328  1.040595512 -0.475091021  0.464290752 -0.247410768
#> [371]  0.222962243 -0.757415247 -1.138759110  0.016631109 -1.012283036
#> [376] -0.985948454  0.931645717  0.698062547  0.298945750  0.470256858
#> [381]  0.482191908 -0.223055116 -0.181316009  0.459608885  0.581071624
#> [386]  0.063233449  0.181361189  1.376360267  0.167414304  0.402412283
#> [391] -0.033225259 -0.261657050 -0.211876724  0.597704464  0.136081522
#> [396]  0.115677394 -0.021062764  0.720167968 -0.187186733  0.432290618
#> [401]  0.868044485 -0.734129156  0.763449730 -0.339137920  0.436284184
#> [406] -0.007050425  0.642904543 -0.148093270  0.111413073  0.306800933
#> [411]  0.215235923  0.474857652 -0.313084567 -0.398901098  0.305457121
#> [416]  0.348418854  0.320240603 -0.073064800 -0.321773408  0.508436591
#> [421] -0.166537467 -0.001751834 -0.313341523  0.295065477  0.212640811
#> [426]  0.683979865 -0.117508566 -0.644212408 -0.155803473 -0.338707307
#> [431] -0.231115824 -0.117311331  0.477454628  0.238997531 -0.868850693
#> [436]  0.145941213 -0.175660695 -0.064812632  0.363855292 -0.218891087
#> [441] -1.013378840  0.629973954  0.555165116  0.189018772 -0.755953347
#> [446]  0.371587788  0.276076214 -0.342472765  0.130970467  0.274416975
#> [451]  0.217480868  0.176196517  0.006556808  0.150693808 -0.322370936
#> [456]  0.205704189  0.175035227  0.085855045  0.306797876  0.268947796
#> [461] -0.685163773 -0.097891881  0.319373561 -0.678921176  1.267723768
#> [466] -0.403738222 -0.059930522 -0.096848424  0.104538532 -0.048635451
#> [471] -0.031095102 -0.326767711  0.489742729  0.140953515  0.077164968
#> [476]  0.457328942  0.856058543  0.490711548 -0.445549640 -0.741275374
#> [481]  0.001067142 -0.616487869 -0.455003211 -0.664792887 -0.700302600
#> [486] -0.051974013 -0.133673814 -0.571863431 -0.775348730 -0.433945948
#> [491]  0.545801896 -0.149223524  0.061040615 -0.357932901  0.152826354
#> [496]  0.181638771  0.223857619 -0.675710771 -0.322800065 -1.109664762
#> 
#> $observed_statistic
#>        B 
#> 1.133996 
#> 
#> $confidence
#>       2.5%      97.5% 
#> -0.9155879  1.0362043 
#> 
#> $p_value
#> [1] 0.02
#>