Class LeastSqFit

Class LeastSqFit can be used to calculate linear regressions with or without weighting factors. Once constructed, a LeastSqFit can be interrogated for various properties of the fit. Here’s an example:

 

#include <rw/lsqfit.h>

#include <rw/math/mathvec.h>

#include <rw/rand.h>

#include <iostream>

int main()

{

// Two straight lines:

RWMathVec<double> x(50, 0, 1);

RWMathVec<double> y(50, 0, 1);

// Random number generator:

RWTRandUniform<RWRandGenerator> ru;

// Add some noise to y:

y += ru.randValue(50);

// Construct the fit, without using weighting factors:

LeastSqFit lsf(x, y);

// Now get various properties of the fit:

std::cout << "Correlation coefficient is " <<

lsf.correlationCoeff() <<

std::endl;

std::cout << "The slope of the fit is "<<

lsf.slope() << std::endl;

}