TPCCLIB
Loading...
Searching...
No Matches
logan.c File Reference

Regional Logan plot. More...

Go to the source code of this file.

Functions

int best_logan_reed (double *x, double *y, double *wx, double *wy, int nr, int min_nr, double *slope, double *ic, double *nwss, double *sslope, double *sic, double *cx, double *cy, int *bnr, int verbose)
 
int best_logan_regr (double *x, double *y, double *wx, double *wy, int nr, int min_nr, double *slope, double *ic, double *r, double *sslope, double *sic, int *bnr, int verbose)
 

Detailed Description

Regional Logan plot.

Estimation of the tracer distribution volume or distribution volume ratio from regional PET data using Logan multiple-time graphical analysis.

Author
Vesa Oikonen

Definition in file logan.c.

Function Documentation

◆ best_logan_reed()

int best_logan_reed ( double * x,
double * y,
double * wx,
double * wy,
int nr,
int min_nr,
double * slope,
double * ic,
double * nwss,
double * sslope,
double * sic,
double * cx,
double * cy,
int * bnr,
int verbose )

Finds the best least-squares line to (x,y)-data, leaving points out from the beginning.

Returns
Returns 0, if ok.
Parameters
xPlot x axis values
yPlot y axis values
wxWeighting factors for x
wyWeighting factors for y
nrNr of plot data points
min_nrMin nr of points to use in the fit; must be >=4
slopeSlope is returned in here
icY axis intercept is returned in here
nwsssqrt(WSS)/wsum is returned here
sslopeExpected sd of slope at calculated points
sicExpected sd of intercept at calculated points
cxCalculated x data points
cyCalculated y data points
bnrNumber of points in the best fit (incl data with w=0)
verboseVerbose level; if zero, then nothing is printed to stderr or stdout

Definition at line 717 of file logan.c.

748 {
749 int from, to, ret, from_min, to_min;
750 double *w, lic, lslope, lnwss, nwss_min;
751
752 if(verbose>0) printf("best_logan_reed()\n");
753 /* Check the data */
754 if(x==NULL || y==NULL || nr<min_nr || nr<2) return(1);
755 /* Check parameters */
756 if(min_nr<4) return(2);
757
758 /* Make room for work vector */
759 w=(double*)malloc(nr*sizeof(double)); if(w==NULL) return(3);
760
761 /* Search the plot range that gives the best nwss */
762 nwss_min=9.99E+99; from_min=to_min=-1;
763 for(from=0, to=nr-1; from<nr-min_nr; from++) {
764 ret=llsqwt(x+from, y+from, (to-from)+1, wx+from, wy+from, 1.0E-10, w,
765 &lic, &lslope, &lnwss, NULL, NULL, cx+from, cy+from);
766 if(verbose>1) {
767 printf(" range: %d-%d ; nwss=%g ; min=%g ; ret=%d\n",
768 from, to, lnwss, nwss_min, ret);
769 }
770 if(ret==0 && lslope>0.0 && lnwss<nwss_min) {
771 nwss_min=lnwss; from_min=from; to_min=to;}
772 }
773 if(from_min<0) {free(w); return(5);}
774
775 /* Run llsqwt() again with that range, now with better resolution */
776 /* and this time compute also SD's */
777 from=from_min; to=to_min;
778 ret=llsqwt(x+from, y+from, (to-from)+1, wx+from, wy+from, 1.0E-12, w,
779 ic, slope, nwss, sic, sslope, cx+from, cy+from);
780 free(w); if(ret) return(6);
781 *bnr=(to-from)+1;
782
783 return(0);
784}
int llsqwt(double *x, double *y, int n, double *wx, double *wy, double tol, double *w, double *ic, double *slope, double *nwss, double *sic, double *sslope, double *cx, double *cy)
Definition llsqwt.c:48

◆ best_logan_regr()

int best_logan_regr ( double * x,
double * y,
double * wx,
double * wy,
int nr,
int min_nr,
double * slope,
double * ic,
double * r,
double * sslope,
double * sic,
int * bnr,
int verbose )

Find the best regression line to (x,y)-data, leaving points out from the beginning.

Returns
Returns 0, if ok.
Parameters
xPlot x axis values
yPlot y axis values
wxWeighting factors for x
wyWeighting factors for y
nrNr of plot data points
min_nrMin nr of points to use in the fit; must be >=4
slopeSlope is returned in here
icY axis intercept is returned in here
rPearson's correlation coefficient is returned here
sslopeExpected sd of slope at calculated points
sicExpected sd of intercept at calculated points
bnrNumber of points in the best fit (incl data with w=0)
verboseVerbose level; if zero, then nothing is printed to stderr or stdout

Definition at line 792 of file logan.c.

819 {
820 int fi, from, to, ret, from_min, to_min, n;
821 double lic, lslope, lic_sd, lslope_sd, cv, cv_min, f;
822 double *cx, *cy;
823
824 if(verbose>0) printf("best_logan_regr()\n");
825 /* Check the data */
826 if(x==NULL || y==NULL || nr<min_nr || nr<2) return(1);
827 /* Check parameters */
828 if(min_nr<4) return(2);
829
830 /* Construct a checked data with no negative values and weights>0 */
831 cx=(double*)malloc(nr*sizeof(double));
832 cy=(double*)malloc(nr*sizeof(double));
833 if(cx==NULL || cy==NULL) return(3);
834 for(fi=n=0; fi<nr; fi++)
835 if(wx[fi]>0 && wy[fi]>0 && !isnan(x[fi]) && !isnan(y[fi])) {
836 cx[n]=x[fi]; cy[n]=y[fi]; n++;}
837 if(n<min_nr) {free(cx); free(cy); return(4);}
838
839 /* Search the plot range that gives the lowest CV for slope */
840 cv_min=9.99E+99; from_min=to_min=-1;
841 for(from=0, to=n-1; from<n-min_nr; from++) {
842 /* Calculation of linear regression using pearson() */
843 ret=pearson(
844 cx+from, cy+from, (to-from)+1,
845 &lslope, &lslope_sd, &lic, &lic_sd, r, &f
846 );
847 if(ret==0 && lslope>0) {
848 cv=lslope_sd/lslope;
849 } else cv=9.99E+99;
850 if(cv<cv_min) {
851 cv_min=cv; from_min=from; to_min=to;}
852 }
853 if(from_min<0) {free(cx); free(cy); return(5);}
854
855 /* Run pearson() again with that range */
856 from=from_min; to=to_min;
857 ret=pearson(
858 cx+from, cy+from, (to-from)+1,
859 slope, sslope, ic, sic, r, &f
860 );
861 free(cx); free(cy); if(ret) return(6);
862 *bnr=(to-from)+1;
863
864 return(0);
865}
int pearson(double *x, double *y, int nr, double *k, double *kSD, double *b, double *bSD, double *r, double *ySD)
Definition pearson.c:14