Here is an (artificial) example.
chamber_sizes <- mtcars$disp/mtcars$cyl form <- hp ~ chamber_sizes model <- lm(form, data = mtcars) print(model) # Call: # lm(formula = form, data = mtcars) # # Coefficients: # (Intercept) chamber_sizes # 2.937 4.104Notice: one of the variables came from a vector in the environment, not from the primary
data.frame
.chamber_sizes
was first looked for in thedata.frame
, and then in the environment theformula
was defined (which happens to be the global environment), and (if that hadn’t worked) in the executing environment (which is again the global environment).Our advice is: do not do that. Place all of your values in columns. Make it unambiguous all variables are names of columns in your
data.frame
of interest. This allows you to write simple code that works over explicit data. The style we recommend looks like the following.
Read the whole thing.