
R version 4.3.0 Patched (2023-06-03 r84496) -- "Already Tomorrow"
Copyright (C) 2023 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> require(robustbase)
Loading required package: robustbase
> 
> set.seed(17)# reproducibility!
> ## to check:
> ## - for the empty model
> summary(lmrob(Y ~ 0, coleman))

Call:
lmrob(formula = Y ~ 0, data = coleman)
 \--> method = ""
Residuals:
   Min     1Q Median     3Q    Max 
 22.70  32.78  35.86  39.95  43.10 

No Coefficients
> ## - with and without an intercept in the  model
> summary(lmrob(Y ~ 1, coleman))

Call:
lmrob(formula = Y ~ 1, data = coleman)
 \--> method = "MM"
Residuals:
     Min       1Q   Median       3Q      Max 
-12.8605  -2.7855   0.2945   4.3895   7.5395 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)   35.560      1.342    26.5   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Robust residual standard error: 5.48 
Convergence in 9 IRWLS iterations

Robustness weights: 
 one weight is ~= 1. The remaining 19 ones are summarized as
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 0.5611  0.8986  0.9553  0.9044  0.9918  0.9987 
Algorithmic parameters: 
       tuning.chi                bb        tuning.psi        refine.tol 
        1.548e+00         5.000e-01         4.685e+00         1.000e-07 
          rel.tol         scale.tol         solve.tol          zero.tol 
        1.000e-07         1.000e-10         1.000e-07         1.000e-10 
      eps.outlier             eps.x warn.limit.reject warn.limit.meanrw 
        5.000e-03         1.819e-12         5.000e-01         5.000e-01 
     nResample         max.it       best.r.s       k.fast.s          k.max 
           500             50              2              1            200 
   maxit.scale      trace.lev            mts     compute.rd fast.s.large.n 
           200              0           1000              0           2000 
                  psi           subsampling                   cov 
           "bisquare"         "nonsingular"         ".vcov.avar1" 
compute.outlier.stats 
                 "SM" 
seed : int(0) 
> writeLines(sfm <- capture.output(
+                        summary(lmrob(Y ~ ., coleman)))) # and this must be "identical":

Call:
lmrob(formula = Y ~ ., data = coleman)
 \--> method = "MM"
Residuals:
     Min       1Q   Median       3Q      Max 
-4.16181 -0.39226  0.01611  0.55619  7.22766 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 30.50232    6.71260   4.544 0.000459 ***
salaryP     -1.66615    0.43129  -3.863 0.001722 ** 
fatherWc     0.08425    0.01467   5.741 5.10e-05 ***
sstatus      0.66774    0.03385  19.726 1.30e-11 ***
teacherSc    1.16778    0.10983  10.632 4.35e-08 ***
motherLev   -4.13657    0.92084  -4.492 0.000507 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Robust residual standard error: 1.134 
Multiple R-squared:  0.9814,	Adjusted R-squared:  0.9747 
Convergence in 11 IRWLS iterations

Robustness weights: 
 observation 18 is an outlier with |weight| = 0 ( < 0.005); 
 The remaining 19 ones are summarized as
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 0.1491  0.9412  0.9847  0.9279  0.9947  0.9982 
Algorithmic parameters: 
       tuning.chi                bb        tuning.psi        refine.tol 
        1.548e+00         5.000e-01         4.685e+00         1.000e-07 
          rel.tol         scale.tol         solve.tol          zero.tol 
        1.000e-07         1.000e-10         1.000e-07         1.000e-10 
      eps.outlier             eps.x warn.limit.reject warn.limit.meanrw 
        5.000e-03         1.569e-10         5.000e-01         5.000e-01 
     nResample         max.it       best.r.s       k.fast.s          k.max 
           500             50              2              1            200 
   maxit.scale      trace.lev            mts     compute.rd fast.s.large.n 
           200              0           1000              0           2000 
                  psi           subsampling                   cov 
           "bisquare"         "nonsingular"         ".vcov.avar1" 
compute.outlier.stats 
                 "SM" 
seed : int(0) 
> sfm2 <- capture.output(summary(lmrob(Y ~ ., coleman, model=FALSE, x=FALSE, y=FALSE)))
> iCall <- grep("lmrob.*coleman", sfm)# the only line that differs
> stopifnot(sfm[-iCall] == sfm2[-iCall])
> ## w/o intercept:
> summary(lmrob(Y ~ . - 1, coleman, model=FALSE, x=FALSE, y=FALSE))

Call:
lmrob(formula = Y ~ . - 1, data = coleman, model = FALSE, x = FALSE, y = FALSE)
 \--> method = "MM"
Residuals:
     Min       1Q   Median       3Q      Max 
-4.86146 -0.59195 -0.04679  0.87826  5.40639 

Coefficients:
          Estimate Std. Error t value Pr(>|t|)    
salaryP   -1.97540    0.45262  -4.364 0.000555 ***
fatherWc   0.03388    0.02220   1.526 0.147749    
sstatus    0.55922    0.07590   7.367 2.34e-06 ***
teacherSc  1.60446    0.19039   8.427 4.51e-07 ***
motherLev -0.48903    0.90805  -0.539 0.598097    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Robust residual standard error: 1.344 
Multiple R-squared:  0.9987,	Adjusted R-squared:  0.9983 
Convergence in 14 IRWLS iterations

Robustness weights: 
 3 weights are ~= 1. The remaining 17 ones are summarized as
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
0.06899 0.89030 0.95860 0.82750 0.98700 0.99820 
Algorithmic parameters: 
       tuning.chi                bb        tuning.psi        refine.tol 
        1.548e+00         5.000e-01         4.685e+00         1.000e-07 
          rel.tol         scale.tol         solve.tol          zero.tol 
        1.000e-07         1.000e-10         1.000e-07         1.000e-10 
      eps.outlier             eps.x warn.limit.reject warn.limit.meanrw 
        5.000e-03         1.569e-10         5.000e-01         5.000e-01 
     nResample         max.it       best.r.s       k.fast.s          k.max 
           500             50              2              1            200 
   maxit.scale      trace.lev            mts     compute.rd fast.s.large.n 
           200              0           1000              0           2000 
                  psi           subsampling                   cov 
           "bisquare"         "nonsingular"         ".vcov.avar1" 
compute.outlier.stats 
                 "SM" 
seed : int(0) 
> 
> ## - when prior-weights are included
> wts <- c(rep(0.05, 10), rep(2, 10))
> summary(lmrob(Y ~ . - 1, coleman, model=FALSE, x=FALSE, y=FALSE,
+               weights = wts))

Call:
lmrob(formula = Y ~ . - 1, data = coleman, weights = wts, model = FALSE, 
    x = FALSE, y = FALSE)
 \--> method = "MM"
Weighted Residuals:
     Min       1Q   Median       3Q      Max 
-1.01509 -0.04288  0.04892  0.38289  9.00119 

Coefficients:
          Estimate Std. Error t value Pr(>|t|)    
salaryP   -2.68644    0.05871 -45.761  < 2e-16 ***
fatherWc   0.04761    0.00721   6.603 8.39e-06 ***
sstatus    0.58362    0.00314 185.842  < 2e-16 ***
teacherSc  1.77115    0.07918  22.369 6.20e-13 ***
motherLev -1.03171    0.34154  -3.021   0.0086 ** 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Robust residual standard error: 0.423 
Multiple R-squared:  0.9985,	Adjusted R-squared:  0.998 
Convergence in 5 IRWLS iterations

Robustness weights: 
 3 observations c(12,16,18) are outliers with |weight| = 0 ( < 0.005); 
 5 weights are ~= 1. The remaining 12 ones are summarized as
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 0.5441  0.9257  0.9833  0.9303  0.9956  0.9985 
Algorithmic parameters: 
       tuning.chi                bb        tuning.psi        refine.tol 
        1.548e+00         5.000e-01         4.685e+00         1.000e-07 
          rel.tol         scale.tol         solve.tol          zero.tol 
        1.000e-07         1.000e-10         1.000e-07         1.000e-10 
      eps.outlier             eps.x warn.limit.reject warn.limit.meanrw 
        5.000e-03         2.219e-10         5.000e-01         5.000e-01 
     nResample         max.it       best.r.s       k.fast.s          k.max 
           500             50              2              1            200 
   maxit.scale      trace.lev            mts     compute.rd fast.s.large.n 
           200              0           1000              0           2000 
                  psi           subsampling                   cov 
           "bisquare"         "nonsingular"         ".vcov.avar1" 
compute.outlier.stats 
                 "SM" 
seed : int(0) 
> ## - should work for object with NA in the coefficients, and
> ## - should work for object with NA in the observations --> both in ./NAcoef.R
> 
> ## check equality with lm() for classical model
> test <- function(formula, data,
+                  items=c("coefficients", "residuals", "df", "scale",
+                          "r.squared", "adj.r.squared", "weights"),
+                  tol = 1e-4, ...)
+ {
+     lmrCtrl <- lmrob.control(psi = "hampel", tuning.psi = c(1000, 2000, 3000),
+                              method="SMDM", ...)
+     sc <- summary(lm   (formula, data))
+     sr <- summary(lmrob(formula, data, control= lmrCtrl))
+     names(sc)[names(sc) == "sigma"] <- "scale"
+     if(sc$df[1] == 0 && getRversion() <= "3.5.1" && as.numeric(R.version$`svn rev`) < 74993)
+ 	## in the past, lm() returned logical empty matrix
+ 	storage.mode(sc$coefficients) <- "double"
+     ret <- all.equal(sc[items], sr[items], tolerance=tol)
+     if (!isTRUE(ret)) {
+         print(sr)
+         for (i in seq_along(items)) {
+             print(sc[items[i]])
+             print(sr[items[i]])
+         }
+         print(ret)
+         stop(sprintf("all.equal(sc[items], sr[items], tol.. = %g) are not all TRUE",
+                      tol))
+     }
+     ret
+ }
> 
> set.seed(101)
> 
> test(Y ~ 0, coleman, c("residuals", "df", "coefficients",
+                        "r.squared", "adj.r.squared"), tol=1e-10)
[1] TRUE
> test(Y ~ 1,     coleman, tol = 2e-4)
[1] TRUE
> test(Y ~ .,     coleman, tol = 4e-4)
[1] TRUE
> test(Y ~ . - 1, coleman, tol = 4e-4)
[1] TRUE
> 
> 
> proc.time()
   user  system elapsed 
  0.723   0.152   0.894 
