Borrar filtros
Borrar filtros

How to use OutputFcn with fitnlm

33 visualizaciones (últimos 30 días)
Adrian Batista Planas
Adrian Batista Planas el 4 de Ag. de 2024 a las 16:19
Comentada: Matt J el 5 de Ag. de 2024 a las 18:47
Hi all,
How do I execute an OutputFcn function in each iteration inside fitlm? I have tried to run the following code but it does not print anything or throw an exception.
load carbig
tbl = table(Horsepower,Weight,MPG);
modelfun = @(b,x)b(1) + b(2)*x(:,1).^b(3) + ...
b(4)*x(:,2).^b(5);
beta0 = [-50 500 -1 500 -1];
opts = statset("fitnlm");
opts.OutputFcn = @(x)disp(x);
mdl = fitnlm(tbl,modelfun,beta0,"Options",opts);
  1 comentario
dpb
dpb el 4 de Ag. de 2024 a las 16:46
fitnlm doesn't document the 'OutputFcn' field in the options list; I'm guessing it's being ignored.
Try nlinfit instead.
This penchant for multiple nearly identical functions is maddening, indeed...
which -all fitnlm
/MATLAB/toolbox/stats/classreg/fitnlm.m
which -all nlinfit
/MATLAB/toolbox/stats/stats/nlinfit.m

Iniciar sesión para comentar.

Respuestas (1)

Matt J
Matt J el 4 de Ag. de 2024 a las 20:50
Editada: Matt J el 5 de Ag. de 2024 a las 18:45
Only Optimization Toolbox solvers have an OutputFcn option. Perhaps you are thinking of lsqcurvefit,
load carbig
tbl = table(Horsepower,Weight,MPG);
modelfun = @(b,x)b(1) + b(2)*x(:,1).^b(3) + ...
b(4)*x(:,2).^b(5);
beta0 = [-50 500 -1 500 -1];
opts = optimoptions('lsqcurvefit',OutputFcn=@outfun);
beta1 = lsqcurvefit(modelfun,tbl{:,1:2},tbl{:,3},beta0,"Options",opts);
function stop = outfun(x,optimValues,state)
stop = false;
% Check whether directional derivative norm is less than .01.
if strcmp(state,'iter')
disp(x)
end
end
  3 comentarios
Torsten
Torsten el 5 de Ag. de 2024 a las 17:12
If statistics for the fitted parameter were accessible when using "lsqcurvefit", it would be mentionned on the documentation page as possible output from the solver:
But it isn't.
Matt J
Matt J el 5 de Ag. de 2024 a las 18:47
once you have solved with lsqcurvefit, you can take the solution beta1 and use it for beta0 with fitnlm:
load carbig
tbl = table(Horsepower,Weight,MPG);
modelfun = @(b,x)b(1) + b(2)*x(:,1).^b(3) + ...
b(4)*x(:,2).^b(5);
beta0 = [-50 500 -1 500 -1];
opts = optimoptions('lsqcurvefit',OutputFcn=@outfun);
beta1 = lsqcurvefit(modelfun,tbl{:,1:2},tbl{:,3},beta0,"Options",opts);
mdl = fitnlm(tbl,modelfun,beta1,"Options",statset("fitnlm"));
You can then use mdl for whatever statistical analysis you had originally planned.

Iniciar sesión para comentar.

Productos


Versión

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by