Tim
Tim

Reputation: 7464

Predicting with log transformation in formula

Imagine a simple model

fit <- lm(log(mpg) ~ cyl + hp, data = mtcars)

to make predictions we have to take exponent

exp(predict(fit, newdata = mtcars))

Is there any better way to do this then apply it manually? Documentation of ?predict does not give any helpful hints on this.

I guess, the easiest way would be to extract the transforming function from the formula

> formula(fit)
log(mpg) ~ cyl + hp

How can I check if any transformation was applied to the left hand side of formula and if there was any transformation, to extract name of the function?

Upvotes: 0

Views: 92

Answers (1)

StefanK
StefanK

Reputation: 2180

I'm not sure if it helps, but you could test it in this manner: Convert it into character and check if it starts with log/sqrt and the likes:

startsWith(as.character((formula(fit))[2]), "log")

The answer is true of false:

[1] TRUE

Maybe this will help you automate your solution?

Upvotes: 1

Related Questions