R 公式中的星号 (*) 与冒号 (:) [英] Asterisk (*) vs. colon (:) in R formulas

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

问题描述

在 R 公式中添加交互项时,我一直认为 *: 的含义相同.例如:

I always thought that * and : meant the same thing when adding interaction terms in R formulas. For example:

  • amount_of_gas ~ temperature*gas_type
  • amount_of_gas ~ temperature:gas_type

但是,现在我已经开始使用广义线性模型(R 中的 glm()),当我在两者之间切换时,我发现这些模型会生成不同的分数、不同的估计等.有人可以向我解释为什么会发生这种情况吗?R 中的 stats 包有问题吗?

However, now that I've started using Generalized Linear Models (glm() in R) I see that these generate different scores, different estimates, etc. when I switch between the two. Can someone explain to me why this happens? Is it a problem with the stats package in R?

##更新##2021 年 4 月 26 日

##UPDATE## April 26th, 2021

我问这个问题已经有大约 4.5 年了,我一直收到通知说它仍然有很多流量.这是简短的答案:y~x*z 基本上意味着:y~x+z+x:z 而 y~x:z 只是 x 和 z 的相互作用(如下面的答案所述)

It's been ~4.5 years or so since I asked this question and I keep getting notified that it still has a lot of traffic. Here's the short answer: y~x*z basically means: y~x+z+x:z while y~x:z is just the interaction of x and z (as described in the answer below)

推荐答案

来自help(formula):

 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’.

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

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