> # test.varmmod.mgcv.R
> # mgcv has to be tested separately because of clashes between library(gam) and library(mgcv)
> # Stephen Milborrow Apr 2015 Berea
> 
> source("test.prolog.R")
> library(earth)
Loading required package: Formula
Loading required package: plotmo
Loading required package: plotrix
Loading required package: TeachingDemos
> options(warn=2)
> 
> printh <- function(caption)
+     cat("===", caption, "\n", sep="")
> 
> CAPTION <- NULL
> 
> multifigure <- function(caption, nrow=3, ncol=3)
+ {
+     CAPTION <<- caption
+     printh(caption)
+     par(mfrow=c(nrow, ncol))
+     par(cex = 0.8)
+     par(mar = c(3, 3, 5, 0.5)) # small margins but space for right hand axis
+     par(mgp = c(1.6, 0.6, 0))  # flatten axis elements
+     oma <- par("oma") # make space for caption
+     oma[3] <- 2
+     par(oma=oma)
+ }
> do.caption <- function() # must be called _after_ first plot on new page
+     mtext(CAPTION, outer=TRUE, font=2, line=1, cex=1)
> old.par <- par(no.readonly=TRUE)
> 
> library(mgcv)
Loading required package: nlme
This is mgcv 1.8-30. For overview type 'help("mgcv-package")'.
> 
> for(varmod.method in c("gam", "x.gam")) {
+ 
+     multifigure(sprint("varmod.method=\"%s\"", varmod.method), 2, 3)
+     par(mar = c(3, 3, 2, 3)) # space for right margin axis
+ 
+     set.seed(6)
+     earth.mod <- earth(Volume~Girth, data=trees, nfold=3, ncross=3,
+                        varmod.method=varmod.method,
+                        trace=if(varmod.method %in% c("const", "lm", "power")) .3 else 0)
+     printh(sprint("varmod.method %s: summary(earth.mod)", varmod.method))
+     printh("summary(earth.mod)")
+     print(summary(earth.mod))
+ 
+     # summary(mgcv) prints environment as hex address which messes up the diffs
+     printh("skipping summary(mgcv::gam) etc.\n")
+ 
+     printh(sprint("varmod.method %s: predict(earth.mod, interval=\"pint\")", varmod.method))
+     pints <- predict(earth.mod, interval="pint")
+     print(pints)
+ 
+     plotmo(earth.mod$varmod, do.par=FALSE, col.response=2, clip=FALSE,
+            main="plotmo residual model",
+            xlab="x", ylab="varmod residuals")
+ 
+     plotmo(earth.mod, level=.90, do.par=FALSE, col.response=1, clip=FALSE,
+            main="main model plotmo Girth")
+     do.caption()
+ 
+     plot(earth.mod, which=3, do.par=FALSE, level=.95)
+ 
+     # plot.varmod
+     plot(earth.mod$varmod, do.par=FALSE, which=1:3, info=(varmod.method=="earth"))
+ }
===varmod.method="gam"
===varmod.method gam: summary(earth.mod)
===summary(earth.mod)
Call: earth(formula=Volume~Girth, data=trees,
            trace=if(varmod.method%in%c("const","lm","power"))0.3els...),
            nfold=3, ncross=3, varmod.method=varmod.method)

              coefficients
(Intercept)      28.766764
h(13.8-Girth)    -3.427802
h(Girth-13.8)     6.570747

Selected 3 of 4 terms, and 1 of 1 predictors
Termination condition: RSq changed by less than 0.001 at 4 terms
Importance: Girth
Number of terms at each degree of interaction: 1 2 (additive model)
GCV 14.20145  RSS 309.6832  GRSq 0.949137  RSq 0.9617962  CVRSq 0.9266989

Note: the cross-validation sd's below are standard deviations across folds

Cross validation:   nterms 3.00 sd 0.00    nvars 1.00 sd 0.00

     CVRSq    sd     MaxErr   sd
     0.927 0.043       9.81 7.21

varmod: method "gam" (mgcv package)    min.sd 0.381    iter.rsq 0.269

stddev of predictions:
            coefficients iter.stderr iter.stderr%
(Intercept)     3.806614    0.535912           14
s(Volume).1     0.000000          NA           NA
s(Volume).2     0.000000          NA           NA
s(Volume).3     0.000000          NA           NA
s(Volume).4     0.000000          NA           NA
s(Volume).5     0.000000          NA           NA
s(Volume).6     0.000000          NA           NA
s(Volume).7     0.000000          NA           NA
s(Volume).8     0.000000          NA           NA
s(Volume).9     1.929016          NA           NA

                              mean   smallest    largest      ratio
95% prediction interval   14.92165   5.262808   35.55661   6.756206

                                         68%    80%    90%    95% 
response values in prediction interval   74     90     97     97  
===skipping summary(mgcv::gam) etc.

===varmod.method gam: predict(earth.mod, interval="pint")
         fit       lwr      upr
1   9.913855  7.282451 12.54526
2  10.942195  8.065628 13.81876
3  11.627755  8.587747 14.66776
4  17.455018 13.025754 21.88428
5  18.140578 13.547872 22.73328
6  18.483359 13.808931 23.15779
7  19.168919 14.331050 24.00679
8  19.168919 14.331050 24.00679
9  19.511699 14.592109 24.43129
10 19.854479 14.853168 24.85579
11 20.197259 15.114227 25.28029
12 20.540040 15.375287 25.70479
13 20.540040 15.375287 25.70479
14 21.568380 16.158464 26.97830
15 22.596721 16.941642 28.25180
16 25.681742 19.291175 32.07231
17 25.681742 19.291175 32.07231
18 27.052863 20.335412 33.77031
19 28.423983 21.379649 35.46832
20 28.766764 21.640708 35.89282
21 30.080913 22.641556 37.52027
22 31.395063 23.642404 39.14772
23 33.366287 25.143676 41.58890
24 43.222408 32.650036 53.79478
25 45.193632 34.151308 56.23596
26 51.764379 39.155548 64.37321
27 53.078529 40.156396 66.00066
28 55.706828 42.158092 69.25556
29 56.363903 42.658516 70.06929
30 56.363903 42.658516 70.06929
31 73.447846 55.669540 91.22615
===varmod.method="x.gam"
===varmod.method x.gam: summary(earth.mod)
===summary(earth.mod)
Call: earth(formula=Volume~Girth, data=trees,
            trace=if(varmod.method%in%c("const","lm","power"))0.3els...),
            nfold=3, ncross=3, varmod.method=varmod.method)

              coefficients
(Intercept)      28.766764
h(13.8-Girth)    -3.427802
h(Girth-13.8)     6.570747

Selected 3 of 4 terms, and 1 of 1 predictors
Termination condition: RSq changed by less than 0.001 at 4 terms
Importance: Girth
Number of terms at each degree of interaction: 1 2 (additive model)
GCV 14.20145  RSS 309.6832  GRSq 0.949137  RSq 0.9617962  CVRSq 0.9266989

Note: the cross-validation sd's below are standard deviations across folds

Cross validation:   nterms 3.00 sd 0.00    nvars 1.00 sd 0.00

     CVRSq    sd     MaxErr   sd
     0.927 0.043       9.81 7.21

varmod: method "x.gam" (mgcv package)    min.sd 0.372    iter.rsq 0.346

stddev of predictions:
            coefficients iter.stderr iter.stderr%
(Intercept)     3.721062    0.481473           13
s(Volume).1     0.000000          NA           NA
s(Volume).2     0.000000          NA           NA
s(Volume).3     0.000000          NA           NA
s(Volume).4     0.000000          NA           NA
s(Volume).5     0.000000          NA           NA
s(Volume).6     0.000000          NA           NA
s(Volume).7     0.000000          NA           NA
s(Volume).8     0.000000          NA           NA
s(Volume).9     1.638241          NA           NA

                             mean   smallest    largest     ratio
95% prediction interval   14.5863   4.292688   29.87908   6.96046

                                         68%    80%    90%    95% 
response values in prediction interval   77     90     97     97  
===skipping summary(mgcv::gam) etc.

===varmod.method x.gam: predict(earth.mod, interval="pint")
         fit       lwr      upr
1   9.913855  7.767511 12.06020
2  10.942195  8.483822 13.40057
3  11.627755  8.961363 14.29415
4  17.455018 13.020460 21.88958
5  18.140578 13.498001 22.78316
6  18.483359 13.736771 23.22995
7  19.168919 14.214312 24.12353
8  19.168919 14.214312 24.12353
9  19.511699 14.453082 24.57032
10 19.854479 14.691853 25.01711
11 20.197259 14.930623 25.46390
12 20.540040 15.169394 25.91069
13 20.540040 15.169394 25.91069
14 21.568380 15.885705 27.25106
15 22.596721 16.602016 28.59142
16 25.681742 18.750950 32.61253
17 25.681742 18.750950 32.61253
18 27.052863 19.706032 34.39969
19 28.423983 20.661114 36.18685
20 28.766764 20.899884 36.63364
21 30.080913 22.006014 38.15581
22 31.395063 23.112144 39.67798
23 33.366287 24.771339 41.96123
24 43.222408 33.067314 53.37750
25 45.193632 34.726509 55.66076
26 51.764379 40.257159 63.27160
27 53.078529 41.363289 64.79377
28 55.706828 43.575549 67.83811
29 56.363903 44.128614 68.59919
30 56.363903 44.128614 68.59919
31 73.447846 58.508304 88.38739
> par(old.par)
> 
> source("test.epilog.R")
