1645d2c2d365ce59e156ca7986784a7bf307355f
[pspp-builds.git] / lib / linreg / coefficient.c
1 /* lib/linreg/coefficient.c
2
3    Copyright (C) 2005 Free Software Foundation, Inc.
4    Written by Jason H Stover.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or (at
9    your option) any later version.
10
11    This program is distributed in the hope that it will be useful, but
12    WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19    02111-1307, USA.
20  */
21
22 /*
23   Accessor functions for matching coefficients and variables.
24  */
25 #include <assert.h>
26 #include "pspp_linreg.h"
27 #include <gl/xalloc.h>
28
29
30 struct varinfo
31 {
32   const struct variable *v; /* Variable associated with this
33                                coefficient. Note this variable may not
34                                be unique. In other words, a
35                                coefficient structure may have other
36                                v_info's, each with its own variable.
37                              */
38   const union value *val; /* Value of the variable v which this
39                              varinfo refers to. This member is relevant
40                              only to categorical variables.
41                           */
42 };
43
44 void pspp_linreg_coeff_free (struct pspp_linreg_coeff *c)
45 {
46   free (c->v_info);
47   free (c);
48 }
49
50 /*
51   Initialize the variable and value pointers inside the
52   coefficient structures for the linear model.
53  */
54 void
55 pspp_linreg_coeff_init (pspp_linreg_cache *c, struct design_matrix *X)
56 {
57   size_t i;
58   size_t j;
59   int n_vals = 1;
60   struct pspp_linreg_coeff *coeff;
61   
62   c->coeff = xnmalloc (X->m->size2 + 1, sizeof (*c->coeff));
63   for (i = 0; i < X->m->size2; i++)
64     {
65       j = i + 1;                /* The first coefficient is the intercept. */
66       coeff = c->coeff + j;
67       coeff->n_vars = n_vals; /* Currently, no procedures allow interactions.
68                                  This will have to change when procedures that
69                                  allow interaction terms are written.
70                               */
71       coeff->v_info = xnmalloc (coeff->n_vars, sizeof (*coeff->v_info));
72       assert (coeff->v_info != NULL);
73       coeff->v_info->v = (const struct variable *) design_matrix_col_to_var (X, i);
74       
75       if (coeff->v_info->v->type == ALPHA)
76         {
77           size_t k;
78           k = design_matrix_var_to_column (X, coeff->v_info->v);
79           assert (k <= i);
80           k = i - k;
81           coeff->v_info->val = cat_subscript_to_value (k, (struct variable *) coeff->v_info->v);
82         }
83     }
84 }
85 void
86 pspp_linreg_coeff_set_estimate (struct pspp_linreg_coeff *c,
87                                 double estimate)
88 {
89   c->estimate = estimate;
90 }
91 void
92 pspp_linreg_coeff_set_std_err (struct pspp_linreg_coeff *c,
93                                double std_err)
94 {
95   c->std_err = std_err;
96 }
97 /*
98   How many variables are associated with this coefficient?
99  */
100 int
101 pspp_linreg_coeff_get_n_vars (struct pspp_linreg_coeff *c)
102 {
103   return c->n_vars;
104 }                             
105 /*
106   Which variable does this coefficient match?
107  */
108 const struct variable *
109 pspp_linreg_coeff_get_var (struct pspp_linreg_coeff *c, int i)
110 {
111   assert (i < c->n_vars);
112   return (c->v_info + i)->v;
113 }
114 /* 
115    Which value is associated with this coefficient/variable comination? 
116 */
117 const union value *
118 pspp_linreg_coeff_get_value (struct pspp_linreg_coeff *c,
119                              const struct variable *v)
120 {
121   int i = 0;
122   const struct variable *candidate;
123
124   while (i < c->n_vars)
125     {
126       candidate = pspp_linreg_coeff_get_var (c, i);
127       if (v->index == candidate->index)
128         {
129           return (c->v_info + i)->val;
130         }
131       i++;
132     }
133   return NULL;
134 }