R 体素明智的Mann-Whitney U/独立样本T检验
我试图用R,以体素的方式,计算和显示Mann-Whitney U测试中的p值,这类似于更熟悉的独立样本t测试,在.nii文件数据上。我从Joset a.Etzel在2014年写的博客中找到了一个很好的演示代码,可以用于一个样本t测试。该代码的摘录如下所示:R 体素明智的Mann-Whitney U/独立样本T检验,r,arrays,nifti,R,Arrays,Nifti,我试图用R,以体素的方式,计算和显示Mann-Whitney U测试中的p值,这类似于更熟悉的独立样本t测试,在.nii文件数据上。我从Joset a.Etzel在2014年写的博客中找到了一个很好的演示代码,可以用于一个样本t测试。该代码的摘录如下所示: library(oro.nifti) library(plyr) rm(list=ls()) in.path <- "VoxelThings/" # where the input NIfTI image
library(oro.nifti)
library(plyr)
rm(list=ls())
in.path <- "VoxelThings/" # where the input NIfTI images are
out.path <- "VoxelThings/demo_outputs/" # where to write the NIfTI t-value image
sub.ids <- c("a", "b", "c", "d") # subject ID codes, corresponding to the input images
img.dims <- c(91,109,91) # expected image dimensions. it would be more elegant coding to read the size out of the input images.
getT <- function(x) { # function to calculate the t-test at each voxel and return the t value
# we can't calculate a t-test if variance is zero, so check before trying.
if (var(x) == 0) { stat <- NA; }
else { stat <- t.test(x, alternative="greater", mu=0.5)$p.value; }
return(stat)
}
# load the images for each person into a big data structure
big <- array(NA, c(img.dims, length(sub.ids))); # 3d array for each person, 4th dimension are the subjects
for (sid in 1:length(sub.ids)) { # sid <- 1;
img <- readNIfTI(paste0(in.path, sub.ids[sid], "_demo.nii.gz"));
if (dim(img)[1] != img.dims[1] | dim(img)[2] != img.dims[2] | dim(img)[3] != img.dims[3]) { stop("not the expected dimensions"); }
big[,,,sid] <- img;
}
big.out <- aaply(big, c(1,2,3), getT); # this line calls the t-test function at each voxel. aaply isn't particularly fast, but is easy to code.
if (dim(big.out)[1] != img.dims[1] | dim(big.out)[2] != img.dims[2] | dim(big.out)[3] != img.dims[3]) { stop("not the expected big.out dims"); }
或者在我真正想要的Mann-Whitney U测试中,它是这样的:
wilcox.test(dependent_variable ~ grouping_variable, data = df, exact = TRUE, correct = TRUE)
这两种测试都需要将数据指定到一个组中;例如,a&b可以与c&d相比较——我在这里怎么做?
非常感谢您的帮助,谢谢
wilcox.test(dependent_variable ~ grouping_variable, data = df, exact = TRUE, correct = TRUE)