How to develop "a model" from a given m-file of regression analysis?
Show older comments
With the help of the attached code I need to develop a model for the price variation based on a combined (linear) dependence on model year such as mileage, plot the result, and develop a corresponding R2 value.
But what does this mean? Does it mean to get a function, where the price is the function f(x) and mileage and model year are parameters in the form of f(x)=ax+b ?
If so, how is that done? I can only find B, L and c in the output.
Anhy suggestions welcomed.
Thanks!
Accepted Answer
More Answers (1)
Image Analyst
on 29 Feb 2024
1 vote
What I would do is to use the Regression Learner app on the Apps tab of the tool ribbon. It's in the Statistics and Machine Learning Toolbox.
Put in your inputs into a table called predictors, and your "response" variable would be the price vector. Then tell it to use all models to do a regression. Look at the errors and choose the model with the lowest errors. Then click the Export button to save the model into a .mat file. The mat file will contain a string that tells you how to apply the model to obtain an estimated price given a set of predictor values.
Attach your workbook if you need more help.
5 Comments
Image Analyst
on 29 Feb 2024
You didn't tell it to use all models. I did. It didn't crash but some of the model fits failed. It said the best fitting model is a stepwise linear regression. I selected that model and said to export the model to the workspace as trainedModel via the button on the tool ribbon. Then I used this command in the command window to save it from the workspace to a disk file:
save('trainedModel.mat', 'trainedModel');
To load the model from disk and use it on old or new data:
s = load('trainedModel.mat')
trainedModel = s.trainedModel
trainedModel =
struct with fields:
predictFcn: @(x)exportableModel.predictFcn(predictorExtractionFcn(x))
RequiredVariables: {'x_rsm' 'Mil' 'Var4'}
LinearModel: [1×1 LinearModel]
About: 'This struct is a trained model exported from Regression Learner R2023b.'
HowToPredict: 'To make predictions on a new table, T, use: ↵ yfit = c.predictFcn(T) ↵replacing 'c' with the name of the variable that is this struct, e.g. 'trainedModel'. ↵ ↵The table, T, must contain the variables returned by: ↵ c.RequiredVariables ↵Variable formats (e.g. matrix/vector, datatype) must match the original training data. ↵Additional variables are ignored. ↵ ↵For more information, see How to predict using an exported model.'
As you can see, even the best model does not do a perfect prediction on the validation (non-training) data.

Image Analyst
on 29 Feb 2024
Slightly better than that : 0.76

That's on the validation data (not used in training). It should be higher if you included the training data of course, but that's not really fair, especially if you're using it on new data, so the r squared on the validation data is what's fair to use.
Sergio
on 2 Mar 2024
Categories
Find more on Linear Predictive Coding in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
