📄 logitfit.m
字号:
function [b, Ib, Vb] = logitfit(y,x,C);%LOGITFIT Fit a logistic regression model.%% [b, Ib, Vb] = logitfit(y,X,C)%% Fit the model log(p/(1-p)) = X*b, where p is the probability % that y is 1 and not 0. Output b is vector of point estimates, % Ib is confidence intervals, and Vb is the estimated variance % matrix of b. Input C is confidence level for the confidence % intervals, default is 0.95.%% If an intercept is not included in X it is automatically added % as an extra column. Note that the intercept is then the last% coefficient, not the first!%% See also LODDS and LODDSINV.% Anders Holtsberg, 14-12-94% Copyright (c) Anders Holtsbergif nargin<3 C = 0.95;endif size(y,2)>1 error('Input y must be column vector');endn = length(y);if sum(y==1|y==0) < n error('Hey, only 0 or 1 as response varable y')endone = ones(n,1);if any(abs(one-x*(x\one)) > 1e-10) fprintf(' Intercept column added \n') x = [x, ones(n,1)];endnb = size(x,2);b = x\(4*y-2);for i=1:50 z = x*b; g1 = 1+exp(-z); g0 = 1+exp(+z); df1 = -1 ./ g0; df0 = +1 ./ g1; df = sum(((y.*df1+(1-y).*df0)*ones(1,nb)) .* x)'; ddf = (((1 ./ (g0+g1))*ones(1,nb)) .* x)'*x; b = b - (ddf\df); if all(abs(df)<0.0001), break; end;endif i== 50, error('No convergence'), endlogL = y.*log(g1) + (1-y).*log(g0);Vb = inv(ddf);lamda = qnorm(1-(1-C)/2);Ib = lamda*sqrt(diag(Vb));Ib = [b-Ib, b+Ib];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -