4 Copyright (C) 2005 Free Software Foundation, Inc. Written by Jason H. Stover.
6 This program is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2 of the License, or (at your option)
11 This program is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 You should have received a copy of the GNU General Public License along with
17 this program; if not, write to the Free Software Foundation, Inc., 51
18 Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA.
24 #include <gsl/gsl_math.h>
25 #include <gsl/gsl_vector.h>
26 #include <gsl/gsl_matrix.h>
29 struct pspp_linreg_coeff;
41 Options describing what special values should be computed.
43 struct pspp_linreg_opts_struct
45 int resid; /* Should the residuals be returned? */
47 int get_depvar_mean_std;
48 int *get_indep_mean_std; /* Array of booleans
50 independent variables need
51 their means and standard
52 deviations computed within
53 pspp_linreg. This array
55 n_indeps. If element i is
58 variance of indpendent
59 variable i. If element i
60 is 0, it will not compute
62 deviation, and assume the
64 cache->indep_mean[i] is
66 cache->indep_std[i] is the
67 sample standard deviation. */
69 typedef struct pspp_linreg_opts_struct pspp_linreg_opts;
73 Find the least-squares estimate of b for the linear model:
77 where Y is an n-by-1 column vector, X is an n-by-p matrix of
78 independent variables, b is a p-by-1 vector of regression coefficients,
79 and Z is an n-by-1 normally-distributed random vector with independent
80 identically distributed components with mean 0.
82 This estimate is found via the sweep operator or singular-value
83 decomposition with gsl.
88 1. Matrix Computations, third edition. GH Golub and CF Van Loan.
89 The Johns Hopkins University Press. 1996. ISBN 0-8018-5414-8.
91 2. Numerical Analysis for Statisticians. K Lange. Springer. 1999.
94 3. Numerical Linear Algebra for Applications in Statistics. JE Gentle.
95 Springer. 1998. ISBN 0-387-98542-5.
99 struct pspp_linreg_cache_struct
101 int n_obs; /* Number of observations. */
102 int n_indeps; /* Number of independent variables. */
106 The variable struct is ignored during estimation. It is here so
107 the calling procedure can find the variable used in the model.
109 const struct variable *depvar;
111 gsl_vector *residuals;
112 struct pspp_linreg_coeff *coeff;
113 int method; /* Method to use to estimate parameters. */
115 Means and standard deviations of the variables.
116 If these pointers are null when pspp_linreg() is
117 called, pspp_linreg() will compute their values.
119 Entry i of indep_means is the mean of independent
120 variable i, whose observations are stored in the ith
121 column of the design matrix.
125 gsl_vector *indep_means;
126 gsl_vector *indep_std;
131 double ssm; /* Sums of squares for the overall model. */
132 gsl_vector *ss_indeps; /* Sums of squares from each
133 independent variable. */
134 double sst; /* Sum of squares total. */
135 double sse; /* Sum of squares error. */
136 double mse; /* Mean squared error. This is just sse /
137 dfe, but since it is the best unbiased
138 estimate of the population variance, it
139 has its own entry here. */
140 gsl_vector *ssx; /* Centered sums of squares for independent
141 variables, i.e. \sum (x[i] - mean(x))^2. */
142 double ssy; /* Centered sums of squares for dependent
146 Covariance matrix of the parameter estimates.
157 'Hat' or Hessian matrix, i.e. (X'X)^{-1}, where X is our
162 double (*predict) (const struct variable **, const union value **,
166 typedef struct pspp_linreg_cache_struct pspp_linreg_cache;
171 Allocate a pspp_linreg_cache and return a pointer
172 to it. n is the number of cases, p is the number of
173 independent variables.
175 pspp_linreg_cache *pspp_linreg_cache_alloc (size_t n, size_t p);
177 void pspp_linreg_cache_free (pspp_linreg_cache * c);
180 Fit the linear model via least squares. All pointers passed to pspp_linreg
181 are assumed to be allocated to the correct size and initialized to the
182 values as indicated by opts.
185 pspp_linreg (const gsl_vector * Y, const gsl_matrix * X,
186 const pspp_linreg_opts * opts, pspp_linreg_cache * cache);
189 pspp_linreg_predict (const struct variable **, const union value **,
190 const pspp_linreg_cache *, int);
192 pspp_linreg_residual (const struct variable *, const union value **,
193 const union value *, const pspp_linreg_cache *, int);