简体   繁体   English

使用R中的t检验从输出表创建函数

[英]Creating function with output table from t-test in R

I would like to write a function for a neat output table from a t-test as I'm conducting numerous post-hoc t-tests however, writing functions are not my strong suit so I'd like some help. 我想通过t检验为整齐的输出表编写函数,因为我正在进行大量事后t检验,但是,编写函数并不是我的强项,因此我需要一些帮助。 My current code looks like this 我当前的代码如下所示


library(tidyverse)
library(lsr)
library(broom)

t_table <- function(data$col, data$col) {

  t.test(data$col, data$col) %>%
    broom::tidy() %>%
    mutate(Cohens_d = cohensD(data$col, data$col)) %>% # calc. cohen's d
    mutate_at(vars(- c(p.value,method,alternative)), round, 2)
}

One of the errors is: 错误之一是:

Error in data$col : object of type 'closure' is not subsettable. data $ col中的错误:“ closure”类型的对象不可子集化。

I'm assuming data and col are not general names for any data frame and column. 我假设data和col不是任何数据框和列的通用名称。

Essentially I'd like to be able to specify any data frame and column for each variable. 本质上,我希望能够为每个变量指定任何数据框和列。 I'm not even sure if this is possible as it is a very general function I'm trying to create but any help would be much appreciated. 我什至不确定这是否可行,因为这是我尝试创建的非常通用的功能,但是任何帮助将不胜感激。

the input arguments for your function should a) not have the same name and b) should not contain $ . 函数的输入参数应该a)名称不同,b)不应包含$ Other than that your function works fine: 除此之外,您的功能还可以正常工作:

t_table <- function(col1, col2) {

  t.test(col1, col2) %>%
    broom::tidy() %>%
    mutate(Cohens_d = cohensD(col1, col2)) %>% # calc. cohen's d
    mutate_at(vars(- c(p.value,method,alternative)), round, 2)
}

set.seed(1)
t_table(rnorm(100), rnorm(100)+1/2)
  estimate estimate1 estimate2 statistic     p.value parameter conf.low conf.high                  method alternative Cohens_d
1    -0.35      0.11      0.46     -2.69 0.007745151    197.19    -0.61     -0.09 Welch Two Sample t-test   two.sided     0.38

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

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