Chapter 17 Danger Zone
little things to include somewhere: - the danger of misspecified arguments disappearing into ...
don’t give your model lm
class and count on other stuff to magically work. your should implement methods specifically for your class, and if that is just wrapping lm internals, great, but take explicit control
provide an example of extensive formula tests: include a log term in tests to catch formula edge cases
17.1 Warnings / places to use care
model.frame() is not the be all end all
double evaluation issue in
fit <- lm(hp ~ log(mpg), mtcars)
predict(fit, newdata = model.frame(fit))
## Error in eval(predvars, data, env): object 'mpg' not found
don’t do predict(object, newdata = model.frame(object)) since this will breaaaaaaaak
17.2 Anti-patterns
17.2.1 Using the default method of a generic
i.e. funneling everything into confint.default
. Default methods for new generics should throw an error.
What do to about existing generics?
Key principle here: want to guarantee to the user that they are getting the right numbers
don’t funnel everything through augment_columns and add special cases slowly - v hard to maintain. much better to have individualized S3 methods with consistent behavior abstracted out into small helpers.
related idea to the belong: have enough classes! using inheritance appropriately.
17.2.2 the documentation that isn’t documentation and doesn’t feature an actual use case
- cough
?MASS::predict.rlm
cough - misisng doc
?MASS:::predict.polr()
17.2.3 Never use missing arguments
because people have to write more code to pass the objects they want
predict(m) and predict(m, newdata = NULL) should do the same thing you should test this
17.2.4 special casing everything through one workhorse function instead of using S3 methods
basically augment_columns
don’t funnel everything through augment_columns and add special cases slowly - v hard to maintain. much better to have individualized S3 methods with consistent behavior abstracted out into small helpers.
17.3 Things to be aware of
If you call things data
or df
and users fail to specify data arguments, R will try and perform dataframe operations on the functions data
and df
. The resulting error messages for this can be cryptic. In this case you may wish to write an informative error message with a hint:
Error: Can't *do data frame thing* on a function.
Are you sure you passed a tibble to *argument*?