Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用R中的t统计量创建矩阵_R_Loops_T Test - Fatal编程技术网

用R中的t统计量创建矩阵

用R中的t统计量创建矩阵,r,loops,t-test,R,Loops,T Test,我有一个面板数据集,希望创建一个类似于相关矩阵的矩阵,但只包含t检验估计值和t统计量的差异 使用牙齿生长数据,我首先根据其剂量值对supp ID进行分组,然后我想计算亚组之间所有可能组合的t-统计量 我希望我的t检验矩阵如下所示 VC_all VC_0.5 VC_1 VC_all VC_0.5 VC_1 OJ_all OJ_0.5 OJ_1

我有一个面板数据集,希望创建一个类似于相关矩阵的矩阵,但只包含t检验估计值和t统计量的差异

使用牙齿生长数据,我首先根据其剂量值对supp ID进行分组,然后我想计算亚组之间所有可能组合的t-统计量

我希望我的t检验矩阵如下所示

          VC_all  VC_0.5     VC_1  VC_all    VC_0.5  VC_1  OJ_all  OJ_0.5  OJ_1                                                             

VC_all                                                  -4 ( -1.92 )       
VC_0.5
VC_1
VC_all
VC_0.5
VC_1
OJ_all
OJ_0.5
OJ_1
作为示例,我用以下公式填充了一个值

t_test <- t.test(x = filter(ToothGrowth, supp== "VC")$len,
                 y = filter(ToothGrowth, supp== "OJ")$len, var.equal = TRUE)
t\u测试您可以使用

# generate data
df <- data.frame(matrix(rnorm(100*3), ncol= 3))
# name data
names(df) <- c("a", "b", "c")

# or to use for your data
df <- name_of_your_dataframe

# make a dataframe for the results
results <- data.frame(matrix(rep(NA, ncol(df)*ncol(df)), ncol= ncol(df)))
# name the results dataframe
names(results) <- names(df)
rownames(results) <- names(df)
# between which columns do we need to run t-tests?
to_estimate <- t(combn(names(df), 2))
# replace upper triangle of the matrix with the results
results[upper.tri(results)] <- apply(to_estimate, 1, function(to_estimate_i){
t_results <- t.test(df[ , to_estimate_i[1]], df[ , to_estimate_i[2]])
out <-  paste0(round(t_results$estimate[1] - t_results$estimate[2], 2), " (", round(t_results$statistic, 2), ")")
})
# copy upper to lower
results[lower.tri(results)] <- results[upper.tri(results)]
#生成数据

df如果我将df替换为我的数据帧的名称,如果我没有弄错的话,我的数据帧将替换在第一行代码之后。这有什么用:
df@JjBlevins使用df我知道如何将您的代码应用于我的数据。它工作得非常好。非常感谢你的支持。你知道如何在粘贴中插入换行符吗?@JjBlevins通常带有“\n”,但我认为它不适用于数据帧。如果它不起作用,也许你可以把它当作一个新问题来问。很高兴我的代码很有用,我查看了
ToothGrowth
数据。我没有找到你写的名字(VC_0.5,…),VC组和OJ组都可以根据剂量值进一步分组。可以是0.5和1.0
# generate data
df <- data.frame(matrix(rnorm(100*3), ncol= 3))
# name data
names(df) <- c("a", "b", "c")

# or to use for your data
df <- name_of_your_dataframe

# make a dataframe for the results
results <- data.frame(matrix(rep(NA, ncol(df)*ncol(df)), ncol= ncol(df)))
# name the results dataframe
names(results) <- names(df)
rownames(results) <- names(df)
# between which columns do we need to run t-tests?
to_estimate <- t(combn(names(df), 2))
# replace upper triangle of the matrix with the results
results[upper.tri(results)] <- apply(to_estimate, 1, function(to_estimate_i){
t_results <- t.test(df[ , to_estimate_i[1]], df[ , to_estimate_i[2]])
out <-  paste0(round(t_results$estimate[1] - t_results$estimate[2], 2), " (", round(t_results$statistic, 2), ")")
})
# copy upper to lower
results[lower.tri(results)] <- results[upper.tri(results)]