比较模型与dplyr和扫帚:: glance:如果错误产生如何继续? [英] Comparing models with dplyr and broom::glance: How to continue if error is produced?

查看:198
本文介绍了比较模型与dplyr和扫帚:: glance:如果错误产生如何继续?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用R中的lme4包将数据集中的每个变量作为单变量模型运行。我想使用dplyr / tidyr包准备数据,并使用扫帚包来组织每个模型的结果(即do(glance ...),我会非常感谢在这个框架内的帮助,我在R中并不是很棒,但是能够生成一个抛出错误的数据集,并且具有与我使用的数据:

 库(lme4)
库(dplyr)
库(tidyr)
库(扫帚)

鸟< -c(rep(c(0,0,0,0,0,0,0,0,0,0,1,0,0,0 ,0,0,0,0,0,0),10))
停止< -c(rep(seq(1,10),20))
计数< -c (c(rep(c(1,2),each = 10)),每个= 10))
路由<-c(rep(seq(1,10),each = 20))
X1< -rnorm(200,50,10)
X2< -rnorm(200,10,1)
X3 <-c(rep(c(0),200))#麻烦制造者变量

数据< -data.frame(cbind(Bird,Stop,Count,Route,X1,X2,X3))

数据%>%
gather Variabl e,Value,5:7)%>%
group_by(Variable)%>%
do(glance(gl〜 =。,family = binomial)))

最后一个变量产生错误,因此没有输出。我想要的是在输出中产生NA值,如果发生这种情况,或者只是跳过该变量。我尝试使用'try'来打破烦恼者变量:

  do(try(glance(glmer(Bird〜 Value + Stop +(1 + Stop | Route / Count),data =。,family = binomial)))

它是做什么的,但仍然没有产生一个输出,因为它不能强制对数据框架的try-error。不幸的是没有tryharder功能。我已经尝试了一些if对我而不是计算机有意义的语句。我确定我没有做到正确,但如果我使用例如:

  try(glance(glmer(Bird 〜(值)+停止+(1 +停止|路由/计数),数据=。,系列=二项式)) - > mod 
if(is.data.frame(mod)){do(mod)}

我得到下标超出界限的错误。非常感谢您提供的任何投入!

解决方案

在调用之前使用 tryCatch 一瞥

  zz = Data%>%
gather(Variable,Value, 5:7)%>%
group_by(Variable)%>%
do(aa = tryCatch(glmer(Bird〜Value + Stop +(1 + Stop | Route / Count),data =。 ,
family = binomial),error = function(e)data.frame(NA))


zz%>%
glance(aa)


I would like to run each variable in a dataset as a univariate glmer model using the lme4 package in R. I would like to prepare the data with the dplyr/tidyr packages, and organize the results from each model with the broom package (i.e. do(glance(glmer...). I would most appreciate help that stuck within that framework. I'm not that great in R, but was able to produce a dataset that throws an error and has the same structure as the data I'm using:

library(lme4)
library(dplyr)
library(tidyr)
library(broom)

Bird<-c(rep(c(0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0),10))
Stop<-c(rep(seq(1,10), 20))
Count<-c(rep(c(rep(c(1,2), each=10)), each=10))
Route<-c(rep(seq(1,10), each=20))
X1<-rnorm(200, 50, 10)
X2<-rnorm(200, 10, 1)
X3<-c(rep(c(0),200))#trouble maker variable

Data<-data.frame(cbind(Bird, Stop, Count, Route, X1, X2, X3))

Data%>%
  gather(Variable, Value, 5:7)%>%
  group_by(Variable)%>%
  do(glance(glmer(Bird~Value+Stop+(1+Stop|Route/Count), data=.,     family=binomial)))

The last variable produces an error so there is no output. What I would like is it to produce NA values in the output if this occurs, or just skip that variable. I've tried using 'try' to blow past the trouble maker variable:

do(try(glance(glmer(Bird~Value+Stop+(1+Stop|Route/Count), data=.,       family=binomial))))

which it does, but still an output is not produced because it can't coerce a 'try-error' to a data.frame. Unfortunately there is no tryharder function. I've tried some if statements which make sense to me but not the computer. I'm sure I'm not doing it right, but if for example I use:

try(glance(glmer(Bird~Value+Stop+(1+Stop|Route/Count), data=., family=binomial)))->mod
if(is.data.frame(mod)){do(mod)}

I get subscript out of bounds errors. Thanks very much for any input you can provide!

解决方案

Use tryCatch before the call to glance:

zz = Data %>%
  gather(Variable, Value, 5:7) %>%
  group_by(Variable) %>%
  do(aa = tryCatch(glmer(Bird~Value+Stop+(1+Stop|Route/Count), data=., 
                  family=binomial), error = function(e) data.frame(NA))) 


zz %>% 
  glance(aa)

这篇关于比较模型与dplyr和扫帚:: glance:如果错误产生如何继续?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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