```Date: Mon, 24 Feb 1997 10:49:12 -0600 Reply-To: "Matheson, David" Sender: "SPSSX(r) Discussion" From: "Matheson, David" Subject: Re: Geometric Mean Comments: To: "Kenneth B. Hill" Ken, The following 2 technical notes (pasted after your question) should help. The first shows how to calculate harmonic and geometric means. The second shows how to calculate geometric means for weighted data. David Matheson SPSS Technical Support >---------- >From: Kenneth B. Hill[SMTP:Kenneth_B._Hill@class.orednet.org] >Sent: Thursday, February 20, 1997 11:04 AM >To: Multiple recipients of list SPSSX-L >Subject: Geometric Mean > >Does SPSS for Windows provide a method of calculating "geometric mean" as >opposed to "arithmatic mean". > >Ken Hill, MS >Research Analyst >Oregon Health Division >ken_hill@class.orednet.org > >-- Note 1. SPSS_X job to calculate harmonic and geometric means . ****************************************************************** * PROGRAM TO COMPUTE HARMONIC AND GEOMETRIC MEANS. * * * * DESCRIPTIVES THROWN IN FOR COMPARISON TO 'REGULAR' MEAN * * 2/9/88 * ****************************************************************** DATA LIST FREE/V1 . BEGIN DATA . 78 72 58 66 66 68 76 80 78 60 60 68 66 END DATA . DESCRIPTIVES V1/STATS MEAN . COMPUTE A1 = 1/V1 . COMPUTE B1 = LN(V1) . COMPUTE CONSTANT = 1 . AGGREGATE OUTFILE = * /BREAK = CONSTANT /HM = MEAN(A1) /GM=MEAN(B1) . COMPUTE H_MEAN = 1/HM . COMPUTE G_MEAN = EXP(GM) . VARIABLES LABELS H_MEAN 'HARMONIC MEAN' G_MEAN 'GEOMETRIC MEAN' . LIST VARS = H_MEAN G_MEAN . * Mean = 68.9 Harmonic mean = 68.2 Geometric mean = 68.56 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * . Note 2 . Q. How can I compute the geometric mean for a variable when the cases are weighted? A. A geometric mean for a variable X can be computed by: 1. Taking the natural log of X, ln(X); 2. Summing the values of ln(X); 3. Dividing that sum by the number of cases; 4. Raising e (the base of the natural logarithm) to the power of Step 3., i.e. taking the antilog of the result of Step 3. If the cases are weighted, these steps are generalized as follows: 1. Take the natural log of X**W (where W is the case weight). This is equal to W*ln(X); 2. Sum the values of W*ln(X) across cases; 3. Divide that sum by the sum of the weights; 4. Raise e to the result of step 3. The following examples compute the geometric mean for a variable called VARX under each of two conditions: when the cases have been weighted by the WEIGHT BY command; when the cases have not been weighted, but you wish to treat the variable WT as a weight for the purpose of computing the geometric mean. The latter approach allows you to compute the means under each of two or more weight variables, whereas the WEIGHT command is restricted to a single weighting variable. Both approaches use the AGGREGATE procedure and merge the aggregate information with the case-level data, i.e., the original active file. This merging allows the use of the geometric mean in transformations on the case-level data. If such transformations are not required, set the AGGREGATE OUTFILE to *, the new active file, as in Example 2b below, and omit the MATCH FILES command. (In SPSS/PC+, the MATCH FILES command is replaced by the JOIN MATCH command and file names must be enclosed in single or double quote marks. Otherwise, the commands below will work on current versions of SPSS or SPSS/PC+). 1. When cases are weighted by the WEIGHT command. WEIGHT BY WT. COMPUTE WLX = LN(VARX). COMPUTE DUMMY = 1. AGGREGATE OUTFILE = TEMP / BREAK = DUMMY / GMSUM = SUM(WLX) / WTSUM = N. MATCH FILES /FILE = * / TABLE = TEMP / BY DUMMY. COMPUTE GMX = EXP(GMSUM/WTSUM). FORMATS GMX (F10.6). 2.a When cases are NOT weighted by the WEIGHT command, but the geometric mean is to be weighted by the value of WT. COMPUTE WLX = WT*LN(VARX). COMPUTE DUMMY = 1. AGGREGATE OUTFILE = TEMP / BREAK = DUMMY / GMSUM WTSUM = SUM(WLX WT). MATCH FILES /FILE = * / TABLE = TEMP /BY DUMMY. COMPUTE GMX = EXP(GMSUM/WTSUM). FORMATS GMX (F10.6). 2.b When cases are not weighted by the WEIGHT command, and the geometric mean of VARX is desired under each of two weight variables, WT1 and WT2. The corresponding means will be called GMX1 and GMX2. Note that this example also shows an alternate order to the file merging and the computation of the geometric mean from the aggregate variables. In Example 2a, the aggregate file was never the active file. The result is that the computation of the geometric mean(s) from the aggregate variables GMSUM and WTSUM was recalculated for each case in the file. In Example 2b, the aggregate file is the active file upon creation. The result is that the geometric mean is calculated once and then copied to each case by the merge. The case-level file must therefore be saved before the aggregate procedure is invoked. (This is the tradeoff for a single calculation of the mean.) COMPUTE WLX1 = WT1*LN(VARX). COMPUTE WLX2 = WT2*LN(VARX). COMPUTE DUMMY = 1. SAVE OUTFILE = TEMP. AGGREGATE OUTFILE = * / BREAK = DUMMY / GMSUM1 WTSUM1 GMSUM2 WTSUM2 = SUM(WLX1 WT1 WLX2 WT2). COMPUTE GMX1 = EXP(GMSUM1/WTSUM1). COMPUTE GMX2 = EXP(GMSUM2/WTSUM2). FORMATS GMX1 GMX2 (F10.6). MATCH FILES /FILE = TEMP / TABLE = * / DROP = GMSUM1 WTSUM1 GMSUM2 WTSUM2 / BY DUMMY. ```

Back to: Top of message | Previous page | Main SPSSX-L page