1

I wrote a macro in SAS that did what I am wanting, but now I want to get a function in R that does the same thing.

I want a function, that can transform a specific predictor variable any number of ways, and then build a separate regression model using each transformation, and then compare the results.

For example, consider the following linear regression model:

lm(y ~ x1 x2,data=df)

I want to transform x1 three different ways, by taking the log, taking the square, and taking it to the power of .5, and then build the regression equation three times, once with each transformation and then compare the results.

Is there a function that could be built, or other functionality that would do this for me?

Thanks.

1
  • 2
    Something like: lapply(c(log,sqrt), function(f) lm(y ~ f(x), data=df) ) should do it. If anyone wants to expand on this, go for it. Commented Mar 26, 2015 at 2:09

1 Answer 1

4

This function could probably be improved upon. Tip of the hat to @thelatemail.

lm.3Trans = function(y, x1, x2, transformations = c(log,sqrt,pwer)){
  pwer = function(x, p = 2) poly(x,p)
  res = lapply(transformations, function(f) lm(y ~ f(x1) + x2))
  res
}

This will output your models into a list to which you could then do something like:

lapply(lm.3Trans(y,x,x1), summary)

To get more detailed summaries

It might be worth expanding this function to take a formula, and perhaps another arguement to identify which predictor should be transformed. Please let me know if that is useful.

Sign up to request clarification or add additional context in comments.

4 Comments

replace x^p with poly(x,p) -- more numerically stable
Sam, this is awesome. I am trying to modify this for my regression model, which has 76 variables. Here is my attempt to tell it to transform the variable, and then perform the regression using all available variables. I get an error with this though. (lm.3Trans = function(y, x1, transformations = c(log,sqrt,pwer)){ pwer = function(x, p = 2) poly(x,p) res = lapply(transformations, function(f) x1 <- f(x1), lm(y ~ .,data=train[,2:77])) res } lapply(lm.3Trans(revenue,P1), summary)) @SamPassmore
Can I just ask for a couple of details. Do you want to apply the 3 transformations to only a single predictor in the regression? or Do you want to apply it to each predictor?
@SamPassmore I only want to apply it to a single predictor. That way I can see which transformation of just that single predictor gives the best results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.