R中的tilde(〜)运算符 [英] tilde(~) operator in R

查看:57
本文介绍了R中的tilde(〜)运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据R文档:公式中使用〜运算符来分隔公式的右侧和左侧.右侧是自变量,左侧是因变量.我知道lm()包中何时使用〜.但是,以下是什么意思?

According to the R documentation: ~ operator is used in formula to separate the right and left hand side of the formula. The right hand side is independent variable and the left hand side is dependent variable. I understand when ~ is used in lm() package. However what does following mean?

x~ 1

右侧是1.这是什么意思?可以是其他任何数字而不是1吗?

The right hand side is 1. what does it mean? Can it be any other number instead of 1?

推荐答案

来自?lm :

[..]当拟合线性模型y〜x-1时,将通过来源[..]

[..] when fitting a linear model y ~ x - 1 specifies a line through the origin [..]

公式中的-" 删除指定的术语.

The "-" in the formula removes a specified term.

所以 y〜1 只是一个具有常数(拦截)且没有回归变量的模型.

So y ~ 1 is just a model with a constant (intercept) and no regressor.

lm(mtcars$mpg ~ 1)
#Call:
#lm(formula = mtcars$mpg ~ 1)
#
#Coefficients:
#(Intercept)  
#      20.09  

可以是其他数字而不是1吗?

Can it be any other number instead of 1?

否,请尝试看看.

lm(mtcars $ mpg〜0)告诉R删除常量(等于 y〜-1 ),并删除 lm(mtcars $ mpg〜2)给出错误(正确).

lm(mtcars$mpg ~ 0) tells R to remove the constant (equal to y ~ -1), and lm(mtcars$mpg ~ 2) gives an error (correctly).

您应该在公式内将 y〜1 读为 y〜constant ,这不是一个简单的数字.

You should read y ~ 1 as y ~ constant inside the formula, it's not a simple number.

这篇关于R中的tilde(〜)运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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