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 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
Function Form 4 Format function.name <- function(arguments) { computations on the arguments some other code return } David Luckett and Sharon Nielsen — UDF
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
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
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
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
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
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
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
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
In dplyr 13 Coefficient of variation library(plyr) library(dplyr) mtcars %>% group_by(cyl) %>% summarise(cvpc(mpg)) David Luckett and Sharon Nielsen — UDF
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
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
Returning output 16 return mx <- function(xvar, ...) { xout <- mean(xvar) return(xout) } mx(xx) result <- mx(xx) result David Luckett and Sharon Nielsen — UDF
One-step solution 17 << − “super gets” mx <- function(xvar, ...) { xout <<- mean(xvar) } mx(xx) xout David Luckett and Sharon Nielsen — UDF
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
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
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
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
Picks and Tips 22 David Luckett and Sharon Nielsen — UDF
Merry Christmas 23 David Luckett and Sharon Nielsen — UDF
Recommend
More recommend