earl y w arning s y stems
play

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


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

  2. 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 ts T / F for TRUE / FALSE DEFENSIVE R PROGRAMMING

  3. Problem 1: TRUE and FALSE TRUE and FALSE are special v al u es We can ' t o v erride them TRUE <- 5 # Error in TRUE <- 5 : invalid (do_set) left-hand side to assignment DEFENSIVE R PROGRAMMING

  4. Problem 2: TRUE and FALSE S u ppose w e are w orking o u t an F - statistic . It w o u ld be nat u ral to ha v e # df is the F-density function (F <- df(1, 9, 67)) [1] 0.7798 B u t R treats positi v e n u mbers as TRUE , so if(F) message("Yer aff yer heid!") Yer aff yer heid! F is no w treated as TRUE ! DEFENSIVE R PROGRAMMING

  5. Get in the habit of u sing TRUE and FALSE not T and F If y o u testing for TRUE , u se isTRUE() isTRUE(T) [1] TRUE isTRUE(2) [1] FALSE T <- 10 isTRUE(T) [1] FALSE DEFENSIVE R PROGRAMMING

  6. Let ' s ha v e a little practice D E FE N SIVE R P R OG R AMMIN G

  7. The message () f u nction D E FE N SIVE R P R OG R AMMIN G Dr . Colin Gillespie J u mping Ri v ers

  8. The message () f u nction Signals to the u ser the state of a process This isn ' t an error - it ' s j u st helpf u l information For e x ample , s u ppose y o u' re r u nning cross -v alidation , then o u tp u t co u ld be CV 1 of 10 complete CV 2 of 10 complete CV 3 of 10 complete DEFENSIVE R PROGRAMMING

  9. We can t u rn it o � w ith 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 DEFENSIVE R PROGRAMMING

  10. Telling packages to be q u iet Occasionall y, packages can be a bit nois y Sometimes loading ggplot 2 , it presents a message Don ' t w orr y, w e can tell it to be q u iet suppressPackageStartupMessages(library("ggplot2")) DEFENSIVE R PROGRAMMING

  11. Using message () The message() f u nction is helpf u l for le � ing y o u and other u sers kno w w hat ' s happening . It ' s v er y hand y for long r u nning processes DEFENSIVE R PROGRAMMING

  12. Let ' s do some w ork ! D E FE N SIVE R P R OG R AMMIN G

  13. The w arning () f u nction D E FE N SIVE R P R OG R AMMIN G Dr . Colin Gillespie J u mping Ri v ers

  14. The w arning message The warning() f u nction warning("You have been warned!") # Warning message: # You have been warned! signals that something ma y ha v e gone w rong R contin u es (u nlike an error ) " Warning message :" is ( pre ) appended DEFENSIVE R PROGRAMMING

  15. S u ppress Warnings Similar to messages , y o u can s u ppress w arnings v ia suppressWarnings()` This is almost ne v er a good idea Fi x the u nderl y ing problem ! DEFENSIVE R PROGRAMMING

  16. When sho u ld y o u u se a w arning ? DEFENSIVE R PROGRAMMING

  17. A good u se of w arning S u ppose w e ' re performing regression on d = data.frame(y = 1:4, x1 = 1:4) d$x2 = d$x1 + 1 So x2 = x1 + 1 When w e � t a m u ltiple 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 DEFENSIVE R PROGRAMMING

  18. Yo u r t u rn D E FE N SIVE R P R OG R AMMIN G

  19. Stop ( right no w) D E FE N SIVE R P R OG R AMMIN G Dr . Colin Gillespie J u mping Ri v ers

  20. I sa w the sign Sometimes things are j u st broken We need to raise an error For e x ample : 1 + "stuff" Error in 1 + "stuff": non-numeric argument to binary operator DEFENSIVE R PROGRAMMING

  21. Stop right no w thank y o u v er y m u ch To raise an error , w e u se the stop() f u nction 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) } DEFENSIVE R PROGRAMMING

  22. Catch em w hile y o u can Yo u can ' t s u ppress ( or ignore ) errors The de � nition of an error is that R can ' t contin u e Instead , w e catch errors DEFENSIVE R PROGRAMMING

  23. The tr y() f u nction The try() f u nction acts a bit like suppress() res <- try("Scotland" + "World cup", silent = TRUE) It tries to e x ec u te something , if it doesn ' t w ork , it mo v es on DEFENSIVE R PROGRAMMING

  24. The tr y() 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> DEFENSIVE R PROGRAMMING

  25. The tr y() idiom result <- try("Scotland" + "World cup", silent = TRUE) class(result) [1] "try-error" if(class(result) == "try-error") ## Do something useful DEFENSIVE R PROGRAMMING

  26. Let ' s practice D E FE N SIVE R P R OG R AMMIN G

Recommend


More recommend