How to quickly transform a stepwiselm result into a function for further optimization?

3 visualizzazioni (ultimi 30 giorni)
Dear MathWorks Community,
Could you help me in finding an "automized" and quick way to transform the result of a multivariate regression using stepwiselm function of Matlab into a function, which I then use to find a global minimum?
Matlab gives me this for the regression result:
Linear regression model:
y ~ [Linear formula with 19 terms in 7 predictors]
Estimated Coefficients:
Estimate SE tStat pValue
___________ __________ _______ __________
(Intercept) 2.5665 0.36019 7.1252 3.3076e-10
x2 -0.093719 0.018019 -5.2012 1.3745e-06
x3 -0.032841 0.0065094 -5.0451 2.5826e-06
x4 -0.068186 0.013673 -4.9869 3.2601e-06
x5 -0.13995 0.024853 -5.631 2.3202e-07
x6 0.011896 0.0077462 1.5357 0.12837
x7 -0.012796 0.0061187 -2.0913 0.039521
x8 -0.047325 0.01442 -3.2819 0.001503
x2^2 0.0014954 0.00042943 3.4823 0.00079162
x2:x4 0.001958 0.00052507 3.7291 0.000348
x2:x5 0.0022529 0.00051424 4.381 3.3908e-05
x3:x5 0.0022308 0.00048064 4.6414 1.2633e-05
x4:x5 0.0018265 0.00050092 3.6462 0.00046034
x5^2 0.001424 0.00040481 3.5177 0.00070503
x6^2 -0.00066448 0.0002251 -2.9519 0.0040932
x5:x7 0.0015826 0.00040548 3.903 0.00019101
x2:x8 0.0010058 0.00057961 1.7352 0.086365
x4:x8 0.0014672 0.00044572 3.2917 0.0014578
x5:x8 0.0016849 0.00054847 3.072 0.002866
and until now I manually created the function below based on the proprosed regression factors:
>> fun=@(x)+2.5665-0.093719*x(2)-0.032841*x(3)-0.068186*x(4)-0.13995*x(5)+0.011896*x(6)-0.012796*x(7)-0.047325*x(8)+0.0014954*x(2)^2+0.001958*x(2)*x(4)+0.0022529*x(2)*x(5)+0.0022308*x(3)*x(5)+0.0018265*x(4)*x(5)+0.001424*x(5)^2-0.00066448*x(6)^2+0.0015826*x(5)*x(7)+0.0010058*x(2)*x(8)+0.0014672*x(4)*x(8)+0.0016849*x(5)*x(8);
Thanks a lot!!!
Best regards and Merry Christmas!
Philipp

Risposte (1)

Shubham
Shubham il 16 Feb 2024
Modificato: Shubham il 16 Feb 2024
Hi Philip,
It seems that you are trying to use the results from a stepwiselm function for further optimization. You can access the output of regression model from the “Fitted” parameter in your resultant “Linear Model”. However, if you wish to use the results for further analysis, you can create a function as well.
Have a look at the following example for a simple regression model (y ~ 1 + x1 + x2):
load hald
mdl = stepwiselm(ingredients,heat,'PEnter',0.06)
1. Adding x4, FStat = 22.7985, pValue = 0.000576232 2. Adding x1, FStat = 108.2239, pValue = 1.105281e-06 3. Adding x2, FStat = 5.0259, pValue = 0.051687 4. Removing x4, FStat = 1.8633, pValue = 0.2054
mdl =
Linear regression model: y ~ 1 + x1 + x2 Estimated Coefficients: Estimate SE tStat pValue ________ ________ ______ __________ (Intercept) 52.577 2.2862 22.998 5.4566e-10 x1 1.4683 0.1213 12.105 2.6922e-07 x2 0.66225 0.045855 14.442 5.029e-08 Number of observations: 13, Error degrees of freedom: 10 Root Mean Squared Error: 2.41 R-squared: 0.979, Adjusted R-Squared: 0.974 F-statistic vs. constant model: 230, p-value = 4.41e-09
coeff_names = mdl.CoefficientNames;
estimates = mdl.Coefficients.Estimate;
nRows = mdl.NumObservations;
len = length(coeff_names);
% Initializing output as Intercept
out = estimates(1);
for i = 2:len
% extracting column number
var = coeff_names{i} ;
var_num = var(2:end) ;
col_no = str2num(var_num ) ;
% Calculating prediction
out = out + ingredients(:,col_no).* estimates(i) ;
end
out
out = 13×1
80.0740 73.2509 105.8147 89.2585 97.2925 105.1525 104.0021 74.5754 91.2755 114.5375
Based on your requirements, you can manipulate theCoefficientNames” as per the Wilkinson Notation”:
You can split the string using delimiters based on the operation being performed:
For example, for product of terms, you can try splitting the string using “:” as delimiter:
variable = "x1:x2";
ans = split(variable,":")
ans = 2×1 string array
"x1" "x2"
You can try splitting the string based on precedence of operators (operator with least precedence to be accounted first). After splitting the string into tokens, you can perform various operations accordingly. For multiple operators, you can perform this task recursively.
I hope this helps!

Prodotti


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by