R的lm()公式的格式与转换 [英] Format of R's lm() Formula with a Transformation

查看:304
本文介绍了R的lm()公式的格式与转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太清楚如何在一行中执行以下操作:

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)) )

但这会导致错误:

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

以R的公式格式使用转换后的变量时,显然缺少某些东西.为什么这行不通?

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

推荐答案

您正在寻找函数I,以便将^运算符视为公式中的算术运算,即.

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))

这篇关于R的lm()公式的格式与转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆