Logit-Transformation backwards

8.7k views Asked by At

I've transformed some values from my dataset with the logit transformation from the car-package. The variable "var" represent these values and consists of percentage values.

However, if I transform them back via inv.logit from the boot-package, the values dont match the original ones.

data$var
46.4, 69.5, 82.7, 61.7, 76.4, 84.8, 69.1

data["var_logit"] <- logit(data$var, percents=TRUE)

data$var_logit
-0.137013943, 0.778005062, 1.454239241, 0.452148763, 1.102883518, 1.589885549, 0.760443432

data$var_logback <- inv.logit(data$var_logit)
0.46580 0.68525 0.81065 0.61115 0.75080 0.83060 0.68145

It looks like I have to multiply the result with 100 to get the previous values (or at least some very similar values), but I feel like I'm missing something.

Thanks for the help!

2

There are 2 answers

0
lejlot On BEST ANSWER

You set the percents=TRUE flag, which divides your values by 100, and the inverse command does not know about it.

0
Ben Bolker On

The other thing that's going on here is that car::logit automatically adjusts the data if there are 0 or 1 values:

adjust: adjustment factor to avoid proportions of 0 or 1; defaults to ‘0’ if there are no such proportions in the data, and to ‘.025’ if there are.

library(car)
dat <- c(46.4, 69.5, 82.7, 61.7, 76.4, 84.8, 69.1)
(L1 <- logit(dat, percents=TRUE))
## [1] -0.1442496  0.8236001  1.5645131
##      0.4768340  1.1747360  1.7190001  0.8047985
(L2 <- logit(c(dat,0),percents=TRUE))
## [1] -0.1370139  0.7780051  1.4542392  0.4521488
##      1.1028835  1.5898855  0.7604434 -3.6635616
## Warning message:
## In logit(c(0, dat)) : proportions remapped to (0.025, 0.975)

This means you can't invert the results as easily.

Here's a function (using the guts of car::inv.logit with a little help from Wolfram Alpha because I was too lazy to do the algebra) that inverts the result:

inv.logit <- function(f,a) {
   a <- (1-2*a)
   (a*(1+exp(f))+(exp(f)-1))/(2*a*(1+exp(f)))
}
zapsmall(inv.logit(L2,a=0.025)*100)
## [1] 46.4 69.5 82.7 61.7 76.4 84.8 69.1  0.0