【问题标题】:Initialise covariance structure in lme在 lme 中初始化协方差结构
【发布时间】:2013-07-14 00:21:10
【问题描述】:

如何为以下模型初始化非结构化协方差矩阵?

y<-data.frame(response=c(10,19,27,28,9,13,25,29,4,10,20,18,5,6,12,17),
               treatment=factor(rep(1:4,4)),
               subject=factor(rep(1:4,each=4))
               )
fit<-lme(response~-1+treatment,y,random=~1|subject,
         correlation=corSymm(form=~1|subject))

我尝试了一些变体,但每次我得到错误:

Error in lme.formula(response ~ -1 + treatment, y, random = ~1 |  : 
  nlminb problem, convergence error code = 1
  message = function evaluation limit reached without convergence (9)

【问题讨论】:

    标签: r mixed-models


    【解决方案1】:

    除了处理平均效应(4 个参数)、随机效应方差 (1) 和残差方差 (1) 之外,将具有 6 个参数的非结构化相关矩阵拟合到只有 16 个参数的数据集实际上是很困难的点。如果我尝试使用更大的随机版本的数据集,它可以正常工作。

    nSubj <- 20
    respVec <- c(10,19,27,28,9,13,25,29,4,10,20,18,5,6,12,17)
    set.seed(101)
    y<-data.frame(response=sample(respVec,size=4*nSubj,replace=TRUE),
                   treatment=factor(rep(1:4,nSubj)),
                   subject=factor(rep(1:nSubj,each=4))
                   )
    library(nlme)
    fit<-lme(response~-1+treatment,y,random=~1|subject,
             correlation=corSymm(form=~1|subject),
             control=lmeControl(msVerbose=TRUE))
    

    现在我们可以进行实验,看看我们可以摆脱多小的数据集。将上面的东西打包成一个模拟数据并尝试拟合的测试函数,如果拟合失败则返回TRUE

    testFun <- function(nSubj) {
        y<-data.frame(response=sample(respVec,size=4*nSubj,replace=TRUE),
                   treatment=factor(rep(1:4,nSubj)),
                   subject=factor(rep(1:nSubj,each=4))
                   )
        fit <- try(lme(response~-1+treatment,y,random=~1|subject,
             correlation=corSymm(form=~1|subject)),silent=TRUE)
        inherits(fit,"try-error")
    }
    

    尝试测试功能N次,报告失败比例:

    testFun2 <- function(nSubj,N) {
       mean(replicate(N,testFun(nSubj)))
    }
    

    尝试一系列数量的主题(慢):

    set.seed(101)
    testRes <- sapply(4:20,testFun2,N=50)
    

    结果:

    ##  [1] 0.64 0.04 0.00 0.00  ... 0.00
    

    有点令我惊讶的是,这将在三分之一的时间内适用于 4 个主题; 96% 的时间有 5 个科目:并且总是有 >5 个科目。

    【讨论】:

    • 好的,这取决于主题的数量。如果我以以下方式重新采样数据,是否更有意义? respVec &lt;- matrix(c(10,19,27,28,9,13,25,29,4,10,20,18,5,6,12,17),4*nSubj,4,byrow=T) set.seed(101) idx&lt;-sample(1:4,nSubj,replace=T) y&lt;-data.frame(response=c(t(respVec[idx,])), treatment=factor(rep(1:4,nSubj)), subject=factor(rep(1:nSubj,each=4)) )
    • 为了清楚起见,我并不是建议您可以实际上通过这种方式从这组数据中估计参数。我的意思是你根本没有足够的数据来可靠地拟合这个模型。我上面所做的抽样过程只是一种模拟如果你有足够的数据估计完整模型是否可行的方法。
    猜你喜欢
    • 2018-08-22
    • 1970-01-01
    • 2016-03-20
    • 2016-03-20
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多