-
Notifications
You must be signed in to change notification settings - Fork 51
/
030_functions.Rmd
executable file
·105 lines (82 loc) · 3.4 KB
/
030_functions.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<style>@import url(style.css);</style>
[Introduction to Data Analysis](index.html "Course index")
# 3. Functions
We saw a bit of calculus, logic and vector/matrix manipulation last week. This week, we thread on the same topics with slightly more advanced operators. Here, for instance, is the modulus operator in R:
```{r modulus}
4 %/% 3
6 %/% 3
```
Many matrix operators are also available: you might want to go back to the [cheat sheet](https://gastonsanchez.wordpress.com/2012/08/30/cheat-sheet-of-matrix-operations-in-r/) previously mentioned. Here are some basic manipulations on matrixes built out of random integers.
```{r matrix-ops}
# Create a random 3 x 5 matrix.
A <- matrix(as.integer(10*runif(30)), nrow = 3, ncol = 5)
# Check result.
A
# Create a random 2 x 2 (square) matrix.
B <- matrix(as.integer(10*runif(16)), nrow = 2, ncol = 2)
# Check result.
B
# Create another one.
C <- matrix(as.integer(10*runif(16)), nrow = 2, ncol = 2)
# Check result.
C
# Now a basic manipulation: scalar multiplication.
2*A
# Another one: extract the diagonal.
diag(B)
# Last one: matrix transposition.
t(C)
```
As an exercise, explain the result of a square matrix product.
```{r matrix-product}
# Square matrix multiplication.
B %*% C
```
## Functional programming
Suppose that you find yourself doing the same computation over and over again. You do not want to type the whole calculations every time you need: you want to program a function that does the job for you.
Take a basic example: the `sum` command adds a vector of numbers together.
```{r sum}
# Create a vector of 99 random [0,1] values.
x <- runif(9)
# Check result: show first 5 values.
head(x)
# Add them up.
sum(x)
```
Re-defining that function under a different name is trivial, except the example below will only accept two elements to show their sum, as defined by the primitive function `+`. The example is trivial insofar as functions are generally designed to capture more complex operations.
```{r add-function}
# Define function.
add <- function(x, y) {
x + y
}
# Example.
add(2, 4)
```
Statistical computing courses like [the one taught by Cosma Shalizi](http://www.stat.cmu.edu/~cshalizi/statcomp/) contains tons of interesting examples of such functions. A simple function from that course is shown below to illustrate the principle of recursion in computer code:
```{r crs-factorial}
# Calculate a factorial
# Input: a number (n)
# Output: the factorial of n
# Presumes: n is a single positive integer
my.factorial <- function(n) {
if (n == 1) { # Base case
return(1)
} else { # Recursion
return(n*my.factorial(n-1))
}
}
```
## Plotting functions
This course might lead you to write simple functions, but for the moment, let's focus simply on plotting them with the `ggplot2` package, which makes it fairly easy. The example below shows the basic function $y = x$.
```{r fun-identity}
qplot(c(0,2), stat="function", fun = identity, geom = "point")
```
Here's a function that will be more useful to us: the exponential.
```{r fun-exp}
qplot(-10:10, stat="function", fun = exp, geom = c("line", "point"))
```
And finally an example of several embedded functions: the plot first generates a random distribution of $N = 1000$ observations and then draws its empirical cumulative distribution function (ECDF), to which we will come back in due time.
```{r fun-ecdf}
qplot(rnorm(1000), stat = "ecdf", geom = "step")
```
> __Next__: [Control flow](031_control.html).