在MCMCglmm中指定先验

时间:2016-01-30 15:15:46

标签: r

我已经花了好几个小时研究这个问题而且我自己无法解决问题。非常感谢任何帮助。

对于我的因变量(Outcome),我收集了4个试验中每个受试者的4个回答。每个试验都编码为POSITIVENEGATIVEAMBIVALENT。自变量:ConditionGenderAge.Range各有2个级别。

这是我数据的一个子集:

Subject Trial   Outcome Age.Range Gender Condition
PHIL060     1      AMBI         Y      2         1
PHIL060     2  NEGATIVE         Y      2         1
PHIL060     3      AMBI         Y      2         1
PHIL060     4      AMBI         Y      2         1
PHIL056     1  POSITIVE         Y      1         2
PHIL056     2      <NA>         Y      1         2
PHIL056     3  POSITIVE         Y      1         2
PHIL056     4  POSITIVE         Y      1         2
PHIL057     1  NEGATIVE         Y      1         1
PHIL057     2  NEGATIVE         Y      1         1
PHIL057     3  NEGATIVE         Y      1         1
PHIL057     4  NEGATIVE         Y      1         1
PHIL028     1  POSITIVE         Y      1         2
PHIL028     2  NEGATIVE         Y      1         2
PHIL028     3  NEGATIVE         Y      1         2
PHIL028     4  NEGATIVE         Y      1         2
PHIL007     1  POSITIVE         Y      1         2
PHIL007     2  NEGATIVE         Y      1         2
PHIL007     3  NEGATIVE         Y      1         2
PHIL007     4  NEGATIVE         Y      1         2
PHIL109     1      AMBI         Y      2         1
PHIL109     2      AMBI         Y      2         1
PHIL109     3      AMBI         O      2         1
PHIL109     4      AMBI         O      2         1
PHIL031     1  NEGATIVE         O      2         2
PHIL031     2  NEGATIVE         O      2         2 
PHIL031     3  NEGATIVE         O      2         2
PHIL031     4  NEGATIVE         O      2         2
PHIL032     1  NEGATIVE         O      2         2
PHIL032     2  NEGATIVE         O      2         2
PHIL032     3  NEGATIVE         O      2         2
PHIL032     4  POSITIVE         O      2         2
PHIL042     1  NEGATIVE         1      1         2
PHIL042     2  NEGATIVE         1      1         2
PHIL042     3  NEGATIVE         1      1         2
PHIL042     4  NEGATIVE         1      1         2
PHIL100     1  NEGATIVE         1      1         1
PHIL100     2  NEGATIVE         1      1         1
PHIL100     3  NEGATIVE         1      1         1
PHIL100     4  NEGATIVE         1      1         1
PHIL017     1  POSITIVE         1      1         1
PHIL017     2  POSITIVE         1      1         1
PHIL017     3  POSITIVE         1      1         1
PHIL017     4  NEGATIVE         1      1         1
PHIL018     1  NEGATIVE         1      1         1
PHIL018     2  NEGATIVE         1      1         1
PHIL018     3      AMBI         1      1         1
PHIL018     4  NEGATIVE         1      1         1
PHIL020     1  NEGATIVE         1      2         2
PHIL020     2  NEGATIVE         1      2         2
PHIL020     3  NEGATIVE         1      2         2
PHIL020     4  NEGATIVE         1      2         2
PHIL043     1  NEGATIVE         1      1         2
PHIL043     2  NEGATIVE         1      1         2
PHIL043     3  NEGATIVE         1      1         2
PHIL043     4  NEGATIVE         1      1         2
PHIL078     1  NEGATIVE         1      1         2
PHIL078     2  POSITIVE         1      1         2
PHIL078     3  POSITIVE         1      1         2
PHIL078     4  POSITIVE         1      1         2

我正在尝试MCMCglmm功能:

AP.PHIL <- read.csv ("AP_PHIL.csv")
library(MCMCglmm)
k <- length(levels(my.data$Outcome))
I <- diag(k-1)
J <- matrix(rep(1, (k-1)^2), c(k-1, k-1))

m <- MCMCglmm(Outcome ~ Condition*Age.Range + Condition*Gender, 
random = ~ Subject,
rcov = ~ us(trait):units,
prior = list(
R = list(fix=1, V=0.5 * (I + J), n = 3),
G = list(G1 = list(V = diag(3), n = 3))),
      burnin = 15000,
      nitt = 40000,
      family = "categorical",
      data = my.data)

我不断收到此错误消息:

  

在先格式化中出错(if(NOpriorG){:       对于某些先前的$ G /先前$ R元素,V是错误的维度

有人可以告诉我如何最好地编辑我的先辈吗?

1 个答案:

答案 0 :(得分:5)

看起来您的分组(G)先前应该只是1x1矩阵/标量,而不是3x3,所以

G = list(G1 = list(V = diag(1), n = 3)))

应该有用。

事后看来这很清楚,因为你有一个简单的(仅限拦截)分组因子模型(因此随机效应只有一个方差),但我想出来的方法是查看错误信息,猜测我需要debug(MCMCglmm:::priorformat),然后单步执行该功能直到

if (any(dim(prior$V) != sum(nfl))) {
    stop("V is the wrong dimension for some prior$G/prior$R elements")
}
## Browse[2]> dim(prior$V)
## [1] 3 3
## Browse[2]> sum(nfl)
## [1] 1

如果将错误消息修改为更具信息性,则可能会有所帮助,例如

if (any(dim(prior$V) != sum(nfl))) {
    stop("V is the wrong dimension for ",
         "some prior$G/prior$R elements: (G element dimension=(",
         paste(dim(prior$V),collapse=","),
         "), proper dimension=",sum(nfl),")")

}