Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
glmer()以一个因子级别作为基线收敛,但在将基线重新调整到另一个级别时无法收敛_R_Logistic Regression_Lme4_Mixed Models_Convergence - Fatal编程技术网

glmer()以一个因子级别作为基线收敛,但在将基线重新调整到另一个级别时无法收敛

glmer()以一个因子级别作为基线收敛,但在将基线重新调整到另一个级别时无法收敛,r,logistic-regression,lme4,mixed-models,convergence,R,Logistic Regression,Lme4,Mixed Models,Convergence,背景:我正在拟合一个具有两个固定效应和两个交叉随机效应的逻辑混合模型。构成固定效应的两个变量都是二进制分类变量(即,没有数值预测) 这些数据来自眼动跟踪器,随着时间的推移,每个对象都有数千个0和1。我想考虑的一个模型是一个Logistic混合模型,考虑了个体内的0和1s的相关性,以及在给定的时间内个体正在观察的图像(在整个实验中,他们从12种可能的图像中看六个不同的图像)。(顺便说一句,我确实计划用增长曲线分析来分析这些数据,但我也很想尝试一下,暂时忽略时间过程。) 问题:我使用两个二进制预测值

背景:我正在拟合一个具有两个固定效应和两个交叉随机效应的逻辑混合模型。构成固定效应的两个变量都是二进制分类变量(即,没有数值预测)

这些数据来自眼动跟踪器,随着时间的推移,每个对象都有数千个0和1。我想考虑的一个模型是一个Logistic混合模型,考虑了个体内的0和1s的相关性,以及在给定的时间内个体正在观察的图像(在整个实验中,他们从12种可能的图像中看六个不同的图像)。(顺便说一句,我确实计划用增长曲线分析来分析这些数据,但我也很想尝试一下,暂时忽略时间过程。)

问题:我使用两个二进制预测值的虚拟编码运行模型,每个预测值都有默认的基线水平。模型收敛了。根据我已经做过的数据图,这也是有意义的:估计值在预期的方向上,固定效应的显著性如预期的那样,等等

然后,我想用另一个基线水平上的一个预测值对重新参数化的模型进行拟合,以获得相关的p值(参见,例如:)。我使用了
relevel()
来实现这一点,然后再次安装完全相同的型号。然而,这一次出现了趋同警告。我又试了一次(因为为什么不试),但又失败了。我想不出为什么基线水平会影响趋同(或缺乏趋同)

数据结构:这是一个小的虚拟数据集,显示数据的一般结构(比实际数据集小得多,每个图像每个个体约有5000个观察值(0和1),总长度超过100万行(实际数据集中约有60个受试者))。注意,这里的模型中没有时间戳,因为我在分析中没有考虑时间过程。AOI_look中的“AOI”代表“感兴趣区域”,这是眼球跟踪数据中的一个常用术语

structure(list(Subject = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), Image = c("A", "A", 
"A", "B", "B", "B", "C", "C", "C", "D", "D", "D", "E", "E", "E", 
"F", "F", "F", "G", "G", "G", "E", "E", "E", "D", "D", "D", "A", 
"A", "A", "J", "J", "J", "L", "L", "L"), Time_stamp = c(0L, 1L, 
10L, 0L, 8L, 16L, 0L, 7L, 16L, 0L, 1L, 10L, 0L, 8L, 16L, 0L, 
7L, 16L, 0L, 8L, 16L, 0L, 7L, 16L, 0L, 1L, 9L, 0L, 8L, 16L, 0L, 
8L, 16L, 0L, 1L, 9L), Intervention = c("Pre", "Pre", "Pre", "Pre", 
"Pre", "Pre", "Pre", "Pre", "Pre", "Post", "Post", "Post", "Post", 
"Post", "Post", "Post", "Post", "Post", "Pre", "Pre", "Pre", 
"Pre", "Pre", "Pre", "Pre", "Pre", "Pre", "Post", "Post", "Post", 
"Post", "Post", "Post", "Post", "Post", "Post"), Sex = c("Female", 
"Female", "Female", "Female", "Female", "Female", "Female", "Female", 
"Female", "Female", "Female", "Female", "Female", "Female", "Female", 
"Female", "Female", "Female", "Male", "Male", "Male", "Male", 
"Male", "Male", "Male", "Male", "Male", "Male", "Male", "Male", 
"Male", "Male", "Male", "Male", "Male", "Male"), AOI_look = c(1L, 
1L, 1L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 
1L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 
1L, 1L, 1L)), class = "data.frame", row.names = c(NA, -36L))

模型1:干预=作为基线发布:无收敛警告

logit_model01 <- glmer(AOI_look ~ factor(Sex)*factor(Intervention) + 
                                  (1 | Image) + 
                                  (1 | Subject), 
                       data = data01, 
                       family="binomial")
虽然出现了警告,但我仍然能够通过
summary()
获得型号2的输出:

摘要(logit\u model02)
广义线性混合模型的最大似然拟合(拉普拉斯近似)[glmerMod]
家庭:二项式(logit)
公式:AOI|U外观~因子(性别)*因子(干预)+(1|图像)+
(1 |主题)
资料:data01
AIC BIC logLik偏差df.resid
1519023.6 1519095.5 -759505.8 1519011.6   1182007 
标度残差:
最小1季度中值3季度最大值
-2.6200 -0.9733  0.5504  0.8444  2.2317 
随机效应:
组名为Variance Std.Dev。
主题(截距)0.2320 0.4816
图像(截距)0.1318 0.3631
obs数量:1182013,分组:受试者,59;图9
固定效果:
估计标准误差z值Pr(>z)
(截距)0.309400 0.038106 8.120 4.68e-16***
因子(性别)男性-0.0325220.042520-0.7650.444
因子(干预)后-0.257262 0.006158-41.774<2e-16***
因子(性别)男性:因子(干预)后0.220234 0.007926 27.787<2e-16***
---
签名。代码:0'***'0.001'***'0.01'*'0.05'.'0.1''1
固定效应的相关性:
(Intr)fc(S)M fc(I)P
fctr(性别)M-0.107
fctr(内部)Pst 0.009 0.024
f(S)M:(I)P-0.013-0.025-0.748
收敛代码:0
模型未能收敛到最大|梯度|=0.00210935(tol=0.002,组件1)

为清晰/完整起见,添加了本·博尔克答案下方评论中的信息:

模型3:与模型1相同,但基于聚合数据(0和1按主题和图像组合总结为成功和失败):

尽管模型1没有收敛警告,但结果与模型1的结果不一致(适用于非聚集的0/1数据)。也就是说,所有的点估计都是相似的,但并非所有的SEs都是相似的:交互和干预(受试者内部)的SEs是相似的,但性别(受试者之间)和截距的SEs是显著不同的:

logit_model03 <- glmer(cbind(Successes, Failures) ~ factor(Sex)*factor(Intervention) +
                                                    (1 | Image) + 
                                                    (1 | Subject), 
                       data = data01_agg, 
                       family="binomial")

summary(logit_model03)

Fixed effects:
                                         Estimate Std. Error z value Pr(>|z|)    
(Intercept)                              0.052151   0.150564   0.346    0.729    
factor(Sex)Male                          0.187734   0.125563   1.495    0.135    
factor(Intervention)Pre                  0.257261   0.006227  41.314   <2e-16 ***
factor(Sex)Male:factor(Intervention)Pre -0.220239   0.008061 -27.322   <2e-16 ***
logit|u model03|z|)
(截距)0.052151 0.150564 0.346 0.729
因子(性别)男性0.187734 0.125563 1.495 0.135

因子(干预)0.257261之前0.006227 41.314重新参数化一个因子以获得不同的参考水平,对模型的收敛性和结果的解释都没有影响

首先考虑结果,
干预
是一个两级因素。因此,
重新排序
的效果是改变相应参数估计的符号,从而使
Pre
成为参考水平(显然是明智的做法)。(因此,在您的结果中,
0.257262
更改为
-0.257262
)其他任何内容都不应更改:p值、std错误等都保持不变。不仅仅是干预,而是模型中的每一个术语

事实上,不仅事情会发生变化,而且模型也无法收敛,这表明你的模型有问题

logit_model <- glmer(AOI_look ~ factor(Sex)*factor(Intervention) + 
                                (1 | Image) + 
                                (1 | Subject)
logit\u模型不将收敛性检查作为全部和结束检查
虽然从理论上讲,调整(如更改随机项的顺序、重新调整因子、居中/缩放等)根本不应改变模型拟合的结果,但它们确实会稍微改变线性代数,因此可能会使模型诊断越过一个阈值,从而报告不收敛。要记住的主要一点是,对mo的警告
Warning message:
In checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv,  :
  Model failed to converge with max|grad| = 0.00210935 (tol = 0.002, component 1)
summary(logit_model02)

Generalized linear mixed model fit by maximum likelihood (Laplace Approximation) [glmerMod]
Family: binomial  ( logit )
Formula: AOI_look ~ factor(Sex) * factor(Intervention) + (1 | Image) +  
         (1 | Subject)
Data: data01

      AIC       BIC    logLik  deviance  df.resid 
1519023.6 1519095.5 -759505.8 1519011.6   1182007 

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-2.6200 -0.9733  0.5504  0.8444  2.2317 

Random effects:
 Groups    Name        Variance Std.Dev.
 Subject   (Intercept) 0.2320   0.4816  
 Image     (Intercept) 0.1318   0.3631  
Number of obs: 1182013, groups:  Subject, 59; Image, 9

Fixed effects:
                                          Estimate Std. Error z value Pr(>|z|)    
(Intercept)                               0.309400   0.038106   8.120 4.68e-16 ***
factor(Sex)Male                          -0.032522   0.042520  -0.765    0.444    
factor(Intervention)Post                 -0.257262   0.006158 -41.774  < 2e-16 ***
factor(Sex)Male:factor(Intervention)Post  0.220234   0.007926  27.787  < 2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Correlation of Fixed Effects:
              (Intr) fc(S)M fc(I)P
fctr(Sex)M    -0.107               
fctr(Int)Pst   0.009  0.024        
f(S)M:(I)P    -0.013 -0.025  -0.748
convergence code: 0
Model failed to converge with max|grad| = 0.00210935 (tol = 0.002, component 1)
logit_model03 <- glmer(cbind(Successes, Failures) ~ factor(Sex)*factor(Intervention) +
                                                    (1 | Image) + 
                                                    (1 | Subject), 
                       data = data01_agg, 
                       family="binomial")

summary(logit_model03)

Fixed effects:
                                         Estimate Std. Error z value Pr(>|z|)    
(Intercept)                              0.052151   0.150564   0.346    0.729    
factor(Sex)Male                          0.187734   0.125563   1.495    0.135    
factor(Intervention)Pre                  0.257261   0.006227  41.314   <2e-16 ***
factor(Sex)Male:factor(Intervention)Pre -0.220239   0.008061 -27.322   <2e-16 ***
logit_model04 <- glmer(AOI_look ~ factor(Sex)*factor(Intervention) + 
                                  (1 | Image) + 
                                  (1 | Subject), 
                       data = smaller_raw_data, 
                       family="binomial")

summary(logit_model04)

Fixed effects:
                                        Estimate Std. Error z value Pr(>|z|)    
(Intercept)                             -0.03212    0.15564  -0.206 0.836505    
factor(Sex)Male                          0.28384    0.14121   2.010 0.044429 *  
factor(Intervention)Pre                  0.22727    0.06449   3.524 0.000425 ***
factor(Sex)Male:factor(Intervention)Pre -0.25061    0.08410  -2.980 0.002884 ** 
logit_model05 <- glmer(cbind(Successes, Failures) ~ factor(Sex)*factor(Intervention) + 
                                                    (1 | Image) + 
                                                    (1 | Subject), 
                       data = smaller_agg_data, 
                       family="binomial")

summary(logit_model05)

Fixed effects:
                                        Estimate Std. Error z value Pr(>|z|)    
(Intercept)                             -0.03211    0.15569  -0.206 0.836582    
factor(Sex)Male                          0.28383    0.14126   2.009 0.044500 *  
factor(Intervention)Pre                  0.22727    0.06450   3.524 0.000425 ***
factor(Sex)Male:factor(Intervention)Pre -0.25061    0.08412  -2.979 0.002890 ** 
logit_model <- glmer(AOI_look ~ factor(Sex)*factor(Intervention) + 
                                (1 | Image) + 
                                (1 | Subject)
cc1 <- c(0.052133, 0.187736, 0.257262, -0.220240)
cc2 <- c(0.309400,-0.032522,-0.257262,0.220234)

df2 <- expand.grid(Sex=c("Female","Male"),Intervention=c("Pre","Post"))
df1 <- transform(df2,Intervention=relevel(Intervention,"Post"))


X1 <- model.matrix(~Sex*Intervention, data=df1)
X2 <- model.matrix(~Sex*Intervention, data=df2)
cbind(drop(X1 %*% cc1), drop(X2 %*% cc2))
##       [,1]     [,2]
## 1 0.309395 0.309400
## 2 0.276891 0.276878
## 3 0.052133 0.052138
## 4 0.239869 0.239850

all.equal(drop(X1 %*% cc1), drop(X2 %*% cc2))
## [1] "Mean relative difference: 4.78203e-05"