尝试对每一行执行t.test并计算p值小于0.05的所有行 [英] trying to perform a t.test for each row and count all rows where p-value is less than 0.05

查看:259
本文介绍了尝试对每一行执行t.test并计算p值小于0.05的所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去四个小时来,我一直在努力寻找解决R问题的方法,这正使我发疯.我到处都在寻找一个不错的答案,但到目前为止,我一直都碰壁.我现在呼吁这个优秀社区的善意寻求帮助.

I've been wrecking my head for the past four hours trying to find the solution to an R problem, which is driving me nuts. I've searching everywhere for a decent answer but so far I've been hitting wall after wall. I am now appealing to your good will of this fine community for help.

考虑以下数据集:

set.seed(2112)
DataSample <- matrix(rnorm(24000),nrow=1000)
colnames(DataSample) <- c(paste("Trial",1:12,sep=""),paste("Control",13:24,sep=""))

我需要对DataSample中的每一行执行一次t检验,以便找出TRIAL和CONTROL组是否不同(均等方差).

I need to perform a t-test for every row in DataSample in order to find out if groups TRIAL and CONTROL differ (equal variance applies).

然后,我需要计算p值等于或低于的行数.

Then I need to count the number of rows with a p-value equal to, or lower than 0.05.

这是我尝试的代码,我知道这是错误的:

So here is the code I tried, which I know is wrong:

set.seed(2112)
DataSample <- matrix(rnorm(24000),nrow=1000)
colnames(DataSample) <- c(paste("Trial",1:12,sep=""),paste("Control",13:24,sep=""))

pValResults <- apply(
  DataSample[,1:12],1,function(x) t.test(x,DataSample[,13:24], var.equal=T)$p.value
  )

sum(pValResults < 0.05) # Returns the wrong answer (so I was told)

我确实尝试过围绕stackoverflow寻找许多类似的问题,但是我经常会遇到语法错误或尺寸不匹配的情况.上面的代码是我可以获得的最好的代码,而不会返回R错误-但是由于代码返回错误的答案,我感到无比自豪.

I did try looking at many similar questions around stackoverflow, but I would often end-up with syntax errors or a dimensional mismatch. The code above is the best I could get without returning me an R error -- but I since the code is returning the wrong answer I have nothing to feel proud of.

任何建议将不胜感激!预先感谢您的宝贵时间.

Any advice will be greatly appreciated! Thanks in advance for your time.

推荐答案

一种选择是遍历计算每一行的t检验的数据集,但这并不那么优雅.

One option is to loop over the data set calculating the t test for each row, but it is not as elegant.

set.seed(2112)
DataSample <- matrix(rnorm(24000),nrow=1000)
colnames(DataSample) <- c(paste("Trial",1:12,sep=""),paste("Control",13:24,sep=""))

# initialize vector of stored p-values
pvalue <- rep(0,nrow(DataSample))

for (i in 1:nrow(DataSample)){
   pvalue[i] <- t.test(DataSample[i,1:12],DataSample[i,13:24])$p.value
}
# finding number that are significant
sum(pvalue < 0.05)

这篇关于尝试对每一行执行t.test并计算p值小于0.05的所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆