if (FALSE) {
rm(list=ls())
# A 2D example
Branin <- function(x1,x2) {
x1 <- 1/2*(15*x1+5)
x2 <- 15/2*(x2+1)
(x2 - 5.1/(4*pi^2)*(x1^2) + 5/pi*x1 - 6)^2 + 10*(1 - 1/(8*pi))*cos(x1) + 10
}
# A 2D uniform design with n points in [-1,1]^2
n <- 50
X <- matrix(runif(n*2,-1,1),ncol=2,nrow=n)
Y <- Branin(X[,1],X[,2])
Z <- (Y-mean(Y))/sd(Y)
# Construction of a PolyMARS model with a penalty parameter equal to 2
library(polspline)
modPolyMARS <- modelFit(X,Z,type = "PolyMARS",gcv=2.2)
# Prediction and comparison between the exact function and the predicted one
xtest <- seq(-1, 1, length= 21)
ytest <- seq(-1, 1, length= 21)
Zreal <- outer(xtest, ytest, Branin)
Zreal <- (Zreal-mean(Y))/sd(Y)
Zpredict <- modelPredict(modPolyMARS,expand.grid(xtest,ytest))
m <- min(floor(Zreal),floor(Zpredict))
M <- max(ceiling(Zreal),ceiling(Zpredict))
persp(xtest, ytest, Zreal, theta = 30, phi = 30, expand = 0.5,
col = "lightblue",main="Branin function",zlim=c(m,M),
ticktype = "detailed")
persp(xtest, ytest, matrix(Zpredict,nrow=length(xtest),
ncol=length(ytest)), theta = 30, phi = 30, expand = 0.5,
col = "lightblue",main="PolyMARS Model",zlab="Ypredict",zlim=c(m,M),
ticktype = "detailed")
# Comparison of models
modelComparison(X,Y,type=c("Linear", "StepLinear","PolyMARS","Kriging"),
formula=Y~X1+X2+X1:X2+I(X1^2)+I(X2^2),penalty=log(dim(X)[1]), gcv=4)
# see also the demonstration example in dimension 5 (source: IRSN)
demo(IRSN5D)
}
Run the code above in your browser using DataLab