2

As the stepAIC() function from the MASS package has problems when used within a function, I use it with do.call() (described here). My problem sounds very easy but I could't find a solution for it: When I use do.call() for a lm() model with several raster layers, all the layers are saved within the model. If I want to print a summary() of the model, it writes all the layers in the output and it gets really confusing. How do I get a "normal" summary output, as I would get without using do.call?

Here is a short example:

Create a list of raster layers:

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

Convert them in a data.frame:

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

xz <- as.data.frame(xz)

Use do.call for the lm model:

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

The summary() output looks like this:

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 

This doesn't look to bad in this small example, but it becomes a mess when you are using 10 or more raster layers with about 32k values each. So I would like to make the output look like as I would just use the summary(lm) function without 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 
Community
  • 1
  • 1
viktor_r
  • 701
  • 1
  • 10
  • 21

1 Answers1

2

You can redefine your lm function like this :

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

testing it :

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
agstudy
  • 119,832
  • 17
  • 199
  • 261