【问题标题】:Model Matrices Incompatible - Error in update in Biglm package in R模型矩阵不兼容 - R 中 Biglm 包中的更新错误
【发布时间】:2015-05-28 04:23:36
【问题描述】:

我正在逐块运行大型数据集,并在使用 biglm 函数时更新线性模型列表。当特定块不包含我的线性模型中的所有因素时会出现此问题,并且我收到此错误:

Error in update.biglm(model, new) : model matrices incompatible

update.biglm 的描述提到所有块的因子级别必须相同。我可能会想出一个解决方法来避免这种情况,但必须有更好的方法。 This pdf,在 'biglm' 页面上,提到“因素必须有完整的等级 指定(不一定存在于数据块中)”。所以我认为有一些方法可以指定所有可能的级别,这样我就可以在不存在所有因素的情况下更新模型,但我不知道该怎么做.

这是一个示例代码来说明我的问题:

df = data.frame(a = rnorm(12),b = as.factor(rep(1:4,each = 3)),c = rep(0:1,6))
model = biglm(a~b+c,data = df

df.new = data.frame(a = rnorm(6),b = as.factor(rep(1:2,each = 3)),c =rep(0:1, 3))
model.new = update(model,df.new)

感谢您的任何建议。

【问题讨论】:

    标签: r bigdata regression lm


    【解决方案1】:

    我也遇到过这个问题。在将它们分成块之前,是否将大型数据框中的变量指定为因子?另外,数据集是否格式化为数据框?

    large_df <- as.data.frame(large_data_set) # just to make sure it's a df.
    large_df$factor.vars <- as.factor(large_df$factor.vars)
    

    如果是这种情况,那么即使在将数据框分成块之后,所有因子水平也应保留在因子变量中。这将确保 biglm 从第一次调用中创建正确的设计矩阵,并且所有后续更新都将兼容。

    如果您从一开始就有不同的数据帧(如您在示例中说明的那样),也许您应该在分解成块之前将它们合并为一个。继续你的例子:

    df.large <- rbind(df,df.new)
    chunk1 <- df.large[1:12,]
    chunk2 <- df.large[13:18,]
    
    model <- biglm(a~b+c,data = chunk1)
    model.new <- update(model,chunk2)   # this is now compatible  
    

    【讨论】:

      猜你喜欢
      • 2020-07-09
      • 2015-02-16
      • 1970-01-01
      • 1970-01-01
      • 2019-03-18
      • 1970-01-01
      • 2015-12-09
      • 1970-01-01
      • 2021-03-27
      相关资源
      最近更新 更多