# # Problem EPage 159 # # Written by: # -- # John L. Weatherwax 2009-04-21 # # email: wax@alum.mit.edu # # Please send comments and especially bug reports to the # above email address. # #----- save_plots = F library(caret) library(kernlab) # Generate data to predict: set.seed(100) x = runif(100,min=2,max=100) y = sin(x) + rnorm(length(x)) * 0.25 sinData = data.frame(x=x,y=y) if( save_plots ){ postscript("../../WriteUp/Graphics/Chapter7/chap_7_prob_1_vary_sigma.eps", onefile=FALSE, horizontal=FALSE) } #if( save_plots ){ postscript("../../WriteUp/Graphics/Chapter7/chap_7_prob_1_vary_cost.eps", onefile=FALSE, horizontal=FALSE) } #if( save_plots ){ postscript("../../WriteUp/Graphics/Chapter7/chap_7_prob_1_vary_epsilon.eps", onefile=FALSE, horizontal=FALSE) } plot(x,y) ## Create a grid of x values to use for prediction dataGrid = data.frame(x=seq(2,100,length=100)) # Train some SVM models on this data and plot them: # # Reasonable looking predictions: # rbfSVM = ksvm( x=x, y=y, data=sinData, kernel="rbfdot", kpar="automatic", C=1, epsilon=0.1 ) modelPrediction = predict( rbfSVM, newdata=dataGrid ) points( x=dataGrid$x, y=modelPrediction[,1], type="l", col="blue" ) if( TRUE ){ # A large value for sigma: # rbfSVM = ksvm( x=x, y=y, data=sinData, kernel="rbfdot", kpar=list(sigma=100.0), C=1, epsilon=0.1 ) modelPrediction = predict( rbfSVM, newdata=dataGrid ) points( x=dataGrid$x, y=modelPrediction[,1], type="l", col="red" ) # A small value for sigma: # rbfSVM = ksvm( x=x, y=y, data=sinData, kernel="rbfdot", kpar=list(sigma=1e-2), C=1, epsilon=0.1 ) modelPrediction = predict( rbfSVM, newdata=dataGrid ) points( x=dataGrid$x, y=modelPrediction[,1], type="l", col="green" ) if( save_plots ){ dev.off() } } if( FALSE ){ # A very large value of the cost C (should overfit): # rbfSVM = ksvm( x=x, y=y, data=sinData, kernel="rbfdot", kpar="automatic", C=10^7, epsilon=0.1 ) modelPrediction = predict( rbfSVM, newdata=dataGrid ) points( x=dataGrid$x, y=modelPrediction[,1], type="l", col="red" ) # A very small value of the cost C (should underfit): # rbfSVM = ksvm( x=x, y=y, data=sinData, kernel="rbfdot", kpar="automatic", C=10^(-2), epsilon=0.1 ) modelPrediction = predict( rbfSVM, newdata=dataGrid ) points( x=dataGrid$x, y=modelPrediction[,1], type="l", col="green", main='changes in cost' ) if( save_plots ){ dev.off() } } if( FALSE ){ # A large value for epsilon: # rbfSVM = ksvm( x=x, y=y, data=sinData, kernel="rbfdot", kpar="automatic", C=1, epsilon=0.5 ) modelPrediction = predict( rbfSVM, newdata=dataGrid ) points( x=dataGrid$x, y=modelPrediction[,1], type="l", col="red" ) # A small value for epsilon: # rbfSVM = ksvm( x=x, y=y, data=sinData, kernel="rbfdot", kpar="automatic", C=1, epsilon=0.001 ) modelPrediction = predict( rbfSVM, newdata=dataGrid ) points( x=dataGrid$x, y=modelPrediction[,1], type="l", col="green" ) if( save_plots ){ dev.off() } }