如何绘制来自极大数据集的交互效果(特别是来自rxGlm输出)

时间:2017-11-02 16:37:13

标签: r glm microsoft-r

我目前正在计算大型数据集的glm模型。 glm甚至speedglm都需要数天才能计算出来。

我目前有大约3M观察结果和总共400个变量,其中只有一些用于回归。在我的回归中,我使用4个整数自变量(iv1iv2iv3iv4),1个二元自变量作为因子(iv5),交互项(x * y,其中x是整数,y是二元虚拟变量作为因子)。最后,我在多年ff1和公司ID ff2上有固定效果。我有15年和3000个公司。我通过添加它们作为因素来介绍固定效果。我观察到,特别是3000公司的固定效果在stats glmspeedglm中使计算速度变慢。

因此,我决定尝试使用Microsoft R rxGlm(RevoScaleR),因为这可以解决更多线程和处理器内核问题。实际上,分析的速度要快得多。此外,我将子样本的结果与标准glm的结果进行了比较,并将它们匹配。

我使用了以下功能:

mod1 <- rxGlm(formula = dv ~ 
                      iv1 + iv2 + iv3+ 
                      iv4 + iv5 +
                      x * y +
                      ff1  + ff2,
                    family = binomial(link = "probit"), data = dat,
                    dropFirst = TRUE, dropMain = FALSE, covCoef = TRUE, cube = FALSE)

但是,在尝试使用effects包绘制交互项时遇到问题。在调用以下函数时,我收到以下错误:

> plot(effect("x*y", mod1))
Error in terms.default(model) : no terms component nor attribute

我认为问题是rxGlm不存储绘制交互所需的数据。我相信是因为rxGlm对象比glm对象小很多,因此可能包含较少的数据(80 MB与几GB)。

我现在尝试通过rxGlmglm对象转换为as.glm()。仍然,effects()调用不会产生结果,并导致以下错误消息:

Error in dnorm(eta) : 
  Non-numerical argument for mathematical function
In addition: Warning messages:
1: In model.matrix.default(mod, data = list(dv = c(1L, 2L,  :
  variable 'x for y' is absent, its contrast will be ignored

如果我现在将原始glm与转换后的glm&#34;进行比较,我发现转换后的glm包含的项目少得多。例如,它不包含effects,对于对比,它仅为每个变量指出contr.treatment

我现在主要寻找一种以一种格式转置rxGlm输出对象的方法,这样我可以使用effect()函数。如果无法这样做,我如何使用RevoScaleR包中的函数获取交互图,例如rxLinePlot()rxLinePlot()也合理地快速绘制,但是,我还没有找到一种方法如何从中获得典型的交互效果图。我想避免先计算完整的glm模型,然后进行绘图,因为这需要很长时间。

1 个答案:

答案 0 :(得分:0)

如果可以获得系数,就不能自己滚动系数吗? 这不是数据集大小问题

# ex. data
n = 2000
dat <- data.frame( dv = sample(0:1, size = n, rep = TRUE), 
                   iv1 = sample(1:10, size = n, rep = TRUE),
                   iv2 = sample(1:10, size = n, rep = TRUE),
                   iv3 = sample(1:10, size = n, rep = TRUE),
                   iv4 = sample(0:10, size = n, rep = TRUE),
                   iv5 = as.factor(sample(0:1, size = n, rep = TRUE)),
                   x = sample(1:100, size = n, rep = TRUE),
                   y = as.factor(sample(0:1, size = n, rep = TRUE)),
                   ff1  = as.factor(sample(1:15, size = n, rep = TRUE)),
                   ff2  = as.factor(sample(1:100, size = n, rep = TRUE))
                   )

mod1 <- glm(formula = dv ~ 
                      iv1 + iv2 + iv3+ 
                      iv4 + iv5 +
                      x * y +
                      ff1  + ff2,
                    family = binomial(link = "probit"), data = dat)

# coefficients for x, y and their interaction
x1 <- coef(mod1)['x']
y1 <- coef(mod1)['y1']
xy <- coef(mod1)['x:y1']

x <- 1:100
a <- x1*x
b <- x1*x + y1 + xy*x

plot(a~x, type= 'line', col = 'red', xlim = c(0,max(x)), ylim = range(c(a, b)))
lines(b~x, col = 'blue')
legend('topright', c('y = 0', 'y = 1'), col = c('red', 'blue'))

here is how to make a reproduceable