Docsity

Dr. Colin Gillespie, Exams of Advanced Computer Programming

Partial preview of the text

Download Dr. Colin Gillespie and more Exams Advanced Computer Programming in PDF only on Docsity!

Early warning

systems

D E F E N S I V E R P R O G R A M M I N G Dr. Colin Gillespie Jumping Rivers

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

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. 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!

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

The message()

function

D E F E N S I V E R P R O G R A M M I N G Dr. Colin Gillespie Jumping Rivers

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

Telling packages to be quiet

Occasionally, packages can be a bit noisy Sometimes loading ggplot 2 , it presents a message Don't worry, we can tell it to be quiet suppressPackageStartupMessages(library("ggplot2"))

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

The warning()

function

D E F E N S I V E R P R O G R A M M I N G Dr. Colin Gillespie Jumping Rivers

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

When should you use a warning?

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

Stop (right now)

D E F E N S I V E R P R O G R A M M I N G Dr. Colin Gillespie Jumping Rivers

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