Earl y w arning s y stems D E FE N SIVE R P R OG R AMMIN G Dr . - - PowerPoint PPT Presentation

earl y w arning s y stems
SMART_READER_LITE
LIVE PREVIEW

Earl y w arning s y stems D E FE N SIVE R P R OG R AMMIN G Dr . - - PowerPoint PPT Presentation

Earl y w arning s y stems D E FE N SIVE R P R OG R AMMIN G Dr . Colin Gillespie J u mping Ri v ers Earl y w arning s y stems A v oid problems w here possible Handle iss u es as the y arise in a sensible w a y As an e x ample , u sing the shortc u


slide-1
SLIDE 1

Early warning systems

D E FE N SIVE R P R OG R AMMIN G

  • Dr. Colin Gillespie

Jumping Rivers

slide-2
SLIDE 2

DEFENSIVE R PROGRAMMING

Early warning systems

Avoid problems where possible Handle issues as they arise in a sensible way As an example, using the shortcuts T / F for TRUE / FALSE

slide-3
SLIDE 3

DEFENSIVE R PROGRAMMING

Problem 1: TRUE and FALSE

TRUE and FALSE are special values We can't override them

TRUE <- 5 # Error in TRUE <- 5 : invalid (do_set) left-hand side to assignment

slide-4
SLIDE 4

DEFENSIVE R PROGRAMMING

Problem 2: TRUE and FALSE

Suppose we are working out an F-statistic. It would be natural to have

# df is the F-density function (F <- df(1, 9, 67)) [1] 0.7798

But R treats positive numbers as TRUE , so

if(F) message("Yer aff yer heid!") Yer aff yer heid!

F is now treated as TRUE !

slide-5
SLIDE 5

DEFENSIVE R PROGRAMMING

Get in the habit of using TRUE and FALSE not T and F If you testing for TRUE , use isTRUE()

isTRUE(T) [1] TRUE isTRUE(2) [1] FALSE T <- 10 isTRUE(T) [1] FALSE

slide-6
SLIDE 6

Let's have a little practice

D E FE N SIVE R P R OG R AMMIN G

slide-7
SLIDE 7

The message() function

D E FE N SIVE R P R OG R AMMIN G

  • Dr. Colin Gillespie

Jumping Rivers

slide-8
SLIDE 8

DEFENSIVE R PROGRAMMING

The message() function

Signals to the user the state of a process This isn't an error - it's just helpful information For example, suppose you're running cross-validation, then output could be

CV 1 of 10 complete CV 2 of 10 complete CV 3 of 10 complete

slide-9
SLIDE 9

DEFENSIVE R PROGRAMMING

We can turn it o with suppressMessages()

noisy = function(a, b) { message("I'm doing stuff") a + b } noisy(1, 2) I'm doing stuff # [1] 3 suppressMessages(noisy(1, 2)) # [1] 3

slide-10
SLIDE 10

DEFENSIVE R PROGRAMMING

Telling packages to be quiet

Occasionally, packages can be a bit noisy Sometimes loading ggplot2, it presents a message Don't worry, we can tell it to be quiet

suppressPackageStartupMessages(library("ggplot2"))

slide-11
SLIDE 11

DEFENSIVE R PROGRAMMING

Using message()

The message() function is helpful for leing you and other users know what's happening. It's very handy for long running processes

slide-12
SLIDE 12

Let's do some work!

D E FE N SIVE R P R OG R AMMIN G

slide-13
SLIDE 13

The warning() function

D E FE N SIVE R P R OG R AMMIN G

  • Dr. Colin Gillespie

Jumping Rivers

slide-14
SLIDE 14

DEFENSIVE R PROGRAMMING

The warning message

The warning() function

warning("You have been warned!") # Warning message: # You have been warned!

signals that something may have gone wrong R continues (unlike an error) "Warning message:" is (pre) appended

slide-15
SLIDE 15

DEFENSIVE R PROGRAMMING

Suppress Warnings

Similar to messages, you can suppress warnings via

suppressWarnings()`

This is almost never a good idea Fix the underlying problem!

slide-16
SLIDE 16

DEFENSIVE R PROGRAMMING

When should you use a warning?

slide-17
SLIDE 17

DEFENSIVE R PROGRAMMING

A good use of warning

Suppose we're performing regression on

d = data.frame(y = 1:4, x1 = 1:4) d$x2 = d$x1 + 1

So x2 = x1 + 1 When we t a multiple linear regression model

m = lm(y ~ x1 + x2, data = d) summary(m) # Some output removed # Warning message: # In summary.lm(m) : essentially perfect fit: summary may be unreliable

slide-18
SLIDE 18

Your turn

D E FE N SIVE R P R OG R AMMIN G

slide-19
SLIDE 19

Stop (right now)

D E FE N SIVE R P R OG R AMMIN G

  • Dr. Colin Gillespie

Jumping Rivers

slide-20
SLIDE 20

DEFENSIVE R PROGRAMMING

I saw the sign

Sometimes things are just broken We need to raise an error For example:

1 + "stuff" Error in 1 + "stuff": non-numeric argument to binary operator

slide-21
SLIDE 21

DEFENSIVE R PROGRAMMING

Stop right now thank you very much

To raise an error, we use the stop() function

stop("A Euro 1996 error has occurred") # Error: A Euro 1996 error has occurred conf_int <- function(mean, std_dev) { if(std_dev <= 0) stop("Standard deviation must be positive") c(mean - 1.96 * std_dev, mean - 1.96 * std_dev) }

slide-22
SLIDE 22

DEFENSIVE R PROGRAMMING

Catch em while you can

You can't suppress (or ignore) errors The denition of an error is that R can't continue Instead, we catch errors

slide-23
SLIDE 23

DEFENSIVE R PROGRAMMING

The try() function

The try() function acts a bit like suppress()

res <- try("Scotland" + "World cup", silent = TRUE)

It tries to execute something, if it doesn't work, it moves on

slide-24
SLIDE 24

DEFENSIVE R PROGRAMMING

The try() idiom

res <- try("Scotland" + "World cup", silent = TRUE) res [1] 'Error in "Scotland" + "World cup": non-numeric argument to binary operator' attr(,"class") [1] "try-error" attr(,"condition") <simpleError in "Scotland" + "World cup": non-numeric argument to binary operator>

slide-25
SLIDE 25

DEFENSIVE R PROGRAMMING

The try() idiom

result <- try("Scotland" + "World cup", silent = TRUE) class(result) [1] "try-error" if(class(result) == "try-error") ## Do something useful

slide-26
SLIDE 26

Let's practice

D E FE N SIVE R P R OG R AMMIN G