R中的自定义对比度:对比度系数矩阵还是对比度矩阵/编码方案?以及如何到达那里? [英] Custom contrasts in R: contrast coefficient matrix or contrast matrix / coding scheme? And how to get there?

查看:228
本文介绍了R中的自定义对比度:对比度系数矩阵还是对比度矩阵/编码方案?以及如何到达那里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自定义对比度在分析中非常广泛地使用,例如:此三级因子的1级和3级DV值是否显着不同?"

Custom contrasts are very widely used in analyses, e.g.: "Do DV values at level 1 and level 3 of this three-level factor differ significantly?"

直觉上,这种对比用单元均值表示为:

Intuitively, this contrast is expressed in terms of cell means as:

c(1,0,-1)

这些对比度中的一个或多个(绑定为列)形成一个对比度系数矩阵,例如.

One or more of these contrasts, bound as columns, form a contrast coefficient matrix, e.g.

mat = matrix(ncol = 2, byrow = TRUE, data = c(
    1,  0,
    0,  1,
   -1, -1)
)
     [,1] [,2]
[1,]    1    0
[2,]    0    1
[3,]   -1   -1

但是,在运行由系数矩阵指定的这些对比时,网络和书籍中都有很多(显然是矛盾的)信息.我的问题是哪些信息正确?

However, when it comes to running these contrasts specified by the coefficient matrix, there is a lot of (apparently contradictory) information on the web and in books. My question is which information is correct?

在某些示例中,向用户显示可以直接通过

In some examples, the user is shown that the intuitive contrast coefficient matrix can be used directly via the contrasts() or C() functions. So it's as simple as:

contrasts(myFactor) <- mat

声明2:变换系数以创建编码方案

在其他地方(例如 UCLA统计),我们被告知使用前必须将系数矩阵(或基本矩阵)从系数矩阵转换为对比度矩阵.这涉及对系数矩阵的变换取反:(mat')⁻¹,或在Rish中:

Claim 2: Transform coefficients to create a coding scheme

Elsewhere (e.g. UCLA stats) we are told the coefficient matrix (or basis matrix) must be transformed from a coefficient matrix into a contrast matrix before use. This involves taking the inverse of the transform of the coefficient matrix: (mat')⁻¹, or, in Rish:

contrasts(myFactor) = solve(t(mat))

此方法要求使用用于拦截的均值的初始列填充矩阵.为了避免这种情况,一些网站建议使用广义逆函数,该函数可以处理非平方矩阵,即MASS::ginv()

This method requires padding the matrix with an initial column of means for the intercept. To avoid this, some sites recommend using a generalized inverse function which can cope with non-square matrices, i.e., MASS::ginv()

contrasts(myFactor) = ginv(t(mat))

第三种选择:通过变换进行预乘,取反,然后通过变换进行后乘

再次在其他地方(例如 SPSS支持中的注释) ,我们知道正确的代数是:(mat'mat)-¹ mat'

Third option: premultiply by the transform, take the inverse, and post multiply by the transform

Elsewhere again (e.g. a note from SPSS support), we learn the correct algebra is: (mat'mat)-¹ mat'

对我来说,创建对比度矩阵的正确方法应该是:

Implying to me that the correct way to create the contrasts matrix should be:

x = solve(t(mat)%*% mat)%*% t(mat)
     [,1] [,2] [,3]
[1,]    0    0    1
[2,]    1    0   -1
[3,]    0    1   -1

contrasts(myFactor) = x

我的问题是,对吗? (如果我要准确地解释和描述每条建议).如何在R中为lmlme等指定自定义对比度?

My question is, which is right? (If I am interpreting and describing each piece of advice accurately). How does one specify custom contrasts in R for lm, lme etc?

参考

推荐答案

声明2是正确的(请参阅答案此处),有时也会要求1.这是因为在某些情况下,(转置的)系数矩阵的广义逆等于矩阵本身.

Claim 2 is correct (see the answers here and here) and sometimes claim 1, too. This is because there are cases in which the generalized inverse of the (transposed) coefficient matrix is equal to the matrix itself.

这篇关于R中的自定义对比度:对比度系数矩阵还是对比度矩阵/编码方案?以及如何到达那里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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