在 R 编程语言中使用 ~(波浪号) [英] Use of ~ (tilde) in R programming Language
问题描述
我在有关回归建模的教程中看到以下命令:
I saw in a tutorial about regression modeling the following command:
myFormula <- Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width
这个命令到底是做什么的,命令中~
(波浪号)的作用是什么?
What exactly does this command do, and what is the role of ~
(tilde) in the command?
推荐答案
<-
右边的东西是一个 formula
对象.它常被用来表示一个统计模型,其中~
左边的东西是响应,而~
右边的东西是解释变量.所以在英语中,你会说物种取决于萼片长度、萼片宽度、花瓣长度和花瓣宽度".
The thing on the right of <-
is a formula
object. It is often used to denote a statistical model, where the thing on the left of the ~
is the response and the things on the right of the ~
are the explanatory variables. So in English you'd say something like "Species depends on Sepal Length, Sepal Width, Petal Length and Petal Width".
该行的 myFormula <-
部分将公式存储在名为 myFormula
的对象中,以便您可以在 R 代码的其他部分使用它.
The myFormula <-
part of that line stores the formula in an object called myFormula
so you can use it in other parts of your R code.
R 中公式对象的其他常见用途
lattice
包使用它们来指定要绘制的变量.ggplot2
包使用它们来指定绘图面板.dplyr
包将它们用于 non-标准评价.
The lattice
package uses them to specify the variables to plot.
The ggplot2
package uses them to specify panels for plotting.
The dplyr
package uses them for non-standard evaulation.
这篇关于在 R 编程语言中使用 ~(波浪号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!