R 如何从成对测试中获得df和t值?

R 如何从成对测试中获得df和t值?,r,statistics,R,Statistics,是否有办法从相依数据的成对t.test中获取t值和df 例如: data<-c(2,3,2,2,5,2,4,2,4,3,4,2) time<-c(1,1,1,1,2,2,2,2,3,3,3,3) pairwise.t.test(data, time,p.adjust.method= "bonf",paired=TRUE) 我希望t值和df的格式相同 谢谢你的帮助 可以从t.test中提取df和统计值 t.test(data, time, paired = TRUE) Pa

是否有办法从相依数据的
成对t.test
中获取t值和df

例如:

 data<-c(2,3,2,2,5,2,4,2,4,3,4,2)
 time<-c(1,1,1,1,2,2,2,2,3,3,3,3)

 pairwise.t.test(data, time,p.adjust.method= "bonf",paired=TRUE)
我希望t值和df的格式相同


谢谢你的帮助

可以从
t.test
中提取df和统计值

t.test(data, time, paired = TRUE)

Paired t-test

data:  data and time
t = 2.9304, df = 11, p-value = 0.01368
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 0.2281644 1.6051689
 sample estimates:
 mean of the differences 
               0.9166667

#Extract parameters
result<-t.test(data,time,paired = TRUE)
result$statistic
       t 
2.930375 
result$parameter
df 
11 
t.test(数据、时间、配对=TRUE)
配对t检验
数据:数据和时间
t=2.9304,df=11,p值=0.01368
替代假设:平均值的真实差异不等于0
95%置信区间:
0.2281644 1.6051689
样本估计:
差异的平均值
0.9166667
#提取参数

结果您可以通过编写自定义函数从pairwise.t.test中获取df和t值。这里有一个函数可以实现这一点:

pairwise.t.test.with.t.and.df <- function (x, g, p.adjust.method = p.adjust.methods, pool.sd = !paired, 
                                           paired = FALSE, alternative = c("two.sided", "less", "greater"), 
                                           ...) 
{
    if (paired & pool.sd) 
        stop("pooling of SD is incompatible with paired tests")
    DNAME <- paste(deparse(substitute(x)), "and", deparse(substitute(g)))
    g <- factor(g)
    p.adjust.method <- match.arg(p.adjust.method)
    alternative <- match.arg(alternative)
    if (pool.sd) {
        METHOD <- "t tests with pooled SD"
        xbar <- tapply(x, g, mean, na.rm = TRUE)
        s <- tapply(x, g, sd, na.rm = TRUE)
        n <- tapply(!is.na(x), g, sum)
        degf <- n - 1
        total.degf <- sum(degf)
        pooled.sd <- sqrt(sum(s^2 * degf)/total.degf)
        compare.levels <- function(i, j) {
            dif <- xbar[i] - xbar[j]
            se.dif <- pooled.sd * sqrt(1/n[i] + 1/n[j])
            t.val <- dif/se.dif
            if (alternative == "two.sided") 
                2 * pt(-abs(t.val), total.degf)
            else pt(t.val, total.degf, lower.tail = (alternative == 
                                                         "less"))
        }
        compare.levels.t <- function(i, j) {
            dif <- xbar[i] - xbar[j]
            se.dif <- pooled.sd * sqrt(1/n[i] + 1/n[j])
            t.val = dif/se.dif 
            t.val
        }       
    }
    else {
        METHOD <- if (paired) 
            "paired t tests"
        else "t tests with non-pooled SD"
        compare.levels <- function(i, j) {
            xi <- x[as.integer(g) == i]
            xj <- x[as.integer(g) == j]
            t.test(xi, xj, paired = paired, alternative = alternative, 
                   ...)$p.value
        }
        compare.levels.t <- function(i, j) {
            xi <- x[as.integer(g) == i]
            xj <- x[as.integer(g) == j]
            t.test(xi, xj, paired = paired, alternative = alternative, 
                   ...)$statistic
        }
        compare.levels.df <- function(i, j) {
            xi <- x[as.integer(g) == i]
            xj <- x[as.integer(g) == j]
            t.test(xi, xj, paired = paired, alternative = alternative, 
                   ...)$parameter
        }
    }
    PVAL <- pairwise.table(compare.levels, levels(g), p.adjust.method)
    TVAL <- pairwise.table.t(compare.levels.t, levels(g), p.adjust.method)
    if (pool.sd) 
        DF <- total.degf
    else
        DF <- pairwise.table.t(compare.levels.df, levels(g), p.adjust.method)           
    ans <- list(method = METHOD, data.name = DNAME, p.value = PVAL, 
                p.adjust.method = p.adjust.method, t.value = TVAL, dfs = DF)
    class(ans) <- "pairwise.htest"
    ans
}
pairwise.table.t <- function (compare.levels.t, level.names, p.adjust.method) 
{
    ix <- setNames(seq_along(level.names), level.names)
    pp <- outer(ix[-1L], ix[-length(ix)], function(ivec, jvec) sapply(seq_along(ivec), 
        function(k) {
            i <- ivec[k]
            j <- jvec[k]
            if (i > j)
                compare.levels.t(i, j)               
            else NA
        }))
    pp[lower.tri(pp, TRUE)] <- pp[lower.tri(pp, TRUE)]
    pp
}

pairwise.t.test.with.t.and.df来自文档:
此方法实际上不调用t.test,因此会忽略额外的参数。
检查
?pairwise.t.test
。您可能需要自己计算这些值。扩展
stats::pairwise.t.test
是很聪明的,但是这种方法的缺点是您要执行三次t-test,而不是重用这些值。这可能会对性能产生重大影响。
pairwise.t.test.with.t.and.df <- function (x, g, p.adjust.method = p.adjust.methods, pool.sd = !paired, 
                                           paired = FALSE, alternative = c("two.sided", "less", "greater"), 
                                           ...) 
{
    if (paired & pool.sd) 
        stop("pooling of SD is incompatible with paired tests")
    DNAME <- paste(deparse(substitute(x)), "and", deparse(substitute(g)))
    g <- factor(g)
    p.adjust.method <- match.arg(p.adjust.method)
    alternative <- match.arg(alternative)
    if (pool.sd) {
        METHOD <- "t tests with pooled SD"
        xbar <- tapply(x, g, mean, na.rm = TRUE)
        s <- tapply(x, g, sd, na.rm = TRUE)
        n <- tapply(!is.na(x), g, sum)
        degf <- n - 1
        total.degf <- sum(degf)
        pooled.sd <- sqrt(sum(s^2 * degf)/total.degf)
        compare.levels <- function(i, j) {
            dif <- xbar[i] - xbar[j]
            se.dif <- pooled.sd * sqrt(1/n[i] + 1/n[j])
            t.val <- dif/se.dif
            if (alternative == "two.sided") 
                2 * pt(-abs(t.val), total.degf)
            else pt(t.val, total.degf, lower.tail = (alternative == 
                                                         "less"))
        }
        compare.levels.t <- function(i, j) {
            dif <- xbar[i] - xbar[j]
            se.dif <- pooled.sd * sqrt(1/n[i] + 1/n[j])
            t.val = dif/se.dif 
            t.val
        }       
    }
    else {
        METHOD <- if (paired) 
            "paired t tests"
        else "t tests with non-pooled SD"
        compare.levels <- function(i, j) {
            xi <- x[as.integer(g) == i]
            xj <- x[as.integer(g) == j]
            t.test(xi, xj, paired = paired, alternative = alternative, 
                   ...)$p.value
        }
        compare.levels.t <- function(i, j) {
            xi <- x[as.integer(g) == i]
            xj <- x[as.integer(g) == j]
            t.test(xi, xj, paired = paired, alternative = alternative, 
                   ...)$statistic
        }
        compare.levels.df <- function(i, j) {
            xi <- x[as.integer(g) == i]
            xj <- x[as.integer(g) == j]
            t.test(xi, xj, paired = paired, alternative = alternative, 
                   ...)$parameter
        }
    }
    PVAL <- pairwise.table(compare.levels, levels(g), p.adjust.method)
    TVAL <- pairwise.table.t(compare.levels.t, levels(g), p.adjust.method)
    if (pool.sd) 
        DF <- total.degf
    else
        DF <- pairwise.table.t(compare.levels.df, levels(g), p.adjust.method)           
    ans <- list(method = METHOD, data.name = DNAME, p.value = PVAL, 
                p.adjust.method = p.adjust.method, t.value = TVAL, dfs = DF)
    class(ans) <- "pairwise.htest"
    ans
}
pairwise.table.t <- function (compare.levels.t, level.names, p.adjust.method) 
{
    ix <- setNames(seq_along(level.names), level.names)
    pp <- outer(ix[-1L], ix[-length(ix)], function(ivec, jvec) sapply(seq_along(ivec), 
        function(k) {
            i <- ivec[k]
            j <- jvec[k]
            if (i > j)
                compare.levels.t(i, j)               
            else NA
        }))
    pp[lower.tri(pp, TRUE)] <- pp[lower.tri(pp, TRUE)]
    pp
}
data<-c(2,3,2,2,5,2,4,2,4,3,4,2)
time<-c(1,1,1,1,2,2,2,2,3,3,3,3)

result <- pairwise.t.test.with.t.and.df(data, time,p.adjust.method= "bonf",paired=TRUE)

# Print t-values
result[[5]]

# Print dfs
result[[6]]