Search code examples
rclassbigdataglmemmeans

emmeans Can't handle an object of class “bigglm” in R


I'm using "bigglm" function in R and I also would like to use the "emmeans" function to make post-hoc analyzes and ploting. However the function emmeans Can't handle an object of class “bigglm”.

There is a way to construct an object of class "glm" from class "bigglm"?

Here is an example

library(biglm)
library(emmeans)
data(trees)
trees$f <- factor(rep(c("A","B"),length.out = nrow(trees)))
ff <-log(Volume)~f

a <- glm(ff,data=trees)
summary(a)
emmeans(a,~f)

b <- bigglm(ff,data=trees, chunksize=10)
summary(b)
emmeans(b,~f)

Thank you in advance.


Solution

  • It turns out that "biglm" and "bigglm" objects are very similar to "lm" and "glm" objects, with only minor changes to their structure. So it appears that all we have to do is to trick emmeans into thinking it is one of those. Here is a utility for that purpose:

    as.glm = function(mod) {
        mod$coefficients = coef(mod)
        mod$df.residual = mod$df.resid
        class(mod) = c(class(mod), "glm", "lm")
        mod
    }
    

    Now we get:

    > emmeans(as.glm(b), "f")
     f   emmean        SE  df asymp.LCL asymp.UCL
     A 3.288124 0.1337527 Inf  3.025974  3.550275
     B 3.256313 0.1381392 Inf  2.985565  3.527061
    
    Confidence level used: 0.95