function [trainedModel, validationRMSE] = trainRegressionModel(training_w21,validation_w21)
inputTable = training_w21;
predictorNames = {'x02', 'x11', 'x13', 'x18', 'x29', 'x36'};
predictors = inputTable(:, predictorNames);
response = inputTable.x21;
isCategoricalPredictor = [false, false, false, false, false, false];
regressionGP = fitrgp(...
'BasisFunction', 'constant', ...
'KernelFunction', 'matern52', ...
'OptimizeHyperparameters','auto',...
predictorExtractionFcn = @(t) t(:, predictorNames);
gpPredictFcn = @(x) predict(regressionGP, x);
trainedModel.predictFcn = @(x) gpPredictFcn(predictorExtractionFcn(x));
trainedModel.RequiredVariables = {'x02', 'x11', 'x13', 'x18', 'x29', 'x36'};
trainedModel.RegressionGP = regressionGP;
trainedModel.About = 'This struct is a trained model exported from Regression Learner R2017a.';
trainedModel.HowToPredict = sprintf('To make predictions on a new table, T, use: \n yfit = c.predictFcn(T) \nreplacing ''c'' with the name of the variable that is this struct, e.g. ''trainedModel''. \n \nThe table, T, must contain the variables returned by: \n c.RequiredVariables \nVariable formats (e.g. matrix/vector, datatype) must match the original training data. \nAdditional variables are ignored. \n \nFor more information, see <a href="matlab:helpview(fullfile(docroot, ''stats'', ''stats.map''), ''appregression_exportmodeltoworkspace'')">How to predict using an exported model</a>.');
inputTable_tr = training_w21;
predictorNames_tr = {'x02', 'x11', 'x13', 'x18', 'x29', 'x36'};
predictors_tr = inputTable_tr(:, predictorNames);
response_tr = inputTable_tr.x21;
isCategoricalPredictor_tr = [false, false, false, false, false, false];
inputTable_val = validation_w21;
predictorNames_val = {'x02', 'x11', 'x13', 'x18', 'x29', 'x36'};
predictors_val = inputTable_val(:, predictorNames);
response_val = inputTable_val.x21;
isCategoricalPredictor_val = [false, false, false, false, false, false];
trainingPredictors = predictors_tr;
trainingResponse = response_tr;
trainingIsCategoricalPredictor = isCategoricalPredictor_tr;
valPredictors = predictors_val;
valResponse = response_val;
valIsCategoricalPredictor = isCategoricalPredictor_val;
regressionGP = fitrgp(...
'BasisFunction', 'constant', ...
'KernelFunction', 'matern52', ...
'OptimizeHyperparameters','auto',...
gpPredictFcn = @(x) predict(regressionGP, x);
validationPredictFcn = @(x) gpPredictFcn(x);
trainingPredictions=trainedModel.predictFcn(trainingPredictors);
trainingr = trainingResponse-trainingPredictions;
trainingnormr = norm(trainingr);
trainingSSE = trainingnormr.^2;
trainingSST = norm(trainingResponse-mean(trainingResponse))^2;
trainingR2 = 1 - trainingSSE/trainingSST
validationr = valResponse-validationPredictFcn(valPredictors);
validationnormr = norm(validationr);
validationSSE = validationnormr.^2;
validationSST = norm(valResponse-mean(valResponse))^2;
validationR2 = 1 - validationSSE/validationSST