

> # test.earth.R
> # Check for porting problems by building a few simple models.
> # For much more comprehensive tests see earth\inst\slowtests.
> library(earth)
Loading required package: Formula
Loading required package: plotmo
Loading required package: plotrix
> options(digits=3) # prevent floating point implementation issues across machines
> data(trees)
> earth.mod <- earth(Volume~., data=trees)
> print(summary(earth.mod))
Call: earth(formula=Volume~., data=trees)

              coefficients
(Intercept)         29.060
h(14.2-Girth)       -3.420
h(Girth-14.2)        6.230
h(Height-75)         0.581

Selected 4 of 5 terms, and 2 of 2 predictors
Termination condition: RSq changed by less than 0.001 at 5 terms
Importance: Girth, Height
Number of terms at each degree of interaction: 1 3 (additive model)
GCV 11.3    RSS 209    GRSq 0.96    RSq 0.974
> allowed.func <- function(degree, pred, parents, namesx)
+ {
+     namesx[pred] != "Height"  # disallow "Height"
+ }
> set.seed(2019)
> earth.mod2 <- earth(Volume~., data=trees, allowed=allowed.func, trace=1)
x[31,2] with colnames Girth Height
y[31,1] with colname Volume, and values 10.3, 10.3, 10.2, 16.4, 18.8,...
Forward pass term 1, 2, 4
RSq changed by less than 0.001 at 3 terms (DeltaRSq 9.7e-05)
After forward pass GRSq 0.940 RSq 0.962
Prune backward penalty 2 nprune null: selected 3 of 3 terms, and 1 of 2 preds
After pruning pass GRSq 0.949 RSq 0.961
> print(summary(earth.mod2))
Call: earth(formula=Volume~., data=trees, trace=1, allowed=allowed.func)

              coefficients
(Intercept)          30.66
h(14.2-Girth)        -3.57
h(Girth-14.2)         6.76

Selected 3 of 3 terms, and 1 of 2 predictors
Termination condition: RSq changed by less than 0.001 at 3 terms
Importance: Girth, Height-unused
Number of terms at each degree of interaction: 1 2 (additive model)
GCV 14.3    RSS 313    GRSq 0.949    RSq 0.961
> # multiple response model using class "Formula" and a factor predictor
> data(iris)
> earth.mod3 <- earth(Sepal.Length + Sepal.Width ~ Species, data=iris)
> print(summary(earth.mod3))
Call: earth(formula=Sepal.Length+Sepal.Width~Species, data=iris)

                  Sepal.Length Sepal.Width
(Intercept)               5.01       3.428
Speciesversicolor         0.93      -0.658
Speciesvirginica          1.58      -0.454

Selected 3 of 3 terms, and 2 of 2 predictors
Termination condition: RSq changed by less than 0.001 at 3 terms
Importance: Speciesvirginica, Speciesversicolor
Number of terms at each degree of interaction: 1 2 (additive model)

               GCV  RSS  GRSq   RSq
Sepal.Length 0.278 39.0 0.597 0.619
Sepal.Width  0.121 17.0 0.367 0.401
All          0.399 55.9 0.547 0.571
> plot(earth.mod3, nresponse="Sepal.Length", which=c(1,3), do.par=2, legend.pos="topleft")
> plotmo(earth.mod3, nresponse=1, pt.col=2, do.par=0)
> 
