#Clear everything in the R console rm(list=ls(all = TRUE)) #Install needed packages install.packages("tseries") install.packages("astsa") install.packages("lmtest") install.packages("moments") install.packages("quantmod") install.packages("plyr") install.packages("Matrix") #--- Please select a CRAN mirror for use in this session --- library(quantmod) library(ggplot2) library(plyr) library(moments) #==============================================================# #Basic operations a <- 3 b <- 2 / 3 print(class(a)) print(a+b) print(a*b) print(a**2) print(a^2) print(paste0("a square is:", a**2)) print(as.integer(3.3)) print(class(as.integer(3.3))) print(class(as.double(3))) #Strings Str <- c('My', 'name', 'is') class(Str) print(Str[1]) Str2 <- c(Str, 'Name') Str2 <- append(Str, "Name") Str3 <- append(Str2, "first", after = 1) #Doubles Dou_1 <- c(1,2,3) Dou_2 <- c(1:10) Dou_3 <- 1:5 Even <- seq(from = 2, to = 30, by = 2) #Print Even divided by 2, multipied by 2, the first element of Even, the last element of Even # # # # print(Even / 2) print(Even*2) print(Even[1]) #first element print(Even[2:4]) #2nd, 3rd and 4th element of Even print(Even[-1]) #all but first element print(Even[-2]) #all but 2nd element print(Even[-c(1,3)]) #all but first and 3rd elements print(Even[length(Even)]) #Lists List <- list(1:10,2:5) print(List[1]) #1:10 print(List[[1]][1]) #1 c(List[1],List[2]) #or append(List[1], List[2]) #Sort the list in ascending and descending orders, using sort sort(unlist(List)) sort(unlist(List), decreasing = TRUE) #sort sorts the vector, order returns the indices of the vector in a sorted order sort(c(3, 1, 2, 5, 4)) #1 2 3 4 5 order(c(3, 1, 2, 5, 4)) #2 3 1 5 4 #Arrays Arr <- array(1:12, dim=c(3,4)) print(class(Arr)) #matrix Arr_Ones <- array(1:1, dim=c(3,4)) print( Arr * Arr_Ones) #Print the first row / column on Arr, the first 2 columns, the 1st and 3rd columns, the dimensions of Arr, add a column to Arr, sort Arr by increasing / decreasing order # # # # print(Arr[1,]) #First row of Arr print(Arr[,1]) #First column of Arr print(Arr[,-1]) #Arr minus 1st column print(Arr[,1:2]) #First two coolumns of Arr print(Arr[,c(1,3)]) #First and third column of Arr print(dim(Arr)) print(Arr[Arr>3]) #All elements of Arr >3 print(cbind(Arr, c(1,1,1))) #Add column print(rbind(Arr, c(1,1,1,1))) #Add row print(sort(Arr)) print(sort(Arr), decreasing = TRUE) print(Arr[sort(-Arr[,3]),]) #sort Arr by 3rd column decreasing order B <- matrix(c(2,4,6,1,3,5), nrow = 3, ncol = 2) #will fill by columns C <- matrix(c(2,4,6,1,3,5), nrow = 2, ncol = 3, byrow = TRUE) t(B) #transpose c(B) #to get all the elements of the matrix B #Get the rank of a matrix library(Matrix) rankMatrix(B)[1] #or qr(B)$rank qr(array(0:0, dim = c(2,2)))$rank #Calculate B squared, B multiplied by 2 and C divided by 2, Multiply the two matrices B and C # # # # print(B^2) print(2*B) print(C / 2) print(B %*% C) print(C %*% B) #Create identity matrix Id <- diag(nrow = 2, ncol = 2) #Calculate determinant det(Id) #Calculate inverse qr.solve(2*Id) #Sanity check: qr.solve(2*Id)*(2*Id) #==============================================================# #Generate random numbers and distributions #Generate random numbers runif(1) #between 0 and 1 runif(10) #generate 10 random numbers runif(3, min=0, max=100) #Generate random numbers following the Normal distribution rnorm(10,0,1) #(n,mean,var) #Generate 10000 rdm numbers following N(0,1) and plot their density # # # # plot(density(rnorm(10000,0,1))) #==============================================================# #Functions Multi <- function(a,b){ return(a*b) } nb = Multi(3,4) nb = Multi(1,2) #if statement if (nb>10){ print("Number greater than 10") } else{ print("Number smaller than 10") } #For loop summ = 0 for (i in 2:10) { summ = summ + i } print(summ) #Exercise: #Create a function to check if a number is prime or not # # # # isPrime <- function(n){ flag = 0 if (n>1) { flag = 1 for (i in 2:(n-1)) { if ((n %% i) == 0) { flag = 0 break } else{ flag = 1 } } } if (n ==2){ flag = 1 } if (flag == 1){ print(paste(n, "is a prime number")) } else{ print(paste(n, "is not a prime number")) } } isPrime(2) isPrime(10) isPrime(7) #==============================================================# #Import financial data from Yahoo Finance / Google getSymbols('MSFT',src='yahoo') MSFT #Gives Open, High, Low, Close prices, Volume and Adjusted Close price MSFT[,1] #gives the Open prices #Select time frame for Download #auto.assign=FALSE: the data will be returned from the call, and will require the user to assign the results himself MSFT <- getSymbols('MSFT',src='yahoo', from = "2018-01-01", to = Sys.Date(), auto.assign=FALSE) SP <- getSymbols('^GSPC',src='yahoo', from = "2018-01-01", to = Sys.Date(), auto.assign=FALSE) #Get monthly data (daily, weekly or monthly) MSFT <- getSymbols('MSFT',src='yahoo', from = "2018-01-01", to = Sys.Date(), periodicity = "monthly") #==============================================================# #Dealing with data #Nb of rows and columns nrow(MSFT) ncol(MSFT) #Visualise first few rows head(MSFT) tail(MSFT) print(index(MSFT)) plot.ts(MSFT[,1]) plot.ts(MSFT$MSFT.Open) #Rename columns colnames(SP)[1] <- "SP.Open" #Delete column with Adjusted prices MSFT <- MSFT[,-6] #or MSFT$MSFT.Adjusted <- NULL #Get number of missing values per column colSums(is.na(MSFT)) #Print summary statistics library(moments) summary(MSFT) mean(MSFT[,1], na.rm = TRUE) sd(MSFT[,1], na.rm = TRUE) #standard deviation skewness(MSFT, na.rm = TRUE) kurtosis(MSFT, na.rm = TRUE) #Delete missing data: data <- na.omit(MSFT) #backward fill - last observation carried forward data2 <- na.locf(MSFT) #forward fill - Forward observation carried backwards data3 <- na.locf(MSFT, fromlast = TRUE) #Plot the Open and Closing Prices in the same graph #Add legend, axis labels and title ggplot(MSFT, aes(index(MSFT), y = value, color = Variable)) + geom_line(aes(y = MSFT.Open, col = "Open prices")) + geom_line(aes(y = MSFT.Close, col = "Close prices")) + scale_color_manual(values = c("blue", "red")) + labs(title = "MSFT Prices", x = "Date", y = "Prices") #Plot the density of the Closing prices for MSFT #Toy example: generate 1000 rdm numbers following N(0,1) and plot their density plot(density(rnorm(1000,0,1))) #With financial data plot(density(MSFT[,1])) #==============================================================# #How to merge two dataframes by date, and put NA values when missing data in one dataframe library(zoo) print(class(MSFT)) print(class(SP)) #Already zoo data, can merge directly, otherwise need to convert to zoo first, using # MSFT <- read.zoo(MSFT) mergData <- merge(MSFT, SP) #merged Data by index, will put NA when to corresponding values #Convert to DataFrame Data <- as.data.frame(mergData) colnames(Data) #Scatter plot between SP and MSFT Closing Prices #ann = FALSE to remove the labels ggplot(Data, aes(x = Data$MSFT.Close, y = Data$GSPC.Close), ann = FALSE) + geom_point() #Save dataframe write.csv(Data, "/Users/Chloe/Downloads/Data.csv") #Load data dataset <- read.csv("/Users/Chloe/Downloads/Data.csv", header = TRUE) #==============================================================# #Download Microsoft and SP 500 data between 1/5/2019 and 30/9/2019 MSFT <- getSymbols('MSFT',src='yahoo', from = "2019-05-01", to ="2019-09-30", auto.assign=FALSE) SP <- getSymbols('^GSPC',src='yahoo', from = "2019-05-01", to = "2019-09-30", auto.assign=FALSE) #Calculate log Returns of MSFT Closing prices and plot them #Toy example Ex <- c(3:8) Diff_Ex <- diff(Ex,1) #first difference of elements diff(Ex, 2) #second difference of elements # # # # log_Rt <- diff(log(MSFT$MSFT.Close),1) head(log_Rt) plot(index(log_Rt), log_Rt, type = "l") #Add log_Rt to the mergData dataset and rename the column 'log_returns' # # # # mergBis <- merge(MSFT, log_Rt) colnames(mergBis)[ncol(mergBis)] <- "log_returns" head(mergBis) #Calculate lag of log_Rt # # # # head(lag(log_Rt,1))