help-gsl
[Top][All Lists]
Advanced

[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:

https://en.wikipedia.org/wiki/Coefficient_of_determination

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.

Patrick

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

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):

https://www.gnu.org/software/gsl/manual/html_node/Mean-and-standard-deviation-and-variance.html#Mean-and-standard-deviation-and-variance

Best,
Tim

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

Send Help-gsl mailing list submissions to
         address@hidden

To subscribe or unsubscribe via the World Wide Web, visit
         https://lists.gnu.org/mailman/listinfo/help-gsl
or, via email, send a message with subject or body 'help' to
         address@hidden

You can reach the person managing the list at
         address@hidden

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


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 ?
Thanks for your help in advance.
JM



------------------------------

_______________________________________________
Help-gsl mailing list
address@hidden
https://lists.gnu.org/mailman/listinfo/help-gsl


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





reply via email to

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