logistic regression in matlab

Viewed 8667

i would like to implement logistic regression in matlab, i have following few code for this

function B=logistic_regression(x,y)

f=@(a)(sum(y.*log((exp(a(1)+a(2)*x)/(1+exp(a(1)+a(2)*x))))+(1-y).*log((1-((exp(a(1)+a(2)*x)/(1+exp(a(1)+a(2)*x))))))));
a=[0.1, 0.1];
options = optimset('PlotFcns',@optimplotfval);
B = fminsearch(f,a, options);

end

logistic regression is following :

first we are calculating logit which is equal to

L=b0+b1*x

then we are calculating probability which is equal to p=e^L/(1+e^L)

and finally we are calculating

y*ln(p)+(1-y)*ln(1-p)

i decided to write all those stuff in one line, but when i am running code , it gives me following error

>> B=logistic_regression(x,y)
Assignment has more non-singleton rhs dimensions than non-singleton subscripts

Error in fminsearch (line 200)
fv(:,1) = funfcn(x,varargin{:});

Error in logistic_regression (line 6)
B = fminsearch(f,a, options);

how can i fix this problem? thanks in advance

1 Answers

In order to implement a logistic regression model, I usually call the glmfit function, which is the simpler way to go. The syntax is:

 b = glmfit(x,y,'binomial','link','logit');

b is a vector that contains the coefficients for the linear portion of the logistic regression (the first element is the constant term alpha of the regression). x contains the predictors data, with one row for each observation and one column for each variable. y contains the target variable, usually a vector of boolean (0 or 1) values representing the outcome.

Once you obtain the coefficients, you have to apply the linear part of the regression to your predictors:

z = b(1) + (x * b(2));

To finish, you must apply the logistic function to the output of the linear part:

z = 1 ./ (1 + exp(-z));

If you need more tinkering on your data or on your output, and you require more flexibility and control over your model, I suggest you to look at this implementation:

https://github.com/mohammadaltaleb/Logistic-Regression

Related