[Rd] glm Argument-Evaluation Does Not Match Documentation.

Jorgen Harmse Jorgen@H@rm@e @ending from @@m@club@com
Fri Aug 3 18:22:14 CEST 2018

Details in documentation: "All of ‘weights’, ‘subset’, ‘offset’, ‘etastart’ and ‘mustart’ are evaluated in the same way as variables in ‘formula’, that is first in ‘data’ and then in the environment of ‘formula’."
In fact, `data` is usually not an environment, and I have not seen arguments evaluated in `environment(formula)` when `data` is provided. (Information in `environment(formula)` is used, so presumably they are evaluated in an environment whose parent is `environment(formula)`.)

R --vanilla

R version 3.4.3 (2017-11-30) -- "Kite-Eating Tree"
Copyright (C) 2017 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin15.6.0 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> environment(glm)
<environment: namespace:stats>
> maintainer('stats')
[1] "R Core Team <R-core using r-project.org>"
> df <- data.frame(x=1:3, y=c(pi,7,sqrt(2)), z=c(1,2.3,3))
> a <- 4:6; b <- 7:9
> lm <- glm(z ~ x+y, data=df, weights = a*b -> v)
> v # Check whether weights was evaluated in .GlobalEnv, the environment of the formula.
Error: object 'v' not found
> env.df <- as.environment(df); lm <- glm(z ~ x+y, data=env.df, weights = a*b -> v)
Error in list(z, x, y) : could not find function "list"
> parent.env(env.df) <- .GlobalEnv; lm <- glm(z ~ x+y, data=env.df, weights = a*b -> v)
> v
Error: object 'v' not found
> ls(env.df) # show that weights was evaluated in env.df
[1] "v" "x" "y" "z"

Suggested rewrite: "All of ‘weights’, ‘subset’, ‘offset’, ‘etastart’ and ‘mustart’ are evaluated in the same way as variables in ‘formula’. Values needed are found first in `data` and then usually in the environment of ‘formula’ & its ancestors. (If `data` is provided but is not an environment then expressions are evaluated in an environment with content taken from `data` whose parent is `environment(formula)`.) Side effects of evaluating the arguments will usually NOT occur in the caller's frame."
I think this is correct, but what really happens is inside other functions with unusual argument evaluation, so I'm not certain.

Jorgen Harmse
Sam’s Club Technology
Phone 512.633.2226 
jorgen.harmse using samsclub.com
This e-mail and any files transmitted with it are confidential and intended solely
for the individual or entity to whom they are addressed.  If you have received
this e-mail in error, destroy it immediately.  Wal-Mart Confidential.

More information about the R-devel mailing list