我正在使用dplyr/broom包对多个传感器进行线性回归。当我在do语句中使用lm()时,来自broom的glance()函数将不起作用,但如果我使用biglm(),则会起作用。这不是问题,但我希望r^2,F统计量和p-val对于传统的lm()返回得相当漂亮。
我已经在其他地方找过了,但找不到类似的错误:
Error in data.frame(r.squared = r.squared, adj.r.squared = adj.r.squared, :
object 'fstatistic' not found可能的预感:
?Anova
"The comparison between two or more models will only be valid if they are
fitted to the same dataset. This may be a problem if there are missing
values and R's default of na.action = na.omit is used."代码如下:
library(tidyr)
library(broom)
library(biglm) # if not install.packages("biglm")
library(dplyr)
regressionBig <- tidied_rm_outliers %>%
group_by(sensor_name, Lot.Tool, Lot.Module, Recipe, Step, Stage, MEAS_TYPE) %>%
do(fit = biglm(MEAS_AVG ~ value, data = .)) #note biglm is used
regressionBig
#extract the r^2 from the complex list type from the data frame we just stored
glances <- regressionBig %>% glance(fit)
glances %>%
ungroup() %>%
arrange(desc(r.squared))
#Biglm works but if i try the same thing with regular lm It errors on glance()
ErrorDf <- tidied_rm_outliers %>%
group_by(sensor_name, Lot.Tool, Lot.Module, Recipe, Step, Stage, MEAS_TYPE) %>%
do(fit = lm(MEAS_AVG ~ value, data = .)) #note lm is normal
ErrorDf %>% glance(fit)
#Error in data.frame(r.squared = r.squared, adj.r.squared = adj.r.squared, :
#object 'fstatistic' not found我讨厌上传整个数据帧,因为我知道这在S/O上通常是不可接受的,但我不确定是否可以在不这样做的情况下创建可重现的示例。https://www.dropbox.com/s/pt6xe4jdxj743ka/testdf.Rda?dl=0
R会话信息在pastebin上,如果你喜欢它的here!
发布于 2015-08-05 06:42:44
它在ErrorDf中看起来像是一个糟糕的模型。我在运行for循环时诊断了它。
for (i in 1:nrow(ErrorDf)){
print(i)
glance(ErrorDf$fit[[i]])
}对于模型# 94,似乎没有value的系数可以估计。我没有做任何进一步的调查,但它提出了一个有趣的问题,即broom应该如何处理这一问题。
发布于 2015-12-29 08:35:56
我是在遇到同样的问题后发现这篇文章的。如果lm()因为某些分组的案例太少而失败,那么您可以通过在运行do()循环之前预先过滤数据以删除这些分组来解决这个问题。下面的通用代码显示了如何筛选出数据点少于30个的组。
require(dplyr)
require(broom)
data_grp = ( data
%>% group_by(factor_a, factor_b)
%>% mutate(grp_cnt=n())
%>% filter(grp_cnt>30)
)发布于 2016-05-08 06:22:16
在我的故障排除中找到这篇文章后,我写了一个函数来处理这个问题。包的维护者可能(将)有一个更聪明的解决方案,但我认为它应该适用于大多数情况。感谢@Benjamin提供的循环灵感。
collect_glance=function(mdldF){
# mdldF should be a data frame from dplyr/broom with the column 'mdl' for the object models
mdlglance=data_frame() #initialize empty dataframe
metadF=mdldF %>% slice(0) %>% select(-ncol(mdldF))#create an empty data frame with only the group info
i=1
for(i in 1:nrow(mdldF)){
# fill in metadata for each group for each modeling iteration
for(colnums in 1:ncol(mdldF)-1){
metadF[1,colnames(mdldF)[colnums]]=mdldF[i,colnames(mdldF[colnums])]
}
# attempt glance(). if succesful, bind to metadata. if not, return empty dataframe
gtmp=tryCatch(glance(mdldF$mdl[[i]]) %>% bind_cols(metadF,.), error = function(e) {
data_frame()
})
# test for empty dataframe. bind to mdlglance data frame if glance was successful. otherwise use full_join to join mdlglance and metadata by group names and get NA for all the other glance columns.
if(nrow(gtmp)!=0) {
mdlglance=bind_rows(mdlglance,gtmp)
} else {
mdlglance=full_join(mdlglance,metadF)
}
}
return(mdlglance)
}https://stackoverflow.com/questions/31818475
复制相似问题