How to quickly transform a stepwiselm result into a function for further optimization?
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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
1 Commento
Risposte (1)
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)
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
Based on your requirements, you can manipulate the “CoefficientNames” 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,":")
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!
0 Commenti
Vedere anche
Categorie
Scopri di più su Linear Regression in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!