1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2005, 2011 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_matrix.h>
22 #include <gsl/gsl_vector.h>
27 LINREG_CONDITIONAL_INVERSE,
35 Options describing what special values should be computed.
37 struct pspp_linreg_opts_struct
39 int get_depvar_mean_std;
40 int *get_indep_mean_std; /* Array of booleans
42 independent variables need
43 their means and standard
44 deviations computed within
45 pspp_linreg. This array
47 n_indeps. If element i is
50 variance of indpendent
51 variable i. If element i
52 is 0, it will not compute
54 deviation, and assume the
56 cache->indep_mean[i] is
58 cache->indep_std[i] is the
59 sample standard deviation. */
61 typedef struct pspp_linreg_opts_struct pspp_linreg_opts;
65 Find the least-squares estimate of b for the linear model:
69 where Y is an n-by-1 column vector, X is an n-by-p matrix of
70 independent variables, b is a p-by-1 vector of regression coefficients,
71 and Z is an n-by-1 normally-distributed random vector with independent
72 identically distributed components with mean 0.
74 This estimate is found via the sweep operator or singular-value
75 decomposition with gsl.
80 1. Matrix Computations, third edition. GH Golub and CF Van Loan.
81 The Johns Hopkins University Press. 1996. ISBN 0-8018-5414-8.
83 2. Numerical Analysis for Statisticians. K Lange. Springer. 1999.
86 3. Numerical Linear Algebra for Applications in Statistics. JE Gentle.
87 Springer. 1998. ISBN 0-387-98542-5.
94 double n_obs; /* Number of observations. */
95 int n_indeps; /* Number of independent variables. */
96 int n_coeffs; /* The intercept is not considered a
100 Pointers to the variables.
102 const struct variable *depvar;
103 const struct variable **indep_vars;
107 int method; /* Method to use to estimate parameters. */
109 Means and standard deviations of the variables.
110 If these pointers are null when pspp_linreg() is
111 called, pspp_linreg() will compute their values.
113 Entry i of indep_means is the mean of independent
114 variable i, whose observations are stored in the ith
115 column of the design matrix.
119 gsl_vector *indep_means;
120 gsl_vector *indep_std;
125 double ssm; /* Sums of squares for the overall model. */
126 gsl_vector *ss_indeps; /* Sums of squares from each
127 independent variable. */
128 double sst; /* Sum of squares total. */
129 double sse; /* Sum of squares error. */
130 double mse; /* Mean squared error. This is just sse /
131 dfe, but since it is the best unbiased
132 estimate of the population variance, it
133 has its own entry here. */
135 Covariance matrix of the parameter estimates.
145 struct variable *pred;
146 struct variable *resid;
147 int dependent_column; /* Column containing the dependent variable. Defaults to last column. */
150 typedef struct linreg_struct linreg;
154 linreg *linreg_alloc (const struct variable *, const struct variable **,
157 void linreg_unref (linreg *);
158 void linreg_ref (linreg *c);
161 Fit the linear model via least squares. All pointers passed to pspp_linreg
162 are assumed to be allocated to the correct size and initialized to the
163 values as indicated by opts.
166 linreg_fit (const gsl_matrix *, linreg *);
169 linreg_predict (const linreg *, const double *, size_t);
171 linreg_residual (const linreg *, double, const double *, size_t);
172 const struct variable ** linreg_get_vars (const linreg *);
175 Return or set the standard deviation of the independent variable.
177 double linreg_get_indep_variable_sd (linreg *, size_t);
178 void linreg_set_indep_variable_sd (linreg *, size_t, double);
180 Mean of the independent variable.
182 double linreg_get_indep_variable_mean (linreg *, size_t);
183 void linreg_set_indep_variable_mean (linreg *, size_t, double);
185 double linreg_mse (const linreg *);
187 double linreg_intercept (const linreg *);
189 gsl_matrix * linreg_cov (const linreg *);
190 double linreg_coeff (const linreg *, size_t);
191 const struct variable * linreg_indep_var (const linreg *, size_t);
192 size_t linreg_n_coeffs (const linreg *);
193 double linreg_n_obs (const linreg *);
194 double linreg_sse (const linreg *);
195 double linreg_ssreg (const linreg *);
196 double linreg_dfmodel (const linreg *);
197 double linreg_sst (const linreg *);
198 void linreg_set_depvar_mean (linreg *, double);
199 double linreg_get_depvar_mean (linreg *);