R:转换do.call() - 汇总摘要

时间:2013-07-11 09:37:45

标签: r summary lm do.call

由于stepAIC()包中的MASS函数在函数中使用时存在问题,因此我将其与do.call()一起使用(描述为here)。 我的问题听起来很简单,但我无法找到解决方案:当我使用do.call()用于具有多个栅格图层的lm()模型时,所有图层都保存在模型中。如果我想打印模型的summary(),它会在输出中写入所有图层,这会让人感到困惑。如何在不使用summary的情况下获得“正常”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.call模型使用lm

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 个答案:

答案 0 :(得分: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