# Oct 2020: This is the Rout file generated with "gcc -O2 -mfpmath=387"

> # test.numstab.R: Expose numerical instability of earth if any across platforms.
> #
> # This file was in Oct 2020 created by running earth and plotmo slowtests
> # with earth on Win7 built with "--mfpmath=387" (instead of "-mtune=native"
> # or "-mfpmath=sse -msse2").
> # Differences between the output in the test suites from standard earth
> # were collected and put into this file.
> # So this code duplicates code in earth and plotmo slowtests.
> # Most but not all differences were captured and put into this file.
> 
> source("test.prolog.R")
> 
> library(earth)
Loading required package: Formula
Loading required package: plotmo
Loading required package: plotrix
> library(mda)
Loading required package: class
Loaded mda 0.5-3

> data(ozone1)
> data(trees)
> data(etitanic)
> 
> cat("\n#=== from test.full.R ===========================================\n")

#=== from test.full.R ===========================================
> set.seed(2020)
> 
> PLOT <- TRUE                # TRUE to do plots too, FALSE for speed
> options.old <- options()
> options(warn=1) # print warnings as they occur
> 
> printh <- function(x, expect.warning=FALSE, max.print=0) # like print but with a header
+ {
+     cat("===", deparse(substitute(x)), " ", sep="")
+     if(expect.warning)
+         cat(" expect warning -->")
+     else if (NROW(x) > 1)
+         cat("\n")
+     if (max.print > 0)
+         print(head(x, n=max.print))
+     else
+         print(x)
+ }
> 
> ozone.test <- function(itest, sModel, x, y, degree=2, nk=51,
+                     plotit=PLOT, trace=0, smooth.col="red")
+ {
+     fite <- earth(x, y, degree=degree, nk=nk, trace=trace)
+     fitm <- mars(x, y, degree=degree, nk=nk)
+ 
+     cat("itest",
+         sprint("%-3d", itest),
+         sprint("%-32s", sModel),
+         "degree", sprint("%-2d", degree), "nk", sprint("%-3g", nk),
+         "nTerms",  sprint("%-2d", sum(fite$selected.terms != 0)),
+         "of", sprint("%-3d", nrow(fite$dirs)),
+         "GRSq", sprint("%4.2g", fite$grsq),
+         "GRSq ratio", fite$grsq/mars.to.earth(fitm)$grsq,
+         "\n")
+     caption <- paste("itest ", itest, ": ", sModel, " degree=", degree, " nk=", nk, sep="")
+     printh(summary(fite))
+     printh(summary(fite, style="bf"))
+     if(plotit) {
+         fitme <- mars.to.earth(fitm)
+         plotmo(fite, caption=paste("EARTH", caption), trace=-1)
+         plotmo(fitme, caption=paste("MARS", caption), trace=-1)
+         plot(fite, npoints=500, smooth.col=smooth.col, caption=paste("EARTH", caption), info=TRUE)
+         plot(fitme, caption=paste("MARS", caption), info=TRUE)
+         fitme <- update(fitme)  # generate model selection data
+         plot.earth.models(list(fite, fitme), caption=paste(itest, ": Compare earth to mars ", sModel, sep=""))
+     }
+     fite
+ }
> set.seed(2020)
> data(ozone1)
> attach(ozone1)
> itest <- 1
> 
> set.seed(2020)
> cat("--Expect warning from mda::mars: NAs introduced by coercion\n") # why do we get a warning?
--Expect warning from mda::mars: NAs introduced by coercion
> x.global <- cbind(wind, exp(humidity))
> y <- doy
> # smooth.col is 0 else get loess errors
> # trace==2 so we see "Fixed rank deficient bx by removing 2 terms, 7 terms remain"
> ozone.test(itest, "doy ~ wind+exp(humidity)", x.global, y, degree=1, nk=21, smooth.col=0, trace=2)
x[330,2] with colnames wind x2
y[330,1] with colname y, and values 33, 34, 35, 36, 37, 38, 39, 4...
Forward pass: minspan 5 endspan 8   x[330,2] 5.16 kB   bx[330,21] 54.1 kB

         GRSq    RSq     DeltaRSq Pred     PredName         Cut  Terms   Par Deg
1      0.0000 0.0000                    (Intercept)
2      0.0887 0.1107       0.1107    1         wind           3  2   3         1 
4      0.0906 0.1235      0.01274    1         wind           9  4             1 
6      0.0821 0.1262     0.002668    1         wind           4  5             1 
8      0.0730 0.1285     0.002307    1         wind           6  6             1 
10     0.0633 0.1304     0.001925    2           x2  1.7848e+08< 7             1 
12     0.0543 0.1330     0.002654    2           x2  1.7848e+08< 8             1 
14     0.0451 0.1357     0.002654    2           x2  1.7848e+08< 9             1 
16     0.0327 0.1357            0    -                                       reject (no DeltaRsq)

RSq changed by less than 0.001 at 15 terms, 9 terms used (DeltaRSq 0)
After forward pass GRSq 0.033 RSq 0.136
Forward pass complete: 15 terms, 9 terms used
Fixed rank deficient bx by removing 2 terms, 7 terms remain
Prune backward penalty 2 nprune null: selected 3 of 7 terms, and 1 of 2 preds
After pruning pass GRSq 0.101 RSq 0.123
Warning in storage.mode(tagx) <- "integer" :
  NAs introduced by coercion to integer range
Converted mars(x=x, y=y, degree=degree, nk=nk)

to earth(x=x, y=y, degree=degree, nk=nk)

itest 1   doy ~ wind+exp(humidity)         degree 1  nk 21  nTerms 3  of 7   GRSq  0.1 GRSq ratio 1.318534 
===summary(fite) 
Call: earth(x=x, y=y, trace=trace, degree=degree, nk=nk)

            coefficients
(Intercept)    202.17924
h(3-wind)       50.04004
h(wind-9)      -61.15513

Selected 3 of 7 terms, and 1 of 2 predictors
Termination condition: RSq changed by less than 0.001 at 7 terms
Importance: wind, x2-unused
Number of terms at each degree of interaction: 1 2 (additive model)
GCV 9821.564    RSS 3143644    GRSq 0.1012101    RSq 0.1229323
===summary(fite, style = "bf") 
Call: earth(x=x, y=y, trace=trace, degree=degree, nk=nk)

y =
  202.1792
  + 50.04004 * bf1
  - 61.15513 * bf2

   bf1  h(3-wind)
   bf2  h(wind-9)

Selected 3 of 7 terms, and 1 of 2 predictors
Termination condition: RSq changed by less than 0.001 at 7 terms
Importance: wind, x2-unused
Number of terms at each degree of interaction: 1 2 (additive model)
GCV 9821.564    RSS 3143644    GRSq 0.1012101    RSq 0.1229323
Converted mars(x=x, y=y, degree=degree, nk=nk)

to earth(x=x, y=y, degree=degree, nk=nk)

Selected 3 of 7 terms, and 1 of 2 predictors
Termination condition: RSq changed by less than 0.001 at 7 terms
Importance: wind, x2-unused
Number of terms at each degree of interaction: 1 2 (additive model)
GCV 9821.564    RSS 3143644    GRSq 0.1012101    RSq 0.1229323
> 
> # test Auto.linpreds with data sent in by a user
> ndata <- matrix(data=c(
+ -0.0781, -0.6109, -0.216, -1.5172, 0.8184, -1.1242,
+ -0.0781, -0.5885, -0.216, -1.3501, 0.8184, -0.8703,
+ -0.0781, -0.5885, -0.216, -1.3501, 0.8184, -0.9549,
+ -0.0781, -0.5885, -0.216, -1.3501, 1.4136, -0.8703,
+ -2.5759, -0.5885, 1.1665, -1.3501, 2.0089, -0.9549,
+ -2.5759, -0.5885, 1.1665, -1.3501, 2.0089, -0.8703,
+ -0.0781, -0.4937, -0.216, -0.9949, -0.372, -1.0396,
+ -0.0781, -0.4463, -0.216, -0.8278, -0.372, -0.447,
+ -0.0781, -0.4463, -0.216, -0.8278, -0.372, -0.701,
+ -0.0781, -0.4463, -0.216, -0.8278, -0.372, -0.6163,
+ -0.0781, -0.4463, -0.216, -0.8278, 0.8184, -0.447,
+ -0.0781, -0.4463, -0.216, -0.8278, 0.8184, -0.6163,
+ -0.0781, -0.4463, 1.1665, -0.8278, 0.8184, -0.447,
+ -0.0781, -0.4379, 1.1665, 0.2585, -0.372, -0.1085,
+ -0.0781, -0.2147, 1.1665, 0.0496, -0.372, -0.1085,
+ -0.0781, -0.2147, -0.216, 0.2585, -0.372, -0.0238,
+ -0.0781, -0.1589, -0.216, 0.2585, -0.372, -0.1931,
+ -0.0781, -0.1589, -0.216, 0.2585, -0.372, -0.1085,
+ -0.0781, -0.1589, 1.1665, 0.2585, -0.372, -0.1931,
+ -0.0781, -0.1589, -0.216, 0.2585, 0.8184, -0.1085,
+ -0.0781, -0.1589, -0.216, 0.2585, 0.8184, 0.0608,
+ -0.0781, -0.1589, -0.216, 1.0942, 0.8184, -0.0238,
+ -0.0781, 0.0643, 1.1665, 1.0942, -0.372, 0.2301,
+ -0.0781, 0.0643, -0.216, 1.0942, -1.5624, 0.3148,
+ -0.0781, 0.0643, -0.216, 1.0942, -0.9672, 0.1455,
+ -0.0781, 0.0643, 1.1665, 1.4284, 0.2232, 0.4841,
+ -0.0781, 0.1563, -0.216, 1.0942, -0.372, 0.5687,
+ 2.4197, 0.3432, -0.216, 1.0942, -1.5624, 1.0766,
+ -0.0781, 0.3432, -0.216, 1.0942, -1.5624, 1.1613,
+ -0.0781, 0.3432, 1.1665, 1.0942, 0.2232, 0.738,
+ 2.4197, 2.7145, -2.9811, 1.0942, -1.5624, 2.5156,
+ 2.4197, 4.3884, -2.9811, 1.0942, -1.5624, 3.5314),
+ ncol=6)
> colnames(ndata) <- c("x1", "x2", "x3", "x4", "x5", "y")
> ndata <- as.data.frame(ndata)
> 
> set.seed(2020)
> cat("Auto.linpreds=TRUE pmethod=\"none\":\n")
Auto.linpreds=TRUE pmethod="none":
> # trace==2 so we see "Fixed rank deficient bx by removing terms"
> # TODO why are we getting the rank deficient message?
> auto.linpreds.true.pmethod.none <- earth(y~., data=ndata, degree=2, nk=21, trace=2, pmethod="none")
x[32,5] with colnames x1 x2 x3 x4 x5
y[32,1] with colname y, and values -0.372, 0.5687, 2.42, 0.3432,...
Forward pass: minspan 4 endspan 9   x[32,5] 1.25 kB   bx[32,21] 5.25 kB

         GRSq    RSq     DeltaRSq Pred     PredName         Cut  Terms   Par Deg
1      0.0000 0.0000                    (Intercept)
2      0.0583 0.3376       0.3376    1           x1     -0.9549  2   3         1 
4     -0.2012 0.4488       0.1112    3           x3     -0.0781  4   5         1 
6     -0.1801 0.5797        0.131    4           x4      -0.372< 6         2   2 
8     -0.2458 0.6681      0.08841    3           x3     -0.8278< 7         2   2 
10    -0.4175 0.7312      0.06304    3           x3     -0.8278< 8         3   2 
12    -0.8452 0.7677      0.03649    1           x1      -0.216  9             1 
14    -1.7625 0.7923      0.02464    1           x1     -2.5759< 10        5   2 
16   -13.8221 0.8111      0.01875    2           x2      -0.216  11  12        1 reject (negative GRSq)

Reached minimum GRSq -10 at 15 terms, 10 terms used (GRSq -14)
After forward pass GRSq -13.822 RSq 0.811
Forward pass complete: 15 terms, 10 terms used
Prune none penalty 3 nprune null: selected 10 of 10 terms, and 3 of 5 preds
After pruning pass GRSq -1.76 RSq 0.792
> print(summary(auto.linpreds.true.pmethod.none, decomp="none"))
Call: earth(formula=y~., data=ndata, pmethod="none", trace=2, degree=2, nk=21)

                    coefficients
(Intercept)             2.433296
h(x1- -0.9549)         -1.423639
h(-0.9549-x1)           7.344119
h(x3- -0.0781)         -3.590121
h(-0.0781-x3)          -5.326988
h(x1- -0.9549) * x4    -1.258111
h(x1- -0.9549) * x3     0.881005
h(-0.9549-x1) * x3     30.606021
h(x1- -0.216)           1.862792
x1 * h(-0.0781-x3)     -5.595905

Selected 10 of 10 terms, and 3 of 5 predictors (pmethod="none")
Termination condition: GRSq -10 at 10 terms
Importance: x1, x4, x3, x2-unused, x5-unused
Number of terms at each degree of interaction: 1 5 4
GCV 8.371258    RSS 18.90073    GRSq -1.762519    RSq 0.792308
> cat("\nAuto.linpreds=FALSE pmethod=\"none\":\n")

Auto.linpreds=FALSE pmethod="none":
> auto.linpreds.false.pmethod.none <- earth(y~., data=ndata, degree=2, nk=21, trace=2, Auto.linpreds=FALSE, pmethod="none")
x[32,5] with colnames x1 x2 x3 x4 x5
y[32,1] with colname y, and values -0.372, 0.5687, 2.42, 0.3432,...
Forward pass: minspan 4 endspan 9   x[32,5] 1.25 kB   bx[32,21] 5.25 kB

         GRSq    RSq     DeltaRSq Pred     PredName         Cut  Terms   Par Deg
1      0.0000 0.0000                    (Intercept)
2      0.0583 0.3376       0.3376    1           x1     -0.9549  2   3         1 
4     -0.2012 0.4488       0.1112    3           x3     -0.0781  4   5         1 
6     -0.1801 0.5797        0.131    4           x4      -0.372< 6         2   2 
8     -0.2458 0.6681      0.08841    3           x3     -0.8278< 7         2   2 
10    -0.4175 0.7312      0.06304    3           x3     -0.8278< 8         3   2 
12    -0.8452 0.7677      0.03649    1           x1      -0.216  9             1 
14    -1.7625 0.7923      0.02464    1           x1     -2.5759< 10        5   2 
16   -13.8221 0.8111      0.01875    2           x2      -0.216  11  12        1 reject (negative GRSq)

Reached minimum GRSq -10 at 15 terms, 12 terms used (GRSq -14)
After forward pass GRSq -13.822 RSq 0.811
Forward pass complete: 15 terms, 12 terms used
Fixed rank deficient bx by removing 2 terms, 10 terms remain
Prune none penalty 3 nprune null: selected 10 of 10 terms, and 3 of 5 preds
After pruning pass GRSq -1.76 RSq 0.792
> print(summary(auto.linpreds.false.pmethod.none, decomp="none"))
Call: earth(formula=y~., data=ndata, pmethod="none", trace=2, degree=2, nk=21,
            Auto.linpreds=FALSE)

                                coefficients
(Intercept)                         2.433296
h(x1- -0.9549)                     -1.684918
h(-0.9549-x1)                     -17.991545
h(x3- -0.0781)                     -3.590121
h(-0.0781-x3)                       9.087502
h(x1- -0.9549) * h(x4- -0.372)     -1.258111
h(x1- -0.9549) * h(x3- -0.8278)     0.881005
h(-0.9549-x1) * h(x3- -0.8278)     30.606021
h(x1- -0.216)                       1.862792
h(x1- -2.5759) * h(-0.0781-x3)     -5.595905

Selected 10 of 10 terms, and 3 of 5 predictors (pmethod="none")
Termination condition: GRSq -10 at 10 terms
Importance: x1, x4, x3, x2-unused, x5-unused
Number of terms at each degree of interaction: 1 5 4
GCV 8.371258    RSS 18.90073    GRSq -1.762519    RSq 0.792308
> stopifnot(isTRUE(all.equal(predict(auto.linpreds.true.pmethod.none), predict(auto.linpreds.false.pmethod.none))))
> 
> set.seed(2020)
> cat("\nAuto.linpreds=TRUE:\n")

Auto.linpreds=TRUE:
> auto.linpreds.true <- earth(y~., data=ndata, degree=2, nk=21, trace=2)
x[32,5] with colnames x1 x2 x3 x4 x5
y[32,1] with colname y, and values -0.372, 0.5687, 2.42, 0.3432,...
Forward pass: minspan 4 endspan 9   x[32,5] 1.25 kB   bx[32,21] 5.25 kB

         GRSq    RSq     DeltaRSq Pred     PredName         Cut  Terms   Par Deg
1      0.0000 0.0000                    (Intercept)
2      0.0583 0.3376       0.3376    1           x1     -0.9549  2   3         1 
4     -0.2012 0.4488       0.1112    3           x3     -0.0781  4   5         1 
6     -0.1801 0.5797        0.131    4           x4      -0.372< 6         2   2 
8     -0.2458 0.6681      0.08841    3           x3     -0.8278< 7         2   2 
10    -0.4175 0.7312      0.06304    3           x3     -0.8278< 8         3   2 
12    -0.8452 0.7677      0.03649    1           x1      -0.216  9             1 
14    -1.7625 0.7923      0.02464    1           x1     -2.5759< 10        5   2 
16   -13.8221 0.8111      0.01875    2           x2      -0.216  11  12        1 reject (negative GRSq)

Reached minimum GRSq -10 at 15 terms, 10 terms used (GRSq -14)
After forward pass GRSq -13.822 RSq 0.811
Forward pass complete: 15 terms, 10 terms used
Prune backward penalty 3 nprune null: selected 4 of 10 terms, and 3 of 5 preds
After pruning pass GRSq 0.209 RSq 0.546
> print(summary(auto.linpreds.true, decomp="none"))
Call: earth(formula=y~., data=ndata, trace=2, degree=2, nk=21)

                    coefficients
(Intercept)             1.371239
h(x3- -0.0781)         -1.882810
h(x1- -0.9549) * x4    -1.413220
h(-0.9549-x1) * x3      4.319452

Selected 4 of 10 terms, and 3 of 5 predictors
Termination condition: GRSq -10 at 10 terms
Importance: x1, x4, x3, x2-unused, x5-unused
Number of terms at each degree of interaction: 1 1 2
GCV 2.396481    RSS 41.35802    GRSq 0.20916    RSq 0.5455344
> cat("\nAuto.linpreds=FALSE:\n")

Auto.linpreds=FALSE:
> auto.linpreds.false <- earth(y~., data=ndata, degree=2, nk=21, trace=2, Auto.linpreds=FALSE)
x[32,5] with colnames x1 x2 x3 x4 x5
y[32,1] with colname y, and values -0.372, 0.5687, 2.42, 0.3432,...
Forward pass: minspan 4 endspan 9   x[32,5] 1.25 kB   bx[32,21] 5.25 kB

         GRSq    RSq     DeltaRSq Pred     PredName         Cut  Terms   Par Deg
1      0.0000 0.0000                    (Intercept)
2      0.0583 0.3376       0.3376    1           x1     -0.9549  2   3         1 
4     -0.2012 0.4488       0.1112    3           x3     -0.0781  4   5         1 
6     -0.1801 0.5797        0.131    4           x4      -0.372< 6         2   2 
8     -0.2458 0.6681      0.08841    3           x3     -0.8278< 7         2   2 
10    -0.4175 0.7312      0.06304    3           x3     -0.8278< 8         3   2 
12    -0.8452 0.7677      0.03649    1           x1      -0.216  9             1 
14    -1.7625 0.7923      0.02464    1           x1     -2.5759< 10        5   2 
16   -13.8221 0.8111      0.01875    2           x2      -0.216  11  12        1 reject (negative GRSq)

Reached minimum GRSq -10 at 15 terms, 12 terms used (GRSq -14)
After forward pass GRSq -13.822 RSq 0.811
Forward pass complete: 15 terms, 12 terms used
Fixed rank deficient bx by removing 2 terms, 10 terms remain
Prune backward penalty 3 nprune null: selected 5 of 10 terms, and 3 of 5 preds
After pruning pass GRSq 0.223 RSq 0.643
> print(summary(auto.linpreds.false, decomp="none"))
Call: earth(formula=y~., data=ndata, trace=2, degree=2, nk=21,
            Auto.linpreds=FALSE)

                               coefficients
(Intercept)                        1.635321
h(-0.9549-x1)                    -12.155291
h(x3- -0.0781)                    -1.555091
h(x1- -0.9549) * h(x4- -0.372)    -1.220702
h(-0.9549-x1) * h(x3- -0.8278)    22.975120

Selected 5 of 10 terms, and 3 of 5 predictors
Termination condition: GRSq -10 at 10 terms
Importance: x1, x4, x3, x2-unused, x5-unused
Number of terms at each degree of interaction: 1 2 2
GCV 2.354961    RSS 32.4543    GRSq 0.2228618    RSq 0.6433736
> # following fails because of different pruning because of different term count
> # stopifnot(isTRUE(all.equal(predict(auto.linpreds.true), predict(auto.linpreds.false))))
> 
> cat("\n#=== from test.weights.R ===========================================\n")

#=== from test.weights.R ===========================================
> set.seed(2020)
> 
> noise <- .01 * c(1,2,3,2,1,3,5,2,0)
> data <- data.frame(x1=c(1,2,3,4,5,6,7,8,9), x2=c(1,2,3,3,3,6,7,8,9), y=(1:9)+noise)
> data[5,] <- c(5, 5, 6)
> colnames(data) <- c("x1", "x2", "y")
> 
> a21.noweights <- earth(y~., data=data, # no weights for comparison
+                        minspan=1, endspan=1, penalty=-1, thresh=1e-8, trace=-1)
> print(summary(a21.noweights))
Call: earth(formula=y~., data=data, trace=-1, minspan=1, endspan=1, penalty=-1,
            thresh=1e-08)

            coefficients
(Intercept)        5.545
h(x1-3)            0.475
h(5-x1)           -0.515
h(x1-5)           -0.940
h(x1-7)           -0.050
h(x1-8)            0.010
h(6-x2)           -0.495
h(x2-6)            1.485

Selected 8 of 8 terms, and 2 of 2 predictors
Termination condition: Reached maximum RSq 1.0000 at 8 terms
Importance: x1, x2
Number of terms at each degree of interaction: 1 7 (additive model)
GCV 0    RSS 0    GRSq 1    RSq 1
> weights <- c(1, 1, 1, 1, .5, 1, 1, 1, 1)
> a10  <- earth(y~., data=data, weights=weights,
+               minspan=1, endspan=1, penalty=-1, thresh=1e-8, trace=-1)
> print(summary(a10))
Call: earth(formula=y~., data=data, weights=weights, trace=-1, minspan=1,
            endspan=1, penalty=-1, thresh=1e-08)

            coefficients
(Intercept)        5.070
h(5-x1)           -0.990
h(x1-5)           -0.465
h(x1-8)            0.010
h(x2-3)            0.475
h(6-x2)           -0.020
h(x2-6)            1.010
h(x2-7)           -0.050

Selected 8 of 8 terms, and 2 of 2 predictors
Termination condition: Reached maximum RSq 1.0000 at 8 terms
Importance: x1, x2
Weights: 1, 1, 1, 1, 0.5, 1, 1, 1, 1
Number of terms at each degree of interaction: 1 7 (additive model)
GCV 0    RSS 0    GRSq 1    RSq 1
> 
> cat("\n#=== from test.glm.R ===========================================\n")

#=== from test.glm.R ===========================================
> 
> cat("a12: compare family=gaussian to standard earth model with two responses\n\n")
a12: compare family=gaussian to standard earth model with two responses

> a12 <- earth(cbind(etitanic$sex, (as.integer(etitanic$age)^2)) ~ ., data=etitanic, degree=2, glm=list(family="gaussian"), trace=4)
Call: earth(formula=cbind(etitanic$sex,(as.integer(etitanic$age)^2))~.,
            data=etitanic, trace=4, glm=list(family="gaussian"), degree=2)

x[1046,5]:
     pclass2nd pclass3rd survived sibsp parch
1            0         0        1     0     0
2            0         0        1     1     2
3            0         0        0     1     2
...          0         0        0     1     2
1046         0         1        0     0     0

y[1046,2]:
     y1  y2
1     1 841
2     2   0
3     1   4
...   2 900
1046  2 841

Forward pass: minspan 6 endspan 9   x[1046,5] 40.9 kB   bx[1046,21] 172 kB

         GRSq    RSq     DeltaRSq Pred     PredName         Cut  Terms   Par Deg
1      0.0000 0.0000                    (Intercept)
2      0.1060 0.1103       0.1103    2    pclass3rd           0< 2             1 
4      0.1653 0.1733      0.06303    1    pclass2nd           0< 3             1 
6      0.2059 0.2210      0.04767    5        parch           2  4   5         1 
8      0.2210 0.2396      0.01859    3     survived           0< 6             1 
10     0.2223 0.2481     0.008525    5        parch           2  7   8     2   2 
12     0.2234 0.2565      0.00837    4        sibsp           1  9   10        1 
14     0.2219 0.2587     0.002244    5        parch           1  11        2   2 
16     0.2231 0.2635     0.004765    5        parch           1  12            1 
18     0.2226 0.2702     0.006718    5        parch           1  13  14    4   2 
20     0.2232 0.2743      0.00415    1    pclass2nd           0< 15        7   2 final (reached nk 21)

Reached nk 21
After forward pass GRSq 0.223 RSq 0.274
Forward pass complete: 21 terms, 15 terms used

Using EvalSubsetsUsingXtx (rather than leaps) because this is a multiple response model
     nTerms iTerm    DeltaRss     RSq
         15     2 2.8941e+007  0.2461 min
         15     3 4.1155e+005  0.2739 min
         15     4 3.0548e+007  0.2445
         15     5 1.9834e+006  0.2724
         15     6 2.0705e+007  0.2541
         15     7 2.4297e+007  0.2506
         15     8 6.3042e+006  0.2682
         15     9 6.3458e+006  0.2681
         15    10      8231.6  0.2743 min
         15    11 1.5243e+007  0.2594
         15    12 1.4677e+007  0.2600
         15    13 7.8586e+005  0.2736
         15    14  5.964e+006  0.2685
         15    15 4.2448e+006  0.2702

         14     2 2.9403e+007  0.2456 min
         14     3 4.0992e+005  0.2739 min
         14     4 3.0578e+007  0.2444
         14     5 1.9758e+006  0.2724
         14     6  2.074e+007  0.2541
         14     7 2.4514e+007  0.2504
         14     8 6.4997e+006  0.2680
         14     9  6.995e+006  0.2675
         14    11 1.5597e+007  0.2591
         14    12  1.467e+007  0.2600
         14    13 7.8286e+005  0.2736
         14    14 5.9908e+006  0.2685
         14    15 4.2486e+006  0.2702

         13     2 2.9318e+007  0.2453 min
         13     4 3.4284e+007  0.2404
         13     5 1.9771e+006  0.2720 min
         13     6 2.0927e+007  0.2535
         13     7 2.5764e+007  0.2488
         13     8 6.4562e+006  0.2676
         13     9  7.283e+006  0.2668
         13    11 1.5187e+007  0.2591
         13    12 1.4282e+007  0.2600
         13    13 7.6983e+005  0.2732 min
         13    14 1.6727e+007  0.2576
         13    15 3.4888e+007  0.2398

         12     2 2.9426e+007  0.2444 min
         12     4 3.7551e+007  0.2365
         12     5  1.982e+006  0.2712 min
         12     6 2.1198e+007  0.2525
         12     7 2.7686e+007  0.2461
         12     8 6.4937e+006  0.2668
         12     9  7.059e+006  0.2663
         12    11 1.8035e+007  0.2556
         12    12  1.855e+007  0.2551
         12    14  1.673e+007  0.2568
         12    15 3.4909e+007  0.2391

         11     2 4.1892e+007  0.2303 min
         11     4 4.4477e+007  0.2278
         11     6 2.0636e+007  0.2511 min
         11     7 2.7648e+007  0.2442
         11     8 5.1374e+006  0.2662 min
         11     9 7.0891e+006  0.2643
         11    11 1.9442e+007  0.2522
         11    12 4.2683e+007  0.2295
         11    14 1.7813e+007  0.2538
         11    15 4.0764e+007  0.2314

         10     2 1.9259e+008  0.0780 min
         10     4 4.3937e+007  0.2233 min
         10     6 2.3667e+007  0.2431 min
         10     7 2.2527e+007  0.2442 min
         10     9 1.1557e+007  0.2549 min
         10    11 1.5895e+007  0.2507
         10    12 4.1804e+007  0.2254
         10    14 1.7356e+007  0.2493
         10    15 4.0292e+007  0.2268

          9     2 1.9815e+008  0.0612 min
          9     4 4.6123e+007  0.2098 min
          9     6 2.1283e+007  0.2341 min
          9     7 1.6444e+007  0.2389 min
          9    11 9.1512e+006  0.2460 min
          9    12 4.5701e+007  0.2103
          9    14 1.8792e+007  0.2366
          9    15 4.2341e+007  0.2135

          8     2 1.9213e+008  0.0582 min
          8     4 3.6978e+007  0.2098 min
          8     6 2.2769e+007  0.2237 min
          8     7 7.3146e+006  0.2388 min
          8    12 4.2428e+007  0.2045
          8    14 1.7261e+007  0.2291
          8    15 3.8504e+007  0.2083

          7     2 2.0358e+008  0.0398 min
          7     4 5.1358e+007  0.1886 min
          7     6  2.259e+007  0.2167 min
          7    12  3.956e+007  0.2002
          7    14 1.7694e+007  0.2215 min
          7    15 3.9765e+007  0.2000

          6     2 1.9363e+008  0.0322 min
          6     4  4.834e+007  0.1743 min
          6     6 2.7208e+007  0.1949 min
          6    12 3.6257e+007  0.1861
          6    15  6.052e+007  0.1624

          5     2 1.6654e+008  0.0321 min
          5     4 5.3902e+007  0.1422 min
          5    12 4.0495e+007  0.1553 min
          5    15 4.6141e+007  0.1498

          4     2 1.4973e+008  0.0090 min
          4     4 1.3585e+007  0.1421 min
          4    15 3.1228e+007  0.1248

          3     2 1.4521e+008  0.0001 min
          3    15 3.2522e+007  0.1103 min

          2     2 1.1281e+008 -0.0000 min

Subset size        GRSq     RSq  DeltaGRSq nPreds  Terms (col nbr in bx)
          1      0.0000  0.0000     0.0000      0  1
          2      0.1060  0.1103     0.1060      1  1 2
          3      0.1338  0.1421     0.0278      3  1 2 15
          4      0.1431  0.1553     0.0093      3  1 2 4 15
          5      0.1793  0.1949     0.0362      3  1 2 4 12 15
          6      0.2026  0.2215     0.0233      4  1 2 4 6 12 15
          7      0.2165  0.2388     0.0139      4  1 2 4 6 12 14 15
          8      0.2201  0.2460     0.0036      4  1 2 4 6 7 12 14 15
          9      0.2256  0.2549     0.0055      4  1 2 4 6 7 11 12 14 15
         10      0.2336  0.2662     0.0080      5  1 2 4 6 7 9 11 12 14 15
chosen   11      0.2351  0.2712     0.0015      5  1 2 4 6 7 8 9 11 12 14 15
         12      0.2334  0.2732    -0.0017      5  1 2 4 5 6 7 8 9 11 12 14 15
         13      0.2304  0.2739    -0.0030      5  1 2 4 5 6 7 8 9 11 12 13 14 15
         14      0.2270  0.2743    -0.0034      5  1 2 3 4 5 6 7 8 9 11 12 13 14 15
         15      0.2232  0.2743    -0.0038      5  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Prune backward penalty 3 nprune null: selected 11 of 15 terms, and 5 of 5 preds
After pruning pass GRSq 0.235 RSq 0.271

glm y[1046,1]:
     y1
1     1
2     2
3     1
...   2
1046  2

glm weights: NULL

GLM y1 devratio 0.32 dof 1035/1045 iters 2
glm y[1046,1]:
      y2
1    841
2      0
3      4
...  900
1046 841

glm weights: NULL

GLM y2 devratio 0.27 dof 1035/1045 iters 2
> cat("\nsummary(a12, details=TRUE)\n\n", sep="")

summary(a12, details=TRUE)

> print(summary(a12, details=TRUE))
Call: earth(formula=cbind(etitanic$sex,(as.integer(etitanic$age)^2))~.,
            data=etitanic, trace=4, glm=list(family="gaussian"), degree=2)

Earth coefficients
                                y1         y2
(Intercept)             1.87118586  2022.0223
pclass3rd              -0.24639972 -1720.7486
survived               -0.52209649  -313.2841
h(sibsp-1)              0.01624482  -159.5522
h(parch-1)             -0.11109875  -995.9459
h(parch-2)              0.05537183  2458.2205
pclass2nd * h(1-parch)  0.09821900  1359.2200
pclass2nd * h(2-parch) -0.05519696 -1055.2056
pclass3rd * h(parch-2) -0.03777698 -2620.2967
pclass3rd * h(2-parch)  0.13171785   311.8731
pclass3rd * h(parch-1)  0.04818545  1443.0507

GLM coefficients
                                y1         y2
(Intercept)             1.87118586  2022.0223
pclass3rd              -0.24639972 -1720.7486
survived               -0.52209649  -313.2841
h(sibsp-1)              0.01624482  -159.5522
h(parch-1)             -0.11109875  -995.9459
h(parch-2)              0.05537183  2458.2205
pclass2nd * h(1-parch)  0.09821900  1359.2200
pclass2nd * h(2-parch) -0.05519696 -1055.2056
pclass3rd * h(parch-2) -0.03777698 -2620.2967
pclass3rd * h(2-parch)  0.13171785   311.8731
pclass3rd * h(parch-1)  0.04818545  1443.0507

GLM y1 deviance residuals:
       Min          1Q      Median          3Q         Max  
-0.9044667  -0.3369144   0.1117782   0.1409891   0.9602237  

GLM y1 coefficients (family gaussian, link identity)
                          Estimate  Std. Error   t value Pr(>|t|)
(Intercept)             1.87118586  0.03058203  61.18579  < 2e-16
pclass3rd              -0.24639972  0.10657574  -2.31197 0.020975
h(parch-2)              0.05537183  0.14776100   0.37474 0.707931
survived               -0.52209649  0.02764612 -18.88498  < 2e-16
pclass3rd * h(parch-2) -0.03777698  0.19976891  -0.18910 0.850049
pclass3rd * h(2-parch)  0.13171785  0.05515850   2.38799 0.017119
h(sibsp-1)              0.01624482  0.02402220   0.67624 0.499038
pclass3rd * h(parch-1)  0.04818545  0.13119405   0.36728 0.713482
h(parch-1)             -0.11109875  0.06110990  -1.81802 0.069351
pclass2nd * h(1-parch)  0.09821900  0.12909899   0.76080 0.446948
pclass2nd * h(2-parch) -0.05519696  0.06625249  -0.83313 0.404963

GLM y1 dispersion parameter for gaussian family taken to be 0.1606984

GLM y2 deviance residuals:
       Min          1Q      Median          3Q         Max  
-1733.0223   -501.0223   -187.7382    299.9801   4691.2618  

GLM y2 coefficients (family gaussian, link identity)
                          Estimate  Std. Error  t value   Pr(>|t|)
(Intercept)             2022.02230    64.74562 31.23026 < 2.22e-16
pclass3rd              -1720.74861   225.63289 -7.62632 5.4561e-14
h(parch-2)              2458.22048   312.82673  7.85809 9.7355e-15
survived                -313.28406    58.52996 -5.35254 1.0679e-07
pclass3rd * h(parch-2) -2620.29668   422.93336 -6.19553 8.3706e-10
pclass3rd * h(2-parch)   311.87310   116.77677  2.67068  0.0076885
h(sibsp-1)              -159.55220    50.85771 -3.13723  0.0017537
pclass3rd * h(parch-1)  1443.05073   277.75263  5.19545 2.4589e-07
h(parch-1)              -995.94592   129.37656 -7.69804 3.2160e-14
pclass2nd * h(1-parch)  1359.21996   273.31715  4.97305 7.7115e-07
pclass2nd * h(2-parch) -1055.20558   140.26401 -7.52300 1.1597e-13

GLM y2 dispersion parameter for gaussian family taken to be 720277.3

GLM (family gaussian, link identity):
       nulldev   df         dev   df   devratio     AIC iters converged
y1 2.44077e+02 1045 1.66323e+02 1035      0.319    1069     2         1
y2 1.02296e+09 1045 7.45487e+08 1035      0.271   17090     2         1

Earth selected 11 of 15 terms, and 5 of 5 predictors
Termination condition: Reached nk 21
Importance: pclass3rd, pclass2nd, parch, survived, sibsp
Number of terms at each degree of interaction: 1 5 5

Earth
          GCV       RSS      GRSq       RSq
y1       0.17       166 0.2847494 0.3185625
y2  749499.58 745486964 0.2350858 0.2712468
All 749499.75 745487130 0.2350858 0.2712468
> 
> cat("\n#=== from test.plotmo.R ===========================================\n")

#=== from test.plotmo.R ===========================================
> 
> # check various types of predictors with grid.func and ndiscrete
> 
> varied.type.data <- data.frame(
+     y    = 1:13,
+     num  = c(1, 3, 2, 3, 4, 5, 6, 4, 5, 6.5, 3, 6, 5), # 7 unique values (but one is non integral)
+     int  = c(1L, 1L, 3L, 3L, 4L, 4L, 3L, 5L, 3L, 6L, 7L, 8L, 10L), # 8 unique values
+     bool = c(F, F, F, F, F, T, T, T, T, T, T, T, T),
+     date = as.Date(
+            c("2018-08-01", "2018-08-02", "2018-08-03",
+              "2018-08-04", "2018-08-05", "2018-08-06",
+              "2018-08-07", "2018-08-08", "2018-08-08",
+              "2018-08-08", "2018-08-10", "2018-08-11", "2018-08-11")),
+     ord  = ordered(c("ord3", "ord3", "ord3",
+                      "ord1", "ord2", "ord3",
+                      "ord1", "ord2", "ord3",
+                      "ord1", "ord1", "ord1", "ord1"),
+                    levels=c("ord1", "ord3", "ord2")),
+     fac  = as.factor(c("fac1", "fac1", "fac1",
+                        "fac2", "fac2", "fac2",
+                        "fac3", "fac3", "fac3",
+                        "fac1", "fac2", "fac3", "fac3")),
+     str  = c("str1", "str1", "str1", # will be treated like a factor
+              "str2", "str2", "str2",
+              "str3", "str3", "str3",
+              "str3", "str3", "str3", "str3"))
> 
> varied.type.earth <- earth(y ~ ., data = varied.type.data, thresh=0, penalty=-1, trace=1)
x[13,10] with colnames num int boolTRUE date ord.L ord.Q facfac2 facfac3 strstr2...
y[13,1] with colname y, and values 1, 2, 3, 4, 5, 6, 7, 8, 9, 10...
Forward pass term 1, 2, 4, 6, 8, 10, 12, 14, 16, 18
Reached maximum RSq 1.0000 at 19 terms, 13 terms used (RSq 1.0000)
After forward pass GRSq 1.000 RSq 1.000
Prune backward penalty -1 nprune null: selected 13 of 13 terms, and 9 of 10 preds
After pruning pass GRSq 1 RSq 1
> print(summary(varied.type.earth))
Call: earth(formula=y~., data=varied.type.data, trace=1, thresh=0, penalty=-1)

              coefficients
(Intercept)      9.5964912
boolTRUE        -2.0473684
ord.L            0.4986964
ord.Q            0.0859470
facfac2         -4.4157895
facfac3         -3.1526316
strstr2          3.2526316
h(4-num)         1.4105263
h(num-4)        -0.3157895
h(4-int)         2.1157895
h(int-4)         0.3421053
h(17749-date)   -3.8210526
h(date-17749)    1.4368421

Selected 13 of 13 terms, and 9 of 10 predictors
Termination condition: Reached maximum RSq 1.0000 at 13 terms
Importance: date, facfac2, facfac3, int, strstr2, num, boolTRUE, ord.L, ...
Number of terms at each degree of interaction: 1 12 (additive model)
GCV 0    RSS 0    GRSq 1    RSq 1
> 
> cat("\n#=== from test.plotmo.args.R ===========================================\n")

#=== from test.plotmo.args.R ===========================================
> set.seed(2020)
> 
> oz2 <- ozone1[1:40,]
> set.seed(2015)
> a <- earth(O3~temp+wind, dat=oz2, deg=2, nk=21, ncr=3, nfo=3, varmod.me="lm")
> print(summary(a))
Call: earth(formula=O3~temp+wind, data=oz2, degree=2, nfold=3, ncross=3,
            varmod.method="lm", nk=21)

            coefficients
(Intercept)    3.8636364
h(temp-42)     0.1581028

Selected 2 of 13 terms, and 1 of 2 predictors
Termination condition: Reached nk 21
Importance: temp, wind-unused
Number of terms at each degree of interaction: 1 1 (additive model)
GCV 4.813872  RSS 160.332  GRSq 0.1971602  RSq 0.2967894  CVRSq -0.04034235

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

Cross validation:   nterms 2.11 sd 0.33    nvars 1.22 sd 0.44

     CVRSq    sd     MaxErr   sd
     -0.04 0.285        8.7 4.68

varmod: method "lm"    min.sd 0.253    iter.rsq 0.069

stddev of predictions:
            coefficients iter.stderr iter.stderr%
(Intercept)    1.0479440    0.855721           82
O3             0.2703642    0.161551           60

                             mean   smallest    largest      ratio
95% prediction interval   9.93681   8.202579   12.72666   1.551544

                                         68%    80%     90%     95% 
response values in prediction interval   78     92     100     100  
> plotmo(a, caption.col=3, caption.font=2, grid.col="pink",
+        level=.8, SHOWCALL=TRUE)
 plotmo grid:    temp wind
                 53.5    4
> 
> cat("\n#=== from test.plotmo3.R ===========================================\n")

#=== from test.plotmo3.R ===========================================
> set.seed(2020)
> 
> # basic tests of plotmo on abbreviated titanic data
> 
> get.tita <- function()
+ {
+     tita <- etitanic
+     pclass <- as.character(tita$pclass)
+     # change the order of the factors so not alphabetical
+     pclass[pclass == "1st"] <- "first"
+     pclass[pclass == "2nd"] <- "class2"
+     pclass[pclass == "3rd"] <- "classthird"
+     tita$pclass <- factor(pclass, levels=c("class2", "classthird", "first"))
+     # log age is so we have a continuous predictor even when model is age~.
+     set.seed(2015)
+     tita$logage <- log(tita$age) + rnorm(nrow(tita))
+     tita$parch <- NULL
+     # by=12 gives us a small fast model with an additive and a interaction term
+     tita[seq(1, nrow(etitanic), by=12), ]
+ }
> tita <- get.tita()
> # tita[,4] is age
> set.seed(2020)
> mod.earth.tita.age <- earth(tita[,-4], tita[,4], degree=2, nfold=3, ncross=3, trace=.5, varmod.method="lm")
Model with pmethod="backward": GRSq 0.335 RSq 0.512 nterms 6
CV fold  1.1  CVRSq -0.047   n.oof 58 34%   n.infold.nz 58 100%   n.oof.nz 30 100%
CV fold  1.2  CVRSq -0.022   n.oof 59 33%   n.infold.nz 59 100%   n.oof.nz 29 100%
CV fold  1.3  CVRSq -0.045   n.oof 59 33%   n.infold.nz 59 100%   n.oof.nz 29 100%
CV fold  2.1  CVRSq  0.133   n.oof 58 34%   n.infold.nz 58 100%   n.oof.nz 30 100%
CV fold  2.2  CVRSq  0.338   n.oof 59 33%   n.infold.nz 59 100%   n.oof.nz 29 100%
CV fold  2.3  CVRSq  0.149   n.oof 59 33%   n.infold.nz 59 100%   n.oof.nz 29 100%
CV fold  3.1  CVRSq  0.419   n.oof 58 34%   n.infold.nz 58 100%   n.oof.nz 30 100%
CV fold  3.2  CVRSq  0.107   n.oof 59 33%   n.infold.nz 59 100%   n.oof.nz 29 100%
CV fold  3.3  CVRSq -0.048   n.oof 59 33%   n.infold.nz 59 100%   n.oof.nz 29 100%
CV all        CVRSq  0.109                  n.infold.nz 88 100%


varmod method="lm" rmethod="hc12" lambda=1 exponent=1 conv=1 clamp=0.1 minspan=-3:
     iter weight.ratio coefchange% (Intercept) tita[, 4]
        1          1.2        0.00          13   -0.0158
        2          1.1        3.74          12   -0.0086
        3          1.1        1.69          12   -0.0118
        4          1.1        0.75          12   -0.0104

> cat("\nsummary(mod.earth.tita.age)\n")

summary(mod.earth.tita.age)
> print(summary(mod.earth.tita.age))
Call: earth(x=tita[,-4], y=tita[,4], trace=0.5, degree=2, nfold=3, ncross=3,
            varmod.method="lm")

                            coefficients
(Intercept)                    25.664968
pclassfirst                     9.028974
h(sibsp-1)                    -12.096706
h(1.68119-logage)              -7.502937
sexmale * h(logage-2.48137)     5.062358
sibsp * h(logage-1.68119)       3.280947

Selected 6 of 14 terms, and 4 of 6 predictors
Termination condition: Reached nk 21
Importance: logage, sexmale, pclassclassthird-unused, sibsp, pclassfirst, ...
Number of terms at each degree of interaction: 1 3 2
GCV 174.7603  RSS 11022.31  GRSq 0.335155  RSq 0.5124778  CVRSq 0.1092556

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

Cross validation:   nterms 4.00 sd 1.00    nvars 3.22 sd 0.97

     CVRSq    sd     MaxErr   sd
     0.109 0.173       41.4 34.3

varmod: method "lm"    min.sd 1.52    iter.rsq 0.000

stddev of predictions:
            coefficients iter.stderr iter.stderr%
(Intercept)   15.6092331     2.77993           18
tita[, 4]     -0.0129731   0.0843626          650

                              mean   smallest    largest      ratio
95% prediction interval   59.62756   58.24825   61.60404   1.057612

                                         68%    80%    90%    95% 
response values in prediction interval   84     91     97     99  
> plotmo(mod.earth.tita.age, SHOWCALL=TRUE)
 plotmo grid:    pclass survived  sex sibsp  logage
             classthird        0 male     0 3.06991
> 
> set.seed(2020)
> mod.earth.sex <- earth(sex~., data=tita, degree=2, nfold=3, ncross=3, varmod.method="earth", glm=list(family=binomial))
> cat("\nsummary(mod.earth.sex)\n")

summary(mod.earth.sex)
> print(summary(mod.earth.sex))
Call: earth(formula=sex~., data=tita, glm=list(family=binomial), degree=2,
            nfold=3, ncross=3, varmod.method="earth")

GLM coefficients
                                   male
(Intercept)                   2.2150018
survived * age               -0.1784818
pclassclassthird * h(31-age) -0.1140654
survived * h(age-31)          0.4396082

GLM (family binomial, link logit):
 nulldev df       dev df   devratio     AIC iters converged
 111.559 87   67.1647 84      0.398   75.16     5         1

Earth selected 4 of 12 terms, and 3 of 6 predictors
Termination condition: Reached nk 21
Importance: survived, age, pclassclassthird, pclassfirst-unused, ...
Number of terms at each degree of interaction: 1 0 3
Earth GCV 0.1413927  RSS 10.15497  GRSq 0.374517  RSq 0.4777105  CVRSq -0.07918303

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

Cross validation:   nterms 3.67 sd 1.22    nvars 2.89 sd 1.17

  CVRSq    sd ClassRate   sd MaxErr    sd   AUC    sd MeanDev   sd CalibInt
 -0.079 0.617     0.745 0.08     -1 0.951 0.726 0.137    2.54 2.74    0.272
    sd CalibSlope    sd
 0.615      0.632 0.585

varmod: method "earth"    min.sd 0.0408    iter.rsq 0.249

stddev of predictions:
                 coefficients iter.stderr iter.stderr%
(Intercept)         0.9095818    0.102755           11
h(0.428854-male)   -1.3805358    0.363514           26
h(male-0.428854)   -1.2913970    0.244534           19

                              mean    smallest    largest      ratio
95% prediction interval   1.600401   0.9672622   3.565495   3.686172

                                         68%    80%    90%    95% 
response values in prediction interval   90     92     94     94< 
> plotmo(mod.earth.sex, SHOWCALL=TRUE)
> 
> cat("\n#=== from test.unusual.vars.R ===========================================\n")

#=== from test.unusual.vars.R ===========================================
> set.seed(2020)
> 
> vdata <- data.frame(
+     resp = 1:13,
+     bool = c(F, F, F, F, F, T, T, T, T, T, T, T, T),
+     ord  = ordered(c("ORD1", "ORD1", "ORD1",
+                      "ORD1", "ORD1", "ORD1",
+                      "ORD3", "ORD3", "ORD3",
+                      "ORD2", "ORD2", "ORD2", "ORD2"),
+                    levels=c("ORD1", "ORD3", "ORD2")),
+     fac  = as.factor(c("FAC1", "FAC1", "FAC1",
+                        "FAC2", "FAC2", "FAC2",
+                        "FAC3", "FAC3", "FAC3",
+                        "FAC1", "FAC2", "FAC3", "FAC3")),
+     str  = c("STR1", "STR1", "STR1", # WILL BE TREATED LIKE A FACTOR
+              "STR2", "STR2", "STR2",
+              "STR3", "STR3", "STR3",
+              "STR3", "STR3", "STR3", "STR3"),
+     num  = c(1, 3, 2, 3, 4, 5, 6, 4, 5, 6.5, 3, 6, 5), # 7 unique values (but one is non integral)
+     sqrt_num  = sqrt(c(1, 3, 2, 3, 4, 5, 6, 4, 5, 6.5, 3, 6, 5)),
+     int  = c(1L, 1L, 3L, 3L, 4L, 4L, 3L, 5L, 3L, 6L, 7L, 8L, 10L), # 8 unique values
+     date = as.Date(
+            c("2018-08-01", "2018-08-02", "2018-08-03",
+              "2018-08-04", "2018-08-05", "2018-08-06",
+              "2018-08-07", "2018-08-08", "2018-08-08",
+              "2018-08-08", "2018-08-10", "2018-08-11", "2018-08-11")),
+     date_num = as.numeric(as.Date(
+            c("2018-08-01", "2018-08-02", "2018-08-03",
+              "2018-08-04", "2018-08-05", "2018-08-06",
+              "2018-08-07", "2018-08-08", "2018-08-08",
+              "2018-08-08", "2018-08-10", "2018-08-11", "2018-08-11"))))
> 
> vdata$off <- (1:nrow(vdata)) / nrow(vdata)
> 
> resp2 <- 13:1
> 
> vweights <- rep(1, length.out=nrow(vdata))
> vweights[1] <- 2
> 
> set.seed(2020)
> lognum.bool.ord.off <- earth(resp ~ log(num) + bool + ord + offset(off), degree=2, weights=vweights,
+            data=vdata, pmethod="none", varmod.method="lm",
+            nfold=2, ncross=3,
+            trace=1)
x[13,4] with colnames log(num) boolTRUE ord.L ord.Q
y[13,1] with colname resp, and values 0.9231, 1.846, 2.769, 3.692, ...
weights[13]: 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
Forward pass term 1, 2, 4, 6, 8
GRSq -Inf at 7 terms, 5 terms used
After forward pass GRSq -in RSq 0.966
Prune none penalty 3 nprune null: selected 5 of 5 terms, and 3 of 4 preds
After pruning pass GRSq -0.732 RSq 0.952

CV fold  1.1  CVRSq  0.708   n.oof  6 54%   n.infold.nz  6 100%   n.oof.nz  7 100%
CV fold  1.2  CVRSq  0.823   n.oof  7 46%   n.infold.nz  7 100%   n.oof.nz  6 100%
CV fold  2.1  CVRSq  0.461   n.oof  6 54%   n.infold.nz  6 100%   n.oof.nz  7 100%
CV fold  2.2  CVRSq  0.816   n.oof  7 46%   n.infold.nz  7 100%   n.oof.nz  6 100%
CV fold  3.1  CVRSq  0.559   n.oof  6 54%   n.infold.nz  6 100%   n.oof.nz  7 100%
CV fold  3.2  CVRSq  0.698   n.oof  7 46%   n.infold.nz  7 100%   n.oof.nz  6 100%
CV all        CVRSq  0.677                  n.infold.nz 13 100%


varmod method="lm" rmethod="hc12" lambda=1 exponent=1 conv=1 clamp=0.1 minspan=-3:
     iter weight.ratio coefchange% (Intercept)  resp
        1          2.3        0.00        0.98 0.050
        2          2.4        3.54        0.96 0.053
        3          2.4        0.53        0.96 0.053

> print(summary(lognum.bool.ord.off))
Call: earth(formula=resp~log(num)+bool+ord+offset(off), data=vdata,
            weights=vweights, pmethod="none", trace=1, degree=2, nfold=2,
            ncross=3, varmod.method="lm")

                                 coefficients
(Intercept)                          7.384615
h(-7.85046e-17-ord.L)               -9.171908
h(ord.L- -7.85046e-17)               4.568998
log(num) * h(-7.85046e-17-ord.L)     3.100021
boolTRUE * h(-7.85046e-17-ord.L)     1.571761

Selected 5 of 5 terms, and 3 of 4 predictors (pmethod="none")
Termination condition: GRSq -Inf at 5 terms
Importance: ord.L, log(num), boolTRUE, ord.Q-unused
Offset: off with values 0.07692308, 0.1538462, 0.2307692, 0.3076923, 0.3...
Weights: 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
Number of terms at each degree of interaction: 1 2 2
GCV 28.70012  RSS 8.830806  GRSq -0.7319038  RSq 0.9518916  CVRSq 0.6774271

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

Cross validation:   nterms 2.33 sd 0.52    nvars 1.00 sd 0.00

     CVRSq    sd     MaxErr   sd
     0.677 0.143       3.77 3.77

varmod: method "lm"    min.sd 0.167    iter.rsq 0.076

stddev of predictions:
            coefficients iter.stderr iter.stderr%
(Intercept)   1.20311748    0.484791           40
resp          0.06662794   0.0699109          105

                              mean   smallest    largest      ratio
95% prediction interval   6.544853   4.971048   7.749802   1.558988

                                         68%     80%     90%     95% 
response values in prediction interval   92     100     100     100  
> 
> cat("\n#=== from test.caret.R ===========================================\n")

#=== from test.caret.R ===========================================
> set.seed(2020)
> 
> library(caret)
Loading required package: lattice
Loading required package: ggplot2
> set.seed(2015)
> a.bag3 <- bagEarth(survived~., data=etitanic, degree=2, B=3, trace=1)
x[1046,7] with colnames pclass1st pclass2nd pclass3rd sexmale age sibsp parch
y[1046,1] with colname subY, and values 0, 0, 0, 0, 1, 1, 1, 0, 0, 0,...
weights: no weights (because all weights equal)
Forward pass term 1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20
Reached nk 21
After forward pass GRSq 0.434 RSq 0.473
Prune backward penalty 3 nprune null: selected 10 of 16 terms, and 5 of 7 preds
After pruning pass GRSq 0.442 RSq 0.466
x[1046,7] with colnames pclass1st pclass2nd pclass3rd sexmale age sibsp parch
y[1046,1] with colname subY, and values 0, 0, 1, 1, 1, 0, 0, 0, 1, 1,...
weights: no weights (because all weights equal)
Forward pass term 1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20
Reached nk 21
After forward pass GRSq 0.385 RSq 0.434
Prune backward penalty 3 nprune null: selected 12 of 18 terms, and 6 of 7 preds
After pruning pass GRSq 0.402 RSq 0.433
x[1046,7] with colnames pclass1st pclass2nd pclass3rd sexmale age sibsp parch
y[1046,1] with colname subY, and values 1, 1, 0, 1, 1, 1, 0, 1, 0, 0,...
weights: no weights (because all weights equal)
Forward pass term 1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20
Reached nk 21
After forward pass GRSq 0.451 RSq 0.487
Prune backward penalty 3 nprune null: selected 13 of 15 terms, and 6 of 7 preds
After pruning pass GRSq 0.456 RSq 0.487
> print(a.bag3)

Call:
bagEarth.formula(formula = survived ~ ., data = etitanic, B = 3,     degree = 2, trace = 1)

Data:
   # variables:	 7 
   # samples:	 1046 
case weights used

B: 3 
> plotmo(a.bag3, clip=F, caption="bagEarth, etitanic", trace=1, SHOWCALL=TRUE)
stats::predict(bagEarth.object, data.frame[3,7], type="response")
stats::fitted(object=bagEarth.object)
fitted() was unsuccessful, will use predict() instead
assuming "survived" in the model.frame is the response, because terms(object) did not return the terms
assuming "survived" in the model.frame is the response, because terms(object) did not return the terms
got model response from model.frame(survived ~ .,
                                    data=call$data, na.action="na.fail")

 plotmo grid:    pclass1st pclass2nd pclass3rd sexmale age sibsp parch
                         0         0         0       1  28     0     0

> plotres(a.bag3, clip=F, trace=1, SHOWCALL=TRUE)
stats::residuals(object=bagEarth.object, type="response")
residuals() was unsuccessful, will use predict() instead
stats::predict(bagEarth.object, data.frame[3,7], type="response", clip=FALSE)
stats::fitted(object=bagEarth.object)
fitted() was unsuccessful, will use predict() instead
assuming "survived" in the model.frame is the response, because terms(object) did not return the terms
assuming "survived" in the model.frame is the response, because terms(object) did not return the terms
got model response from model.frame(survived ~ .,
                                    data=call$data, na.action="na.fail")
assuming "survived" in the model.frame is the response, because terms(object) did not return the terms

training rsq 0.44
> 
> # Following commented out because too slow
> #
> # cat("\n#=== from test.parsnip.R ===========================================\n")
> # set.seed(2020)
> #
> # cat("loading parsnip libraries\n") # these libraries take several seconds to load
> # library(tidymodels)
> # library(timetk)
> # library(lubridate)
> # cat("loaded parsnip libraries\n")
> # cat("parsnip version:", as.character(packageVersion("parsnip")[[1]]), "\n")
> #
> # vdata <- data.frame(
> #     resp    = 1:23,
> #     bool = c(F, F, F, F, F, T, T, T, T, T, T, T, T, F, F, T, T, T, T, T, T, T, T),
> #     ord  = ordered(c("ORD1", "ORD1", "ORD1",
> #                      "ORD1", "ORD1", "ORD1",
> #                      "ORD1", "ORD3", "ORD1",
> #                      "ORD2", "ORD2", "ORD2", "ORD2",
> #                      "ORD2", "ORD2", "ORD2",
> #                      "ORD3", "ORD3", "ORD3",
> #                      "ORD2", "ORD2", "ORD2", "ORD2"),
> #                    levels=c("ORD1", "ORD3", "ORD2")),
> #     fac  = as.factor(c("FAC1", "FAC1", "FAC1",
> #                        "FAC2", "FAC2", "FAC2",
> #                        "FAC3", "FAC1", "FAC1",
> #                        "FAC1", "FAC2", "FAC2", "FAC2",
> #                        "FAC2", "FAC2", "FAC2",
> #                        "FAC3", "FAC3", "FAC3",
> #                        "FAC1", "FAC3", "FAC3", "FAC3")),
> #     str  = c("STR1", "STR1", "STR1", # WILL BE TREATED LIKE A FACTOR
> #              "STR1", "STR1", "STR1",
> #              "STR2", "STR2", "STR2",
> #              "STR3", "STR3", "STR2", "STR3",
> #              "STR2", "STR3", "STR2",
> #              "STR3", "STR3", "STR3",
> #              "STR3", "STR3", "STR3", "STR3"),
> #     num  = c(1, 9, 2, 3, 14, 5, 6, 4, 5, 6.5, 3, 6, 5,
> #              3, 4, 5, 6, 4, 5, 16.5, 3, 16, 15),
> #     sqrt_num  = sqrt(
> #            c(1, 9, 2, 3, 14, 5, 6, 4, 5, 6.5, 3, 6, 5,
> #              3, 4, 5, 6, 4, 5, 16.5, 3, 16, 15)),
> #     int  = c(1L, 1L, 3L, 3L, 4L, 4L, 3L, 5L, 3L, 6L, 7L, 8L, 10L,
> #              13L, 14L, 3L, 13L, 5L, 13L, 16L, 17L, 18L, 11L),
> #     date = as.Date(
> #            c("2018-08-01", "2018-08-02", "2018-08-03",
> #              "2018-08-04", "2018-08-05", "2018-08-06",
> #              "2018-08-07", "2018-08-08", "2018-08-08",
> #              "2018-08-10", "2018-08-10", "2018-08-11", "2018-08-11",
> #              "2018-08-11", "2018-08-12", "2018-08-13",
> #              "2018-08-10", "2018-08-15", "2018-08-17",
> #              "2018-08-04", "2018-08-19", "2018-08-03", "2018-08-18")),
> #     date_num = as.numeric(as.Date(
> #            c("2018-08-01", "2018-08-02", "2018-08-03",
> #              "2018-08-04", "2018-08-05", "2018-08-06",
> #              "2018-08-07", "2018-08-08", "2018-08-08",
> #              "2018-08-10", "2018-08-10", "2018-08-11", "2018-08-11",
> #              "2018-08-11", "2018-08-12", "2018-08-13",
> #              "2018-08-10", "2018-08-15", "2018-08-17",
> #              "2018-08-04", "2018-08-19", "2018-08-03", "2018-08-18"))))
> #
> # set.seed(2020)
> # splits <- initial_time_split(vdata, prop=.9)
> #
> # cat("===m750a first example===\n")
> # set.seed(2020)
> # m750a <- m4_monthly %>%
> #     filter(id == "M750") %>%
> #     select(-id)
> # print(m750a) # a tibble
> # set.seed(2020)
> # splits_a <- initial_time_split(m750a, prop = 0.9)
> # earth_m750a <- earth(log(value) ~ as.numeric(date) + month(date, label = TRUE), data = training(splits_a), degree=2)
> # print(summary(earth_m750a))
> 
> source("test.epilog.R")
