cross-validate fitlm

5 views (last 30 days)
Toby Feld
Toby Feld on 8 May 2021
Answered: Sameer on 16 Sep 2024
Hello,
I would like to use fitlm to also take non-linear relationships into account.
I have X1 which is a 100 x 3 matrix, predictive variables (for training). X2 is a 50 x 3 matrix, predictive variables (for testing). I have Y1 which is a 100 x 1 matrix, predicted variable (for training). Y2 is a 50 x 1 matrix, predicted variable (for testing).
lm = fitlm(X1,Y1,'quadratic');
This gives me a very complex variable, a "1x1 linear model".
Now how can I use the model lm to cross-validate? I can see that there are estimated coefficients, but there are also coefficients for interactions (e.g., "x1:x2"). I am not sure how to do this, but I want something like "testlm(X2,Y2,'quadratic')".
Thanks,
Tim

Answers (1)

Sameer
Sameer on 16 Sep 2024
Hi Tim
From my understanding, you want to perform cross-validation on a quadratic model you have already fitted using "fitlm" in MATLAB and use the model to predict outcomes for your test dataset and evaluate its performance.
Here’s a step-by-step guide to achieve this:
1. Model Fitting: A quadratic model is fitted using "fitlm" with the training data ('X1' and 'Y1').
lm = fitlm(X1, Y1, 'quadratic');
2. Prediction: The "predict" function is used to generate predictions for the test data ('X2').
Y2_pred = predict(lm, X2);
3. Performance Evaluation: The predicted values ('Y2_pred') are compared with the actual test outcomes ('Y2'). Metrics such as Mean Squared Error and R-squared can be calculated to assess model performance.
% Calculate Mean Squared Error
mse = mean((Y2 - Y2_pred).^2);
disp(['Mean Squared Error: ', num2str(mse)]);
% Calculate R-squared
ss_res = sum((Y2 - Y2_pred).^2);
ss_tot = sum((Y2 - mean(Y2)).^2);
r_squared = 1 - (ss_res / ss_tot);
disp(['R-squared: ', num2str(r_squared)]);
This approach effectively tests the model on new data and evaluates its generalization capability. The "fitlm" function manages the complexity of quadratic terms and interactions, enabling direct use of the "predict" function for predictions without manually specifying the terms again.
Hope this helps!

Tags

Community Treasure Hunt

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

Start Hunting!