Speedlm更新"需要具有呼叫组件的对象"

时间:2015-10-13 13:41:38

标签: r components lm

我尝试使用speedlm (speedglm package)的更新选项,因为我没有足够的内存来同时计算整个模型,仅biglm使用一个CPU 下面的代码是一个可重复的例子,说明出了什么问题。

library(speedglm)
formula <- Sepal.Length ~ Sepal.Width
chunk1 <- iris[1:10,]
chunk2 <- iris[11:20,]
chunk3 <- iris[21:30,]
lmfit <- speedlm(formula, chunk1)
lmfit <- update(lmfit, chunk2)
lmfit <- update(lmfit, chunk3)

我收到以下错误:

> lmfit <- speedlm(formula, chunk1)
> lmfit <- update(lmfit, chunk2)
> lmfit <- update(lmfit, chunk3)
Error in update.default(lmfit, chunk3) : 
  need an object with call component
> 

如果是由于update而不是updateWithMoreData,我会在使用chunk2更新后发现错误。

想知道解决这个问题的方法,或者我是否必须使用替代方案 提前谢谢!

使用updateWithMoreData获取以下错误:

> lmfit <- speedlm(formula, chunk1)
> lmfit <- updateWithMoreData(lmfit, chunk2)
Error: object of type 'symbol' is not subsettable
> lmfit <- updateWithMoreData(lmfit, chunk3)
Error: object of type 'symbol' is not subsettable
> 

以下代码有效,支持@LyzandeR

> library(speedglm)
> chunk1 <- iris[1:10,]
> chunk2 <- iris[11:20,]
> chunk3 <- iris[21:30,]
> lmfit  <- speedlm(Sepal.Length ~ Sepal.Width, chunk1)
> 
> for (i in list(11,20, 21:30)){
+   lmfit2 <- updateWithMoreData(lmfit, iris[i,])
+ }
> lmfit2
Linear Regression Model of class 'speedlm':

Call:  speedlm(formula = Sepal.Length ~ Sepal.Width, data = chunk1) 

Coefficients:
(Intercept)  Sepal.Width  
     2.9876       0.5813  

> 

1 个答案:

答案 0 :(得分:4)

要使用update更新模型,您需要根据@Roland的评论使用updateWithMoreData。但是有一个问题。

如果你使用这样的代码,你会得到:

library(speedglm)
formula <- Sepal.Length ~ Sepal.Width
chunk1 <- iris[1:10,]
chunk2 <- iris[11:20,]
chunk3 <- iris[21:30,]
lmfit <- speedlm(formula, chunk1)
#runs fine up to here

#but this gives an error
lmfit2 <- updateWithMoreData(lmfit, chunk2)
Error: object of type 'symbol' is not subsettable

显然,这是因为它试图调用它(来自追溯):

as.formula(object$call[[2]])

失败,因为lmfit$call[[2]]返回formula。但是,如果将其更改为此,则可以正常工作:

library(speedglm)
chunk1 <- iris[1:10,]
chunk2 <- iris[11:20,]
chunk3 <- iris[21:30,]
#use the actual formula below so lmfit$call[[2]] will return it
#and now all the three lines below work fine
lmfit  <- speedlm(Sepal.Length ~ Sepal.Width, chunk1)
lmfit2 <- updateWithMoreData(lmfit, chunk2)
lmfit3 <- updateWithMoreData(lmfit, chunk3)

请注意,当您打印它们时,两者都会说它们是块1,因为呼叫保持不变但结果是正确的:

输出:

> lmfit2
Linear Regression Model of class 'speedlm':

Call:  speedlm(formula = Sepal.Length ~ Sepal.Width, data = chunk1) 

Coefficients:
(Intercept)  Sepal.Width  
     1.8398       0.9181  

> lmfit
Linear Regression Model of class 'speedlm':

Call:  speedlm(formula = Sepal.Length ~ Sepal.Width, data = chunk1) 

Coefficients:
(Intercept)  Sepal.Width  
     2.3882       0.7468  
相关问题