| Author |
Topic  |
|
|
cosy
Germany
Posts |
Posted - 10/25/2004 : 05:50:51 AM
|
Origin Version (Select Help-->About Origin): Operating System: Hi, I wanted to write a program to make a fit to a portion of the input data, first without weight and the subsequent ones with weights.I tried it with the function nag_simple_linear_regression.It works fine without weights buts returns 399 (On entry, at least one of the weights is negative)when the data are weighted.The weights are all positive.
Here is the code:
void slope(string datasetname, int sets,int iter)//datasetname,number of data points to be included and number of iterations {
Worksheet wksW(datasetname); Dataset dsA(wksW,0);// Input data X, Dataset dsB(wksW,1);//Input data Y Dataset dsC(wksW,2);//X column for the portion of data for fitting Dataset dsD(wksW,3);//Y column for the portion of data for fitting Dataset dsE(wksW,4);//weights dsC.SetSize(sets);// dsD.SetSize(sets); dsE.SetSize(sets); //Nag_SumSquare mean; int kk; kk = dsA.GetUpperBound(); printf("Regression of %s\n",datasetname); printf("Number of data points:%d\n",kk); char m, w; int i, n; double a, b, err_a, err_b, rsq, rss, df; double *wtptr; int success; printf("Starting fit for first polarization cylce without weights\n");
int ii; int setnum=0;
for(ii=0;ii<sets;ii++) { dsC[ii]=dsA[ii+setnum]; dsD[ii]=dsB[ii+setnum]; dsE[ii]=1; // first fit with no weights }
int doit; for(doit=0;doit<iter;doit++) { success = nag_simple_linear_regression(Nag_AboutMean,sets,dsC,dsD,dsE, &a, &b, &err_a, &err_b,&rsq, &rss, &df); if(success == 0) { printf("\n A ErrA B ErrB RegCoeff SSR DOF\n"); printf("%6.6f\t%6.6f\t%6.6f\t%6.6f\t%6.6f\t%6.6f\t%6.6f\n",a,err_a,b,err_b,rsq,rss,df); } else { printf("Error %d :The function nag_simple_linear_regression does not work\n",success); break; } printf("Proceeding to weighted fit with weight=1/sigma\n"); double data; for(ii=0;ii<sets;ii++) { data =dsD[ii]-(a+(b*dsC[ii])); dsE[ii]= 1/sqrt(data*data); // now fit with weights (rugged norm) } } } ======================================================================== and another question about the degrees of freedom : As far as I have understood it is the number of independent variables needed to describe a system and as a rule of thumb it is the number of data points minus the number of parameters in the fit i.e., if I fit a straight line (A+BX) to a dataset containing 100 parameters the DOF is 98. But the df value in the function returns a much lower value for the same. Is there anything wrong with my understanding of DOF? Thanks, Deepak Samuel.
Edited by - cosy on 10/25/2004 08:48:14 AM |
|
|
easwar
USA
1965 Posts |
Posted - 10/25/2004 : 10:48:48 PM
|
Hi Deepak,
You cannot pass dataset objects to a NAG function call. You first need to copy them into vectors and then pass the vectors, such as below:
vector vecC, vecD, vecE; vecC = dsC; vecD = dsD; vecE = dsE; int doit; for(doit=0;doit<iter;doit++) { success = nag_simple_linear_regression(Nag_AboutMean,sets,vecC,vecD,vecE, &a, &b, &err_a, &err_b,&rsq, &rss, &df); // etc
The code then works fine, and I get proper DOF values as well. Please try with vectors and post again if you run into more errors.
Easwar OriginLab
|
 |
|
| |
Topic  |
|
|
|