- /*
- Get the covariances related to the intercept.
- */
- xtx = gsl_matrix_submatrix (sw, 0, 0, cache->n_indeps, cache->n_indeps);
- xmxtx = gsl_matrix_submatrix (cache->cov, 0, 1, 1, cache->n_indeps);
- xm = gsl_matrix_calloc (1, cache->n_indeps);
- for (i = 0; i < xm->size2; i++)
- {
- gsl_matrix_set (xm, 0, i,
- pspp_linreg_get_indep_variable_mean (cache, design_matrix_col_to_var (dm, i)));
- }
- rc = gsl_blas_dsymm (CblasRight, CblasUpper, cache->mse,
- &xtx.matrix, xm, 0.0, &xmxtx.matrix);
- gsl_matrix_free (xm);
- if (rc == GSL_SUCCESS)
- {
- tmp = cache->mse / cache->n_obs;
- for (i = 1; i < 1 + cache->n_indeps; i++)
- {
- tmp -= gsl_matrix_get (cache->cov, 0, i)
- * pspp_linreg_get_indep_variable_mean (cache, design_matrix_col_to_var (dm, i - 1));
- }
- gsl_matrix_set (cache->cov, 0, 0, tmp);
-
- cache->intercept = m;
- }
- else
- {
- fprintf (stderr, "%s:%d:gsl_blas_dsymm: %s\n",
- __FILE__, __LINE__, gsl_strerror (rc));
- exit (rc);
- }
-}
-
-/*
- Fit the linear model via least squares. All pointers passed to pspp_linreg
- are assumed to be allocated to the correct size and initialized to the
- values as indicated by opts.
- */
-int
-pspp_linreg (const gsl_vector * Y, const struct design_matrix *dm,
- const pspp_linreg_opts * opts, pspp_linreg_cache * cache)
-{
- int rc;
- gsl_matrix *design = NULL;
- gsl_matrix_view xtx;
- gsl_vector_view xty;
- gsl_vector_view xi;
- gsl_vector_view xj;
- gsl_vector *param_estimates;
- struct pspp_coeff *coef;
- const struct variable *v;
- const union value *val;
-
- size_t i;
- size_t j;
- double tmp;
- double m;
- double s;
- double ss;
-
- if (cache == NULL)
- {
- return GSL_EFAULT;
- }
- if (opts->get_depvar_mean_std)
- {
- linreg_mean_std (gsl_vector_const_subvector (Y, 0, Y->size),
- &m, &s, &ss);
- cache->depvar_mean = m;
- cache->depvar_std = s;
- cache->sst = ss;
- }
- cache_init (cache);
- cache->n_coeffs = dm->m->size2;
- for (i = 0; i < dm->m->size2; i++)
- {
- if (opts->get_indep_mean_std[i])
- {
- linreg_mean_std (gsl_matrix_const_column (dm->m, i), &m, &s, &ss);
- v = design_matrix_col_to_var (dm, i);
- val = NULL;
- if (var_is_alpha (v))
- {
- j = i - design_matrix_var_to_column (dm, v);
- val = cat_subscript_to_value (j, v);
- }
- coef = pspp_linreg_get_coeff (cache, v, val);
- pspp_coeff_set_mean (coef, m);
- pspp_coeff_set_sd (coef, s);
- gsl_vector_set (cache->ssx, i, ss);