Created
July 18, 2019 21:24
-
-
Save ptompalski/7b2a69e4ce40bd95f0533c07e1e89d92 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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