The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 Forum for Origin C
 sequential linear fit of all rows in the column
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

michaeli98

USA
Posts

Posted - 07/19/2012 :  4:51:10 PM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Ver. and Service Release (Select Help-->About Origin): OriginPro 8.6.0 SR3 b99
Operating System: XP SP3

Hi,

I am trying to sequentially fit two columns to a straight line (first 5 rows, 6, 7, 8, 9, and so on until the end of the column). I am trying to use a for-loop to do this, but seems like the index does not change. any suggestions are greatly appreciated. Here is the code that I have so far.

void linearfit()
{

// Get XY data from worksheet window
Worksheet wks = Project.ActiveLayer();
if(!wks)
return; // need to activate a worksheet with data
WorksheetPage wp = wks.GetPage();

DataRange dr;

int nRows = wks.GetNumRows();

printf("Number of rows is %d\n", nRows);

for(int ii=5; ii<nRows; ii++)
{
dr.Add("X", wks, 1, 0, ii, 0); // x column
dr.Add("Y", wks, 1, 1, ii, 1); // y column

vector vX;
dr.GetData(&vX, 0); // get data of x column to vector

vector vY;
dr.GetData(&vY, 1); // get data of y column to vector

// Do linear fit with the above input dataset and fit option settings
int nSize = vX.GetSize(); // data size
printf("Data size is %d\n", nSize);

FitParameter psFitParameter[2]; // two parameters
RegStats stRegStats; // regression statistics
RegANOVA stRegANOVA; // anova statistics

int nRet = ocmath_linear_fit(vX, vY, nSize, psFitParameter, NULL,
0, NULL, &stRegStats, &stRegANOVA);
if(nRet != STATS_NO_ERROR)
{
out_str("Error");
return;
}
// Output analysis result to Script window, Result Log and Worksheet
// print the values of fitting parameters to the Script Window
vector<string> vsParams = {"Intercept", "Slope"};
for(int iPara = 0; iPara < vsParams.GetSize(); iPara++)
{
printf("%s = %g\n", vsParams[iPara], psFitParameter[iPara].Value);
}
}
}

Thanks,
Michael

Penn

China
644 Posts

Posted - 07/19/2012 :  10:19:27 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi,

The problem is when you declare the DataRange in your code. dr is declared out the "for" loop, then for each loop, dr.Add will add pair of XY into this DataRange, and the original one is still there. Then dr.GetData will always get the first pair of XY. You can set a break point to see it.

Please move the line DataRange dr; into the "for" loop, so to make it as local variable, such as

for(int ii=5; ii<nRows; ii++)
{
    DataRange dr;
    dr.Add(...

By the way, the index is based of zero, in your code line dr.Add("X", wks, 1, 0, ii, 0);, it will start from the second row to the (ii+1)th row. According to your description, you wanted to fit frist 5 rows, 6, 7, 8, etc., so maybe you need to change your code as follows, please make sure this point.

for(int ii=4; ii<nRows; ii++)  // ii start from 4 for the first 5 rows
{
    DataRange dr;  // declare inside the loop as local variable
    dr.Add("X", wks, 0, 0, ii, 0);  // x column, from first row to (ii+1)th row
    dr.Add("Y", wks, 0, 1, ii, 1);  // y column
    ...


Penn
Go to Top of Page

michaeli98

USA
Posts

Posted - 07/20/2012 :  12:14:41 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Penn,

Thank you so much, moving it inside of the for-loop worked like a charm.
Could you please possibly point me to an example of reporting the results? I wanted to report the slope and intercept as new columns in a separate worksheet. Is it possible to copy the x values from the first worksheet, then add the values for the slope and intercept in the new rows as the results are calculated (x(ii), slope, intercept)?

Thanks again for all your suggestions.

Best regards,
Michael
Go to Top of Page

Penn

China
644 Posts

Posted - 07/23/2012 :  04:51:31 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Michael,

Maybe you can refer to this page.

Penn
Go to Top of Page

michaeli98

USA
Posts

Posted - 04/03/2013 :  7:45:13 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi All,

I am back onto this project. I have the script that sequentially fits the data in the active worksheet to a straight line (5 data points, 6 data points, etc.) and reports the slope, intercept, and rmse for each fit into the script window.
Is there an easy way to report the results into a separate worksheet?

Here is a script that I have so far.
void linearfit()
{

// Get XY data from worksheet window
Worksheet wks = Project.ActiveLayer();
if(!wks)
return; // need to activate a worksheet with data
WorksheetPage wp = wks.GetPage();

Please point me in the right direction.
int nRows = wks.GetNumRows();

printf("Number of rows is %d\n", nRows);

{
for(int ii=4; ii<nRows; ii++) // ii start from 4 for the first 5 rows
{
DataRange dr; // declare inside the loop as local variable
dr.Add("X", wks, 0, 0, ii, 0); // x column, from first row to (ii+1)th row
dr.Add("Y", wks, 0, 1, ii, 1); // y column

vector vX;
dr.GetData(&vX, 0); // get data of x column to vector

vector vY;
dr.GetData(&vY, 1); // get data of y column to vector

// Do linear fit with the above input dataset and fit option settings
int nSize = vX.GetSize(); // data size
printf("Data size is %d\n", nSize);

FitParameter psFitParameter[2]; // two parameters
RegStats stRegStats; // regression statistics
RegANOVA stRegANOVA; // anova statistics

int nRet = ocmath_linear_fit(vX, vY, nSize, psFitParameter, NULL, 0, NULL, &stRegStats, &stRegANOVA);
if(nRet != STATS_NO_ERROR)
{
out_str("Error");
return;
}
// Output analysis result to Script window, Result Log and Worksheet
// print the values of fitting parameters to the Script Window
vector<string> vsParams = {"Intercept", "Slope"};
for(int iPara = 0; iPara < vsParams.GetSize(); iPara++)
{
printf("%s = %g\n", vsParams[iPara], psFitParameter[iPara].Value);
}
printf("RMSE : %lf\n", stRegStats.RMSESD);

}
}
}
Go to Top of Page

Penn

China
644 Posts

Posted - 04/05/2013 :  4:25:52 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Michael,

The link I provided includes the codes on how to get and set values to worksheet. Also, you can refer to this page on how to put the result to worksheet with normal column view or tree format view. To make it simple, the SetCell method can be your option.

Penn
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000