user defined functions in r
play

User Defined Functions in R David Luckett and Sharon Nielsen 10 th - PowerPoint PPT Presentation

1 Quantitative Consulting Unit User Defined Functions in R David Luckett and Sharon Nielsen 10 th December 2015 David Luckett and Sharon Nielsen UDF Welcome 2 David Luckett and Sharon Nielsen UDF Introduction 3 A function is a


  1. 1 Quantitative Consulting Unit User Defined Functions in R David Luckett and Sharon Nielsen 10 th December 2015 David Luckett and Sharon Nielsen — UDF

  2. Welcome 2 David Luckett and Sharon Nielsen — UDF

  3. Introduction 3 A function is a piece of code written to carry out a specified task; it may accept arguments or parameters (or not) and it may return one or more values (or not!). ◮ Something you do repeatedly ◮ Automation ◮ Less typing ◮ Reduces errors ◮ Cleaner script files ◮ Share with others ◮ Stores the ‘complexity’ ◮ Eventually make your own package David Luckett and Sharon Nielsen — UDF

  4. Function Form 4 Format function.name <- function(arguments) { computations on the arguments some other code return } David Luckett and Sharon Nielsen — UDF

  5. Plot Example 5 Fit a linear regression model to cars data dat.lm <- lm(dist ~ speed, data = cars) plot(dat.lm) David Luckett and Sharon Nielsen — UDF

  6. Plot Example 6 Checking the model assumptions qqnorm(dat.lm$residuals, new = FALSE) qqline(dat.lm$residuals, new = FALSE) hist(dat.lm$residuals, xlab = "Residuals", main = "Histogram") plot(dat.lm$residuals ~ dat.lm$fitted.values, xlab = "Fitted", ylab = "Residuals", main = "Residual Plot") plot(dat.lm, which = 4, id.n = 0) David Luckett and Sharon Nielsen — UDF

  7. Linear Model Residual Plot Function 7 Making a function lmresplot <- function(asrobject = dat.lm){ par(mfrow = c(2, 2)) qqnorm(asrobject$residuals, new = FALSE) qqline(asrobject$residuals, new = FALSE) hist(asrobject$residuals, xlab = "Residuals", main = "Histogram") plot(asrobject$residuals ~ asrobject$fitted.values, xlab = "Fitted", ylab = "Residuals", main = "Residual Plot") plot(asrobject, which = 4, id.n = 0) } David Luckett and Sharon Nielsen — UDF

  8. Using your function in R 8 You need to make sure that your function is in R’s memory before you try to use it. List R Objects ls() This lists all the objects in R’s memory. Alternatively, in R Studio, check the Functions window. Run your function lmresplot() David Luckett and Sharon Nielsen — UDF

  9. Changing existing functions 9 head() function head(cars) Returns the first 6 rows in the cars dataframe. Change existing function h <- function(d) {head(d, n=3)} Now it returns the first three rows only. Using the function h(cars) NOTE: To see the source code of any function, just type its name. David Luckett and Sharon Nielsen — UDF

  10. What do these UDF’s do? 10 now() now <- function() {format(Sys.time(), "%I:%M %p")} na count na_count <- function(x) {sum(is.na(x))} David Luckett and Sharon Nielsen — UDF

  11. A new function 11 CV% = (standard deviation / mean) x 100 Coefficient of variation cvpc <- function(x) { (sd(x, na.rm=TRUE)/ mean(x, na.rm=TRUE))*100 } David Luckett and Sharon Nielsen — UDF

  12. Using the function 12 Coefficient of variation cvpc(mtcars[, "hp"]) cvpc(mtcars[, 6]) # Weight lapply(mtcars[,3:5], cvpc) David Luckett and Sharon Nielsen — UDF

  13. In dplyr 13 Coefficient of variation library(plyr) library(dplyr) mtcars %>% group_by(cyl) %>% summarise(cvpc(mpg)) David Luckett and Sharon Nielsen — UDF

  14. Extra arguments 14 The “...” construct allows any other valid arguments to be passed to your new function. Try this h(mtcars, n=5) “...” h <- function(d, ...) {head(d, ...)} h(mtcars, n=5) David Luckett and Sharon Nielsen — UDF

  15. Returning output 15 It is all about ‘environments’. A simple example xx <- c(6,9,2,3,12) mx <- function(xvar, ...) { xout <- mean(xvar) } mx(xx) xout is not found outside function. David Luckett and Sharon Nielsen — UDF

  16. Returning output 16 return mx <- function(xvar, ...) { xout <- mean(xvar) return(xout) } mx(xx) result <- mx(xx) result David Luckett and Sharon Nielsen — UDF

  17. One-step solution 17 << − “super gets” mx <- function(xvar, ...) { xout <<- mean(xvar) } mx(xx) xout David Luckett and Sharon Nielsen — UDF

  18. One-step solution (assign) 18 assign mx <- function(xvar, ...) { xout <- mean(xvar) assign("result", xout, envir=.GlobalEnv) } mx(xx) result David Luckett and Sharon Nielsen — UDF

  19. Return multiple values 19 The conventional way to return multiple values is to bundle them into a list. Example addsub <- function(x, y) { list(add=(x + y), sub=(x - y)) } David Luckett and Sharon Nielsen — UDF

  20. Sharon’s tips for writing functions 20 Tips and tricks 1. Write simple R code first 2. Check that it works exactly the way you intended it to work 3. Work out the information that the code needs to work - these things form the arguments in your function 4. Work out what objects (if any) need to be returned from the function 5. Wrap your code in a function 6. Test and check the function David Luckett and Sharon Nielsen — UDF

  21. Let’s give it a go... 21 AIC and BIC We want to create a function that returns the AIC and the BIC for a linear model that we have fitted: AIC = n × ln ( SS residual ) + 2 × p (1) BIC = n × ln ( SS residual ) + ln ( n ) × p (2) Start by writing the code to do these calculations (Hint: use the linear model dat.lm that we used earlier in this session) David Luckett and Sharon Nielsen — UDF

  22. Picks and Tips 22 David Luckett and Sharon Nielsen — UDF

  23. Merry Christmas 23 David Luckett and Sharon Nielsen — UDF

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend