log transformation in R

Logarithmic transformation in R, inverse logarithmic transformation in R

Logarithmic transformation in R is one of the transformations that is typically used in time series forecasting. If your forecasting results have negative values, then log transformation of the target value will prevent from going below zero. In other words, logarithmic transformation stabilizes the variance of the time series and ensures that predictions stay positive.

Logarithmic transformation in R

Here is how to perform common a base-10 log transformation.

value <- c(221, 181, 227, 176, 201)

value <- log(value)

value
#[1] 5.398163 5.198497 5.424950 5.170484 5.303305

Inverse logarithmic transformation in R

After forecasting, you should back-transform the results to get them on the original scale.
In this case, the reverse transformation is done with the exp function.
Here is how it looks with previously log-transformed data.

exp(value)
#[1] 221 181 227 176 201

The issue with log transformation if time series have 0

If your time series contains 0 values, then the result of log transformation is infinity, and that might not be suitable with forecasting models.

value <- c(221, 181, 227, 176, 201, 0, 0)

log(value)
#[1] 5.398163 5.198497 5.424950 5.170484 5.303305     -Inf     -Inf

To avoid problems of log(0), you can include an offset in the transformation. By adding 1, you can overcome the limitation of using log transformations on data that contains zeros. Even easier is to use the log1p function that computes log(1+x) accurately.

value <- c(221, 181, 227, 176, 201, 0, 0)

log(value + 1)
#[1] 5.402677 5.204007 5.429346 5.176150 5.308268 0.000000 0.000000

log1p(value)
#[1] 5.402677 5.204007 5.429346 5.176150 5.308268 0.000000 0.000000

Reverse logarithmic transformation to get data back to the original scale usually works well enough like this. You can subtract 1 or use the expm1 function.

value <- c(221, 181, 227, 176, 201, 0, 0)

value <- log1p(value)

exp(value) - 1
#[1] 221 181 227 176 201   0   0

expm1(value)
#[1] 221 181 227 176 201   0   0

Logarithmic transformation in tidymodels

If you are using tidymodels and preprocessing your data with the recipes, then you can use step_log that creates a recipe step that will log transform data.

Try to use an offset parameter that equals 1 to achieve the same results as in the previous method. It is an optional parameter to add to the data before logging.

require(recipes)

value <- tidyr::tibble('value' = c(221, 181, 227, 176, 201, 0, 0))

recipe_spec <- recipe(value) %>% step_log(value, offset = 1)

recipe_spec %>% prep() %>% juice()

## A tibble: 7 x 1
#value
#
#1  5.40
#2  5.20
#3  5.43
#4  5.18
#5  5.31
#6  0   
#7  0

Back transform your predictions like this.

df <- recipe_spec %>% prep() %>% juice()

exp(df$value) - 1
#[1] 221 181 227 176 201   0   0

 

Tips & tricks

Here are 2 posts that contain useful tips and tricks for R users. Top 10 tips and tricks by using dplyr and RStudio.





Posted

in

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *