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

user defined functions in r
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

1

Quantitative Consulting Unit

User Defined Functions in R

David Luckett and Sharon Nielsen

10th December 2015

David Luckett and Sharon Nielsen — UDF

slide-2
SLIDE 2

2

Welcome

David Luckett and Sharon Nielsen — UDF

slide-3
SLIDE 3

3

Introduction

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

  • ne 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

slide-4
SLIDE 4

4

Function Form

Format

function.name <- function(arguments) { computations on the arguments some other code return }

David Luckett and Sharon Nielsen — UDF

slide-5
SLIDE 5

5

Plot Example

Fit a linear regression model to cars data

dat.lm <- lm(dist ~ speed, data = cars) plot(dat.lm)

David Luckett and Sharon Nielsen — UDF

slide-6
SLIDE 6

6

Plot Example

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

slide-7
SLIDE 7

7

Linear Model Residual Plot Function

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

slide-8
SLIDE 8

8

Using your function in R

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

slide-9
SLIDE 9

9

Changing existing functions

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

slide-10
SLIDE 10

10

What do these UDF’s do?

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

slide-11
SLIDE 11

11

A new function

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

slide-12
SLIDE 12

12

Using the function

Coefficient of variation

cvpc(mtcars[, "hp"]) cvpc(mtcars[, 6]) # Weight lapply(mtcars[,3:5], cvpc)

David Luckett and Sharon Nielsen — UDF

slide-13
SLIDE 13

13

In dplyr

Coefficient of variation

library(plyr) library(dplyr) mtcars %>% group_by(cyl) %>% summarise(cvpc(mpg))

David Luckett and Sharon Nielsen — UDF

slide-14
SLIDE 14

14

Extra arguments

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

slide-15
SLIDE 15

15

Returning output

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

slide-16
SLIDE 16

16

Returning output

return

mx <- function(xvar, ...) { xout <- mean(xvar) return(xout) } mx(xx) result <- mx(xx) result

David Luckett and Sharon Nielsen — UDF

slide-17
SLIDE 17

17

One-step solution

<< − “super gets”

mx <- function(xvar, ...) { xout <<- mean(xvar) } mx(xx) xout

David Luckett and Sharon Nielsen — UDF

slide-18
SLIDE 18

18

One-step solution (assign)

assign

mx <- function(xvar, ...) { xout <- mean(xvar) assign("result", xout, envir=.GlobalEnv) } mx(xx) result

David Luckett and Sharon Nielsen — UDF

slide-19
SLIDE 19

19

Return multiple values

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

slide-20
SLIDE 20

20

Sharon’s tips for writing functions

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

slide-21
SLIDE 21

21

Let’s give it a go...

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(SSresidual) + 2 × p (1) BIC = n × ln(SSresidual) + 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

slide-22
SLIDE 22

22

Picks and Tips

David Luckett and Sharon Nielsen — UDF

slide-23
SLIDE 23

23

Merry Christmas

David Luckett and Sharon Nielsen — UDF