简体   繁体   English

如何使用rstatix获取R读取function变量

[英]How to get R to read function variables with rstatix

I am trying to perform multiple, independent t-tests on a large data frame.我正在尝试对大型数据框执行多个独立的 t 检验。 When I create a function to loop over to run the tests rstatix will not read the function variables as variables and input their value.当我创建一个 function 循环运行测试时,rstatix 不会将 function 变量读取为变量并输入它们的值。

Example data示例数据

if(!require(rstatix)){install.packages("rstatix");library('rstatix')}

set.seed(1)
df <- data.frame(
Type = sprintf("Type_%s", rep.int(1:2, times = 10)),
Read = rnorm(20))

T-test T检验

stat.test <- df %>%
  t_test(Read ~ Type, paired = FALSE)
stat.test

Plot without statistics Plot 无统计

ggplot(df, aes(x = Type, y = Read))  + 
      geom_boxplot(aes(fill= Type)) +
      geom_dotplot(binaxis='y', stackdir='center', dotsize=1, binwidth = 1/30)

在此处输入图像描述

Example function (works fine!)示例 function(工作正常!)

my.function <-
function(df, var1, var2) {
    
    ggplot(df, aes_string(x = var1, y = var2))  + 
      geom_boxplot(aes_string(fill= var1)) +
      geom_dotplot(binaxis='y', stackdir='center', dotsize=1, binwidth = 1/30)
}
my.function(df, 'Type', 'Read')

在此处输入图像描述

My issue我的问题

my.function <-
function(df, var1, var2) {
    stat.test <- df %>%
      t_test(var2 ~ var1, paired = FALSE)
    
    ggplot(df, aes_string(x = var1, y = var2))  + 
      geom_boxplot(aes_string(fill= var1)) +
      geom_dotplot(binaxis='y', stackdir='center', dotsize=1, binwidth = 1/30) + 
      stat_pvalue_manual(stat.test, label = "p", y.position = 2.1)
}
my.function(df, 'Type', 'Read')

The above returns an error because rstatix thinks var1 and var2 are columns in the example data frame.上面返回一个错误,因为 rstatix 认为var1var2是示例数据框中的列。

I have tried the following to get R to stop the behavior but both attempts fail.我尝试了以下方法来让 R 停止该行为,但两次尝试都失败了。

Attempt 1尝试 1

my.function <-
function(df, var1, var2) {
    stat.test <- df %>%
      t_test(eval(parse(var2)) ~ eval(parse(var1)), paired = FALSE)
    
    ggplot(df, aes_string(x = var1, y = var2))  + 
      geom_boxplot(aes_string(fill= var1)) +
      geom_dotplot(binaxis='y', stackdir='center', dotsize=1, binwidth = 1/30) + 
      stat_pvalue_manual(stat.test, label = "p", y.position = 2.1)
}
my.function(df, 'Type', 'Read')

Attempt 2尝试 2

my.function <-
function(df, var1, var2) {
    stat.test <- df %>%
      t_test(eval(as.name(paste(var2))) ~ eval(as.name(paste(var1))), paired = FALSE)
    
    ggplot(df, aes_string(x = var1, y = var2))  + 
      geom_boxplot(aes_string(fill= var1)) +
      geom_dotplot(binaxis='y', stackdir='center', dotsize=1, binwidth = 1/30) + 
      stat_pvalue_manual(stat.test, label = "p", y.position = 2.1)
}
my.function(df, 'Type', 'Read')

I went into the t_test function to see if there would be any indicators of why my attempts to get this custom function to run would fail.我进入 t_test function 以查看是否有任何迹象表明为什么我尝试运行此自定义 function 会失败。 I suspected the issue had something to do with the way R handles formulas and functions.我怀疑这个问题与 R 处理公式和函数的方式有关。 After a bit of manipulation of my original script, I finally got it working.在对我的原始脚本进行了一些操作之后,我终于让它工作了。

my.function <-
function(df, var1, var2) {
    stat.test <- df %>%
      t_test(as.formula(paste(var2, '~', var1)), paired = FALSE)
    
    ggplot(df, aes_string(x = var1, y = var2))  + 
      geom_boxplot(aes_string(fill= var1)) +
      geom_dotplot(binaxis='y', stackdir='center', dotsize=1, binwidth = 1/30) + 
      stat_pvalue_manual(stat.test, label = "p", y.position = 2.1)
}
my.function(df, 'Type', 'Read')

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM