Tuesday, April 27, 2010

Calculation of percentile and projecting


Hi everyone. I'm going to post about how to calculate percentiles for infants and how to calculate projecting.

Percentile Data Files with LMS Values. How to calculate measurement, z-score and percentile

Data used to produce the United States Growth Charts smoothed percentile curves are contained in 3 Excel data files representing the 3 different growth curves for infants (weight-for-age, length-for-age, head circumference-for-age). The file and corresponding chart names are below. These data remain unchanged from the initial release on May 30, 2000 of the growth charts.
  1. WTAGEINF [XLS - 34 KB]
    Weight-for-age charts, birth to 36 months, LMS parameters and selected smoothed weight percentiles in kilograms, by sex and age

  2. LENAGEINF [XLS - 67 KB]
    Length-for-age charts, birth to 36 months, LMS parameters and selected smoothed recumbent length percentiles in centimeters, by sex and age

    Errata: The selected percentile values for length for age have been updated to correspond exactly to the published LMS values. The 50th percentile values have not changed and the outer percentiles differ only at or beyond the second decimal place. There is no visibly perceptible difference in the graphs of the growth curves compared to those published in 2000. Thus, no changes to printed graphical growth charts are needed.
    percentiles in kilograms, by sex and recumbent length (in centimeters)

  3. HCAGEINF [XLS - 34 KB]
    Head circumference-for-age charts, birth to 36 months, LMS parameters and selected smoothed head circumference percentiles in centimeters, by sex and age

These files contain the L, M and S parameters needed to generate exact percentiles and z-scores along with the percentile values for the 3rd, 5th, 10th, 25th, 50th, 75th, 90th, 95th and 97th percentiles by sex (1=male; 2=female) and single month of age. Age is listed at the half month point for the entire month; for example, 1.5 months represents 1.0-1.99 months or 1.0 month up to but not including 2.0 months of age.

The LMS parameters are the median (M), the generalized coefficient of variation (S) and the power in the Box-Cox transformation (L). To obtain the value (X) of a given physical measurement at a particular z-score, use the following equation:
X = M (1 + LSZ)**(1/L),  L != 0

or

X = M exp(SZ), L == 0

** indicates an exponent, such that M(1+LSZ)**(1/L) means raising (1+LSZ) to the (1/L)th power and then multiplying the M; exp(X) is the exponentiation function, e to the power X. Z is the z-score that corresponds to the percentile. Z-scores correspond exactly to percentiles, e.g., z-scores of -1.881, -1.645, -1.282, -0.674, 0, 0.674, 1.036, 1.282, 1.645 and 1.881 correspond to the 3rd, 5th, 10th, 25th, 50th, 75th, 85th, 90th, 95th and 97th percentiles, respectively.

For example, to obtain the 5th percentile of weight-for-age for a 9-month-old male, we would look up the L, M and S values from the WTAGEINF table, which are L=-0.1600954, M=9.476500305, and S=0.11218624. For the 5th percentile, we would use Z=-1.645. Using the equation above, we calculate that the 5th percentile is 7.90 kg.

To obtain the z-score (Z) for a given measurement (X), use the following equation:
((X/M)**L) - 1
Z = ------------------, L != 0
LS

or

Z = ln(X/M)/S, L == 0

where X is the physical measurement (e.g. weight, length or head circumference) and L, M and S are the values from the appropriate table corresponding to the age in months of the child (or length/stature). (X/M)**L means raising the quantity (X/M) to the Lth power.

For example, to obtain the weight-for-age z-score of a 9-month-old male who weighs 9.7 kg, we would look up the L, M and S values from the WTAGEINF table, which are L=-0.1600954, M=9.476500305, and S=0.11218624. Using the equation above, we calculate that the z-score for this child is 0.207. This z-score corresponds to the 58th percentile.

Alex and I found a class which has methods to calculate cumulative distribution and inverse cumulative distribution. Now we are using that class in the Android application. It has 2 simple methods which uses only log, sqrt and exp functions to calculate percentile and z-score, so it can be used in iPhone and Windows Phone 7 as well.

Below is the java code for that class.

/**
* This class contains routines to calculate the normal cumulative distribution function (CDF) and its inverse.
*/

public class CDFNormal {

    /**
     * This method calculates the normal cdf inverse function.
     *
     *@param percentile
     *            p must lie between 0 and 1. inverseCumulativeProbability returns the normal cdf inverse evaluated at
     *            percentile.
     */

    public static double inverseCumulativeProbability(double percentile) {
        if (percentile < 0 || percentile > 1) {
            throw new IllegalArgumentException("p should lie in [0, 1]");
        }
        double arg, t, t2, t3, xnum, xden, qinvp, pc;

        final double[] c = { 2.515517, .802853, .010328 };

        final double[] d = { 1.432788, .189269, .001308 };

        if (percentile <= 0.5) {
            arg = -2.0 * Math.log(percentile);
            t = Math.sqrt(arg);
            t2 = t * t;
            t3 = t2 * t;

            xnum = c[0] + c[1] * t + c[2] * t2;
            xden = 1.0 + d[0] * t + d[1] * t2 + d[2] * t3;
            qinvp = t - xnum / xden;

            return -qinvp;
        } else {
            pc = 1.0 - percentile;
            arg = -2.0 * Math.log(pc);
            t = Math.sqrt(arg);
            t2 = t * t;
            t3 = t2 * t;

            xnum = c[0] + c[1] * t + c[2] * t2;
            xden = 1.0 + d[0] * t + d[1] * t2 + d[2] * t3;

            return t - xnum / xden;
        }
    }


    /**
     * This method calculates the normal cumulative distribution function.
     * <p>
     * It is based upon algorithm 5666 for the error function, from:
     * </p>
     *
     * <pre>
     *       Hart, J.F. et al, 'Computer Approximations', Wiley 1968
     * </pre>
     *
     * @param zScore
     *            The method returns the value of the normal cumulative distribution function at zScore.
     */

    public static double cumulativeProbability(double zScore) {
        double zabs;
        double p;
        double expntl, pdf;

        final double p0 = 220.2068679123761;
        final double p1 = 221.2135961699311;
        final double p2 = 112.0792914978709;
        final double p3 = 33.91286607838300;
        final double p4 = 6.373962203531650;
        final double p5 = 0.7003830644436881;
        final double p6 = 0.3526249659989109E-1;

        final double q0 = 440.4137358247522;
        final double q1 = 793.8265125199484;
        final double q2 = 637.3336333788311;
        final double q3 = 296.5642487796737;
        final double q4 = 86.78073220294608;
        final double q5 = 16.06417757920695;
        final double q6 = 1.755667163182642;
        final double q7 = 0.8838834764831844E-1;

        final double cutoff = 7.071;
        final double root2pi = 2.506628274631001;

        zabs = Math.abs(zScore);

        // |z| > 37
        if (zScore > 37) {
            return 1;
        }
        if (zScore < -37) {
            return 0;
        }

        // |z| <= 37.
        expntl = Math.exp(-0.5 * zabs * zabs);
        pdf = expntl / root2pi;

        // |z| < cutoff = 10/sqrt(2).
        if (zabs < cutoff) {
            p = expntl * ((((((p6 * zabs + p5) * zabs + p4) * zabs + p3) * zabs + p2) * zabs + p1) * zabs + p0) / (((((((q7 * zabs + q6) * zabs + q5) * zabs + q4) * zabs + q3) * zabs + q2) * zabs + q1) * zabs + q0);
        } else {
            p = pdf / (zabs + 1.0 / (zabs + 2.0 / (zabs + 3.0 / (zabs + 4.0 / (zabs + 0.65)))));
        }

        return (zScore < 0 ? p : 1 - p);
    }
}

How to calculate projecting

In the Android application we are using the following scheme to calculate the projecting:
  • Get percentiles for last measurements (if there is no, use 55)

  • If 1 < percentile < 99, calculate projecting using this percentile

  • There might be problems when percentile < 1 or percentile > 99, so we calculate measurement using 1 or 99 percentile respectively, and then add/subtract necessary value.

For example, if baby's weight is 12kg which percentile is great than 99 and measurement for 99 percentile is 10 we calculate the measurement using 99 percentile (e.g. measurement = 15kg) and then add 20% to it (15kg + 20% = 18kg).

1 comment:

  1. I am unable to find percentile, I have LMS value along with 3rd, 5th, 10th, 25th, 50th, 75th, 85th, 90th, 95th and 97th percentiles in Data base. How to calculate it?
    1. Just I have matched your z score value which is not matching to 3rd to 97th value, Kindly explain that you have putted z value on page how have you find it?

    ReplyDelete