R 波浪号运算符 : What does ~0+a means?

标签 r

我已经看到如何在公式中使用 ~ 运算符。例如 y~x意味着:y 分布为 x。

但是我真的很困惑 ~0+a在这段代码中意味着:

require(limma)
a = factor(1:3)
model.matrix(~0+a)

为什么只是 model.matrix(a)不起作用?为什么model.matrix(~a)的结果不同于 model.matrix(~0+a) ?最后这里的 ~ 运算符是什么意思?

最佳答案

~创建一个公式 - 它将公式的左右两边分开

来自 ?`~`

Tilde is used to separate the left- and right-hand sides in model formula



引用公式帮助

The models fit by, e.g., the lm and glm functions are specified in a compact symbolic form. The ~ operator is basic in the formation of such models. An expression of the form y ~ model is interpreted as a specification that the response y is modelled by a linear predictor specified symbolically by model. Such a model consists of a series of terms separated by + operators. The terms themselves consist of variable and factor names separated by : operators. Such a term is interpreted as the interaction of all the variables and factors appearing in the term.

In addition to + and :, a number of other operators are useful in model formulae. The * operator denotes factor crossing: a*b interpreted as a+b+a:b. The ^ operator indicates crossing to the specified degree. For example (a+b+c)^2 is identical to (a+b+c)*(a+b+c) which in turn expands to a formula containing the main effects for a, b and c together with their second-order interactions. The %in% operator indicates that the terms on its left are nested within those on the right. For example a + b %in% a expands to the formula a + a:b. The - operator removes the specified terms, so that (a+b+c)^2 - a:b is identical to a + b + c + b:c + a:c. It can also used to remove the intercept term: when fitting a linear model y ~ x - 1 specifies a line through the origin. A model with no intercept can be also specified as y ~ x + 0 or y ~ 0 + x.



所以关于 ~a+0 的具体问题
  • 您创建一个没有截距的模型矩阵。如 a是一个因子,model.matrix(~a)将返回一个拦截列,即 a1 (您需要 n-1 指标才能完全指定 n 类)

  • 每个功能的帮助文件都写得很好,很详细,很容易找到!

    为什么不model.matrix(a)工作
    model.matrix(a)不起作用,因为 afactor变量,而不是公式或术语对象

    来自 model.matrix 的帮助

    object an object of an appropriate class. For the default method, a model formula or a terms object.


    R通过传递公式 ~a 正在寻找特定类别的对象您正在传递一个类 formula 的对象. model.matrix(terms(~a))也可以,(传递对应于公式 ~a 的术语对象)

    一般说明

    @BenBolker 在他的评论中很有帮助地指出,这是 Wilkinson-Rogers 符号的修改版本。

    the Introduction to R中有很好的描述.

    关于R 波浪号运算符 : What does ~0+a means?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12737783/

    相关文章:

    r - 使用 groupby 另一列填充缺失日期

    r - 如何更有效地使用map_dfr()对数据集进行filter()?

    r - dplyr:如何处理多个值

    r - 合并多个data.table

    r - 从 Googlesheets 导入时,返回一个我无法在 R 中转换为日期的列表

    在 R 中重新排序和 reshape 列

    r - 在表达式中添加竖线以绘图

    r - 使用 ggplot2 从列表中绘制数据

    r - 检查 R 中李克特量表的有效值

    r - rpart.plot 中的颜色节点按颜色匹配节点列表