Professional Documents
Culture Documents
Programming
Computational Economics Practice
Winter Term 2015/16
Stefan Feuerriegel
Today’s Lecture
Objectives
Advanced R 2
Outline
1 Visualization
2 Control Flow
3 Timing
4 Wrap-Up
Advanced R 3
Outline
1 Visualization
2 Control Flow
3 Timing
4 Wrap-Up
Advanced R: Visualization 4
Point Plot
I Creating simple point plots (also named scatter plots) via plot(...)
I Relies upon vectors denoting the x-axis and y-axis locations
I Various options can be added to change appearance
●
30
d$age
26
●
22
●
●
Advanced R: Visualization
d$height 5
Adding Title, Labels and Annotations
I Title is added through additional parameter main
I Axis labels are set via xlab and ylab
I Annotations next to points with text(...)
plot(d$height, d$age,
main="Title", # title for the plot
xlab="Height", ylab="Age") # labels for x and y axis
text(d$height, d$age, d$name) # d$name are annotations
Title
Jerry
●
30
Age
26
Robin
●
Julia
●
22
Max
●
Kevin
●
Height
Advanced R: Visualization 6
Line Plot
Generate line plot using the additional option type="l"
5
0
0 1 2 3 4
Advanced R: Visualization 7
Exercise: Plotting
Question
I How would you reproduce the above plot?
I plot(x, kink(x), type="l", main="")
Advanced R: Visualization 8
3D Plots
I Consider the function f (x ,y ) = x 3 + 3y − y 3 − 3x
f <- function(x, y) x^3+3*y-y^3-3*x
I Create axis ranges for plotting
x <- seq(-5, 5, 0.1)
y <- seq(-5, 5, 0.1)
I Function outer(x,y,f) evaluates f all combinations of x and y
z <- outer(x, y, f)
Advanced R: Visualization 9
3D Plots
Function persp(...) plots the plane through x, y and z in 3D
persp(x, y, z)
z
Advanced R: Visualization x 10
3D Plots
Turn on ticks on axes via ticktype="detailed"
persp(x, y, z, ticktype="detailed")
200
100
0
z
4
2
−100 0
y
−2
−200−4
Advanced R: Visualization−4 −2 0 2 4 11
x
3D Plots
Parameters theta (left/right) and phi (up/down) control viewing angle
y
y
x
x
Advanced R: Visualization 12
Contour Plots
I A contour line is a curve along which the function has the same value
I image(...) plots a grid of pixels colored corresponding to z-value
I contour(..., add=TRUE) adds contour lines to an existing plot
0 −100
−15
4
−50
0
2
0
0
y
−2
0
0
−4
0 50
100 150
Advanced R: Visualization 13
Contour Plots
Question
6 6
0.2
0.6
16
16
12
12
8
8
4
4
4
−0.6
−0.2
4
−0.8
−0.4
0
0
0
y
y
1
2
3
−4
−4
−4
0.4
0.8
14
14
10
10
6
6
5 6
−6 −2 2 6 −6 −2 2 6 −6 −2 2 6
x x x
##
## Call:
## lm(formula = rate ~ len + slim, data = Highway1)
##
## Coefficients:
## (Intercept) len slim
## 16.61050 -0.09151 -0.20906
Advanced R: Visualization 15
Plotting a Regression Plane
res <- persp(x=x1seq, y=x2seq, z=z,
theta=50, phi=-10)
dp <- trans3d(Highway1$len, Highway1$slim,
Highway1$rate, pmat=res)
points(dp, pch=20, col="red")
●
●
●
●
●
●
● ●
● ● ● ●
● ● ●
●●● ●
z
●● ●●● ●● ● ●
● ● ● ●
●
●● ● ●
●
●
x1se
q x2seq
Advanced R: Visualization 16
Outline
1 Visualization
2 Control Flow
3 Timing
4 Wrap-Up
Main Program
Function
## [1] 100
## [1] "world"
## [1] 50.5
x <- "A"
g <- function(x) {
x <- "B"
return(x)
}
x <- "C"
I What are the values?
g(x) # Return value of function x
x # Value of x after function execution
I Solution
## [1] "B"
## [1] "C"
x <- "A"
g <- function(x) {
x <- "B"
return(x)
}
x <- "C"
I What are the values?
g(x) # Return value of function x
x # Value of x after function execution
I Solution
## [1] "B"
## [1] "C"
f <- expression(x^3+3*y-y^3-3*x)
f
## expression(x^3 + 3 * y - y^3 - 3 * x)
I If evaluation of certain parameters becomes necessary, one can use
eval(...)
x <- 2
y <- 3
eval(f)
## [1] -16
True False
Condition
Block 1 Block 2
grade <- 2
ifelse(grade <= 4, "Passed", "Failed")
## [1] "Passed"
I ifelse(...) can also work with vectors as if it was applied to each
element separately
grades <- c(1, 2, 3, 4, 5)
ifelse(grades <= 4, "Passed", "Failed")
## [1] "Passed" "Passed" "Passed" "Passed" "Failed"
Conditional Code
If condition
is true
Condition
If condition
is false
I Examples
z <- 1 z <- 1
# same behavior as for loop # iterates all odd numbers
while (z <= 4) { while (z <= 5) {
print(z) z <- z + 2
z <- z + 1 print(z)
} }
## [1] 1 ## [1] 3
## [1] 2 ## [1] 5
## [1] 3 ## [1] 7
## [1] 4
Advanced R: Control Flow 33
Outline
1 Visualization
2 Control Flow
3 Timing
4 Wrap-Up
Advanced R: Timing 34
Measuring Timings via Stopwatch
I Efficiency is a major issue with larger datasets and complex codes
I Timings can help in understanding scalability and bottlenecks
I Use a stopwatch approach measuring the duration between two
proc.time() calls
g <- rnorm(100000)
h <- rep(NA, 100000)
for (i in 1:100000) { # Loop over vector, always add +1
h[i] <- g[i] + 1
}
Advanced R: Timing 35
Measuring Timings via Stopwatch
I Results of duration have the following format
## user system elapsed
## 0.71 0.02 0.72
I Timings are generally grouped into 3 categories
I User time measures the understanding of the R instructions
I System time measures the underlying execution time
I Elapsed is the difference since starting the stopwatch (= user +
system)
I Alternative approach avoiding loop
start.time <- proc.time() # Start clock
g <- rnorm(100000)
h <- g + 1
proc.time() - start.time # Stop clock
## user system elapsed
## 0.08 0.00 0.08
system.time(slowloop(1:1000000))
Advanced R: Timing 37
Outline
1 Visualization
2 Control Flow
3 Timing
4 Wrap-Up
Advanced R: Wrap-Up 38
Fancy Diagrams with ggplot2
library(ggplot2)
0.5 ● Type
3.0
● 2.5
2.0
0.0
Axis2
● 1.5
1.0
−0.5 Plant
● Plant1
Plant2
Advanced R: Wrap-Up 40
Summary: Programming
Advanced R: Wrap-Up 41
Outlook
Additional Material
Future Exercises
R will be used to implement optimization algorithms
Advanced R: Wrap-Up 42