I have data for which I would like to take the log transformation before doing OLS. The data include zeros. Thus, I want to do a log(x + c). I know a traditional c to choose is 1. I am wondering whether there is a way to have the data choose c such that there would be no skew anymore using features like the sample mean or variance? Is there a formaula?
|
|
Because the intention is to do OLS, the choice of $c$ should be made in this context. In general, we ought to fit $c$ simultaneously with the rest of the regression. A quick and dirty way to do this recognizes that the regression $R^2$ is proportional to the log likelihood, so we could seek a value of $c$ that maximizes $R^2$. This is a special example of the problem of choosing among a parameterized family of transformations $y \to f(y; \theta)$ to achieve the best possible fit of $y$ to explanatory values $x$. This can be solved in
(I am glossing over a somewhat delicate matter of choosing good starting values for the parameter: it is possible to obtain bad solutions with As an example of the use of
Evidently $\log(y-100)$ is drawn iid from a Normal distribution. I will apply
In cases (1) and (2) I will plot the histogram of $y$ (to show it is highly skewed), the scatterplot of $y$ against $x$ (to exhibit the data), and the scatterplot of the transformed $y$ against $x$ with the OLS line superimposed, to see the result of the transformation. In the third case those scatterplots are meaningless, so I only report the value of $c$ found by
The top row is the first case and the second row of plots are for the second case. Please observe:
If we were to use the "universal" value of $c$ found in the third case (essentially by ignoring the $x$ values), here is what the scatterplot would look like in conjunction with the $x$ values from case 1:
For these particular $x$ values, the OLS line fit to the transformed $y$ values is a terrible description of the relationship between $y$ and $x$. Notice how it underestimates most values of $y$ but grossly overestimates a few of them for $x$ between $140$ and $200$. In summary, if you want to transform the response variable for a regression (to achieve symmetry or linearity), you must account for the regression itself. This is because the regression only "cares" about the residuals, not the raw values of $y$. As the extraordinarily bad value of $c$ in the first case shows, ignoring this advice could produce awful results. |
||||
|
|
|
You could try to minimize the square of the sample skewness with respect to the constant $c$. Here is a quick
Here I generate some data, minimize the squared skewness of the transformed data with respect to $c$, and plot the histograms of the original data and the transformed data.
|
|||||
|
|
In a comment, I asserted there is a formula for a constant $c$ which eliminates the "skewness" of a dataset $(y_i)$ upon applying the "started logarithm" $y \to \log(y+c)$. There are many formulas. This answer describes a family of formulas that are extremely simple, fast to compute, robust, and flexible. (Note, though, that another answer I posted explains why you might not want to apply such a formula to the dependent variable in a regression context and gives an alternative for such cases.) MotivationIn practice the term "skewness" in the questions usually refers qualitatively to the shape of dataset, not to the sample skewness. The sample skewness (a standardized central third moment) is highly sensitive to even moderate outliers. Procedures that are sensitive to outliers are good procedures only for--letting outliers ruin your analysis. Thus, sample skewness is usually a poor choice to use for exploring or characterizing data. (Yes, there do exist some statistic tests based on skewness, but they tend to be inferior to more robust tests or of limited application.) Instead, pick some low percent $\alpha$--typically between $.05$ and $0.25$--and examine the $\alpha$ and $1-\alpha$ percentiles of the dataset $(y_i)$. Let's call these $q_{-}$ and $q_{+}$, respectively. Let the median of the dataset be $m$. A good measure of symmetry of the data is the difference between the upper spread $q_{+}-m$ and the lower spread $m - q_{-}$: in a perfectly symmetric data set, these two spreads around the median are equal no matter what the value of $\alpha$. Accommodating the reality that almost no dataset is, or can readily be made, symmetric, we anticipate that achieving symmetry of the spreads at the $\alpha$ and $1-\alpha$ percentiles will likely do a good job of symmetrizing the whole dataset, except possibly for some outlying values beyond those percentiles. The formulaHere is the signal beauty of this approach: because we seek a monotonic re-expression of the data, say $y_i \to f(y_i,c)$ for a given function $f$ (the logarithm) and parameter $c$ yet to be found, the $\alpha$ percentile of the re-expressed data $(f(y_i,c))$ is (up to a tiny difference related to the discreteness of the data) equal to $f$ applied to the $\alpha$ percentile of $(y_i)$. Therefore, it will suffice to find $c$ so that $$\log(q_{+} + c) - \log(m + c) = \log(m + c) - \log(q_{-} + c).$$ By means of elementary properties of logarithms, this equation implies $$(q_{+} + c)(q_{-} + c) = (m + c)^2.$$ Provided the data are not already symmetric (that is, there actually is a difference in the original spreads), this has at most one solution $$c = \frac{m^2 - q_{+}q_{-}}{q_{+} + q_{-} - 2m}.$$ Because a fixed quantile of $n$ data can be found with $O(n)$ computation, this is a fast procedure. For large datasets, applying this algorithm to a small random sample (as small as a few dozen values) will work fine, leading to an extremely fast constant time algorithm. ImplementationThere are some messy practical details to deal with if one wants a general-purpose software solution. In order to apply logarithms, we will need all of the $(y_i+c)$ to be positive. Thus, when the solution $c$ is less than or equal to $-\min(\{y_i\})$, some alternative must be found. One reasonable approach includes two things:
The adjustment should be relative to the range of values in $(y_i)$. That is, we should select some other percent $\beta$ and extrapolate from the $\beta$ percentile of the $(y_i)$ down below $\min(\{y_i\}$ by a tiny proportion $\gamma \gt 0$. That is the solution adopted by the following
ExampleAs an example of its use, let's generate data $(y_i)$ for which $\log(y_i+100)$ are iid Normal:
Apply the formula. Here I chose $\alpha=0.10$, in an effort to symmetrize the middle $1-2\alpha$ = $80$% of the data, allowing for as much as $10$% in the upper or lower tails (or both) to exhibit outlying behavior:
Display the distributions of the data (in the left column) and the transformed data (in the right column):
The almost symmetric histogram and nearly linear q-q plot in the right column of graphs show this choice of $c$ (which was $-97.7$, quite close to canceling the value of $100$ used to generate the data) works very well in this instance. |
||||
|
|


