r - R 中的配对样本 t 检验 : a question of direction

标签 r statistics

我有一个关于使用不同数据结构但相同数据的配对样本 t 检验中 t 符号的问题。我知道该标志在重要性方面没有什么区别,但是,它通常会告诉用户是否随着时间的推移而减少或随着时间的推移而增加。因此,我需要确保我提供的代码产生相同的结果,或者解释正确。

我必须解释一下结果(和代码),作为我们向软件用户提供的示例,该软件使用 R(C# 程序中的 Rdotnet)进行统计。我不清楚 R 中这两种方法中变量的正确顺序。

方法 1 使用两个矩阵

## Sets seed for repetitive number generation
set.seed(2820)

## Creates the matrices
preTest <- c(rnorm(100, mean = 145, sd = 9))
postTest <- c(rnorm(100, mean = 138, sd = 8))

## Runs paired-sample T-Test just on two original matrices
t.test(preTest,postTest, paired = TRUE)

结果显示显着性,并且 t 为正值,告诉我从测试前到测试后的平均差异有所减小。

    Paired t-test

data:  preTest and postTest
t = 7.1776, df = 99, p-value = 1.322e-10
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
  6.340533 11.185513
sample estimates:
mean of the differences 
               8.763023

但是,大多数人不会从两个矩阵中获取数据,而是从包含 BEFORE 和 AFTER 值的文件中获取数据。我会将这些数据保存在 csv 中,并在演示期间导入它们。因此,为了模仿这一点,我需要以我们软件的用户习惯看到的结构创建数据框架。 “pstt”应该看起来像导入 csv 后的数据框。

方法 2:使用平面文件结构

## Converts the matrices into a dataframe that looks like the way these 
data are normally stored in a csv or Excel

ID <- c(1:100)
pstt <- data.frame(ID,preTest,postTest)

## Puts the data in a form that can be used by R (grouping var | data var)
pstt2 <- data.frame(
                group = rep(c("preTest","postTest"),each = 100),
                weight = c(preTest, postTest)
                )

## Runs paired-sample T-Test on the newly structured data frame
t.test(weight ~ group, data = pstt2, paired = TRUE)

此 t 检验的结果为 t 负值,这可能向用户表明所研究的变量随着时间的推移而增加。

    Paired t-test

data:  weight by group 
t = -7.1776, df = 99, p-value = 1.322e-10
alternative hypothesis: true difference in means is not equal to 0 
95 percent confidence interval:
 -11.185513  -6.340533 
sample estimates:
mean of the differences 
              -8.763023

有没有办法明确定义哪个组是 BEFORE,哪个是 AFTER?或者,在方法 2 中您是否必须首先拥有 AFTER 组。

感谢您的帮助/解释。

这是我使用的完整 R 程序:

## sets working dir
#  setwd("C:\\Temp\\")

## runs file from command line
#  source("paired_ttest.r",echo=TRUE)

## Sets seed for repetitive number generation
set.seed(2820)

## Creates the matrices
preTest <- c(rnorm(100, mean = 145, sd = 9))
postTest <- c(rnorm(100, mean = 138, sd = 8))
ID <- c(1:100)

## Converts the matrices into a dataframe that looks like the way these 
   data are normally stored
pstt <- data.frame(ID,preTest,postTest)

## Puts the data in a form that can be used by R (grouping var | data var)
pstt2 <- data.frame(
                group = rep(c("preTest","postTest"),each = 100),
                weight = c(preTest, postTest)
                )

print(pstt2)                

## Runs paired-sample T-Test just on two original matrices
#  t.test(preTest,postTest, paired = TRUE)

## Runs paired-sample T-Test on the newly structured data frame
t.test(weight ~ group, data = pstt2, paired = TRUE)

最佳答案

由于 group 是一个因素,因此 t.test 将使用该因素的第一个水平作为引用水平。默认情况下,因子水平按字母顺序排序,“AFTER”位于“BEFORE”之前,“postTest”位于“preTest”之前。您可以使用relevel()显式设置因子的引用水平。

t.test(weight ~ relevel(group, "preTest"), data = pstt2, paired = TRUE)

关于r - R 中的配对样本 t 检验 : a question of direction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53620352/

相关文章:

r - 如何将公式转换为函数,或将公式应用于某些值?

R错误: some group is too small for 'qda'

r - R 中工作区背后的哲学是什么?

r - summary() 舍入

javascript - 从两个变量计算任务的紧迫性

machine-learning - Word2Vec 本身是判别模型还是生成模型?

ruby - 如何衡量点击率的统计显着性?

r - 为GGPlot2直方图中的X值以上的任何内容创建一个bin

python - 如何用置信区间解释数据点的上限/下限?

r - 有没有办法计算 R 中多个因变量高于阈值的峰值数量?