if (FALSE) {
# Example 1.
library(MASS)
data(Rubber)
# ShapeSelect can be used to go through all models to find the best model
fit <- ShapeSelect(loss ~ shapes(hard, set = "s.9") + shapes(tens, set = "s.9"),
data = Rubber, genetic = FALSE)
# the user can also choose to find the best model by the genetic algorithm
# given any total number of possible models
fit <- ShapeSelect(loss ~ shapes(hard, set = "s.9") + shapes(tens, set = "s.9"),
data = Rubber, genetic = TRUE)
# check the best model
fit$top
# check the running time
fit$tm
# Example 2.
# simulate a data set such that the mean is smoothly increasing-convex in x1 and x2
n <- 100
x1 <- runif(n)
x2 <- runif(n)
y0 <- x1^2 + x2 + x2^3
z <- rep(0:1, 50)
for (i in 1:n) {
if (z[i] == 1)
y0[i] = y0[i] * 1.5
}
# add some random errors and call the routine
y <- y0 + rnorm(n)
# include factor(z) in the formula and determine if factor(z) should be in the model
fit <- ShapeSelect(y ~ shapes(x1, set = "s.9") + shapes(x2, set = "s.9") + in.or.out(factor(z)))
# use the genetic algorithm
fit <- ShapeSelect(y ~ shapes(x1, set = "s.9") + shapes(x2, set = "s.9") + in.or.out(factor(z)),
npop = 300, per.mutate=0.02)
# include z as a linear term in the formula and determine if z should be in the model
fit <- ShapeSelect(y ~ shapes(x1, set = "s.9") + shapes(x2, set = "s.9") + in.or.out(z))
# include z as a linear term in the model
fit <- ShapeSelect(y ~ shapes(x1, set = "s.9") + shapes(x2, set = "s.9") + z)
# include factor(z) in the model
fit <- ShapeSelect(y ~ shapes(x1, set = "s.9") + shapes(x2, set = "s.9") + factor(z))
# check the best model
bf <- fit$best.fit
# make a 3D plot of the best fit
plotpersp(bf, categ = "z")
}
Run the code above in your browser using DataLab