ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  Plynomial Regression
Posted by Pandey on Dec-06-2013 19:55
Hi have a strange problem.
While using trend coefficients for polynomial regression of order 3 I get different values for x^3 data when I plot in Excel and when I do it with chart director. Pl see below

Just copy pasted the data from Excel

X vals=1.528,1.5053,1.4603, 1.4195, 1.3834, 1.3482, 1.3111

Y vals=3735.5041, 3594.2877, 3237.6288, 2826.1075, 2392.0866, 1905.6873, 1325.4891

y = -0.01339x3 - 25,181.17137x2 + 82,603.12494x - 63,689.42704(from excel)
y=    0x3-25181.22x2 +82603.196x -63689.45(From chart director)

Actual Code:
////////////////////////////
trend0 = c->addTrendLayer2(dataX0, dataY0, mapColorTypes[nCorrectType]);
trend0->setLineWidth(CORRN_1);
int m_nOrderType =3;

trend0->setRegressionType(Chart::PolynomialRegression(m_nOrderType));
trend0->setHTMLImageMap("{disable}");

listOfFormCoeff = gcnew List<double>();

FORM_COEFFICIENT ^coeff;
coeff = gcnew  FORM_COEFFICIENT;
for(int iTemp=0;iTemp<=m_nOrderType;iTemp++) {
String ^strTemp = " ";
bool b = trend0->getCoefficient(iTemp).ToString()->Contains("+");
if(b)
listOfFormCoeff->Add(1122);
else {
listOfFormCoeff->Add(trend0->getCoefficient(iTemp));
}
}

Thanks
Pandey

  Re: Plynomial Regression
Posted by Peter Kwan on Dec-07-2013 00:30
Attachments:
Hi Pandey,

I suspect it is because you use different data in Excel and in ChartDirector.

In a computer, a number is in binary format, usually 64-bit IEEE 754 format. To represent this format in decimal, 17 to 19 significant digits are required. Most programming languages or system by default will only print a number with a low precision (such as using a few decimal places only). So the numbers you see are only an approximation of the actual number in the computer. If you printed out the numbers from ChartDirector, and copy and paste them to Excel, the results can be different, because the printed numbers are only an approximation, and so the Excel and ChartDirector are using different data.

I have just tried your data in ChartDirector, and I get the same results as in Excel. See the attached chart.

Hope this can help.

Regards
Peter Kwan
confidenceband.png

  Re: Plynomial Regression
Posted by Pandey on Dec-08-2013 03:04
Hi Peter,
Actually the data is the same but, getCoefficient(3) and getCoefficient(4) are not getting updated and are getting a constant value.
///////////////////////////////
TrendLayer^ trend0;
ScatterLayer^ scatLayer;
nColor++;
scatLayer =  c->addScatterLayer(dataX0, dataY0, strYParamName , Chart::SquareSymbol, CORRN_8,  mapColorTypes[nColor]);
c->setClipping();
scatLayer->setHTMLImageMap("{disable}");

trend0 = c->addTrendLayer2(dataX0, dataY0, mapColorTypes[nCorrectType]);
trend0->setLineWidth(CORRN_1);
trend0->setRegressionType(Chart::PolynomialRegression(4));

double h=trend0->getCoefficient(0);//gets the correct value
double h1=trend0->getCoefficient(1);//gets the correct value
double h2=trend0->getCoefficient(2);//gets the correct value
double h3=trend0->getCoefficient(3);//gets a junk value
double h4=trend0->getCoefficient(4);//gets a junk value

////////////////////////////////
Not sure whats happening!!!

Thanks
Pandey

  Re: Plynomial Regression
Posted by Peter Kwan on Dec-10-2013 01:49
Hi Pandey,

In your latest code, you are trying to do a degree 4 polynomial (not a degree 3
polynomial as mentioned in your first message). I have used the following test code
(which prints the coefficients to the debugger output), and the result I get for the 5
coefficients are:

-63691.1651846038
82608.0411605042
-25186.3804482109
2.43715875015613
-0.431870357450268

I have double checked them by computing the y values from the x values using the
above polynomial manually, and I found that they are very accurate. So I believe all the
coefficients are accurate.

If the above still does not solve the problem, is it possible to create a complete test code
(like the one below, with hard coded data), and inform me the are coefficients you get?


array<double> ^dataX = {1.528,1.5053,1.4603, 1.4195, 1.3834, 1.3482, 1.3111};
array<double> ^dataY = {3735.5041, 3594.2877, 3237.6288, 2826.1075, 2392.0866,
1905.6873, 1325.4891};

XYChart ^c = gcnew XYChart(500, 500);
c->setPlotArea(50, 50, 400, 400);
c->addScatterLayer(dataX, dataY);
TrendLayer ^layer = c->addTrendLayer2(dataX, dataY);
layer->setRegressionType(Chart::PolynomialRegression(4));
c->makeChart("c:\\\\test.png");

for (int i = 0; i <= 4; ++i)
System::Diagnostics::Debug::WriteLine("" + layer->getCoefficient(i));


Regards
Peter Kwan