# forum.alglib.net

ALGLIB forum
 It is currently Fri May 24, 2024 3:52 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.

 Page 1 of 1 [ 4 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: UsagePosted: Fri Jun 18, 2010 12:04 pm

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

 Post subject: Re: UsagePosted: Fri Jun 18, 2010 5:28 pm

Joined: Fri May 07, 2010 7:06 am
Posts: 915
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

 Post subject: Re: UsagePosted: Mon Jun 21, 2010 2:11 pm

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

 Post subject: Re: UsagePosted: Mon Jun 21, 2010 3:14 pm

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

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 4 posts ]

 All times are UTC

#### Who is online

Users browsing this forum: No registered users and 1 guest

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

Search for: