Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Revert transformation preprocess caret

I transformed data to attend to the requirements of a linear model (normally distributed):

d.reg1 = d.reg %>% preProcess("YeoJohnson") %>% predict(d.reg) 

The adjusted model:

fit = lm(log10(Qmld)~log10(Peq750), data = d.reg1) #potential regression

Predicted data:

a=10^fit$coefficients[1]
b=fit$coefficients[2]

d.reg1$Qmld_predita=a*d.reg1$Peq750^b 

How could I untransform d.reg1$Qmld_predita, since the model was fitted to transformed data and this has no physical significance for me?

like image 474
Arthur Calegario Avatar asked Oct 23 '25 15:10

Arthur Calegario


1 Answers

Here's a model for a function that could be modified based on the initial transformations chosen (e.g. here the initial transformations were c("scale", "center").

library(tidyverse)

revPredict <- function(preproc, data, digits=0) {
  data %>%
    select(one_of(preproc$mean %>% names)) %>%
    map2_df(preproc$std, ., function(sig, dat) dat * sig) %>%
    map2_df(preproc$mean, ., function(mu, dat) dat + mu)
}

revPredict(preprocess_params, df_needing_reverse_transformation)

Since it's been more than 6 months since the question was asked, I assume you've figured a way around this, but it may still be of interest given the similar question being here, too.


To round values, pipe the output of the second map2_df to this:

    mutate_if(is.numeric,funs(round(.,digits = digits)))
like image 89
saladi Avatar answered Oct 26 '25 03:10

saladi