Meaning of "I" in the R Language

Naturally, I had little success searching Google for "R I", "I in R", and "R language I".

  • The R help says "Change the class of an object to indicate that it should be treated ‘as is’.".
  • My O'Reilly R book doesn't have an entry for it in its index.
  • My Cambridge book essentially says, about "I(logdist^2)": "ensures that is taken as the square, rather than as an interaction of logdist".

Can someone explain the "interaction" comment? Can someone explain why "logdist^2" wouldn't be interpreted in the traditional way?

5

2 Answers

From p89 of R in a Nutshell

Caret(^) [is] Used to indicate crossing to a specific degree. For example:

y~(u+w)^2 

is equivalent to

y~(u+w)*(u+w) 

Identity function (I()) Used to indicate that the enclosed expression should be interpreted by it's arithmetic meaning. For example

a+b 

means that both a and b should be included in the formula. The formula:

I(a+b) 

means that "a plus b" should be included in the formula. See also ?AsIs()

I think your confusion is that I is very rarely used as a standalone operator. As the help page states, it's most often used to stop operator characters (^,+,*, etc) from being interpreted as they are in a formula . As user2633645 's answer says, these characters have specific meanings in a formula . Quoting from the help page for stats::formula ,

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: ab 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. While formulae usually involve just variable and factor names, they can also involve arithmetic expressions. The formula log(y) ~ a + log(x) is quite legal. When such arithmetic expressions involve operators which are also used symbolically in model formulae, there can be confusion between arithmetic and symbolic operator use. To avoid this confusion, the function I() can be used to bracket those portions of a model formula where the operators are used in their arithmetic sense. For example, in the formula y ~ a + I(b+c), the term b+c is to be interpreted as the sum of b and c.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like