###
### Data Modelling: Linear Regression
### Script for lesson #6
### Data Science Financial Club KNEU
### Lecturer: Anna Kornyliuk
### Date: 2017-09-19
###

# Source 1: 

# Set working Directory
setwd("D:/PROJECT/R/DSCF/lesson6") # тут треба змінити шлях до файла на СВІЙ !!!


# install.packages("tidyverse")
?summary

summarylibrary(tidyverse)
library(ggplot2)
library(ggthemes)
# Get quantmod
if (!require("quantmod")) {
        install.packages("quantmod")
        library(quantmod)
}


###----
# Download data
###----

# set the period
start <- as.Date("2015-01-01")
end <- as.Date("2017-09-19")

# Download S&P 500 time series
getSymbols("^GSPC", src = "yahoo", from = start, to = end)

# What class of object is SP500?
class(GSPC)

# Let's see the first few rows
head(GSPC)
tail(GSPC)

# Download stock price time series for: "GSPC", "AAPL", "MSFT", "GOOG"
getSymbols(c("GSPC", "AAPL", "MSFT", "GOOG"), src = "yahoo", from = start, to = end)

# Choose Closing Price values
data <- as.xts(data.frame(GSPC = GSPC[, "GSPC.Close"],
                          AAPL = AAPL[, "AAPL.Close"], 
                          MSFT = MSFT[, "MSFT.Close"], 
                          GOOG = GOOG[, "GOOG.Close"]))
# Check your data

###----
# Start from local data file (csv)
###----

# write.table(data, file = "data.csv", row.names=TRUE, sep=";", dec = ",")
data <- read.csv("data.csv", sep=";", dec = ",", header = TRUE)
data
names(data)
head(data,5)
tail(data)
 
# plot your data
qplot(data$GSPC.Close, data$AAPL.Close)

qplot(data$GSPC.Close, data$MSFT.Close)

###----
# Create Linear Regression Models
###----

# 1) Apple vs S&P 500
model1 <- lm(data=data, AAPL.Close~GSPC.Close)
# See results for your model
summary(model1)
#AAPL=-89.59+9.64GSPC

# 2) Google vs S&P 500
model2 <- lm(data=data, GOOG.Close~GSPC.Close)
summary(model2)

# 3) MSFT vs S&P 500
model3 <- lm(data=data, MSFT.Close~GSPC.Close)
summary(model3)

r2 = format(summary(model3)$r.squared, digits = 3)
r2

formula = paste("R^2 ==",as.character(r2))
formula

###----
# Visualize Linear Regression Models
###----

# plot your data
qplot(data$GSPC.Close, data$AAPL.Close)
qplot(data$GSPC.Close, data$MSFT.Close)


# add regression line
ggplot(data,aes(GSPC.Close, AAPL.Close))+
        geom_point()+
        geom_smooth(method='lm',formula=y~x, se=TRUE)

ggplot(data,aes(GSPC.Close, MSFT.Close))+
        geom_point()+
        geom_smooth(method='lm',formula=y~x, se=F)

# customize chart
p <- ggplot(data, aes(GSPC.Close, MSFT.Close))+
        geom_point(shape=2)+
        geom_smooth(method='lm',formula=y~x, color="green", se=FALSE)+
        xlab("S&P 500")+
        ylab("Microsoft")+
        ggtitle("Linear regression")+
        theme_tufte()
        #theme_economist() +
        #theme_base()
p
# add R^2 value
p+annotate("text", label=formula, parse = TRUE, x=20.2, y=74)


###----
# Another Example
###----

data2=data.frame(cond = rep(c("condition_1", "condition_2"), each=10), 
                my_x = 1:100 + rnorm(100,sd=9), 
                my_y = 1:100 + rnorm(100,sd=16) )
# Add a linear trend :
ggplot(data2, aes(x=my_x, y=my_y)) +    
        geom_point(shape=1) +  
        theme_tufte() + 
        geom_smooth(method=lm , color="red", se=FALSE)  # Add linear regression line






