Skip to content

Instantly share code, notes, and snippets.

@ptompalski
Created July 18, 2019 21:24
Show Gist options
  • Save ptompalski/7b2a69e4ce40bd95f0533c07e1e89d92 to your computer and use it in GitHub Desktop.
Save ptompalski/7b2a69e4ce40bd95f0533c07e1e89d92 to your computer and use it in GitHub Desktop.
bcf <- function(observed, model_name) {
# another way to correct for back-transformation bias
# based on this:
# https://stats.stackexchange.com/questions/361618/how-to-back-transform-a-log-transformed-regression-model-in-r-with-bias-correcti
# 1. Compute exp(Xβ^), i.e. the retransformed but unadjusted prediction
a <- exp(predict(model_name))
# 2. Regress Y against exp(Xβ^) without an intercept. Call the resulting regression coefficient γ.
b <- lm(observed ~ a -1) #adding -1 in the formula removes the intercept
b <- as.numeric(coefficients(b))
# 3. Compute the adjusted retransformed prediction as γexp(Xβ^).
return(b * exp(predict(model_name)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment