/* PSPP - a program for statistical analysis.
- Copyright (C) 2005, 2010 Free Software Foundation, Inc.
+ Copyright (C) 2005, 2010, 2011 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
+
+#include "math/linreg.h"
+
#include <gsl/gsl_blas.h>
#include <gsl/gsl_cblas.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_fit.h>
#include <gsl/gsl_linalg.h>
#include <gsl/gsl_multifit.h>
-#include <linreg/sweep.h>
-#include <math/linreg.h>
-#include <src/data/variable.h>
-#include <src/data/value.h>
-#include <gl/xalloc.h>
+
+#include "data/value.h"
+#include "data/variable.h"
+#include "linreg/sweep.h"
+
+#include "gl/xalloc.h"
/*
Find the least-squares estimate of b for the linear model:
c->indep_means = gsl_vector_alloc (p);
c->indep_std = gsl_vector_alloc (p);
- c->ss_indeps = gsl_vector_alloc (p); /* Sums of squares for the
- model parameters.
- */
c->n_obs = n;
c->n_indeps = p;
c->n_coeffs = p;
c->dfe = c->dft - c->dfm;
c->intercept = 0.0;
c->depvar_mean = 0.0;
- c->depvar_std = 0.0;
/*
Default settings.
*/
c->method = LINREG_SWEEP;
- c->pred = NULL;
- c->resid = NULL;
+
+ c->refcnt = 1;
return c;
}
-bool
-linreg_free (void *m)
+
+void
+linreg_ref (linreg *c)
{
- linreg *c = m;
- if (c != NULL)
+ c->refcnt++;
+}
+
+void
+linreg_unref (linreg *c)
+{
+ if (--c->refcnt == 0)
{
gsl_vector_free (c->indep_means);
gsl_vector_free (c->indep_std);
free (c->coeff);
free (c);
}
- return true;
}
static void
gsl_matrix_view xtx;
gsl_matrix_view xmxtx;
double m;
- double tmp;
size_t i;
size_t j;
int rc;
m = l->depvar_mean;
for (i = 0; i < l->n_indeps; i++)
{
- tmp = gsl_matrix_get (sw, i, l->n_indeps);
+ double tmp = gsl_matrix_get (sw, i, l->n_indeps);
l->coeff[i] = tmp;
m -= tmp * linreg_get_indep_variable_mean (l, i);
}
for (i = 0; i < l->n_indeps; i++)
for (j = i; j < l->n_indeps; j++)
{
- tmp = -1.0 * l->mse * gsl_matrix_get (sw, i, j);
+ double tmp = -1.0 * l->mse * gsl_matrix_get (sw, i, j);
gsl_matrix_set (l->cov, i + 1, j + 1, tmp);
}
/*
gsl_matrix_free (xm);
if (rc == GSL_SUCCESS)
{
- tmp = l->mse / l->n_obs;
+ double tmp = l->mse / l->n_obs;
for (i = 1; i < 1 + l->n_indeps; i++)
{
tmp -= gsl_matrix_get (l->cov, 0, i)
return (obs - linreg_predict (c, vals, n_vals));
}
-double linreg_get_indep_variable_sd (linreg *c, size_t j)
-{
- assert (c != NULL);
- return gsl_vector_get (c->indep_std, j);
-}
-
-void linreg_set_indep_variable_sd (linreg *c, size_t j, double s)
-{
- assert (c != NULL);
- gsl_vector_set (c->indep_std, j, s);
-}
-
/*
Mean of the independent variable.
*/
-double linreg_get_indep_variable_mean (linreg *c, size_t j)
+double linreg_get_indep_variable_mean (const linreg *c, size_t j)
{
assert (c != NULL);
return gsl_vector_get (c->indep_means, j);
gsl_vector *xty;
gsl_vector *tau;
gsl_vector *params;
- double tmp = 0.0;
size_t i;
size_t j;
}
}
l->intercept = linreg_get_depvar_mean (l);
- tmp = 0.0;
for (i = 0; i < l->n_indeps; i++)
{
- tmp = linreg_get_indep_variable_mean (l, i);
+ double tmp = linreg_get_indep_variable_mean (l, i);
l->intercept -= l->coeff[i] * tmp;
intercept_variance += tmp * tmp * gsl_matrix_get (q, i, i);
}
return c->intercept;
}
-gsl_matrix *
+const gsl_matrix *
linreg_cov (const linreg *c)
{
return c->cov;
}
double
-linreg_get_depvar_mean (linreg *c)
+linreg_get_depvar_mean (const linreg *c)
{
return c->depvar_mean;
}