所有lambda的glmmLasso try-error [英] glmmLasso try-error for all lambda

查看:255
本文介绍了所有lambda的glmmLasso try-error的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用glmmLasso对混合模型进行变量选择,但是我似乎无法使模型正常工作.我已经在在这里找到的演示类似地设置了模型一个>.我正在使用使用BIC选择lambda的简单方法.

I've been trying to use glmmLasso to do variable selection for a mixed-model but I can't seem to get the model to work. I've setup my model similarily to the demo found here. I'm using the simple method of using BIC to choose lambda.

这是我一直在运行的代码.

This is the code I've been running.

library(glmmLasso)

lambda <- seq(500,0,by=-5)
family = binomial(link = logit)

library(MASS);library(nlme)
PQL<-glmmPQL(y~1,random = ~1|ID,family=family,data=train)
Delta.start<-c(as.numeric(PQL$coef$fixed),rep(0,64),as.numeric(t(PQL$coef$random$ID)))
Q.start<-as.numeric(VarCorr(PQL)[1,1])

BIC_vec<-rep(Inf,length(lambda))

for(j in 1:length(lambda)){
  print(paste("Iteration ", j,sep=""))

  glm1 = try(glmmLasso(y ~ variable1 + ... + as.factor(factorVariable1), rnd = list(ID=~1),  
                       family = family, data = train, lambda=lambda[j],switch.NR=T,final.re=TRUE,
                       control=list(start=Delta.start, q_start=Q.start)),silent = TRUE)

  if(class(glm1) != "try-error"){  
    BIC_vec[j]<-glm1$bic
  }
}

我不确定的一件事是Delta.start.我一直在关注该演示,并假设被替换的0的数量是变量的数量,或者如果您将其加0的因数小于该因数的级别数,则为0.

One thing that I'm unsure of is the Delta.start. I was following the demo and I'm assuming the number of 0s that are repped is the number of variables, or if its a factor you add in 0s for 1 less than the number of levels in the factor.

运行此代码,所有BIC得分仍为Inf.而且,看着glm1我发现了这个错误

Running this code all of the BIC scores were still Inf. And, looking at glm1 I find this error

[1] "Error in if (group.sum[1] == 0 & sqrt(sum(score.beta[1:block[1]]^2)) >  : \n  missing value where TRUE/FALSE needed\n"
attr(,"class")
[1] "try-error"
attr(,"condition")
<simpleError in if (group.sum[1] == 0 & sqrt(sum(score.beta[1:block[1]]^2)) >     lambda_vec[1]) {    grad.lasso[1:block[1]] <- score.beta[1:block[1]] - lambda_vec[1] *         (score.beta[1:block[1]]/sqrt(sum(score.beta[1:block[1]]^2)))} else {    grad.lasso[1:block[1]] <- 0}: missing value where TRUE/FALSE needed>

有人对如何解决此问题有任何想法吗?我不确定这是否会引起问题,但是在火车数据集中,ID大约有7500个级别.

Does anyone have any ideas on how to fix this? I'm not sure if this could cause an issue, but in the train data set, ID has around 7,500 levels.

不幸的是,我不能包含任何数据来使其再现.我希望过去有人遇到过这个问题,并且知道发生了什么事情.我正在尝试生成一些也存在此问题的数据.

Unfortunately, I can't include any data to make this reproducible. I'm hoping someone else has run across this issue in the past and knows what is going on. I'm trying to generate some data that also has this issue.

编辑

现在看来,它与控制中的start=Delta.start有关.当我删除模型适合时.我仍然不确定Delta.start到底是在做什么.

It now looks like it has something to do with start=Delta.start in control. When I remove that the model fits. I still am not sure what exactly about the Delta.start is making this break.

推荐答案

与此有关的问题是因素的水平.将数据采样到训练/测试集中后,train$ID级别的长度大于train$ID中剩余的唯一值的长度.我想我会发布结果,以防其他人遇到此问题.

The issue with this was the levels of the factor. The length of the levels of train$ID was greater than the length of the unique values left in train$ID after sampling the data into train/test sets. I figured I would post my results in case anyone else runs into this issue.

运行

train$ID = factor(train$ID)

固定ID的因子水平.然后,我在演示中使用了第三个示例,该示例链接到问题中,该示例使用先前的运行结果作为参数的初始化.

fixes the levels of factors for ID. Then, I ended up using the 3rd example in the demo that is linked in the question where it uses previous run results as the initialization of the parameters.

Delta.start<-as.matrix(t(rep(0,7+length(levels(train$ID)))))
Q.start<-0.1 

lambda <- seq(500,0,by=-5)
family = binomial(link = logit)

BIC_vec<-rep(Inf,length(lambda))

for(j in 1:length(lambda)){
  print(paste("Iteration ", j,sep=""))

  glm1 = try(glmmLasso(y ~ variable1 + ... + as.factor(factorVariable1), rnd = list(ID=~1),  
                       family = family, data = train, lambda=lambda[j],switch.NR=T,final.re=TRUE,
                       control=list(start=Delta.start[j,], q_start=Q.start[j])),silent = TRUE)

  Delta.start<-rbind(Delta.start,glm3$Deltamatrix[glm3$conv.step,])
  Q.start<-c(Q.start,glm3$Q_long[[glm3$conv.step+1]])

  if(class(glm1) != "try-error"){  
    BIC_vec[j]<-glm1$bic
  }
}

这篇关于所有lambda的glmmLasso try-error的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆