Stream will start soon
Data analysis using R
Course overview and introduction
15.04.2021
Danny Arends
Fachgebiet Züchtungsbiologie und molekulare Tierzüchtung
Humboldt-Universität zu Berlin
Overview
General course announcements
Introduction into R
Some assignments to practice
General course announcements
Slides will be made available online on Moodle
Please check if you can see the course on Moodle, if not contact me
Please attend the lecture on twitch stream
Lectures are supported by practical exercises, spend some time on
them, if you get stuck: send me an email
ZooM meeting every week?
Exam date unknown at this point in time:
Form: Online exam by written examination
General course announcements
Questions ?
Who followed a statistics courses ?
Who has any programming experience ?
What do you want to learn ?
What you will be able to do after this
course
Format your data for R
Load your data in R
Do statistics on your data
Interpret the results
Create nice plots
Suitable for publication
Create your own analysis
‘Publish’ this as an R package for others
What you will be able to do after this
course
Analyze your own data
Line and dot plots
Boxplot
Histogram
Heatmap
Contour
And many more
What you will be able to do after this
course
Analyze your own data
Line and dot plots
Boxplot
Histogram
Heatmap
Contour
And many more
What you will be able to do after this
course
Analyze your own data
Line and dot plots
Boxplot
Histogram
Heatmap
Contour
And many more
What you will be able to do after this
course
Analyze your own data
Line and dot plots
Boxplot
Histogram
Heatmap
Contour
And many more
Introduction into R
15.04.2021
Danny Arends
Fachgebiet Züchtungsbiologie und molekulare Tierzüchtung
Humboldt-Universität zu Berlin
Overview
History
Look and Feel of R
R as a calculator
Types
Indexing
Variables
Scripts
Why history
Why history in an R course ?
“We are like dwarfs on the shoulders of giants, so that we
can see more than they, and things at a greater distance . .
. we are carried high and raised up by their giant size”
- John of Salisbury
From: [Link]
History
2400 BC Abakus (Babylon)
1110 BC Differential gears / south-pointing chariot (China)
200 BC Chinese Abakus (China)
120 BC Antikythera mechanism (Greece / Korinthe)
Abacus, public domain by its author, HB Antikythera mechanism, public domain by its author
History
100 Astrolabe (Unknown)
800 Crypto, frequency analysis (Alkindus, Irak)
1206 Castle clock (Al-Jazari, Mosul, Irak)
1694 Leibniz wheel (Gottfried W. Leibniz, Germany)
“it is beneath the dignity of excellent men to waste their
time in calculation when any peasant could do the work
just as accurately with the aid of a machine.”
- Gottfried Leibniz
History
• Charles Babbage
– 1791 - 1871
– Analytical Engine
• Ada Lovelace
– 1815 - 1852
– First algorithm
• Alan M. Turing
– 1912 - 1954
– Turing Machine
History
Build May 1941
Z3 - The first computer
Konrad Zuse
Wing flutter
Destroyed 1943
(1) Zuse Z1 replica in the German Museum of Technology in Berlin
(2) Zuse Z3-replica in het Deutsches Museum in München
(3) Konrad Zuse working in his lab (unknown)
History
ENIAC – Electronic Numerical Integrator And Computer
Electronic general-purpose computer
Turing-complete
Digital
Capable of being reprogrammed
loops, branches and subroutines
Ballistics tables
Programmers Betty Jean Jennings (left) and Fran Bilas
(right) operate ENIAC's main control panel at the Moore
School of Electrical Engineering. (U.S. Army photo from
the archives of the ARL Technical Library)
History
Von Neumann architecture
1945 - John von Neumann
(1) von Neumann Architecture - licensed under the Creative Commons Attribution-Share Alike 3.0
(2) Los Alamos: Beginning of an era, 1943-1945, Los Alamos Scientific Laboratory, 1986
Present / Future
1976 Quantum information theory
Roman Stanisław Ingarden
1985 Universal quantum computer
David Deutsch
1991 Entanglement secure communication
2011 D-Wave One (128 qubits)
2012 Quantum Artificial Intelligence Lab D-
(512 qubits) NASA, Google, D-Wave
2020 D-Wave Advantage™ (>5000 qubits)
D-Wave One (128 qubits)
Discrete optimization
$10.000.000
ENIAC then: $500,000
now: $6,100,000
Lattice protein folding
D-Wave Systems, Inc. - D-Wave Systems, Inc., CC BY 3.0
D-Wave Advantage
Menten AI
Protein design
Volkswagen
Paint shop scheduling
Save-On-Foods
Grocery optimization
Amazon
Free access for CoVID19 researchers
D-Wave Systems, Inc. - D-Wave Systems, Inc., CC BY 3.0
History - programming languages
Low level
Punch cards
Assembly
Mid level
− C - Dennis Ritchie and Ken Thompson (69 - 73)
High Level
Plankalkül - Konrad Zuse (43 - 45)
AutoCode - Alick Edwards Glennie (50s)
Algol - Committee of American and European computer scientists (Late
50s)
LISP - John McCarthy (58)
COBOL - Grace Hopper (59)
Too many to count, so why R?
From: [Link]
The R programming language
Free and Open-Source
Statistical computing
Graphics
Written in:
R, Fortran, C
Operating System agnostic
Linear and non-linear models
Build-in testing and help
Many add-on packages available: 4000+
Look and Feel
R as a calculator
We can use R as a basic calculator:
So typing in: 1 + 4 will yield 5
5 / 10 will yield 0.5
Some special operations:
Exponents: 5 ^ 2 or 5 ** 2
Euclidean division %/%
Euclidean division remainder %%
Some special numerical constants:
Inf = infinite, NaN = Not A Number, NA = missing
Euclidean division
100 divided by 39
39 / 100 \
Euclidean division
39 / 100 \ 2 Euclidean divisor
78
---
22 Euclidean division remainder
R as a calculator
Build in character constants:
LETTERS
The 26 upper-case letters of the Roman alphabet
letters
The 26 lower-case letters of the Roman alphabet
[Link]
The three-letter abbreviations for the English month names
[Link]
The English names for the months of the year
pi
The ratio of the circumference of a circle to its diameter
R as a calculator
Imaginary numbers
sqrt(-1) = NaN, but sqrt(-1+0i) = 0+1i
Additionally basic trigonometry functions
sin, cos, tan, asin, acos, atan
Logarithms
log(5) natural logarithm of 5
log10(5) base 10 logarithm of 5
exp(1) e^1
Order of operations
Operator precedence
exponents and roots
multiplication and division
addition and subtraction
PEMDAS
Please Excuse My Dear Aunt Sally
10 - 3 x 2 = ???
Break
Short break
Your session
Everything in R is in RAM memory
Functions to manage your session:
getwd() # Where does R save/retrieve files ?
dir() # What files are there ?
setwd() # Set another place to save/retrieve files
ls() # What is in the current environment ?
[Link]("qtl") # Install package
library(qtl) # Load a package
save(object, file = "[Link]") # Save an object as binary
[Link]("[Link]") # Save the whole environment
q("no") # quit the R session
Help is available !
?functionname # Open the help file for function
??term # Search for a term across
?seq # Open the help for the seq function
?"+" # The + function help page
??obesity # Search anything obesity in R
Types of data
Logical
TRUE, FALSE
Numeric
5, 7.9, 100.6
Character
"one", "two", "three"
Vector
v1 <- c(1, 2, 5.3, 6, -2,4) # Numeric vector
v2 <- c("one", "two", "three") # Character vector
v3 <- c(TRUE, TRUE, TRUE, FALSE, TRUE) # Logical vector
Matrix
Y <- matrix(1:20, nrow = 5, ncol = 4) # 5 x 4 numeric matrix
Working with types
Some useful functions to deal with types
length(object) # Number of elements
str(object) # Structure of an object
class(object) # Class or type of an object
names(object) # Names
as.<typename> # Force to a certain type
is.<typename> # Is of a certain type ?
Creating vectors & matrices
Vector
c(object, object, ...) # Combine objects into a vector
seq(from, to, by) # A numerical sequence
rep(object, times) # Repeat an object / number
Matrix
matrix(vector, nrow, ncol) # Create a matrix from a vector
cbind(object, object, ...) # Combine objects as columns
rbind(object, object, ...) # Combine objects as rows
Creating vectors & matrices
Vector
V1 <- 1:4 # 1, 2, 3, 4
V2 <- seq(1, 100, 7) # 1, 8, 15, .. 99
V3 <- rep(1, 4) # 1, 1, 1, 1
V4 <- rep("A", 4) # A, A, A, A
Matrix
Y1 <- matrix(1:20, nrow = 5, ncol = 4) # 5 x 4 numeric matrix
Y2 <- cbind(V1,V2) # 4 x 2 numeric matrix
Y3 <- rbind(V1,V2) # 2 x 4 numeric matrix
Vector and Matrix - Example
Vector
Index by R
Matrix
Indexing a vector
Indexing the a vector called v:
v[5]
v[2:5]
v[c(2:5,8)]
Indexing a matrix
m[1:3, 1]
m[5, 3:6]
m[8, 7]
m[ , 9]
Again: Types of data
Logical
TRUE, FALSE
Numeric
5, 7.9, 100.6
Character
"one", "two", "three"
Vector
a <- c(1, 2, 5.3, 6, -2,4) # Numeric vector
b <- c("one", "two", "three") # Character vector
c <- c(TRUE, TRUE, TRUE, FALSE, TRUE) # Logical vector
Matrix
Y <- matrix(1:20, nrow = 5, ncol = 4) # 5 x 4 numeric matrix
Advanced Types
Data Frame (Not a matrix, can contain multiple basic types)
v1 <- c(1, 2, 3, 4)
v2 <- c("red", "white", "red", NA)
v3 <- c(TRUE, TRUE, TRUE, FALSE)
d <- [Link](v1, v2, v3)
List (Not a vector, can contain anything)
w <- list(name="Fred", numbers = v1, age = 5.3)
Factor
gender <- factor(c(rep("male",20), rep("female", 30)))
Comments (use me often)
# This is a comment, R will ignore this
Quick Type Test
What is the type of:
"TRUE"
"1"
1e+11
0x89
#FF00AA
FALSE
[Link](TRUE)
[Link](1e+11)
Indexing a List: [[ ]]
w <- list(name = "Fred",
numbers = c(1, 2, 3, 4),
age = 5.3,
matrix = matrix(c(1,0,0,1), nrow = 2, ncol = 2))
w[[1]][1]
w$numbers[c(2,3)]
w[[4]][,1]
w$matrix[1,]
Matrix and [Link] functions
nrow(matrix) # Number of rows
ncol(matrix) # Number of columns
rownames(matrix) # Names of the rows
colnames(matrix)
rownames(matrix) <- c("A", "B", "C") # Set row names
colnames(matrix) <- c("A", "B", "C") # Set column names
# Transpose matrix
t(matrix)
Variables
Variables are boxes
You can put things in (use: <- or = )
You can use the box without knowing what is in it
We have already seen them:
variables <- 1.5
can <- c(TRUE, FALSE)
have <- c(1, 2, 3, 4)
many <- c(FALSE, FALSE)
names <- -5
Code clean = create scripts
Use a new directory for a new lecture
Every week, use a new file
Name the files in a logical way
Add a header (comment section) to each file
Name
Date
Purpose of the file
Copyright
Use a LOT of comments
An example
# Analysis of Hardy-Weinberg equilibrium
#
# copyright (c) 2015 - HU Berlin
# written by: Danny Arends
#
# last modified Apr, 2015
# first written Feb, 2015
setwd("D:/Rcourse/assignments")
Use a good text editor
Windows Notepad++
OSX Textwrangler
Linux Use what you want
Most important
Code highlighting
Support for bracket testing
Remember
Clean code = smart code
Questions ?
?
Assignments
Available from Moodle