使用具有用户定义对比度的nparcomp

使用具有用户定义对比度的nparcomp,r,R,我想用以下数据检验假设H0:a2-a1=b2-b1: data.csv a1,a2,b1,b2 0.439,0.066,0.0,0.001 0.451,0.07,0.0,0.0 0.446,0.06,0.0,0.0 0.34,0.056,0.0,0.0 0.294,0.008,0.0,0.0 0.284,0.002,0.001,0.0 1.0,1.0,0.002,0.0 首先,我尝试了方差分析: library(car) data = read.csv('data.csv') labels =

我想用以下数据检验假设
H0:a2-a1=b2-b1

data.csv

a1,a2,b1,b2
0.439,0.066,0.0,0.001
0.451,0.07,0.0,0.0
0.446,0.06,0.0,0.0
0.34,0.056,0.0,0.0
0.294,0.008,0.0,0.0
0.284,0.002,0.001,0.0
1.0,1.0,0.002,0.0
首先,我尝试了方差分析:

library(car)
data = read.csv('data.csv')
labels = factor(rep(c('a1','a2','b1','b2'), 
                c(nrow(data['a1']), nrow(data['a2']), nrow(data['b1']), nrow(data['b2'])))) 
x = C(labels, c(-1, 1, 1, -1), 1) 
y = c(data[['a1']], data[['a2']], data[['b1']], data[['b2']]) 
l = lm(y ~ x) 
a = Anova(l)
print(a$`Pr(>F)`)
给出了
p=0.1891837

但是,由于我不能假设数据来自正态分布,因此我想使用非参数测试。我尝试使用用户定义的对比度矩阵:

library(nparcomp)
data = read.csv('data.csv')
x = factor(rep(c('a1','a2','b1','b2'), 
                c(nrow(data['a1']), nrow(data['a2']), nrow(data['b1']), nrow(data['b2'])))) 
y = c(data[['a1']], data[['a2']], data[['b1']], data[['b2']]) 
nparcomp(y ~ x, data=data.frame(x, y), type="UserDefined", contrast.matrix=t(matrix(c(-1, 1, 1, -1))))
此操作失败,错误为:

Error in nparcomp(y ~ x, data = data.frame(x, y), type =
   "UserDefined",  :   Sums of positive contrast coefficients must be 1!
我通过将对比度矩阵重新调整为
t(矩阵(c(-0.5,0.5,0.5,-0.5))
来修正这个问题。但是,此操作失败,出现另一个错误:

Error in logit.dev %*% cov.bf : non-conformable arguments
使用具有用户定义对比度的nparcomp的正确方法是什么?


另外,我是R的新手,因此对糟糕的编码风格表示歉意。

让我先说一句,我对这一特定的统计领域一点也不熟悉,所以尽管我的代码运行时没有错误,但我的修改可能完全改变了您尝试测试的设计-如果是这样,请告诉我。无论如何,我查看了nparcomp的定义,看看是什么导致了第一个错误,
正对比系数之和必须是1-

if (type == "UserDefined") {
        if (is.null(contrast.matrix)) {
            stop("Please eanter a contrast matrix!")
        }
        Con <- contrast.matrix
        rownames(Con) <- paste("C", 1:nrow(Con))
        for (rc in 1:nrow(Con)) {
            if (sum(Con[rc, ][Con[rc, ] > 0]) != 1) {
                stop("Sums of positive contrast coefficients must be 1!")
            }
        }
        colnames(Con) <- fl
}
将此传递给对比度矩阵不会产生任何错误,但正如我所说的,我不熟悉这个特定的过程/函数,因此我可能完全改变了您尝试测试的假设。以下是完整的代码:

library(car)
library(nparcomp)
##
data <- data.frame(
  a1=c(.439,.451,.446,
       .340,.294,.284,1.00),
  a2=c(.066,.07,.06,.056,
       .008,.002,1.00),
  b1=c(rep(0.0,5),
       .001,.002),
  b2=c(.001,
       rep(0.0,6)))
##
x <- factor(
  rep(names(data),
      each=nrow(data)))
##
y <- c(data[['a1']], data[['a2']], 
       data[['b1']], data[['b2']])
##
contrVec <- t(matrix(c(-1, 1, 1, -1)))
contrMat <- -1*(matrix(
  outer(contrVec,
        t(contrVec)),
  ncol=4)/2)
##
nprce <- nparcomp(
  y ~ x, 
  data=data.frame(x, y), 
  type="UserDefined", 
  contrast.matrix=contrMat)
##
> summary(nprce)

 #------------Nonparametric Multiple Comparisons for relative contrast effects----------# 

 - Alternative Hypothesis:  True relative contrast effect p is less or equal than 1/2 
 - Estimation Method: Global Pseudo ranks 
 - Type of Contrast : UserDefined 
 - Confidence Level: 95 % 
 - Method = Logit - Transformation 

 - Estimation Method: Pairwise rankings 

 #---------------------------Interpretation--------------------------------------------# 
 p(a,b) > 1/2 : b tends to be larger than a 
 #-------------------------------------------------------------------------------------# 

 #----Data Info-------------------------------------------------------------------------# 
  Sample Size
1     a1    7
2     a2    7
3     b1    7
4     b2    7

 #----Contrast--------------------------------------------------------------------------# 
      a1   a2   b1   b2
C 1 -0.5  0.5  0.5 -0.5
C 2  0.5 -0.5 -0.5  0.5
C 3  0.5 -0.5 -0.5  0.5
C 4 -0.5  0.5  0.5 -0.5

 #----Analysis--------------------------------------------------------------------------# 
  Comparison Estimator Lower Upper Statistic   p.Value
1        C 1     0.429 0.345 0.517 -1.593593 0.1110273
2        C 2     0.571 0.483 0.655  1.593593 0.1110273
3        C 3     0.571 0.483 0.655  1.593593 0.1110273
4        C 4     0.429 0.345 0.517 -1.593593 0.1110273

 #----Overall---------------------------------------------------------------------------# 
  Quantile   p.Value
1 1.959966 0.1110273

 #--------------------------------------------------------------------------------------# 
库(车)
图书馆(nparcomp)
##

数据编码风格不错,但是有很多语法错误。在第一段代码中,进行方差分析,在
library(car)
print(a$Pr(>F))
之间,你到底想做什么;将列组合成串联向量
y
;无论
y
的每个元素是否来自a1、a2、b1或b2,创建与
y
标签长度相同的系数
标签
;使用对比矩阵
c(-1,1,1,-1)
(即-a1+a2+b2-b1=0)从
标签中创建对比度
x
;根据
x
y
建立线性模型
l
,并根据该模型计算方差分析p值。代码松散地基于。它运行时没有任何警告,所以我很惊讶你提到了语法错误。对每一个使用相同的对比度(例如
t(matrix(replicate(4,c(-0.5,0.5,0.5,-0.5)),ncol=4))
)也可以。这是一个奇怪的怪癖。谢谢你的回答!不客气,我很高兴你能成功。我同意,这似乎有点奇怪,我很惊讶作者没有在帮助文件中包含使用
UserDefined
对比的示例。
library(car)
library(nparcomp)
##
data <- data.frame(
  a1=c(.439,.451,.446,
       .340,.294,.284,1.00),
  a2=c(.066,.07,.06,.056,
       .008,.002,1.00),
  b1=c(rep(0.0,5),
       .001,.002),
  b2=c(.001,
       rep(0.0,6)))
##
x <- factor(
  rep(names(data),
      each=nrow(data)))
##
y <- c(data[['a1']], data[['a2']], 
       data[['b1']], data[['b2']])
##
contrVec <- t(matrix(c(-1, 1, 1, -1)))
contrMat <- -1*(matrix(
  outer(contrVec,
        t(contrVec)),
  ncol=4)/2)
##
nprce <- nparcomp(
  y ~ x, 
  data=data.frame(x, y), 
  type="UserDefined", 
  contrast.matrix=contrMat)
##
> summary(nprce)

 #------------Nonparametric Multiple Comparisons for relative contrast effects----------# 

 - Alternative Hypothesis:  True relative contrast effect p is less or equal than 1/2 
 - Estimation Method: Global Pseudo ranks 
 - Type of Contrast : UserDefined 
 - Confidence Level: 95 % 
 - Method = Logit - Transformation 

 - Estimation Method: Pairwise rankings 

 #---------------------------Interpretation--------------------------------------------# 
 p(a,b) > 1/2 : b tends to be larger than a 
 #-------------------------------------------------------------------------------------# 

 #----Data Info-------------------------------------------------------------------------# 
  Sample Size
1     a1    7
2     a2    7
3     b1    7
4     b2    7

 #----Contrast--------------------------------------------------------------------------# 
      a1   a2   b1   b2
C 1 -0.5  0.5  0.5 -0.5
C 2  0.5 -0.5 -0.5  0.5
C 3  0.5 -0.5 -0.5  0.5
C 4 -0.5  0.5  0.5 -0.5

 #----Analysis--------------------------------------------------------------------------# 
  Comparison Estimator Lower Upper Statistic   p.Value
1        C 1     0.429 0.345 0.517 -1.593593 0.1110273
2        C 2     0.571 0.483 0.655  1.593593 0.1110273
3        C 3     0.571 0.483 0.655  1.593593 0.1110273
4        C 4     0.429 0.345 0.517 -1.593593 0.1110273

 #----Overall---------------------------------------------------------------------------# 
  Quantile   p.Value
1 1.959966 0.1110273

 #--------------------------------------------------------------------------------------#