【问题标题】:R: Converting do.call()-summary in summaryR:总结转换 do.call()-summary
【发布时间】:2013-07-11 09:37:45
【问题描述】:

由于MASS 包中的stepAIC() 函数在函数中使用时出现问题,我将它与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 个值时,它就会变得一团糟。所以我想让输出看起来像我只使用没有do.callsummary(lm)函数:

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 

【问题讨论】:

    标签: r summary lm do.call


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 2020-11-27
      • 2023-04-03
      • 2022-01-07
      • 2018-11-15
      • 2021-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多