如何使用{rlang}创建用于t检验的配对公式?

gblwokeq  于 8个月前  发布在  其他
关注(0)|答案(1)|浏览(38)

我正在尝试创建一个统一的接口,用于使用{rlang}运行t检验,并且我想使用t.test()的公式方法。在这样做的时候,我可以使用{rlang}构造一个用于非配对t检验的公式,但不知道如何创建用于配对检验的公式。

library(rlang)

two_sample_t_test <- function(data, x, y, paired, ...) {
  x <- ensym(x)
  y <- ensym(y)

  exec(
    t.test,
    formula = if (paired) {
      new_formula(quote(Pair(x, y)), 1)
    } else {
      new_formula(y, x)
    },
    data = data,
    ...
  )
}

# unpaired t-test -------------------------

two_sample_t_test(mtcars, am, wt, paired = FALSE, var.equal = TRUE)
#> 
#>  Two Sample t-test
#> 
#> data:  wt by am
#> t = 5.2576, df = 30, p-value = 1.125e-05
#> alternative hypothesis: true difference in means between group 0 and group 1 is not equal to 0
#> 95 percent confidence interval:
#>  0.8304317 1.8853577
#> sample estimates:
#> mean in group 0 mean in group 1 
#>        3.768895        2.411000

# paired t-test -------------------------

two_sample_t_test(mtcars, am, wt, paired = TRUE)

#> Error in model.frame.default(formula = .Primitive("quote")(Pair(x, y) ~ : invalid type (list) for variable 'Pair(x, y)'

创建于2023-09-11使用reprex v2.0.2

ercv8c1e

ercv8c1e1#

在{rlang}中,我们可以使用expr()来捕获new_formula!!中的表达式,以提前计算xy,这应该会给予我们想要的结果(输出比我使用eval(bquote(...))的旧答案好得多,见下文):

library(rlang)

two_sample_t_test <- function(data, x, y, paired, ...) {

  x <- ensym(x)
  y <- ensym(y)
  
  exec(
    t.test,
    formula = if (paired) {
      new_formula(expr(Pair(!! x, !! y)), 1)
    } else {
      new_formula(y, x)
    },
    data = data,
    ...
  )
}

two_sample_t_test(mtcars, am, wt, paired = TRUE)
#> 
#>  Paired t-test
#> 
#> data:  Pair(am, wt)
#> t = -11.589, df = 31, p-value = 8.457e-13
#> alternative hypothesis: true mean difference is not equal to 0
#> 95 percent confidence interval:
#>  -3.305685 -2.316315
#> sample estimates:
#> mean difference 
#>          -2.811

two_sample_t_test(mtcars, am, wt, paired = FALSE, var.equal = TRUE)
#> 
#>  Two Sample t-test
#> 
#> data:  wt by am
#> t = 5.2576, df = 30, p-value = 1.125e-05
#> alternative hypothesis: true difference in means between group 0 and group 1 is not equal to 0
#> 95 percent confidence interval:
#>  0.8304317 1.8853577
#> sample estimates:
#> mean in group 0 mean in group 1 
#>        3.768895        2.411000

旧答案

我会尝试使用eval(bquote(...))而不是单独使用quote(),然后通过将xy Package 在.()中来提前评估它们,并使用data参数中提供的data.frame作为evalenvir

library(rlang)

two_sample_t_test <- function(data, x, y, paired, ...) {

  x <- ensym(x)
  y <- ensym(y)
  
  exec(
    t.test,
    formula = if (paired) {
      new_formula(eval(bquote(Pair(.(x), .(y))), envir = data), 1)
    } else {
      new_formula(y, x)
    },
    data = data,
    ...
  )
}

two_sample_t_test(mtcars, am, wt, paired = FALSE, var.equal = TRUE)
#> 
#>  Two Sample t-test
#> 
#> data:  wt by am
#> t = 5.2576, df = 30, p-value = 1.125e-05
#> alternative hypothesis: true difference in means between group 0 and group 1 is not equal to 0
#> 95 percent confidence interval:
#>  0.8304317 1.8853577
#> sample estimates:
#> mean in group 0 mean in group 1 
#>        3.768895        2.411000

two_sample_t_test(mtcars, am, wt, paired = TRUE)
#> 
#>  Paired t-test
#> 
#> data:  structure(c(1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 2.62, 2.875, 2.32, 3.215, 3.44, 3.46, 3.57, 3.19, 3.15, 3.44, 3.44, 4.07, 3.73, 3.78, 5.25, 5.424, 5.345, 2.2, 1.615, 1.835, 2.465, 3.52, 3.435, 3.84, 3.845, 1.935, 2.14, 1.513, 3.17, 2.77, 3.57, 2.78), dim = c(32L, 2L), dimnames = list(NULL, c("x", "y")), class = "Pair")
#> t = -11.589, df = 31, p-value = 8.457e-13
#> alternative hypothesis: true mean difference is not equal to 0
#> 95 percent confidence interval:
#>  -3.305685 -2.316315
#> sample estimates:
#> mean difference 
#>          -2.811

创建于2023-09-11使用reprex v2.0.2

相关问题