R 如何从SAS复制lme4中的随机效应?

R 如何从SAS复制lme4中的随机效应?,r,sas,lme4,mixed-models,R,Sas,Lme4,Mixed Models,我希望对因变量DV运行一个线性混合模型,该因变量是在三个不同的时间点在两个不同的条件下收集的。数据结构如下所示: ## dput(head(RawData,5)) structure(list(Participant = structure(c(2L, 2L, 2L, 2L, 4L), .Label = c("Jessie", "James", "Gus", "Hudson", "Flossy", "Bobby", "Thomas", "Alfie", "Charles"

我希望对因变量
DV
运行一个线性混合模型,该因变量是在三个不同的
时间点在两个不同的
条件下收集的。数据结构如下所示:

   ## dput(head(RawData,5))
    structure(list(Participant = structure(c(2L, 2L, 2L, 2L, 4L), 
  .Label = c("Jessie", "James", "Gus", "Hudson", "Flossy", 
 "Bobby", "Thomas", "Alfie", "Charles", "Will", "Mat", "Paul", "Tim", 
  "John", "Toby", "Blair"), class = "factor"), 
 xVarCondition = c(1, 1, 0, 0, 1), 
 Measure = structure(c(1L, 2L, 3L, 4L, 1L), 
.Label = c("1", "2", "3", "4", "5", "6", "7", "8", 
"9", "10", "11", "12"), class = "factor"), 
Sample = structure(c(1L, 2L, 1L, 2L, 1L), 
.Label = c("1", "2"), class = "factor"), 
 Condition = structure(c(2L, 2L, 1L, 1L, 2L),
 .Label = c("AM", "PM"), class = "factor"),
 Timepoint = structure(c(2L, 2L, 2L, 2L, 1L), 
.Label = c("Baseline", "Mid", "Post"), class = "factor"),
 DV = c(83.6381348645853, 86.9813802115179, 69.2691666620429, 
 71.3949807856125, 87.8931998204771)), 
.Names = c("Participant", "xVarCondition", "Measure", 
   "Sample", "Condition", "Timepoint", "DV"), 
 row.names = c(NA, 5L), class = "data.frame")
ifac <- with(RawData,
   interaction(Participant,xVarCondition,Measure,drop=TRUE))
length(levels(ifac)) == nrow(RawData)
每个
参与者
在三个
时间点对每个
条件
进行两次试验,如
测量
所示;但是,缺少数据,因此每个参与者不一定有12个级别。列
xVarCondition
仅仅是一个伪变量,它为条件中的每个AM条目都包含一个1。列
样本
指的是每个
时间点
的每个
条件
的2次试验

我是R用户,但统计员是SAS用户,他认为模型的代码应为:

proc mixed data=RawData covtest cl alpha=&alpha;
class Participant Condition Timepoint Measure Sample;
model &dep=Condition Timepoint/s ddfm=sat outp=pred residual noint;
random int xVarCondition xVarCondition*TimePoint*Sample 
          TimePoint/subject=Participant s;
上面的SAS代码给出了合理的答案,并且工作正常。我们认为上述模型的
lme4
语法为:

TestModel = lmer(DV ~ Condition + Timepoint + 
              (1 | Participant/Timepoint) +
              (0 + xVarCondition | Participant) +
              (1 | Participant:xVarCondition:Measure), data = RawData)
但是,运行此模型时出现以下错误:

Error: number of levels of each grouping factor must be < number of observations
错误:每个分组因子的级别数必须<观察数

随机效应的指定是否正确?

我不能从你的描述中很清楚,但最有可能的是你的
参与者:xVarCondition:Measure
术语构造了一个分组变量,在每个分类级别中不超过一个观察值,这将使
(1 |参与者:xVarCondition:Measure)
术语与残余误差术语冗余,残余误差术语始终包含在
lmer
模型中。如果确实需要,可以通过包括

control=lmerControl(check.nobs.vs.nlev = "ignore")
在函数调用中,但是(如果我正确诊断了问题),这将导致剩余方差和
参与者:xVarCondition:Measure
方差无法共同识别。这种不可识别性通常不会对模型的其余部分造成任何问题,但我更喜欢可识别模型(这种不可识别性总是有可能导致数值问题)

有一个类似的例子

你可以检查我的猜测如下:

   ## dput(head(RawData,5))
    structure(list(Participant = structure(c(2L, 2L, 2L, 2L, 4L), 
  .Label = c("Jessie", "James", "Gus", "Hudson", "Flossy", 
 "Bobby", "Thomas", "Alfie", "Charles", "Will", "Mat", "Paul", "Tim", 
  "John", "Toby", "Blair"), class = "factor"), 
 xVarCondition = c(1, 1, 0, 0, 1), 
 Measure = structure(c(1L, 2L, 3L, 4L, 1L), 
.Label = c("1", "2", "3", "4", "5", "6", "7", "8", 
"9", "10", "11", "12"), class = "factor"), 
Sample = structure(c(1L, 2L, 1L, 2L, 1L), 
.Label = c("1", "2"), class = "factor"), 
 Condition = structure(c(2L, 2L, 1L, 1L, 2L),
 .Label = c("AM", "PM"), class = "factor"),
 Timepoint = structure(c(2L, 2L, 2L, 2L, 1L), 
.Label = c("Baseline", "Mid", "Post"), class = "factor"),
 DV = c(83.6381348645853, 86.9813802115179, 69.2691666620429, 
 71.3949807856125, 87.8931998204771)), 
.Names = c("Participant", "xVarCondition", "Measure", 
   "Sample", "Condition", "Timepoint", "DV"), 
 row.names = c(NA, 5L), class = "data.frame")
ifac <- with(RawData,
   interaction(Participant,xVarCondition,Measure,drop=TRUE))
length(levels(ifac)) == nrow(RawData)

ifac我注意到你的固定效应,
条件和
时间点都是因素。你确定在这种情况下,混合线性模型是最好的方法吗?另外,我没有发现
xVarCondition
Condition
之间的区别。我认为线性混合模型是合适的,因为我们对参与者内部和之间的变化感兴趣。xVarCondition只是一个虚拟变量,每次参与者完成AM条件时都有一个1。不过,感谢您提供了类似的示例,即使我尝试覆盖错误,我仍然会收到错误消息。我是否能够指定随机效果,使其与SAS中的模型匹配?它在SAS中运行平稳,回答合理。您可以尝试r-sig-mixed-models@r-project.org(我今天可能没有时间做这个)。A也会有帮助