## RUSIS lab 2

### Section 1: Review Material
# Subsection 1.2: R

x <- c(0, 5, 12)
odd <- seq(1, 20, 2)
digits10 <- 0:9
seven <- rep(7, 20)

v <- c(x, odd, digits10, seven)

A <- matrix(c(1, 2, 4, 5, 6, 7, 2, 4, 9), 3, 3, byrow = T)
B <- matrix(c(2, 1, 0, 1, 2, 1, 0, 1, 2), 3, 3, byrow = T)

A %*% B
B %*% A
solve(A)
solve(A %*% B)

### Section 2: R - Subsetting
# Subsection 2.1: Vectors

length(v)
v[12]
v[c(3, 6, 25)]
v[c(3, 6, 47)]
v[3:20]
v[seq(1, 43, 2)]

v[-12]
v[-c(3, 6, 25)]
v[-c(3, 6, 47)]
v[-(3:20)] # We need to put parenthesis all around 2:20 
	     # to indicate the exception of all entries.
	     # Otherwise we are saying,
	     # Extract entries from -3rd trhough 20th which make no sense. 

v[-seq(1, 43, 2)]


# Subsection 2.2: Matrices

A[2, 2]
A[3, ]
A[ , 1]
(A[3, ])[1]
(A[3, ])[-1]
A[ , -2]
(A[ , -2])[c(1, 3)]

B[c(1, 3), ]
B[c(2, 3), c(2, 3)]

A[2, ] %*% B[ , 2]

?Extract

### Section 3: Packages in R

?install.packages
install.packages('ggplot2')

??ggplot2
?ggplot

library(ggplot2)
?ggplot


### Section 4: ggplot2

mpg
?mpg
dim(mpg)
head(mpg) # by default we only see the first 6 rows
head(mpg, 10) # we can choose how many rows we want to see, i.e. 10
tail(mpg)

plot(mpg[ ,3], mpg[ ,9])
dev.new()
qplot(mpg[ ,3], mpg[ ,9])


# Subsection 4.1: geoms

qplot(mpg[ ,3], mpg[ ,9], geom = 'point')
qplot(mpg[ ,3], mpg[ ,9], geom = 'line')
qplot(mpg[ ,3], mpg[ ,9], geom = 'smooth')
qplot(mpg[ ,3], mpg[ ,9], geom = 'density2d')
qplot(mpg[ ,3], mpg[ ,9], geom = c('smooth', 'point'), method = 'lm')

# from ?qplot
qplot(displ, hwy, data = mpg, colour = class)


### Subsection 5.1: Binomial Distribution

n <- 1000
x <- 0:n

qplot(x,dbinom(x,size  = n, p = .05))
qplot(x,dbinom(x,size  = n, p = .25))
qplot(x,dbinom(x,size  = n, p = .50))
qplot(x,dbinom(x,size  = n, p = .75))
qplot(x,dbinom(x,size  = n, p = .95))

qplot(x,dbinom(x,size  = n, p = .50), xlim = c(425,575))

samps <- rbinom(100, size = 100, prob = .5)
samps

qplot(samps, geom = 'histogram')
qplot(samps, geom = 'histogram', binwidth = .05)
qplot(samps, geom = 'histogram', binwidth = .5)
qplot(samps, geom = 'histogram', binwidth = 2.5)
qplot(samps, geom = 'histogram', binwidth = 5)
qplot(samps, geom = 'histogram', binwidth = 15)

hist(samps, breaks = "Scott")

x <- 0:25
qplot(x, dpois(x, lambda = 1))
qplot(x, dpois(x, lambda = 5))
qplot(x, dpois(x, lambda = 10))


## 1000 samples

lambda <- 1
samps <- rpois(1000, lambda = 1)
qplot(samps, geom = 'histogram')
qplot(samps, geom = 'histogram', binwidth = .05)
qplot(samps, geom = 'histogram', binwidth = .5)
qplot(samps, geom = 'histogram', binwidth = 2.5)
qplot(samps, geom = 'histogram', binwidth = 5)

hist(samps, breaks = "Scott")
qplot(samps, geom = 'histogram', binwidth = .5)

lambda <- 5
samps <- rpois(1000, lambda = 5)
qplot(samps, geom = 'histogram')
qplot(samps, geom = 'histogram', binwidth = .05)
qplot(samps, geom = 'histogram', binwidth = .5)
qplot(samps, geom = 'histogram', binwidth = 2.5)
qplot(samps, geom = 'histogram', binwidth = 5)

hist(samps, breaks = "Scott")
qplot(samps, geom = 'histogram', binwidth = 1)

lambda <- 10
samps <- rpois(1000, lambda = 10)
qplot(samps, geom = 'histogram')
qplot(samps, geom = 'histogram', binwidth = .05)
qplot(samps, geom = 'histogram', binwidth = .5)
qplot(samps, geom = 'histogram', binwidth = 2.5)
qplot(samps, geom = 'histogram', binwidth = 5)

hist(samps, breaks = "Scott")
qplot(samps, geom = 'histogram', binwidth = 1)


## 50 samples

lambda <- 1
samps <- rpois(50, lambda = 1)
qplot(samps, geom = 'histogram')
qplot(samps, geom = 'histogram', binwidth = .05)
qplot(samps, geom = 'histogram', binwidth = .5)
qplot(samps, geom = 'histogram', binwidth = 2.5)
qplot(samps, geom = 'histogram', binwidth = 5)

hist(samps, breaks = "Scott")
qplot(samps, geom = 'histogram', binwidth = 1)

lambda <- 5
samps <- rpois(50, lambda = 5)
qplot(samps, geom = 'histogram')
qplot(samps, geom = 'histogram', binwidth = .05)
qplot(samps, geom = 'histogram', binwidth = .5)
qplot(samps, geom = 'histogram', binwidth = 2.5)
qplot(samps, geom = 'histogram', binwidth = 5)

hist(samps, breaks = "Scott")
qplot(samps, geom = 'histogram', binwidth = 2)

lambda <- 10
samps <- rpois(50, lambda = 10)
qplot(samps, geom = 'histogram')
qplot(samps, geom = 'histogram', binwidth = .05)
qplot(samps, geom = 'histogram', binwidth = .5)
qplot(samps, geom = 'histogram', binwidth = 2.5)
qplot(samps, geom = 'histogram', binwidth = 5)

hist(samps, breaks = "Scott")
qplot(samps, geom = 'histogram', binwidth = 2)