How to develop "a model" from a given m-file of regression analysis?

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!

 채택된 답변

Star Strider
Star Strider 2024년 2월 29일

2 개 추천

If your data are the same as in your earlier post, you can get all that information from the Curve Fitting Toolbox result.
That call would be:
fordfocusfit = fit(num{:,[1 2]}, num{:,3}, 'poly11') % The ‘poly11’ Model Does A Linear Surface Fit In Each Independent Variable
figure
plot(fordfocusfit, num{:,[1 2]}, num{:,3})
You can of course change the model from 'poly11' to whatever you want that works with 3-dimensional data (two independent variables and one dependent variable in this instance, if the data are the same as previously).
If you return a handle to the plot, you can use it to change the appearance (marker, colour, etc.).

댓글 수: 5

Sergio
Sergio 2024년 2월 29일
편집: Sergio 2024년 2월 29일
@Star Strider thanks, I got this weird message:
Brace indexing is not supported for variables of this type.
Error in bilprisanalys (line 20)
fordfocusfit = fit(num{:,[1 2]}, num{:,3}, 'poly11') % The ‘poly11’ Model Does A Linear Surface Fit In Each Independent Variable
Star Strider
Star Strider 2024년 2월 29일
편집: Star Strider 2024년 2월 29일
As always, my pleasure!
Yes! (I actually used essentially that same call to check my result to be certain it was correct.)
There are a number of models available. See the documentation on fittype for a list of them, and links to other resources to define different models, as well as how to write your own models.
EDIT — (29 Feb 2024 at 14:43)
I don’t have ‘num’ and am assuming thaat it is the same as previously, and that you read it in using readtable (that I used previously because it was not obvious to me what the variables were or how the data were arranged) and not readmatrix. (I did not take a close look at your code earlier.) If you use readtable, my code should work as I wrote it.
If you use readmatrix, replace the curly braces with parentheses:
fordfocusfit = fit(num(:,[1 2]), num(:,3), 'poly11') % The ‘poly11’ Model Does A Linear Surface Fit In Each Independent Variable
figure
plot(fordfocusfit, num(:,[1 2]), num(:,3))
That should work.
.
Sergio
Sergio 2024년 2월 29일
편집: Sergio 2024년 2월 29일
Great, thanks a lot. I try this parallel to Image Analysts idea, which unfortunately crashes at some stage.
@Star Strider did you get an R^2 value with your code? I got one with:
mdl = fitlm(A,B)
The fitlm call will certainly work. I generally use it because I do not have the Curve Fitting Toolbox, so I only use that in MATLAB Answers posts, since everything is available here.
The Curve Fitting Toolbox requires a separate ‘goodness-of-fit’ output:
[fordfocusfit,GOF] = fit(num(:,[1 2]), num(:,3), 'poly11') % The ‘poly11’ Model Does A Linear Surface Fit In Each Independent Variable
The ‘GOF’ output then has the , adjusted , and other statistics.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2024년 2월 29일

1 개 추천

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

Sergio
Sergio 2024년 2월 29일
편집: Sergio 2024년 2월 29일
@Image AnalystThanks for this idea. What I got is this, see attachment
where would the error be?
I trained ALL, and then I got a plot with a regression line. However, when trying to export it as a mat file, MATLAB crashed.
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.
Sergio
Sergio 2024년 2월 29일
편집: Sergio 2024년 2월 29일
@Image Analyst Thanks. Did you get 0.7 R^2 too?
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.

댓글을 달려면 로그인하십시오.

제품

릴리스

R2023b

태그

질문:

2024년 2월 29일

댓글:

2024년 3월 2일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by