R公式y〜1是什么意思? [英] What does the R formula y~1 mean?

查看:226
本文介绍了R公式y〜1是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 R公式上的文档,并试图弄清楚如何使用 depmix(来自depmixS4软件包).

现在,在depmixS4的文档中,样本公式倾向于类似于y ~ 1. 对于像y ~ x这样的简单情况,它定义了输入x和输出y之间的关系,因此我发现它类似于y = a * x + b,其中a是斜率,而b是截距.

如果我们回到y ~ 1,该公式会让我失望.等于y = 1(y = 1处的水平线)吗?

要添加一点上下文,请查看depmixs4文档,下面有一个示例

depmix(list(rt~1,corr~1),data=speed,nstates=2,family=list(gaussian(),multinomial()))

我认为一般来说,以~ 1结尾的公式令我感到困惑.可以解释~ 1y ~ 1是什么意思吗?谢谢一堆!

解决方案

R中的模型公式(星号,加号,插入号)中使用的许多运算符具有特定于模型的含义,这是其中之一:一个'符号表示截距.

换句话说,它是当自变量为零或没有影响时预期因变量具有的值. (要使用模型术语的更常见的数学含义,请将其包装在I()中).通常假定为拦截,因此最常见的情况是在没有拦截的情况下明确声明模型.

有两种方法可以为x上的y的线性回归模型指定相同的模型.第一个具有隐式拦截项,第二个具有显式拦截项:

y ~ x
y ~ 1 + x

以下是通过原点(即没有截距项)对x进行y线性回归的方法:

y ~ 0 + x
y ~ -1 + x
y ~ x - 1

在您提到(y〜1)的特定情况下,y是没有其他变量预测的,因此自然预测是y的平均值,如Paul Hiemstra所述:

> data(city)
> r <- lm(x~1, data=city)
> r

Call:
lm(formula = x ~ 1, data = city)

Coefficients:
(Intercept)  
       97.3  

> mean(city$x)
[1] 97.3

并使用-1删除截距将使您一无所有:

> r <- lm(x ~ -1, data=city)
> r

Call:
lm(formula = x ~ -1, data = city)

No coefficients

formula()是用于从对象中提取公式的函数,其帮助文件不是阅读有关在R中指定模型公式的最佳位置.我建议您查看 R简介.

I was reading the documentation on R Formula, and trying to figure out how to work with depmix (from the depmixS4 package).

Now, in the documentation of depmixS4, sample formula tends to be something like y ~ 1. For simple case like y ~ x, it is defining a relationship between input x and output y, so I get that it is similar to y = a * x + b, where a is the slope, and b is the intercept.

If we go back to y ~ 1, the formula is throwing me off. Is it equivalent to y = 1 (a horizontal line at y = 1)?

To add a bit context, if you look at the depmixs4 documentation, there is one example below

depmix(list(rt~1,corr~1),data=speed,nstates=2,family=list(gaussian(),multinomial()))

I think in general, formula that end with ~ 1 is confusing to me. Can any explain what ~ 1 or y ~ 1 mean? Thanks a bunch!

解决方案

Many of the operators used in model formulae (asterix, plus, caret) in R, have a model-specific meaning and this is one of them: the 'one' symbol indicates an intercept.

In other words, it is the value the dependent variable is expected to have when the independent variables are zero or have no influence. (To use the more common mathematical meaning of model terms, you wrap them in I()). Intercepts are usually assumed so it is most common to see it in the context of explicitly stating a model without an intercept.

Here are two ways of specifying the same model for a linear regression model of y on x. The first has an implicit intercept term, and the second an explicit one:

y ~ x
y ~ 1 + x

Here are ways to give a linear regression of y on x through the origin (that is, without an intercept term):

y ~ 0 + x
y ~ -1 + x
y ~ x - 1

In the specific case you mention ( y ~ 1 ), y is being predicted by no other variable so the natural prediction is the mean of y, as Paul Hiemstra stated:

> data(city)
> r <- lm(x~1, data=city)
> r

Call:
lm(formula = x ~ 1, data = city)

Coefficients:
(Intercept)  
       97.3  

> mean(city$x)
[1] 97.3

And removing the intercept with a -1 leaves you with nothing:

> r <- lm(x ~ -1, data=city)
> r

Call:
lm(formula = x ~ -1, data = city)

No coefficients

formula() is a function for extracting formula out of objects and its help file isn't the best place to read about specifying model formulae in R. I suggest you look at this explanation or Chapter 11 of An Introduction to R.

这篇关于R公式y〜1是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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