你可以用 来很好地做到这一点ri2
,你可以用它来安装install.packages("ri2")
library(ri2)
dat <- data.frame(Y = c(10, 20, 15, 5, 9),
Z = c(0, 0, 0, 1, 1))
declaration <- declare_ra(N = 5, m = 2)
# All 10 possibilities
obtain_permutation_matrix(declaration)
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#> [1,] 0 0 0 0 0 0 1 1 1 1
#> [2,] 0 0 0 1 1 1 0 0 0 1
#> [3,] 0 1 1 0 0 1 0 0 1 0
#> [4,] 1 0 1 0 1 0 0 1 0 0
#> [5,] 1 1 0 1 0 0 1 0 0 0
# Do randomization inference
ri_out <- conduct_ri(formula = Y ~ Z, declaration = declaration, data = dat)
# check out the 10 possibilities
ri_out$sims_df
#> est_sim est_obs coefficient
#> Z.1 -8.0000000 -8 Z
#> Z.2 0.3333333 -8 Z
#> Z.3 -3.0000000 -8 Z
#> Z.4 4.5000000 -8 Z
#> Z.5 1.1666667 -8 Z
#> Z.6 9.5000000 -8 Z
#> Z.7 -3.8333333 -8 Z
#> Z.8 -7.1666667 -8 Z
#> Z.9 1.1666667 -8 Z
#> Z.10 5.3333333 -8 Z
# Get a p-value
summary(ri_out)
#> coefficient estimate two_tailed_p_value null_ci_lower null_ci_upper
#> 1 Z -8 0.2 -7.8125 8.5625