help-gsl
[Top][All Lists]

## [Help-gsl] how to use gsl_stats_tss after gsl_multifit_linear to get R^2

 From: jean-michel richer Subject: [Help-gsl] how to use gsl_stats_tss after gsl_multifit_linear to get R^2 Date: Thu, 21 Jan 2016 17:13:16 +0100 User-agent: Roundcube Webmail/0.7.1


Hi,
I am using "gsl_multifit_linear" to perform multiple linear
regression and it works fine, but I would like to get the
R^2 value. In the documentation it is said that it can be
obtained from :

"R^2 = 1 - \chi^2 / TSS, where the total sum of squares
(TSS) of the observations may be computed from gsl_stats_tss"

However I don't know how to use "gsl_stats_tss", I didn't find
some example on internet !
This function has the following prototype :

double gsl_stats_tss (const double data[], size_t stride, size_t n)

but what is data, stride and n ?

The source code I am using is as follows:
===================================================

int N = v.size();
int P = 10;

gsl_vector *y; // observed data
gsl_matrix *X; // data used to predict : cste + ...
gsl_vector *c; // the coefficients c0, c1, ...
gsl_matrix *cov;

// allocate space for the matrices and vectors
X = gsl_matrix_alloc(N, P); // this is an input
y = gsl_vector_alloc(N); //this is an input

c = gsl_vector_alloc(P); //this is an output
cov = gsl_matrix_alloc(P, P); //this is an output

// fill the matrices X and y
u32 i = 0;
for (auto z : v) {
gsl_matrix_set(X, i, 0, static_cast<f64>(1)); // because cste
gsl_matrix_set(X, i, 1, static_cast<f64>(z->get(Instance::K)));
gsl_matrix_set(X, i, 2, static_cast<f64>(z->get(Instance::L)));
gsl_matrix_set(X, i, 3, static_cast<f64>(z->get(Instance::R)));
gsl_matrix_set(X, i, 4, static_cast<f64>(z->get(Instance::N)));
gsl_matrix_set(X, i, 5, static_cast<f64>(z->get(Instance::G)));
gsl_matrix_set(X, i, 6, static_cast<f64>(z->get(Instance::S)));
gsl_matrix_set(X, i, 7, static_cast<f64>(z->get(Instance::RN)));
gsl_matrix_set(X, i, 8, static_cast<f64>(z->get(Instance::RG)));
gsl_matrix_set(X, i, 9, static_cast<f64>(z->get(Instance::RB)));
// expected value
gsl_vector_set(y, i, static_cast<f64>(z->get(Instance::B)));
++i;
}

// allocate temporary work space for gsl
gsl_multifit_linear_workspace *work;
work = gsl_multifit_linear_alloc(N, P);

// now do the fit
gsl_multifit_linear(X, y, c, cov, &chisq, work);

===================================================
So after this how can I compute R^2 ?