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?
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))
来源:https://stackoverflow.com/questions/30929115/format-of-rs-lm-formula-with-a-transformation