1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2005 Free Software Foundation, Inc. Written by Jason H. Stover.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include <gsl/gsl_math.h>
21 #include <gsl/gsl_vector.h>
22 #include <gsl/gsl_matrix.h>
30 PSPP_LINREG_CONDITIONAL_INVERSE,
38 Options describing what special values should be computed.
40 struct pspp_linreg_opts_struct
42 int get_depvar_mean_std;
43 int *get_indep_mean_std; /* Array of booleans
45 independent variables need
46 their means and standard
47 deviations computed within
48 pspp_linreg. This array
50 n_indeps. If element i is
53 variance of indpendent
54 variable i. If element i
55 is 0, it will not compute
57 deviation, and assume the
59 cache->indep_mean[i] is
61 cache->indep_std[i] is the
62 sample standard deviation. */
64 typedef struct pspp_linreg_opts_struct pspp_linreg_opts;
68 Find the least-squares estimate of b for the linear model:
72 where Y is an n-by-1 column vector, X is an n-by-p matrix of
73 independent variables, b is a p-by-1 vector of regression coefficients,
74 and Z is an n-by-1 normally-distributed random vector with independent
75 identically distributed components with mean 0.
77 This estimate is found via the sweep operator or singular-value
78 decomposition with gsl.
83 1. Matrix Computations, third edition. GH Golub and CF Van Loan.
84 The Johns Hopkins University Press. 1996. ISBN 0-8018-5414-8.
86 2. Numerical Analysis for Statisticians. K Lange. Springer. 1999.
89 3. Numerical Linear Algebra for Applications in Statistics. JE Gentle.
90 Springer. 1998. ISBN 0-387-98542-5.
94 struct pspp_linreg_cache_struct
96 int n_obs; /* Number of observations. */
97 int n_indeps; /* Number of independent variables. */
101 The variable struct is ignored during estimation. It is here so
102 the calling procedure can find the variable used in the model.
104 const struct variable *depvar;
106 gsl_vector *residuals;
107 struct pspp_coeff **coeff;
108 int method; /* Method to use to estimate parameters. */
110 Means and standard deviations of the variables.
111 If these pointers are null when pspp_linreg() is
112 called, pspp_linreg() will compute their values.
114 Entry i of indep_means is the mean of independent
115 variable i, whose observations are stored in the ith
116 column of the design matrix.
120 gsl_vector *indep_means;
121 gsl_vector *indep_std;
126 double ssm; /* Sums of squares for the overall model. */
127 gsl_vector *ss_indeps; /* Sums of squares from each
128 independent variable. */
129 double sst; /* Sum of squares total. */
130 double sse; /* Sum of squares error. */
131 double mse; /* Mean squared error. This is just sse /
132 dfe, but since it is the best unbiased
133 estimate of the population variance, it
134 has its own entry here. */
135 gsl_vector *ssx; /* Centered sums of squares for independent
136 variables, i.e. \sum (x[i] - mean(x))^2. */
137 double ssy; /* Centered sums of squares for dependent
141 Covariance matrix of the parameter estimates.
152 'Hat' or Hessian matrix, i.e. (X'X)^{-1}, where X is our
157 double (*predict) (const struct variable **, const union value **,
159 double (*residual) (const struct variable **,
160 const union value **,
161 const union value *, const void *, int);
163 Returns pointers to the variables used in the model.
165 int (*get_vars) (const void *, const struct variable **);
166 struct variable *resid;
167 struct variable *pred;
171 typedef struct pspp_linreg_cache_struct pspp_linreg_cache;
176 Allocate a pspp_linreg_cache and return a pointer
177 to it. n is the number of cases, p is the number of
178 independent variables.
180 pspp_linreg_cache *pspp_linreg_cache_alloc (size_t n, size_t p);
182 bool pspp_linreg_cache_free (void *);
185 Fit the linear model via least squares. All pointers passed to pspp_linreg
186 are assumed to be allocated to the correct size and initialized to the
187 values as indicated by opts.
190 pspp_linreg (const gsl_vector * Y, const gsl_matrix * X,
191 const pspp_linreg_opts * opts, pspp_linreg_cache * cache);
194 pspp_linreg_predict (const struct variable **, const union value **,
197 pspp_linreg_residual (const struct variable **, const union value **,
198 const union value *, const void *, int);
200 All variables used in the model.
202 int pspp_linreg_get_vars (const void *, const struct variable **);