我知道这很晚了,但我也找不到Mood中位数测试的好软件包,因此我自己决定在R中创建一个函数似乎可以解决问题。
#Mood's median test for a data frame with one column containing data (d),
#and another containing a factor/grouping variable (f)
moods.median = function(d,f) {
#make a new matrix data frame
m = cbind(f,d)
colnames(m) = c("group", "value")
#get the names of the factors/groups
facs = unique(f)
#count the number of factors/groups
factorN = length(unique(f))
#Make a 2 by K table that will be saved to the global environment by using "<<-":
#2 rows (number of values > overall median & number of values <= overall median)
#K-many columns for each level of the factor
MoodsMedianTable <<- matrix(NA, nrow = 2, ncol = factorN)
rownames(MoodsMedianTable) <<- c("> overall median", "<= overall median")
colnames(MoodsMedianTable) <<- c(facs[1:factorN])
colnames(MoodsMedianTable) <<- paste("Factor: ",colnames(MoodsMedianTable))
#get the overall median
overallmedian = median(d)
#put the following into the 2 by K table:
for(j in 1:factorN){ #for each factor level
g = facs[j] #assign a temporary "group name"
#count the number of observations in the factor that are greater than
#the overall median and save it to the table
MoodsMedianTable[1,j] <<- sum(m[,2][ which(m[,1]==g)] > overallmedian)
#count the number of observations in the factor that are less than
# or equal to the overall median and save it to the table
MoodsMedianTable[2,j] <<- sum(m[,2][ which(m[,1]==g)] <= overallmedian)
}
#percent of cells with expected values less than 5
percLT5 = ((sum(chisq.test(MoodsMedianTable)$expected < 5)) /
(length(chisq.test(MoodsMedianTable)$expected)))
#if >20% of cells have expected values less than 5
#then give chi-squared stat, df, and Fisher's exact p.value
if (percLT5 > 0.2) {
return(list(
"Chi-squared" = chisq.test(MoodsMedianTable)$statistic,
"df" = chisq.test(MoodsMedianTable)$parameter,
"Fisher's exact p.value" = fisher.test(MoodsMedianTable)$p.value))
}
#if <= 20% of cells have expected values less than 5
#then give chi-squared stat, df, and chi-squared p.value
if (percLT5 <= 0.2) {
return(list(
"Chi-squared" = chisq.test(MoodsMedianTable)$statistic,
"df" = chisq.test(MoodsMedianTable)$parameter,
"Chi-squared p.value" = chisq.test(MoodsMedianTable)$p.value))
}
}
对于OP的问题,您首先需要运行它以创建一个新的数据框,以保存来自三个具有匹配的“组”变量的组向量的值。
require(reshape2)
df = cbind(group1, group2, group3)
df = melt(df)
colnames(df) = c("observation", "group", "value")
并使用以下命令运行Mood的中位数测试功能 moods.median(df$value, df$group)
median test
。我自己的答案/评论在这里。