library(frbs) ## The four hill function ## Set seed of random numbers set.seed(2) ## Create the four hill function fun <- function(input.xy){ z <- 1 / (input.xy[1]^4 + input.xy[2]^4 -2 * input.xy[1]^2 - 2 * input.xy[2]^2 + 3) } ## Generate data input.xy <- expand.grid(seq(-2, 2, 0.14), seq(-2, 2, by = 0.14)) ## Calculate the function z <- apply(input.xy, 1, fun) ## Create data in a data.frame that contains three columns ("X", "Y", and "Z") data <- cbind(input.xy, z) colnames(data)<- c("X", "Y", "Z") ## Randomize the data data <- data[sample(nrow(data)), ] ## Split the data to training and testing data cut.indx <- round(0.8 * nrow(data)) data.tra <- data[1 : cut.indx, ] data.tst <- data[(cut.indx + 1) : nrow(data), 1 : 2] ## Save actual values for validation real.val <- data[(cut.indx + 1) : nrow(data), 3, drop = FALSE] ## Calculate range of data #range.data <- apply(data, 2, range) range.data <- matrix(c(-2, 2, -2, 2, min(data[, 3]), max(data[, 3])), nrow = 2) ## Set parameters method.type <- "WM" control <- list(num.labels = 5, type.mf = "GAUSSIAN", type.defuz = "WAM", type.tnorm = "MIN", type.snorm = "MAX", type.implication.func = "LUKASIEWICZ", name = "fourhill") ## Construct an FRBS model (called mod.reg) mod.reg <- frbs.learn(data.tra, range.data, method.type, control) ## We can also convert and display the frbsPMML format fourHillpmml <- frbsPMML(mod.reg) ## Convert the model and save it to "modRegress.pmml" write.frbsPMML(fourHillpmml, "modRegress") ## Import the frbsPMML format back to the FRBS model objectReg <- read.frbsPMML("modRegress.frbsPMML") ## Predict new data (called data.tst) res.test <- predict(objectReg, data.tst) ## Calculate the mean square error err.MSE <- mean((real.val - res.test)^2) print(err.MSE)