1. The warning() function
As we've seen, the message() function provides useful feedback to the user. The
warning function is a little different.
2. The warning message
The warning function has three key properties.
First it signals that something may have gone wrong.
Second, R proceeds to the next command. As we'll later see, this is different to
an error, which causes everything to stop.
Third, the phrase "Warning message" is added to the message.
3. Suppress Warnings
Helpfully, it follows a similar naming procedure to message. We have a
suppressWarnings() function.
But be warned (no pun intended). I've found that it's almost always better to
fix the underlying issue than simply suppress the warning.
4. When should you use a warning?
The obvious question is, when should we use a warning instead of a message?
5. A good use of warning
Suppose we have a simple data set. A sample size of 4, with three variables
y, x1 and x2. However, the variable x2 is a linear combination of x1. That is,
x2 is simply x1 plus 1.
If we try and fit a multiple linear regression model,
we get a warning message. We've been able to fit the model, but something isn't quite
right.
This is good use of the warning function!
6. Your turn
Enough of me. Your turn.