[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-gsl] how to use gsl_stats_tss after gsl_multifit_linear to get

From: Patrick Alken
Subject: Re: [Help-gsl] how to use gsl_stats_tss after gsl_multifit_linear to get R^2
Date: Thu, 21 Jan 2016 14:30:09 -0700
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.5.0

This is a good answer. Just want to add you can look at the wiki page:

to see the formula for R^2 (which should help to better understand the comment in the manual).

Perhaps I should add this formula to the documentation of that chapter.


On 01/21/2016 10:50 AM, Timothée Flutre wrote:

I would do the following:

double r2 = 1 - chisq / gsl_stats_tss(y->data, y->stride, y->size);

The function is described in the manual, section 21 (Statistics) subsection
1 (Mean, Standard Deviation and Variance):


2016-01-21 18:00 GMT+01:00 <address@hidden>:

Send Help-gsl mailing list submissions to

To subscribe or unsubscribe via the World Wide Web, visit
or, via email, send a message with subject or body 'help' to

You can reach the person managing the list at

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Help-gsl digest..."

Today's Topics:

    1. how to use gsl_stats_tss after gsl_multifit_linear to get R^2
       (jean-michel richer)


Message: 1
Date: Thu, 21 Jan 2016 17:13:16 +0100
From: jean-michel richer <address@hidden>
To: <address@hidden>
Subject: [Help-gsl] how to use gsl_stats_tss after gsl_multifit_linear
         to get R^2
Message-ID: <address@hidden>
Content-Type: text/plain; charset=UTF-8; format=flowed

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
                 gsl_matrix_set(X, i, 1,
                 gsl_matrix_set(X, i, 2,
                 gsl_matrix_set(X, i, 3,
                 gsl_matrix_set(X, i, 4,
                 gsl_matrix_set(X, i, 5,
                 gsl_matrix_set(X, i, 6,
                 gsl_matrix_set(X, i, 7,
                 gsl_matrix_set(X, i, 8,
                 gsl_matrix_set(X, i, 9,
                 // expected value
                 gsl_vector_set(y, 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 ?
Thanks for your help in advance.


Help-gsl mailing list

End of Help-gsl Digest, Vol 146, Issue 1

reply via email to

[Prev in Thread] Current Thread [Next in Thread]