> # test.incorrect.R: example incorrect model built by earth
> # Stephen Milborrow May 2015 Berea
> 
> source("test.prolog.R")
> library(earth)
Loading required package: Formula
Loading required package: plotmo
Loading required package: plotrix
> options(digits=4)
> 
> printf <- function(format, ...) cat(sprint(format, ...)) # like c printf
> 
> sos <- function(x) sum(as.vector(x^2)) # sum of squares
> 
> func <- function(x) # bivariate with interaction
+ {
+     x[,1] + x[,2] + (x[,1] * x[,2]) + .3 * rnorm(nrow(x))
+ }
> 
> n <- 30
> set.seed(n)
> n <- 11
> seed <- 17
> set.seed(100 + seed)
> x1 <- sort(runif(n, -(n-1), n+1))
> x2 <- runif(n, -(n-1), n+1)
> x <- data.frame(x1=x1, x2=x2)
> set.seed(101 + seed)
> 
> x1test <- runif(10000, -n, n)
> x2test <- runif(10000, -n, n)
> xtest <- data.frame(x1=x1test, x2=x2test)
> colnames(x) <- colnames(xtest) <- c("x1", "x2")
> set.seed(103 + seed)
> ytest <- func(xtest)
> 
> par(mfrow = c(2, 2), mar = c(3, 3, 3, 1), mgp = c(1.5, 0.5, 0))
> correct.mod <- earth(xtest, ytest, degree=2, trace=0, minspan=-1, Force.weights=TRUE)
> plotmo(correct.mod, degree1=0, do.par=FALSE, main="correct model\nx1 + x2 + x1*x2")
> plotmo(correct.mod, degree1=0, do.par=FALSE, main="correct model", type2="im")
> 
> set.seed(102 + seed)
> y <- func(x)
> incorrect.mod <- earth(x, y, degree=2, trace=2, minspan=-1)
x[11,2] with colnames x1 x2
y[11,1] with colname y, and values 18.58, -22.71, -56.16, -3.128...
Forward pass: minspan 6 endspan 4   x[11,2] 176 Bytes   bx[11,21] 1.8 kB

         GRSq    RSq     DeltaRSq Pred     PredName         Cut  Terms   Par Deg
1      0.0000 0.0000                    (Intercept)
2     -2.2926 0.1769       0.1769    2           x2      4.9696  2   3         1 
4     -4.9885 0.6257       0.4489    1           x1     -9.9743< 4         2   2 
6        -inf 0.8630       0.2373    1           x1     -9.9743< 5         3   2 reject (negative GRSq)

GRSq -Inf at 5 terms, 4 terms used
After forward pass GRSq -in RSq 0.863
Forward pass complete: 5 terms, 4 terms used
Prune backward penalty 3 nprune null: selected 2 of 4 terms, and 2 of 2 preds
After pruning pass GRSq 0.0737 RSq 0.479
> print(incorrect.mod)
Selected 2 of 4 terms, and 2 of 2 predictors
Termination condition: GRSq -Inf at 4 terms
Importance: x1, x2
Number of terms at each degree of interaction: 1 0 1
GCV 1278    RSS 6534    GRSq 0.07365    RSq 0.4789
> test.rsq <- 1 - sos(ytest - predict(incorrect.mod, newdata=xtest)) / sos(ytest - mean(ytest))
> plotmo(incorrect.mod, degree1=0, do.par=FALSE, main="incorrect model")
> plotmo(incorrect.mod, degree1=0, do.par=FALSE, main="incorrect model", pt.col=2, type2="im")
> points(xtest[,1], xtest[,2], col=3, pch=20, cex=.05)
> 
> source("test.epilog.R")
