getwd() # Show working directory # Set the working directory # setwd("D:/Programming Guide/R Guide/code") # getwd() # Set the working directory setwd("D:/Programming Guide/code") getwd() ### 2.1 Packages # Use the readxl library to get the data library (readxl) ### 2.2 Importing data UKHP <- read_excel("D:/Programming Guide/data/UKHP.xls ", col_types = c("date", "numeric")) # Show the data View (UKHP) # Data classes class(UKHP) # Variable names names(UKHP) # First 6 observations head(UKHP) # Last 6 observation tail(UKHP) ### 2.3 Data description # Summary statistics summary(UKHP) x = mean(UKHP$'Average House Price') x x <- mean(UKHP$'Average House Price') x ### 2.4 Changing and creating data names(UKHP)[2] = "hp" names(UKHP) # Writing a new variable; return dhp UKHP$dhp = c(NA,100*diff(UKHP$hp)/UKHP$hp[1:nrow(UKHP)-1]) head(UKHP$dhp) dhp = 100*diff(UKHP$hp)/UKHP$hp[1:nrow(UKHP)-1] head(dhp) ### 2.5 Graphs and plots # Time series plot par(cex.axis = 1.5,cex.lab = 1.5,lwd = 2) plot(UKHP$Month,UKHP$hp,type = 'l',xlab ="Date",ylab ="House price",main = "Average house price") # Histogram hist(UKHP$hp) box() ### 2.6 Keeping track of your work ### 3 Linear regression SandPhedge <- read_excel("D:/Programming Guide/data/SandPhedge.xls", col_types = c("date","numeric","numeric")) View(SandPhedge) names(SandPhedge) head(SandPhedge) tail(SandPhedge) # Create log returns SandPhedge$rspot = c(NA,100*diff(log(SandPhedge$Spot))) SandPhedge$rfutures = c(NA,100*diff(log(SandPhedge$Futures))) summary(SandPhedge[c("rspot","rfutures")]) lm_returns = lm(rspot ~ rfutures, data = SandPhedge) summary(lm_returns ) lm_prices = lm(Spot ~ Futures, data = SandPhedge ) summary (lm_prices)