forum.alglib.net

ALGLIB forum
It is currently Sun Dec 22, 2024 9:39 am

All times are UTC


Forum rules


1. This forum can be used for discussion of both ALGLIB-related and general numerical analysis questions
2. This forum is English-only - postings in other languages will be removed.



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Usage
PostPosted: Fri Jun 18, 2010 12:04 pm 
Offline

Joined: Fri Jun 18, 2010 11:59 am
Posts: 3
Hi ,

I need some help using alglib. I want to do a linear regression, how do i set it up using alglib. I can't seem to find it in the examples.

Edit:
I have the following data set:
x y
1.0 2.6
2.3 2.8
3.1 3.1
4.8 4.7
5.6 5.1
6.3 5.3


Greets


Top
 Profile  
 
 Post subject: Re: Usage
PostPosted: Fri Jun 18, 2010 5:28 pm 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
Take a look at http://www.alglib.net/translator/man/ma ... fit_linear for a more general example. your x[] will be first column of fmatrix variable, as for y[i] - it will be y[] :)


Top
 Profile  
 
 Post subject: Re: Usage
PostPosted: Mon Jun 21, 2010 2:11 pm 
Offline

Joined: Fri Jun 18, 2010 11:59 am
Posts: 3
Thanks, i will look into this.

@edit:

Im missing something:
My output is as follows ( using y = mx + b) :
m: 0 b: 2.8

i expect something like this:
m : 0.5842
b : 1.6842

my code is :
#include <iostream>
#include <vector>

#include "lsfit.h"
#include "linreg.h"

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char **argv) {
std::vector<double> xrange;
std::vector<double> yrange;
/*
y = mx + b:

Data Set
x y
1.0 2.6
2.3 2.8
3.1 3.1
4.8 4.7
5.6 5.1
6.3 5.3

m (slope) : 0.5842
b (y - int) : 1.6842
r (corr.coeff) : 0.9741
*/
xrange.push_back(1.0);
xrange.push_back(2.3);
xrange.push_back(3.1);
xrange.push_back(4.8);
xrange.push_back(5.6);
xrange.push_back(6.3);

yrange.push_back(2.6);
yrange.push_back(2.8);
yrange.push_back(3.1);
yrange.push_back(4.7);
yrange.push_back(5.1);
yrange.push_back(5.3);

if( !xrange.empty() && !yrange.empty()){
int npoints = std::min(xrange.size(), yrange.size());//points
ap::real_2d_array xy;
xy.setlength(npoints, npoints);
// Fill up the matrix. First column is x, second column is y
for(int x = 0; x < npoints; x++){
xy(x, 0) = xrange[x];

for(int y = 0; y < npoints; y++) {
xy(x, y) = yrange[y];
}
}

int nvars = 1;
int info = 0;
linearmodel lm;
lrreport ar;
lrbuild(xy,
npoints,
nvars,
info,
lm,
ar);

ap::real_1d_array v;
lrunpack(lm, v, nvars);

if( nvars > 0 ){
std::cout << "m: " << v(0) << " b: " << v(1);
}
}

return 0;
}


Top
 Profile  
 
 Post subject: Re: Usage
PostPosted: Mon Jun 21, 2010 3:14 pm 
Offline

Joined: Fri Jun 18, 2010 11:59 am
Posts: 3
Oh wait, never mind. I filled the matrix wrong.

Should be :
for(int x = 0; x < npoints; x++){
xy(x, 0) = xrange[x];
xy(x, 1) = yrange[x];
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 33 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group