Format of R's lm() Formula with a Transformation

5.8k views Asked by At

I can't quite figure out how to do the following in one line:

data(attenu)
x_temp = attenu$accel^(1/4)
y_temp = log(attenu$dist)
best_line = lm(y_temp ~ x_temp)

Since the above works, I thought I could do the following:

data(attenu)
best_line = lm( log(attenu$dist) ~ (attenu$accel^(1/4)) )

But this gives the error:

Error in terms.formula(formula, data = data) : invalid power in formula

There's obviously something I'm missing when using transformed variables in R's formula format. Why doesn't this work?

1

There are 1 answers

0
Rorschach On BEST ANSWER

You're looking for the function I so that the ^ operator is treated as arithmetic in the formula, ie.

x <- runif(1:100)
y <- x + rnorm(100,0, 3)
lm(log(y) ~ I(x^(1/4))