Mercurial > octave
view scripts/statistics/models/logistic_regression.m @ 21633:dcf8922b724b
Deprecate printf, puts, and scanf. Make fputs a M-file.
* libinterp/corefcn/file-io.cc: extracted printf, puts, and scanf to M-files.
* scripts/deprecated/printf.m: new function, equivalent to version file-io.cc.
* scripts/deprecated/puts.m: new function, equivalent to version file-io.cc.
* scripts/deprecated/scanf.m: new function, equivalent to version file-io.cc.
* scripts/deprecated/module.mk: added printf, puts, and scanf.
* scripts/io/fputs.m: new function, equivalent to version file-io.cc.
* scripts/io/module.mk: added fputs.
* doc/interpreter/io.txi: adapted documentation to current implementation.
* NEWS: announced changes.
* doc/interpreter/basics.txi: replaced printf by fprintf.
* doc/interpreter/stmt.txi: replaced printf by fprintf.
* doc/interpreter/var.txi: replaced printf by fprintf.
* doc/refcard/refcard.tex: removed printf and scanf.
* examples/code/@FIRfilter/display.m: replaced printf by fprintf.
* examples/code/@polynomial/display.m: replaced printf by fprintf.
* scripts/@ftp/display.m: replaced printf by fprintf.
* scripts/general/inputParser.m: replaced printf by fprintf.
* scripts/general/methods.m: replaced printf by fprintf.
* scripts/general/profexplore.m: replaced printf by fprintf.
* scripts/general/profshow.m: replaced printf by fprintf.
* scripts/help/help.m: replaced puts by fputs and printf by fprintf.
* scripts/help/lookfor.m: replaced puts by fputs and printf by fprintf.
* scripts/help/which.m: replaced printf by fprintf.
* scripts/image/imformats.m: replaced printf by fprintf.
* scripts/io/beep.m: replaced puts by fputs.
* scripts/io/textread.m: replaced printf by fprintf.
* scripts/java/javaclasspath.m: replaced printf by fprintf.
* scripts/java/javamem.m: replaced printf by fprintf.
* scripts/miscellaneous/dir.m: replaced printf by fprintf.
* scripts/miscellaneous/dos.m: replaced printf by fprintf.
* scripts/miscellaneous/fact.m: replaced printf by fprintf.
* scripts/miscellaneous/info.m: replaced printf by fprintf.
* scripts/miscellaneous/license.m: replaced printf by fprintf.
* scripts/miscellaneous/ls.m: replaced puts by fputs.
* scripts/miscellaneous/menu.m: replaced printf by fprintf.
* scripts/miscellaneous/mkoctfile.m: replaced printf by fprintf.
* scripts/miscellaneous/private/display_info_file.m:
* scripts/miscellaneous/unix.m: replaced printf by fprintf.
* scripts/miscellaneous/ver.m: replaced printf by fprintf.
* scripts/miscellaneous/what.m: replaced printf by fprintf.
* scripts/ode/ode23.m: replaced printf by fprintf.
* scripts/ode/ode45.m: replaced printf by fprintf.
* scripts/optimization/fminbnd.m: replaced printf by fprintf.
* scripts/optimization/optimset.m: replaced puts by fputs and printf by fprintf.
* scripts/optimization/sqp.m: replaced printf by fprintf.
* scripts/pkg/pkg.m: replaced printf by fprintf.
* scripts/pkg/private/describe.m: replaced printf by fprintf.
* scripts/pkg/private/install.m: replaced printf by fprintf.
* scripts/pkg/private/installed_packages.m: replaced printf by fprintf.
* scripts/pkg/private/list_forge_packages.m: replaced puts by fputs and printf by fprintf.
* scripts/pkg/private/rebuild.m: replaced printf by fprintf.
* scripts/plot/util/__gnuplot_drawnow__.m: replaced puts by fputs and printf by fprintf.
* scripts/signal/stft.m: replaced printf by fprintf.
* scripts/sparse/bicg.m: replaced printf by fprintf.
* scripts/sparse/bicgstab.m: replaced printf by fprintf.
* scripts/sparse/cgs.m: replaced printf by fprintf.
* scripts/sparse/pcg.m: replaced printf by fprintf.
* scripts/sparse/pcr.m: replaced printf by fprintf.
* scripts/sparse/qmr.m: replaced printf by fprintf.
* scripts/statistics/models/logistic_regression.m: replaced printf by fprintf.
* scripts/statistics/tests/anova.m: replaced printf by fprintf.
* scripts/statistics/tests/bartlett_test.m: replaced printf by fprintf.
* scripts/statistics/tests/chisquare_test_homogeneity.m: replaced printf by fprintf.
* scripts/statistics/tests/chisquare_test_independence.m: replaced printf by fprintf.
* scripts/statistics/tests/cor_test.m: replaced printf by fprintf.
* scripts/statistics/tests/f_test_regression.m: replaced printf by fprintf.
* scripts/statistics/tests/hotelling_test.m: replaced printf by fprintf.
* scripts/statistics/tests/hotelling_test_2.m: replaced printf by fprintf.
* scripts/statistics/tests/kolmogorov_smirnov_test.m: replaced printf by fprintf.
* scripts/statistics/tests/kolmogorov_smirnov_test_2.m: replaced printf by fprintf.
* scripts/statistics/tests/kruskal_wallis_test.m: replaced printf by fprintf.
* scripts/statistics/tests/manova.m: replaced printf by fprintf.
* scripts/statistics/tests/mcnemar_test.m: replaced printf by fprintf.
* scripts/statistics/tests/prop_test_2.m: replaced printf by fprintf.
* scripts/statistics/tests/run_test.m: replaced printf by fprintf.
* scripts/statistics/tests/sign_test.m: replaced printf by fprintf.
* scripts/statistics/tests/t_test.m: replaced printf by fprintf.
* scripts/statistics/tests/t_test_2.m: replaced printf by fprintf.
* scripts/statistics/tests/t_test_regression.m: replaced printf by fprintf.
* scripts/statistics/tests/u_test.m: replaced printf by fprintf.
* scripts/statistics/tests/var_test.m: replaced printf by fprintf.
* scripts/statistics/tests/welch_test.m: replaced printf by fprintf.
* scripts/statistics/tests/wilcoxon_test.m: replaced printf by fprintf.
* scripts/statistics/tests/z_test.m: replaced printf by fprintf.
* scripts/statistics/tests/z_test_2.m: replaced printf by fprintf.
* scripts/strings/strtok.m: replaced printf by fprintf.
* scripts/testfun/__run_test_suite__.m: replaced puts by fputs and printf by fprintf.
* scripts/testfun/demo.m: replaced printf by fprintf.
* scripts/testfun/example.m: replaced printf by fprintf.
* scripts/testfun/private/compare_plot_demos.m: replaced printf by fprintf.
* scripts/testfun/rundemos.m: replaced printf by fprintf.
* scripts/testfun/runtests.m: replaced puts by fputs and printf by fprintf.
* scripts/testfun/speed.m: replaced printf by fprintf.
* scripts/testfun/test.m: replaced printf by fprintf.
* scripts/time/calendar.m: replaced puts by fputs and printf by fprintf.
* test/io.tst: replaced printf by fprintf.
author | Kai T. Ohlhus <k.ohlhus@gmail.com> |
---|---|
date | Wed, 20 Apr 2016 17:53:10 +0200 |
parents | 516bb87ea72e |
children | 96518f623c91 |
line wrap: on
line source
## Copyright (C) 1995-2015 Kurt Hornik ## ## This file is part of Octave. ## ## Octave is free software; you can redistribute it and/or modify it ## under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 3 of the License, or (at ## your option) any later version. ## ## Octave is distributed in the hope that it will be useful, but ## WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ## General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with Octave; see the file COPYING. If not, see ## <http://www.gnu.org/licenses/>. ## -*- texinfo -*- ## @deftypefn {} {[@var{theta}, @var{beta}, @var{dev}, @var{dl}, @var{d2l}, @var{p}] =} logistic_regression (@var{y}, @var{x}, @var{print}, @var{theta}, @var{beta}) ## Perform ordinal logistic regression. ## ## Suppose @var{y} takes values in @var{k} ordered categories, and let ## @code{gamma_i (@var{x})} be the cumulative probability that @var{y} ## falls in one of the first @var{i} categories given the covariate ## @var{x}. Then ## ## @example ## [theta, beta] = logistic_regression (y, x) ## @end example ## ## @noindent ## fits the model ## ## @example ## logit (gamma_i (x)) = theta_i - beta' * x, i = 1 @dots{} k-1 ## @end example ## ## The number of ordinal categories, @var{k}, is taken to be the number ## of distinct values of @code{round (@var{y})}. If @var{k} equals 2, ## @var{y} is binary and the model is ordinary logistic regression. The ## matrix @var{x} is assumed to have full column rank. ## ## Given @var{y} only, @code{theta = logistic_regression (y)} ## fits the model with baseline logit odds only. ## ## The full form is ## ## @example ## @group ## [theta, beta, dev, dl, d2l, gamma] ## = logistic_regression (y, x, print, theta, beta) ## @end group ## @end example ## ## @noindent ## in which all output arguments and all input arguments except @var{y} ## are optional. ## ## Setting @var{print} to 1 requests summary information about the fitted ## model to be displayed. Setting @var{print} to 2 requests information ## about convergence at each iteration. Other values request no ## information to be displayed. The input arguments @var{theta} and ## @var{beta} give initial estimates for @var{theta} and @var{beta}. ## ## The returned value @var{dev} holds minus twice the log-likelihood. ## ## The returned values @var{dl} and @var{d2l} are the vector of first ## and the matrix of second derivatives of the log-likelihood with ## respect to @var{theta} and @var{beta}. ## ## @var{p} holds estimates for the conditional distribution of @var{y} ## given @var{x}. ## @end deftypefn ## Original for MATLAB written by Gordon K Smyth <gks@maths.uq.oz.au>, ## U of Queensland, Australia, on Nov 19, 1990. Last revision Aug 3, ## 1992. ## Author: Gordon K Smyth <gks@maths.uq.oz.au>, ## Adapted-By: KH <Kurt.Hornik@wu-wien.ac.at> ## Description: Ordinal logistic regression ## Uses the auxiliary functions logistic_regression_derivatives and ## logistic_regression_likelihood. function [theta, beta, dev, dl, d2l, p] = logistic_regression (y, x, print, theta, beta) ## check input y = round (vec (y)); [my, ny] = size (y); if (nargin < 2) x = zeros (my, 0); endif; [mx, nx] = size (x); if (mx != my) error ("logistic_regression: X and Y must have the same number of observations"); endif ## initial calculations x = -x; tol = 1e-6; incr = 10; decr = 2; ymin = min (y); ymax = max (y); yrange = ymax - ymin; z = (y * ones (1, yrange)) == ((y * 0 + 1) * (ymin : (ymax - 1))); z1 = (y * ones (1, yrange)) == ((y * 0 + 1) * ((ymin + 1) : ymax)); z = z(:, any (z)); z1 = z1(:, any(z1)); [mz, nz] = size (z); ## starting values if (nargin < 3) print = 0; endif; if (nargin < 4) beta = zeros (nx, 1); endif; if (nargin < 5) g = cumsum (sum (z))' ./ my; theta = log (g ./ (1 - g)); endif; tb = [theta; beta]; ## likelihood and derivatives at starting values [g, g1, p, dev] = logistic_regression_likelihood (y, x, tb, z, z1); [dl, d2l] = logistic_regression_derivatives (x, z, z1, g, g1, p); epsilon = std (vec (d2l)) / 1000; ## maximize likelihood using Levenberg modified Newton's method iter = 0; while (abs (dl' * (d2l \ dl) / length (dl)) > tol) iter += 1; tbold = tb; devold = dev; tb = tbold - d2l \ dl; [g, g1, p, dev] = logistic_regression_likelihood (y, x, tb, z, z1); if ((dev - devold) / (dl' * (tb - tbold)) < 0) epsilon /= decr; else while ((dev - devold) / (dl' * (tb - tbold)) > 0) epsilon *= incr; if (epsilon > 1e+15) error ("logistic_regression: epsilon too large"); endif tb = tbold - (d2l - epsilon * eye (size (d2l))) \ dl; [g, g1, p, dev] = logistic_regression_likelihood (y, x, tb, z, z1); disp ("epsilon"); disp (epsilon); endwhile endif [dl, d2l] = logistic_regression_derivatives (x, z, z1, g, g1, p); if (print == 2) disp ("Iteration"); disp (iter); disp ("Deviance"); disp (dev); disp ("First derivative"); disp (dl'); disp ("Eigenvalues of second derivative"); disp (eig (d2l)'); endif endwhile ## tidy up output theta = tb(1 : nz, 1); beta = tb((nz + 1) : (nz + nx), 1); if (print >= 1) fprintf ("\n"); fprintf ("Logistic Regression Results:\n"); fprintf ("\n"); fprintf ("Number of Iterations: %d\n", iter); fprintf ("Deviance: %f\n", dev); fprintf ("Parameter Estimates:\n"); fprintf (" Theta S.E.\n"); se = sqrt (diag (inv (-d2l))); for i = 1 : nz fprintf (" %8.4f %8.4f\n", tb (i), se (i)); endfor if (nx > 0) fprintf (" Beta S.E.\n"); for i = (nz + 1) : (nz + nx) fprintf (" %8.4f %8.4f\n", tb (i), se (i)); endfor endif endif if (nargout == 6) if (nx > 0) e = ((x * beta) * ones (1, nz)) + ((y * 0 + 1) * theta'); else e = (y * 0 + 1) * theta'; endif gamma = diff ([(y * 0), (exp (e) ./ (1 + exp (e))), (y * 0 + 1)]')'; endif endfunction