+
+ model_cov = gsl_matrix_alloc (cm->size1 - n_dropped_model, cm->size2 - n_dropped_model);
+ submodel_cov = gsl_matrix_alloc (cm->size1 - n_dropped_submodel, cm->size2 - n_dropped_submodel);
+
+ fill_submatrix (cm, model_cov, model_dropped);
+ fill_submatrix (cm, submodel_cov, submodel_dropped);
+
+ reg_sweep (model_cov, 0);
+ reg_sweep (submodel_cov, 0);
+
+ gsl_vector_set (ssq, k + 1,
+ gsl_matrix_get (submodel_cov, 0, 0) - gsl_matrix_get (model_cov, 0, 0)
+ );
+
+ gsl_matrix_free (model_cov);
+ gsl_matrix_free (submodel_cov);
+ }
+
+ free (model_dropped);
+ free (submodel_dropped);
+ gsl_matrix_free (cm);
+}
+
+/*
+ Type 3 sums of squares.
+ Populate SSQ with the Type 2 sums of squares according to COV
+ */
+static void
+ssq_type3 (struct covariance *cov, gsl_vector *ssq, const struct glm_spec *cmd)
+{
+ gsl_matrix *cm = covariance_calculate_unnormalized (cov);
+ size_t i;
+ size_t k;
+ bool *model_dropped = xcalloc (covariance_dim (cov), sizeof (*model_dropped));
+ bool *submodel_dropped = xcalloc (covariance_dim (cov), sizeof (*submodel_dropped));
+ const struct categoricals *cats = covariance_get_categoricals (cov);
+
+ double ss0;
+ gsl_matrix *submodel_cov = gsl_matrix_alloc (cm->size1, cm->size2);
+ fill_submatrix (cm, submodel_cov, submodel_dropped);
+ reg_sweep (submodel_cov, 0);
+ ss0 = gsl_matrix_get (submodel_cov, 0, 0);
+ gsl_matrix_free (submodel_cov);
+ free (submodel_dropped);
+
+ for (k = 0; k < cmd->n_interactions; k++)
+ {
+ gsl_matrix *model_cov = NULL;
+ size_t n_dropped_model = 0;
+
+ for (i = cmd->n_dep_vars; i < covariance_dim (cov); i++)
+ {
+ const struct interaction * x =
+ categoricals_get_interaction_by_subscript (cats, i - cmd->n_dep_vars);
+
+ model_dropped[i] = false;
+
+ if ( cmd->interactions [k] == x)
+ {
+ assert (n_dropped_model < covariance_dim (cov));
+ n_dropped_model++;
+ model_dropped[i] = true;
+ }
+ }
+
+ model_cov = gsl_matrix_alloc (cm->size1 - n_dropped_model, cm->size2 - n_dropped_model);
+
+ fill_submatrix (cm, model_cov, model_dropped);
+
+ reg_sweep (model_cov, 0);
+