## 1. How to Install R?
## 2. Interface
## 3. Install Package
## 4. Basic Operators
## 5. Vector and matrix
## 6. Plot
## 7. OLS
## 8. R Resources (Useful Links)

## 4. Basic Operators
<-				# left assignment    
->				# right assignment
+				# plus
-				# minus
*				# multiplication
/				# division
^				#exponentiation
abs()                   # absolute value

# example
var1 <- 15			# var1 equals 15
var1				# shows the value of var1
1x <- 3			# error
15 -> var1              # var1 equals 15
var2 <-5			# var2 equals 5
x <-var1+var2
x           
x <-var1-var2
x
g <-(-3)
abs(g)
G <-5^2			# g is ddifferent from G
ls()                    # list all variables
rm(list=ls(all=T))      # delete all variables in memory


##5. Vector amd matrix
# Creating vectors
vec1 <-as.vector(seq(from =1, to=20, by=2))  #seq(from, to, by=)
vec1[5:7]                                    #the 5th element of vec1
length(vec1)                                 #returns the length of vec1

vec2 <-as.vector(seq(from=20, to=1, by=-2))
vec2[1]<-0
vec2

# Add a constant
vec1
vec1+1				    #add a constant to vec1 (all elements of vec1)
vec2
vec2*3                            #multiply by a constant (all elements of vec2)

#Addition
vec1+vec2                         

#Simple multiplication
vec1
vec2
vec1*vec2                         #element times element


# useful functions in R
vec1 %*% vec2                       #inner product: %*%
vec1 %o% vec2                       #outer product: %o%
max(vec1)                           
min(vec1)
which.max(vec1)                     #return the index of the max element
which.min(vec1)                     #return the index of the min element
sum(vec1)
mean(vec1)
median(vec1)
var(vec1)
sort(vec2)


# matrix
c(vec1, vec2)                       #combine values into a vector: c()
mtx1 <-cbind(vec1, vec2)            #combine by columns: cbind()
mtx2 <-rbind(vec1, vec2)            #combine by rows: rbind()
mtx1
mtx2
dim(mtx1)                           #returns the dimensions of mtx1 and mtx2: dim()     
dim(mtx2)
t(mtx1)                             #returns the transpose of mtx1: t()

mtx3 <-matrix(seq(1,6), ncol=2)     #create a matrix
mtx3[1:2,]
mtx4 <-matrix(0, ncol=2, nrow=3)     
mtx_I <-diag(1, nrow=5, ncol=5)     #itentity matrix

x<-matrix(c(1,2,3,4), ncol=2)
solve(x)                            #inverse of x: solve() 



#generate data from the distribution
x_unif <-runif(100, min=0, max=1)            #uniform distribution: runif(n, min, max), n is the number of data
x_norm <-rnorm(100, mean=0, sd=1)            #normal
x_t    <-rt(100, df=2)                                #Student-t

qnorm(p=0.95, mean=0, sd=1)


## 6. Plot
hist(x_t)
hist(x_t, breaks=seq(-10, 60, by=5))
hist(x_t, breaks=seq(-10, 60, by=1))


## 7. OLS
#Use R build in function lm()
# OLS
rm(list=ls(all=TRUE))
n = 20
x <-rnorm(n)
b1 <-2
b2 <-10
y <- b1 + b2*x + rnorm(n, 0, 1)
lm(y~x)
summary(lm(y~x))
b_hat_ols <-coef(lm(y~x))
plot(x, y)
abline(b_hat_ols[1], b_hat_ols[2], col = "red")

# Calculate OLS coefficients by ourselves
b <-t(matrix(c(b1, b2), ncol = 2))
x <-matrix(c(rep(1, n), x), ncol = 2)
xx_inv <-solve((t(x)%*%x))
b_hat <-xx_inv%*%t(x)%*%y


##   8. R Resources (Useful Links)
# http://www.statmethods.net/index.html
# http://rwiki.sciviews.org/doku.php?id=links%3Alinks
# google
# ?    

library(quantreg)









