2013-07-11 38 views
1

由於MASS包中的stepAIC()函數在函數中使用時有問題,所以我在do.call()(描述爲here)中使用它。 我的問題聽起來很簡單,但我找不到解決方案:當我使用do.call()作爲具有多個柵格圖層的lm()模型時,所有圖層都保存在模型中。如果我想打印模型的summary(),它會在輸出中寫入所有圖層,並且會變得非常混亂。我如何得到一個「正常的」summary輸出,因爲我會得到沒有使用do.callR:轉換do.call() - 總結小結

下面是一個簡單的例子:

創建柵格圖層列表:

xz.list <- lapply(1:5,function(x){ 
    r1 <- raster(ncol=3, nrow=3) 
    values(r1) <- 1:ncell(r1) 
    r1 
}) 

轉換他們在data.frame

xz<-getValues(stack(xz.list)) 

xz <- as.data.frame(xz) 

使用do.calllm型號:

fit1<-do.call("lm", list(xz[,1] ~ . , data = xz)) 

summary()輸出看起來是這樣的:

summary(fit1) 

Call: 
lm(formula = xz[, 1] ~ ., data = structure(list(layer.1 = 1:9, 
    layer.2 = 1:9, layer.3 = 1:9, layer.4 = 1:9, layer.5 = 1:9), .Names = c("layer.1", 
"layer.2", "layer.3", "layer.4", "layer.5"), row.names = c(NA, 
-9L), class = "data.frame")) 

Residuals: 
     Min   1Q  Median   3Q  Max 
-9.006e-16 -2.472e-16 -2.031e-16 -1.370e-16 1.724e-15 

Coefficients: (4 not defined because of singularities) 
      Estimate Std. Error t value Pr(>|t|)  
(Intercept) 1.184e-15 5.784e-16 2.047e+00 0.0798 . 
layer.1  1.000e+00 1.028e-16 9.729e+15 <2e-16 *** 
layer.2   NA   NA  NA  NA  
layer.3   NA   NA  NA  NA  
layer.4   NA   NA  NA  NA  
layer.5   NA   NA  NA  NA  
--- 
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 7.962e-16 on 7 degrees of freedom 
Multiple R-squared:  1, Adjusted R-squared:  1 
F-statistic: 9.465e+31 on 1 and 7 DF, p-value: < 2.2e-16 

這看起來並不壞在這個小例子,但是當您使用10個或更多raster層,每個約32K值變得一團糟。所以,我想使輸出的樣子,我只想用summary(lm)功能而不do.call

fit<-lm(xz[,1] ~ . , data=xz) 
summary(fit) 

Call: 
lm(formula = xz[, 1] ~ ., data = xz) 

Residuals: 
     Min   1Q  Median   3Q  Max 
-9.006e-16 -2.472e-16 -2.031e-16 -1.370e-16 1.724e-15 

Coefficients: (4 not defined because of singularities) 
      Estimate Std. Error t value Pr(>|t|)  
(Intercept) 1.184e-15 5.784e-16 2.047e+00 0.0798 . 
layer.1  1.000e+00 1.028e-16 9.729e+15 <2e-16 *** 
layer.2   NA   NA  NA  NA  
layer.3   NA   NA  NA  NA  
layer.4   NA   NA  NA  NA  
layer.5   NA   NA  NA  NA  
--- 
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 7.962e-16 on 7 degrees of freedom 
Multiple R-squared:  1, Adjusted R-squared:  1 
F-statistic: 9.465e+31 on 1 and 7 DF, p-value: < 2.2e-16 

回答

1

你可以重新定義你的lm功能是這樣的:

lm <- function(form, ...) { fm <- stats::lm(form,...); 
          fm$call <- form; fm } 

測試它:

fit2<-do.call("lm", list(xz[,1] ~ . , data = xz)) 

summary(fit2) 

Call: 
xz[, 1] ~ . 

Residuals: 
     Min   1Q  Median   3Q  Max 
-9.006e-16 -2.472e-16 -2.031e-16 -1.370e-16 1.724e-15 

Coefficients: (4 not defined because of singularities) 
      Estimate Std. Error t value Pr(>|t|)  
(Intercept) 1.184e-15 5.784e-16 2.047e+00 0.0798 . 
layer.1  1.000e+00 1.028e-16 9.729e+15 <2e-16 *** 
layer.2   NA   NA  NA  NA  
layer.3   NA   NA  NA  NA  
layer.4   NA   NA  NA  NA  
layer.5   NA   NA  NA  NA  
--- 
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 7.962e-16 on 7 degrees of freedom 
Multiple R-squared:  1, Adjusted R-squared:  1 
F-statistic: 9.465e+31 on 1 and 7 DF, p-value: < 2.2e-16