1
$\begingroup$

Looking to find geometric mean of monthly data that's been converted into quarterly data. I've tried several methods but only provide simple arithmetic mean as shown below. Thanks.

monthly <- ts(mydata,start=c(1960,1),frequency=12)
quarterly <- aggregate(monthly, nfrequency=4,mean) ---need geometric mean

This looks promising but unable to mimic the above simple arithmetic mean which considers the monthly to quarterly data transformation:

# Function to calculate the geometric mean
geometricMean &lt;- function(array){
 if(!is.numeric(array)){
 stop(&quot;Passed argument must be an array. Consider using sapply for data frames.&quot;)
 }
 if(any(array&lt;0)){
 stop(&quot;All values must be greater than zero. If you are attempting to
 apply this function to rates, convert to +1 format. For example,
 5% becomes 1.05 and -20% becomes .8.&quot;)
 }
 prod(array)^(1/length(array))
}

The data has been transformed into percentage change rates (month_12 - month_11 / month_11)

$\endgroup$
3
  • $\begingroup$ The transformed data-set has some negative values which are a necessary aspect of my analysis. $\endgroup$ Commented Feb 16, 2018 at 22:47
  • $\begingroup$ The data set transformation is percent change (positive or negative values), not explicitly growth rates. $\endgroup$ Commented Feb 17, 2018 at 0:03
  • 1
    $\begingroup$ Please put your comments into the question itself. $\endgroup$
    – Peter Flom
    Commented Feb 24, 2019 at 12:54

2 Answers 2

0
$\begingroup$

The easiest thing to is to convert your data into logarithms and then you can compute the geometric mean by taking the exponent of the sum.

I.e., $ \begin{bmatrix} x_{i} & x_{i+1} & x_{1+2} & \dots & x_{n} \\ \end{bmatrix} \to \begin{bmatrix} Ln [x_{i}] & Ln[x_{i+1}] & Ln[x_{i+2}] & \dots & Ln[x_{n}] \\ \end{bmatrix} $

$\text{Geometric mean} +1 = (\prod_i^n (1+x))^{1/n} = exp\left[ \frac{\sum_i^n ln[x_i]}{ n} \right] = exp\left[\mu_{ln(x)} \right]$

where: $\mu_{ln(x)}$ is the simple arithmetic mean of the logarithm of x.

$\endgroup$
7
  • 1
    $\begingroup$ It is difficult to interpret the part of your formula that states "$\text{Geometric mean} +1 = (\sum_i^n (1+x))^{1/n} = $" in any way that makes it correct. What are you trying to assert with that? $\endgroup$
    – whuber
    Commented Feb 16, 2018 at 21:28
  • $\begingroup$ @DavidAddison Taking a log of negative numbers won't work and I have already converted my data into growth rates. I could "add" a constant integer to all my observations and log it but I don't want it to distort my results. $\endgroup$ Commented Feb 16, 2018 at 23:14
  • $\begingroup$ Rtimeseries, the geometric mean is defined only for non-negative sets of numbers. By requesting the GM, you were therefore implicitly telling us you have no negative numbers. It sounds like you are unsure about what you want to do. $\endgroup$
    – whuber
    Commented Feb 16, 2018 at 23:25
  • $\begingroup$ @Rtimeseries. How are you taking growth rates? Like this: (i.e., $x_t = \frac{x_t}{x_{t-\delta t}} - 1$)? Note that a growth "rate" is only defined for positive x, as is the geometric mean. $\endgroup$ Commented Feb 16, 2018 at 23:50
  • $\begingroup$ @DavidAddison Provided clarification above. $\endgroup$ Commented Feb 17, 2018 at 0:08
0
$\begingroup$

The geometric mean is undefined when any of the data are negative. This has nothing to do with whether the data are monthly or quarterly, nor with any problems with R or your code.

The solution to this problem seems to be right in the code that you quote, in particular:

All values must be greater than zero. If you are attempting to apply this function to rates, convert to +1 format. For example, 5% becomes 1.05 and -20% becomes .8

Have you tried this?

$\endgroup$
1
  • 1
    $\begingroup$ Your formula lacks essential terms. Could it perhaps mean something like "$\left(\prod_{i=1}^n x_i\right)^{1/n}$"? If so, please note that the result is not necessarily complex when data values are negative. What, for instance, does this formula give for the data $(-1,-1)$? This exposes the subtleties that lead to David Addison's formulation in terms of logarithms and exponentials. $\endgroup$
    – whuber
    Commented Feb 24, 2019 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.