Find minimum sse for logistic transformation

1 次查看(过去 30 天)
Hi, I have a little issue with a logistic regression transformation.
I need to find the values of a and b that give the minimum Sum of squared residuals, categorised as:
Where yhat is a logistic transformation of my known variable, categorised as:
Thanks for your time and patience.
The function that I wrote, run with fmincon, gives always an SSE = number of observation;
function [SSE,y]=logit2(Par,x)
if nargin<5;
ir=0; %default ir=0
end;
if rows(Par)<cols(Par); % put parameters in column
Par=Par';
end;
if rows(Par)~=3; % check for the the right number of parameters as inputs
error('Dimension of Par not compatible');
end;
%% Assign the Parameters to the Elements in the Vector
a=Par(1);
b=Par(end-1);
SSE=Par(end)
%%
T=rows(x);
y=zeros(T,1);
for i=1:T
y(i)=1/(1+exp(-(a+b*x(i))));
end
SSE=-sum((y - ones(size(y))).^2)
end

回答(1 个)

Nipun
Nipun 2024-5-22
Hi Andrea,
I understand that you are trying to find the values of a and b that minimize the sum of squared residuals (SSE) for a logistic regression transformation. Your current function seems to have issues since it always gives an SSE equal to the number of observations.
Here is a corrected version of your function that should properly calculate the SSE:
function SSE = logit2(Par, x, y)
if nargin < 3
error('Not enough input arguments.');
end
% Ensure parameters are column vector
if isrow(Par)
Par = Par';
end
% Check if Par has the correct number of parameters
if length(Par) ~= 2
error('Parameter vector must have exactly 2 elements.');
end
% Assign the parameters to the elements in the vector
a = Par(1);
b = Par(2);
% Calculate predicted values using logistic transformation
yhat = 1 ./ (1 + exp(-(a + b * x)));
% Calculate residuals
resid = yhat - y;
% Calculate SSE
SSE = sum(resid.^2);
end
To use the logit2 function with fmincon, you can follow this example:
% Example data
x = [0.5, 1.5, 2.5, 3.5, 4.5]'; % Replace with your x data
y = [0.2, 0.4, 0.6, 0.8, 1.0]'; % Replace with your y data
% Initial guess for parameters [a, b]
initial_guess = [0, 0];
% Define the objective function for fmincon
objective_function = @(Par) logit2(Par, x, y);
% Set options for fmincon
options = optimoptions('fmincon', 'Display', 'iter', 'Algorithm', 'sqp');
% Run fmincon to minimize SSE
[optimal_params, fval] = fmincon(objective_function, initial_guess, [], [], [], [], [], [], [], options);
% Display the results
fprintf('Optimal parameters: a = %.4f, b = %.4f\n', optimal_params(1), optimal_params(2));
fprintf('Minimum SSE: %.4f\n', fval);
Explanation:
  1. The logit2 function now correctly calculates the residuals by subtracting the actual 𝑦y values from the predicted 𝑦^y^ values.
  2. The fmincon function is used to find the values of 𝑎a and 𝑏b that minimize the SSE.
  3. The initial_guess array contains the starting values for 𝑎a and 𝑏b.
  4. The objective_function handles the minimization of the SSE.
Make sure to replace the example data x and y with your actual data points. This code should provide you with the optimal parameters for your logistic regression model.
Hope this helps.
Regards,
Nipun

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by