forum.alglib.net
http://forum.alglib.net/

Usage
http://forum.alglib.net/viewtopic.php?f=2&t=28
Page 1 of 1

Author:  iprins [ Fri Jun 18, 2010 12:04 pm ]
Post subject:  Usage

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

Author:  Sergey.Bochkanov [ Fri Jun 18, 2010 5:28 pm ]
Post subject:  Re: Usage

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

Author:  iprins [ Mon Jun 21, 2010 2:11 pm ]
Post subject:  Re: Usage

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;
}

Author:  iprins [ Mon Jun 21, 2010 3:14 pm ]
Post subject:  Re: Usage

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];
}

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/