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

查看:72
本文介绍了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?

在一些例子中,向用户展示了直观的对比系数矩阵可以直接通过contrasts()C() 函数.所以就这么简单:

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 stats)我们被告知系数矩阵(或基矩阵)在使用前必须从系数矩阵转换为对比矩阵.这涉及对系数矩阵进行逆变换:(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?

参考

推荐答案

Claim 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天全站免登陆