0

Can someone explain why the following code in R gives different results depending on whether = or <- is used to assign the steps argument:

library(MASS)
quine.hi <- aov(log(Days + 2.5) ~ .^4, quine)
quine.nxt <- update(quine.hi, . ~ . - Eth:Sex:Age:Lrn)

quine.stp <- stepAIC(quine.nxt,
                     scope = list(upper = ~Eth*Sex*Age*Lrn, lower = ~1),
                     trace = TRUE,
                     steps = 6)

quine.stp_2 <- stepAIC(quine.nxt,
                       scope = list(upper = ~Eth*Sex*Age*Lrn, lower = ~1),
                       trace = TRUE,
                       steps <- 6)
Cœur
  • 37,241
  • 25
  • 195
  • 267
yesbut
  • 5
  • 2

1 Answers1

4

<- and = are not interchangeable in function calls. It should be surprising when the results are the same, not different. = is used to name parameters in function class; -> does not provide names.

This means that when you call

stepAIC(quine.nxt,
  scope = list(upper = ~Eth*Sex*Age*Lrn, lower = ~1),
  trace = TRUE,
  steps <- 6)

that 4th parameter is not named. It's evaluated and just returns 6 (the -> operator returns the RHS value as a return value) and is treated as an unnamed parameter. Unnamed parameters are passed in the order in which they are received to a function after plugging in the all the named values. The stepAIC function signature is

stepAIC(object, scope, scale ,
    direction, trace, keep , steps , use.start ,k = 2, ...)

So since you've specified scope= and trace=, the value of 6 will go into the scale= parameter. So what you've written is the same as

stepAIC(quine.nxt,
  scope = list(upper = ~Eth*Sex*Age*Lrn, lower = ~1),
  trace = TRUE,
  6)

which is the same as

stepAIC(quine.nxt, 
  scope = list(upper = ~Eth*Sex*Age*Lrn, lower = ~1),
  scale = 6,
  trace = TRUE)

So the important message is that <- should only be used for assignment, and = should only be used to name parameters in function calls.

MrFlick
  • 195,160
  • 17
  • 277
  • 295