+2006-04-07 Jason Stover <jhs@math.gcsu.edu>
+
+ * predict.c (pspp_linreg_predict): Improved handling of null
+ pointer args.
+
+ * predict.c (pspp_linreg_residuals): New function.
+
+ * coefficient.c: Improved handling of null pointer args.
+
2006-04-05 Jason Stover <jhs@math.gcsu.edu>
* predict.c: New file. pspp_linreg_predict() uses a linear model
double
pspp_linreg_coeff_get_est (const struct pspp_linreg_coeff *c)
{
- assert (c != NULL);
+ if (c == NULL)
+ {
+ return 0.0;
+ }
return c->estimate;
}
/*
double
pspp_linreg_coeff_get_std_err (const struct pspp_linreg_coeff *c)
{
- assert (c != NULL);
+ if (c == NULL)
+ {
+ return 0.0;
+ }
return c->std_err;
}
/*
int
pspp_linreg_coeff_get_n_vars (struct pspp_linreg_coeff *c)
{
+ if (c == NULL)
+ {
+ return 0;
+ }
return c->n_vars;
}
const struct variable *
pspp_linreg_coeff_get_var (struct pspp_linreg_coeff *c, int i)
{
+ if (c == NULL)
+ {
+ return NULL;
+ }
assert (i < c->n_vars);
return (c->v_info + i)->v;
}
int i = 0;
const struct variable *candidate;
+ if (c == NULL || v == NULL)
+ {
+ return NULL;
+ }
while (i < c->n_vars)
{
candidate = pspp_linreg_coeff_get_var (c, i);
struct pspp_linreg_coeff *result;
const struct variable *tmp;
- assert (c != NULL);
- assert (c->coeff != NULL);
- assert (c->n_indeps > 0);
- assert (v != NULL);
- assert (val != NULL);
+ if (c == NULL || c->coeff == NULL || c->n_indeps == NULL || v == NULL)
+ {
+ return NULL;
+ }
result = c->coeff + i;
tmp = pspp_linreg_coeff_get_var (result, 0);
{
return result;
}
- else
+ else if (val != NULL)
{
/*
If v is categorical, we need to ensure the coefficient
}
return result;
}
+ return NULL;
}
#ifndef LINREG_H
#define LINREG_H
-
+#include <gsl/gsl_math.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_matrix.h>
double
pspp_linreg_predict (const struct variable *, const union value *,
const pspp_linreg_cache *, int);
+double
+pspp_linreg_residual (const struct variable *, const union value *,
+ const union value *, const pspp_linreg_cache *, int);
#endif
double result;
double tmp;
- assert (predictors != NULL);
- assert (vals != NULL);
- assert (c != NULL);
-
+ if (predictors == NULL || vals == NULL || c == NULL)
+ {
+ return GSL_NAN;
+ }
+ if (c->coeff == NULL)
+ {
+ /* The stupid model: just guess the mean. */
+ return c->depvar_mean;
+ }
result = c->coeff->estimate; /* Intercept. */
/*
}
return result;
}
+
+double
+pspp_linreg_residual (const struct variable *predictors,
+ const union value *vals,
+ const union value *obs,
+ const pspp_linreg_cache *c,
+ int n_vals)
+{
+ double pred;
+ double result;
+
+ if (predictors == NULL || vals == NULL || c == NULL || obs == NULL)
+ {
+ return GSL_NAN;
+ }
+
+ pred = pspp_linreg_predict (predictors, vals, c, n_vals);
+
+ result = gsl_isnan (pred) ? GSL_NAN : (obs->f - pred);
+ return result;
+}