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

自古美人都是妖i 提交于 2019-12-22 05:39:32

问题


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:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!