Nonlinear Regression Techniques

Umer Farooq

2024-04-05

Regression Problems on Non Linear Regression Method:

Question 1: Friedman (1991) introduced several benchmark data sets create by simulation. One of these simulations used the following nonlinear equation to create data:

y = 10sin(\(πx_1x_2\)) + 20(\(x_3\) − 0.5\()^2\) + 10\(x_4\) + 5\(x_5\) + N(0, \(σ^2\))

where the x values are random variables uniformly distributed between [0, 1] (there are also 5 other non-informative variables also created in the simulation). The package mlbench contains a function called mlbench.friedman1 that simulates these data: Which models appear to give the best performance? Does MARS select the informative predictors (those named X1–X5)?

Answer:

Let’s Load the data:

set.seed(200)
trainingData <- mlbench.friedman1(200, sd = 1)
trainingData$x <- data.frame(trainingData$x)

featurePlot(trainingData$x, trainingData$y)

testData <- mlbench.friedman1(5000, sd = 1)
testData$x <- data.frame(testData$x)

Let create a dataframe that will track the model metrics such as RSME, Rsqaured and MAE

tracker <- data.frame(matrix(vector(), 0, 3,
                dimnames=list(c(), c("RMSE","Rsquared","MAE"))),
                stringsAsFactors=F)

Now we are ready to train a few models and compared there metrics. First we will start with K-nearest neighbors. The code for the KNN was provided by the text book so let’s run that

K-nearest Neighbors (KNN):

Creating the model:

Since our data is already split so we can go ahead and train the model. We have used train() function from the caret package with method argument set to knn

knnModel <- train(x = trainingData$x,y = trainingData$y, method = "knn", preProc = c("center", "scale"), tuneLength = 10)

We can check out model:

knnModel
## k-Nearest Neighbors 
## 
## 200 samples
##  10 predictor
## 
## Pre-processing: centered (10), scaled (10) 
## Resampling: Bootstrapped (25 reps) 
## Summary of sample sizes: 200, 200, 200, 200, 200, 200, ... 
## Resampling results across tuning parameters:
## 
##   k   RMSE      Rsquared   MAE     
##    5  3.466085  0.5121775  2.816838
##    7  3.349428  0.5452823  2.727410
##    9  3.264276  0.5785990  2.660026
##   11  3.214216  0.6024244  2.603767
##   13  3.196510  0.6176570  2.591935
##   15  3.184173  0.6305506  2.577482
##   17  3.183130  0.6425367  2.567787
##   19  3.198752  0.6483184  2.592683
##   21  3.188993  0.6611428  2.588787
##   23  3.200458  0.6638353  2.604529
## 
## RMSE was used to select the optimal model using the smallest value.
## The final value used for the model was k = 17.

Predicting:

Now that our model is ready we can go ahead and predict the values using predict() function. We can also updata our tracker data frame with the test RMSE, Rsqaured and MAE of our model.

knnPred <- predict(knnModel, newdata = testData$x)

tracker <- rbind(tracker, postResample(pred = knnPred, obs = testData$y))

Now that our KNN model is ready and the metrics are being updated to the the tracker data frame. We can go ahead and train some other models the same way.

Multivariate Adaptive Regression Splines (MARS):

Creating the model:

In the section we will try to tune multivariate adaptive regression model. First we will define out tuneGrid

marsGrid <- expand.grid(.degree = 1:2, .nprune = 2:38)

Now that we have defined our tuneGrid. We can go ahead and use the train() function from caret package and set method to mars

marsModel <- train(trainingData$x, trainingData$y, method = "earth", preProcess = c("center", "scale"),tuneLength = 10, tuneGrid = marsGrid, trControl = trainControl(method = "cv"))

checking out the model:

marsModel
## Multivariate Adaptive Regression Spline 
## 
## 200 samples
##  10 predictor
## 
## Pre-processing: centered (10), scaled (10) 
## Resampling: Cross-Validated (10 fold) 
## Summary of sample sizes: 180, 180, 180, 180, 180, 180, ... 
## Resampling results across tuning parameters:
## 
##   degree  nprune  RMSE      Rsquared   MAE     
##   1        2      4.462296  0.2176253  3.697979
##   1        3      3.720663  0.4673821  2.949121
##   1        4      2.680039  0.7094916  2.123848
##   1        5      2.333538  0.7781559  1.856629
##   1        6      2.367933  0.7754329  1.901509
##   1        7      1.809983  0.8656526  1.414985
##   1        8      1.692656  0.8838936  1.333678
##   1        9      1.704958  0.8845683  1.339517
##   1       10      1.688559  0.8842495  1.309838
##   1       11      1.669043  0.8886165  1.296522
##   1       12      1.645066  0.8892796  1.271981
##   1       13      1.655570  0.8886896  1.271232
##   1       14      1.666354  0.8879143  1.285545
##   1       15      1.666354  0.8879143  1.285545
##   1       16      1.666354  0.8879143  1.285545
##   1       17      1.666354  0.8879143  1.285545
##   1       18      1.666354  0.8879143  1.285545
##   1       19      1.666354  0.8879143  1.285545
##   1       20      1.666354  0.8879143  1.285545
##   1       21      1.666354  0.8879143  1.285545
##   1       22      1.666354  0.8879143  1.285545
##   1       23      1.666354  0.8879143  1.285545
##   1       24      1.666354  0.8879143  1.285545
##   1       25      1.666354  0.8879143  1.285545
##   1       26      1.666354  0.8879143  1.285545
##   1       27      1.666354  0.8879143  1.285545
##   1       28      1.666354  0.8879143  1.285545
##   1       29      1.666354  0.8879143  1.285545
##   1       30      1.666354  0.8879143  1.285545
##   1       31      1.666354  0.8879143  1.285545
##   1       32      1.666354  0.8879143  1.285545
##   1       33      1.666354  0.8879143  1.285545
##   1       34      1.666354  0.8879143  1.285545
##   1       35      1.666354  0.8879143  1.285545
##   1       36      1.666354  0.8879143  1.285545
##   1       37      1.666354  0.8879143  1.285545
##   1       38      1.666354  0.8879143  1.285545
##   2        2      4.440854  0.2204755  3.686796
##   2        3      3.697203  0.4714312  2.938566
##   2        4      2.664266  0.7149235  2.119566
##   2        5      2.313371  0.7837374  1.852172
##   2        6      2.335796  0.7875253  1.841919
##   2        7      1.833248  0.8623489  1.461538
##   2        8      1.695822  0.8883658  1.329030
##   2        9      1.555106  0.9028532  1.221365
##   2       10      1.497805  0.9088251  1.158054
##   2       11      1.419280  0.9207646  1.139722
##   2       12      1.326566  0.9315939  1.066200
##   2       13      1.266877  0.9354482  1.002983
##   2       14      1.256694  0.9349307  1.006273
##   2       15      1.311401  0.9316487  1.039213
##   2       16      1.292299  0.9336915  1.022410
##   2       17      1.304364  0.9321032  1.031643
##   2       18      1.304364  0.9321032  1.031643
##   2       19      1.304364  0.9321032  1.031643
##   2       20      1.304364  0.9321032  1.031643
##   2       21      1.304364  0.9321032  1.031643
##   2       22      1.304364  0.9321032  1.031643
##   2       23      1.304364  0.9321032  1.031643
##   2       24      1.304364  0.9321032  1.031643
##   2       25      1.304364  0.9321032  1.031643
##   2       26      1.304364  0.9321032  1.031643
##   2       27      1.304364  0.9321032  1.031643
##   2       28      1.304364  0.9321032  1.031643
##   2       29      1.304364  0.9321032  1.031643
##   2       30      1.304364  0.9321032  1.031643
##   2       31      1.304364  0.9321032  1.031643
##   2       32      1.304364  0.9321032  1.031643
##   2       33      1.304364  0.9321032  1.031643
##   2       34      1.304364  0.9321032  1.031643
##   2       35      1.304364  0.9321032  1.031643
##   2       36      1.304364  0.9321032  1.031643
##   2       37      1.304364  0.9321032  1.031643
##   2       38      1.304364  0.9321032  1.031643
## 
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were nprune = 14 and degree = 2.

we can see that after 14 prune and 2 degree there are hardly any change in the values of RMSE, MAE and Rsquared. We can go ahead and predict using MARS

Predicting:

marsPred <- predict(marsModel, newdata = testData$x)
tracker <- rbind(tracker, postResample(pred = marsPred, obs = testData$y))

Now that our MARS model is ready and the metrics are being updated to the the tracker data frame. We can go ahead and train some Neural Networks the same way.

Neural Networks:

Creating the model:

nNetGrid <- expand.grid(.decay = c(0, 0.01, .1), .size = c(1:10), .bag = FALSE)
nNetMaxnwts <- 5 * (ncol(trainingData$x) + 1) + 5 + 1
nNetModel <- train(x = trainingData$x,
                    y = trainingData$y,
                    method = "avNNet",
                    preProcess = c("center", "scale"),
                    tuneGrid = nNetGrid,
                    trControl = trainControl(method = "cv"),
                    linout = TRUE,
                    trace = FALSE,
                    MaxNWts = nNetMaxnwts,
                    maxit = 500)

Predicting:

nNetPred <- predict(nNetModel, newdata = testData$x)
tracker <- rbind(tracker, postResample(pred = nNetPred, obs = testData$y))

Similarly we can train the support vector machine (SVM)

Support Vector machine (SVM):

Creating the modeL:

svmModel <- train(x = trainingData$x,
                   y = trainingData$y,
                   method = "svmRadial",
                   preProcess = c("center", "scale"),
                   tuneLength = 10,
                   trControl = trainControl(method = "cv"))

Predicting:

svmPred <- predict(svmModel, newdata = testData$x)
tracker <- rbind(tracker, postResample(pred = svmPred, obs = testData$y))

Model Comparison:

colnames(tracker) <- c("RMSE","Rsquared","MAE")
tracker$NAME <- c("KNN", "MARS", "Neural Network", "SVM")
tracker <- tracker %>% relocate("NAME") %>% arrange(RMSE)
tracker
##             NAME     RMSE  Rsquared      MAE
## 1           MARS 1.277999 0.9338365 1.014707
## 2            SVM 2.051520 0.8294511 1.556470
## 3 Neural Network 2.061504 0.8305890 1.561138
## 4            KNN 3.204059 0.6819919 2.568346

From the table above, we can see that the MARS model gave the best performance.

varImp(marsModel)
## earth variable importance
## 
##    Overall
## X1  100.00
## X4   75.40
## X2   49.00
## X5   15.72
## X3    0.00

The MARS model important variables output shows that the model did select the informative predictors (X1-X5).


Exercise 6.3 describes data for a chemical manufacturing process. Use the same data imputation, data splitting, and pre-processing steps as before and train several nonlinear regression models.

Several Nonlinear Regression Models:

Laoding, Splitting and Preprocessing The Data:

data("ChemicalManufacturingProcess")

preProcess <- preProcess(ChemicalManufacturingProcess, 
                   method = c("BoxCox", "knnImpute", "center", "scale"))
predPreProcess <- predict(preProcess, ChemicalManufacturingProcess)

predPreProcess$Yield = ChemicalManufacturingProcess$Yield


ind <- sample(seq_len(nrow(predPreProcess)), size = floor(0.85 * nrow(predPreProcess)))
train <- predPreProcess[ind, ]
test <- predPreProcess[-ind, ]

Creating Models:

control <- trainControl(method = "cv")
## KNN Model
knnModel2 <- train(Yield ~., data = train,
                  method = "knn",
                  preProcess = c("center", "scale"),
                  tuneLength = 10)
knnPred2 <- predict(knnModel2, newdata = test)

## Neural Networks Model
nNetModel2 <- train(Yield ~., data = train,
                  method = "avNNet",
                  tuneGrid = nNetGrid,
                  trControl = control,
                  linout = TRUE,
                  trace = FALSE,
                  MaxNWts = 5 * (ncol(train)) + 5 + 1,
                  maxit = 500
                  )
nNetPred2 <- predict(nNetModel2, newdata = test)

## MARS Model
marsModel2 <- train(Yield ~., data=train,
                   method = "earth",
                   tuneGrid = marsGrid,
                   trControl = control)
marsPred2 <- predict(marsModel2, newdata = test)

## SVM Model
svmModel2 <- train(Yield ~., data=train,
                   method = "svmRadial",
                   tuneLength = 15,
                   trControl = control)
svmPred2 <- predict(svmModel2, newdata = test)
  1. Which nonlinear regression model gives the optimal resampling and test set performance?

Comparing the Performance:

as.data.frame(rbind(
  "mars" = postResample(pred = marsPred2, obs = test$Yield),
  "svm" = postResample(pred = svmPred2, obs = test$Yield),
  "net" = postResample(pred = nNetPred2, obs = test$Yield),
  "knn" = postResample(pred = knnPred2, obs = test$Yield)
)) %>% arrange(RMSE)
##          RMSE  Rsquared       MAE
## svm  1.015857 0.6249954 0.8092596
## mars 1.120050 0.5023733 0.8128487
## net  1.182866 0.6089104 0.8999829
## knn  1.340323 0.2917927 1.0227407

If we look at the RMSE values from both tables, we can see that the SVM model gives the optimal test performance.

  1. Which predictors are most important in the optimal nonlinear regression model? Do either the biological or process variables dominate the list? How do the top ten important predictors compare to the top ten predictors from the optimal linear model?

Important Predictors:

as.data.frame(rbind(
  "mars" = postResample(pred = marsPred2, obs = test$Yield),
  "svm" = postResample(pred = svmPred2, obs = test$Yield),
  "net" = postResample(pred = nNetPred2, obs = test$Yield),
  "knn" = postResample(pred = knnPred2, obs = test$Yield)
)) %>% arrange(RMSE)
##          RMSE  Rsquared       MAE
## svm  1.015857 0.6249954 0.8092596
## mars 1.120050 0.5023733 0.8128487
## net  1.182866 0.6089104 0.8999829
## knn  1.340323 0.2917927 1.0227407

The SVM model was optimal for nonlinear regression, and the table above shows the most important predictors. There seems to be an equal number of biological and process variables. The results for 6.3 shows that the optimal linear model was the PLS model. The most important variables list from the PLS model showed that the Manufacturing Process variables were dominating.

  1. Explore the relationships between the top predictors and the response for the predictors that are unique to the optimal nonlinear regression model. Do these plots reveal intuition about the biological or process predictors and their relationship with yield?

Relationship Among Predictors:

varI <- varImp(svmModel2)$importance
varI <- cbind(rownames(varI), data.frame(varI, row.names=NULL))
colnames(varI) <- c("Predictor","Overall")
varI <- varI %>% arrange(Overall) %>% tail(10) %>% select(Predictor)
variables <- as.vector(varI$Predictor)
featurePlot(predPreProcess[,variables], predPreProcess$Yield) 

Looking at the relationship of 10 most important variables with the target variable, we see that a majority of them are tightly clustered and have a linear relationship. All biological material variables are clusters, but some manufacturing process variables a unique relationship (variable 6 and variable 10).