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
 Standard deviation and mean
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

peter.cook

UK
356 Posts

Posted - 02/24/2004 :  08:32:08 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Hi,

I would like to scroll through some (sorted by x) X-Y data and pull out the standard deviation and mean of replicates eg

X Y
1 2
1 3
1 5
2 7
2 8
3 9

The number of replicates may vary so I'm after a generic code.

OK, I can scan through the x dataset to indicate where a new group of data starts and finishes but where I'm stuck is simply at the point of copying the data (by group x) to a dataset or vector (I don't know which is best) for calculation of sd and mean (I don't need other stats).

Any advice for simple quick code would be appreciated,

Cheers,

pete


cpyang

USA
1406 Posts

Posted - 02/24/2004 :  10:30:21 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Origin has built-in functions for these

ExtractOneGroup

Let me put together an example.

CP


Go to Top of Page

hajo_old

Germany
141 Posts

Posted - 02/24/2004 :  11:09:59 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi, Peter

in OC you may try out something like the following code

...

test_peter()
{
Dataset xx(Project.WorksheetPages(0).GetName(),0); // read in column 1 as Dataset
Dataset yy(Project.WorksheetPages(0).GetName(),1); // read in column 1 as Dataset
//Dataset cannot be used as a parameter, but vector can.
vector x, y;
int index = 0;
while(index<=xx.GetSize())
{
if(index == 0)
{
x.Add(xx[index]);
y.Add(yy[index]);
index++;
}
else
{
if(index != xx.GetSize())
{
while(xx[index] == xx[index-1])
{
x.Add(xx[index]);
y.Add(yy[index]);
index++;
}
}
int dsSize = x.GetSize();
// definition of some variables
double xsd,xskew, xkurt, wsum, xmean, xmax, xmin;
double *wt;
int sucess, weight, nvalid = dsSize;
wt = NULL;

if(y.GetSize() != 1)
{
sucess = nag_summary_stats_1var(nvalid, y, wt, &nvalid,
&xmean, &xsd, &xskew,
&xkurt, &xmin, &xmax, &wsum);
printf("x-value: %i -> means: %0.2f\tsd: %0.2f\n",(int)x[index-1], xmean, xsd);
}
else
printf("x-value: %i -> means: %0.2f\tsd: %0.2f\n",(int)x[index-1], y[0], 0.);
y.RemoveAll();

if(index != xx.GetSize())
{
x.Add(xx[index]);
y.Add(yy[index]);
}
index++;
}
}
}


Looks crude .. but works (hopefully for you also ...)

At the points where in my function the prinf is called, you should insert your code to store the results ...

hope that helps ...

Hajo

-- --
Dipl.-Ing. Hans-Joerg Koch
Siemens VDO, Regensburg

SVDO_Origin1 is now hajo
Go to Top of Page

hajo_old

Germany
141 Posts

Posted - 02/24/2004 :  11:14:22 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hello, CP

I searched OriginC help for 'ExtractOneGroup' and failed! (OC-Pro 7.5SR1)
Where is it?

Thanks
Hajo



-- --
Dipl.-Ing. Hans-Joerg Koch
Siemens VDO, Regensburg

SVDO_Origin1 is now hajo
Go to Top of Page

cpyang

USA
1406 Posts

Posted - 02/24/2004 :  2:14:30 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
ExtractOneGroup is inside wksheet.h and not included in documentation as it was one of those developing functions that are not fully tested.

Here is an example, you can try a worksheet with a group column and a data column. Group column can have values as 1,2,3 etc to group data in the data column.




void test_one_group(int nGroupVal = 1, int nGroupCol = 0, int nDataCol = 1)
{
Worksheet wks = Project.ActiveLayer();
Dataset dGroup(wks, nGroupCol);
vector<ushort> vRowMap;
vRowMap = dGroup;// assume group col already contain group numeric values like 1,2,3 etc

vector vResult;
if(wks.ExtractOneGroup(vResult, nDataCol, vRowMap, 0, 0, vRowMap.GetSize()-1, nGroupVal))
{
// save result into new wks to view
Worksheet wResult("Test");
if(wResult == NULL)
{
wResult.Create();
wResult.GetPage().Rename("Test");
wResult.SetSize(-1, 0);
}
int nCol = wResult.AddCol();
Dataset dOut(wResult, nCol);
dOut = vResult;
}
}





CP


Edited by - cpyang on 02/24/2004 2:34:55 PM
Go to Top of Page

Mike Buess

USA
3037 Posts

Posted - 02/24/2004 :  2:29:33 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi CP,

Pete's data are already sorted, but I wonder if ExtractOneGroup also works on unsorted data? I.e., could the group column look like this

3 2 1 2 2 1 3 5 2 3 3 ...

Mike Buess
Origin WebRing Member
Go to Top of Page

cpyang

USA
1406 Posts

Posted - 02/24/2004 :  2:36:01 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Sorting is not needed, you can try the code above with any kind of data.

CP


Go to Top of Page

peter.cook

UK
356 Posts

Posted - 03/01/2004 :  05:01:51 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi all,

Thanks for assist and informative code!

Hajo..v helpful especially as I'm not fully versed with C!
I think (!!) I needed to modify code to take into account where
while(xx[index] == xx[index-1])
fails if dataset not large enough, resized output datasets eg :

void GenMeanSexyTwo(Dataset &xdataset, Dataset &ydataset, Dataset &meanxdataset, Dataset &meandataset, Dataset &sddataset)
{

// assume x y sorted by x

int NumData=xdataset.GetSize();
meandataset.SetSize(NumData);
meanxdataset.SetSize(NumData);
sddataset.SetSize(NumData);

vector y;
int NumXValue=0;
int NumValue=1;
int EstSD=0;
y.Add(ydataset[0]);

for(NumValue=2; NumValue<=NumData; NumValue++)
{
EstSD=1;
if(xdataset[NumValue-1] == xdataset[NumValue-2])
{
y.Add(ydataset[NumValue-1]);
EstSD=0;
if(NumValue==NumData) EstSD=1;
};
if(EstSD==1)
{ // Estimate SD and mean

int dsSize = y.GetSize();
NumXValue++; // Number of 'X groups'
// definition of some variables
double ysd,yskew, ykurt, wsum, ymean, ymax, ymin;
double *wt;
int sucess, weight, nvalid = dsSize;
wt = NULL;

if(y.GetSize() > 1)
{
sucess = nag_summary_stats_1var(nvalid, y, wt, &nvalid, &ymean, &ysd, &yskew, &ykurt, &ymin, &ymax, &wsum);

meandataset[NumXValue-1]=ymean;
meanxdataset[NumXValue-1]=xdataset[NumValue-2];
sddataset[NumXValue-1]=ysd;

}
if(y.GetSize()==1) {
meandataset[NumXValue-1]=ydataset[NumValue-2];
meanxdataset[NumXValue-1]=xdataset[NumValue-2];
sddataset[NumXValue-1]=0;
}

y.RemoveAll();
y.Add(ydataset[NumValue-1]);

}
}



meandataset.SetSize(NumXValue);
meanxdataset.SetSize(NumXValue);
sddataset.SetSize(NumXValue);
}

Maybe this could be neater still but thanks for help..

(Apologies for changing (use of) index variable...personal preference : I find it easier to keep track)

sucess = nag_summary_stats_1var(nvalid, y, wt, &nvalid, &ymean, &ysd)
also seemed to work fine but no speed gain.

Still only a modest 5 fold gain in speed over my LabTalk code. I guess I find if odd (requiring) using a nag function just to get sd and mean!

CP : Thanks for useful code. Could also adapt for double. I think this approach wouldn't be too applicable as I don't know the x values in advance. Happy to sort prior to running above code. I do worry though with

quote:
ExtractOneGroup is inside wksheet.h and not included in documentation as it was one of those developing functions that are not fully tested.

How are we supposed to deal with undocumented (and possibly untested) features?

Cheers,

Pete

Go to Top of Page

cpyang

USA
1406 Posts

Posted - 03/01/2004 :  10:35:41 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
quote:
How are we supposed to deal with undocumented (and possibly untested) features?



Those are usually new features that were included into OC header as we don't want to have separate header file for new verisons. ExtractOneGroup was originally developed for Origin8.

CP


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