2015-10-13 48 views
2

我試圖使用speedlm(speedglm package)的更新選項,因爲我沒有足夠的RAM一次計算整個模型,而biglm只使用一個CPU。
下面的代碼是出錯的可重現示例。Speedlm更新「需要呼叫組件的對象」

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

也許你想'updateWithMoreData'?根據文檔,'update.speedlm'需要一個公式作爲第二個參數。 – Roland

+0

@Roland謝謝,我完全錯過了文檔中的那個。但它不能解決問題。更新我的問題 – Bas

回答

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 
+0

甜!你知道我可以如何在一個循環中使用x的更新嗎? – Bas

+1

這不應該是非常困難的權利?只需在手之前(或者僅僅是data.frame的行)創建塊,然後像'for list in(11:20,21:30)updateWithMoreData(lmfit,iris [i,])'那樣循環。在開始循環之前,確保你運行'speedlm(formula = Sepal.Length〜Sepal.Width,data = iris [1:10,])''。這應該工作。使用這個實驗,你可以隨時發佈一個新的問題,以防你遇到麻煩。 – LyzandeR

+1

只有lmfit < - speedlm()需要與lmfit2 < - updateWithMoreData()不同的名稱。看到我的問題,我是如何做到的。 – Bas