Advanced
programming
Lecture 4
Måns Magnusson
Statistics and Machine learning
Department of computer and information science
Since last time?
sheldon_game <- function(player1, player2){
alt <- c("rock", "lizard", "spock", "scissors", "paper")
stopifnot(player1 %in% alt, player2 %in% alt)
alt1 <- which(alt %in% player1)
alt2 <- which(alt %in% player2)
if(any((alt1 + c(1,3)) %% 5 == alt2)) {
return("Player 1 wins!")
} else {
return("Player 2 wins!")
}
return("Draw!")
}
Linear algebra
using R
Linear algebra in R
Advanced R Programming
Måns Magnusson
Basics in base
Uses LINPACK or LAPACK
Extra functionality : Matrix package
(extra LAPACK functionality)
Linear algebra
Advanced R Programming
Måns Magnusson
# Create matrix
A <- matrix(1:9,ncol=3)
# Block matrices
cbind(A,A)
rbind(A,A)
# Transpose
t(A)
# Addition and subtraction
A + A
A - A
# Matrix multiplication
A%*%A
# Matrix inversion
solve(A)
Linear algebra
Advanced R Programming
Måns Magnusson
# Eigenvalues
eigen(A)
# Determinants
det(A)
# Matrix factorization
svd(A)
qr(A)
# Cholesky decomposition
chol(A)
Dynamic reporting
with knitr and R-markdown
Advanced R Programming
Måns Magnusson
Let us change our traditional attitude to the construction of programs: Instead of imagining that our main task is to instruct a computer what to do, let us concentrate rather on explaining to humans what we want the computer to do.
- Donald E. Knuth, Literate Programming, 1984
Background
Advanced R Programming
Måns Magnusson
Reproducible research
Literate programming
Dynamic (repeated) reports
(Tutorials)
markdown
Advanced R Programming
Måns Magnusson
simple markup language
alternative to HTML (and LaTeX)
developed further by R-studio
(see coursepage)
knitr + md = rmd
Advanced R Programming
Måns Magnusson
Add R to markdown
knitr
.rmd
.md
pandoc
ggplot2
background
Advanced R Programming
Måns Magnusson
popular visualization package
"The grammar of graphics"
- the language of visualization
flexible
the grammar
Advanced R Programming
Måns Magnusson
Three (main) parts:
Create a graph layer by layer
data
geom
aes
The data to visualize (data.frame)
The geometric representation of data
The mapping of colors/shape to data
Store as object (print to plot)
geom
Advanced R Programming
Måns Magnusson
geom_point
Scatterplots
geom_line
Lineplots
geom_boxplot
Boxplot
geom_histogram
Histograms
geom_bar
Barchart
aes
Advanced R Programming
Måns Magnusson
x
y
size
color
shape
aes
Advanced R Programming
Måns Magnusson
geom_point
geom_line
geom_bar
geom
Special
aes
Special
point shape, point size
line type, line size
y min, y max, fill color, outline color
Example
Advanced R Programming
Måns Magnusson
library(ggplot2)
# Preprocessing
data(Nile)
Nile <- as.data.frame(Nile)
colnames(Nile) <- "level"
Nile$years <- 1871:1970
Nile$period <- "- 1900"
Nile$period[Nile$years >= 1900] <- "1900 - 1945"
Nile$period[Nile$years > 1945] <- "1945 + "
Nile$period <- as.factor(Nile$period)
Example
Advanced R Programming
Måns Magnusson
pl <-
ggplot(data=Nile) +
aes(x=years, y=level) +
geom_point()
pl
Example
Advanced R Programming
Måns Magnusson
pl <-
ggplot(data=Nile) +
aes(x=years, y=level) +
geom_line()
pl
Example
Advanced R Programming
Måns Magnusson
pl <-
ggplot(data=Nile) +
aes(x=years, y=level, color=period) +
geom_line(aes(type=period)) +
geom_point(aes(shape=period))
pl
Example
Advanced R Programming
Måns Magnusson
pl + theme_bw()
Object orientation
Object orientation
Advanced R Programming
Måns Magnusson
Programming paragdigm
Mutable states
class
Account
instance/object
Måns
Lisa
Constructor
withdraw
insert
methods
Object orientation
Advanced R Programming
Måns Magnusson
Måns account
currency (12/24) : class variable
current_amount : object variable
no_withdraws : object variable
Fields
Methods
insert()
withdraw()
Object orientation
Advanced R Programming
Måns Magnusson
Savings account
Account
inherits
methods
from
Inheritance
Object orientation in R
Advanced R Programming
Måns Magnusson
S3
S4
RC
Simple
Methods belongs to functions
More formal
Methods belongs to functions
Fields @
Parents
Latest (R 2.12)
no copy-on-modify
Methods belongs to objects
Fields and methods $
S3
Advanced R Programming
Måns Magnusson
# Create object
x <- 1:100
class(x) <- "my_numeric"
# Create method
print.my_numeric <- function(x, ...){
cat("This is my numeric vector.")
}
# Create generic function
f <- function(x) UseMethod("f")
RC
Advanced R Programming
Måns Magnusson
# Create object with fields and methods
Account <- setRefClass("Account",
fields = list(balance = "numeric"),
methods = list(
withdraw = function(x) {
balance <<- balance - x
},
deposit = function(x) {
balance <<- balance + x
}
)
)
object$copy()
Advanced R - Lecture 4
By monsmagn
Advanced R - Lecture 4
Lecture 4 in the course Advanced R programming at Linköping University.
- 1,324