install.packages("ISLR")
library(ISLR)
library(dplyr)
library(ggplot2)
full_wages = tbl_df(ISLR::Wage)  
dim(full_wages)
colnames(full_wages)
#Select variables which we have to use instead of all the variables. We can use the following command
#for dplyr package
wages = full_wages %>% select(wage, education, year, age)  

#To make boxplot for wages against different categories of education we use
ggplot(wages) + aes(x = education, y = wage) + geom_boxplot()
#This is good one but it will be more appealing if add colors
ggplot(wages)+aes(x=education,y=wage,fill=education)+geom_boxplot()
#To see the distribution of variables we use violen chart as boxplot dont give much information about it
ggplot(wages)+aes(x=education,y=wage,fill=education)+geom_violin()

#Wage and Year
ggplot(wages) + aes(x = year, y = wage) + geom_point() + stat_smooth(method=lm)  

#Wage and Age
g<-ggplot(wages)+aes(age,wage)+geom_point()+stat_smooth()
g+ggtitle("Wage Rate vs Age")

ggplot(wages)+aes(age,wage)+geom_point()+stat_smooth(method = lm)

#Looking at more than two variables at once
#Color can be considered as third dimension, we use Education as third dimension
ggplot(wages) + aes(age, wage) + geom_point(size=1, aes(color=education)) + facet_wrap(~ education)



#THE STOCK MARKET DATA SET
smkt = tbl_df(ISLR::Smarket)  
smkt  
ggplot(smkt) + aes(Lag1, fill=Direction, alpha=0.5) + geom_density()  


#Genes Expression Data Set
nci60 = ISLR::NCI60  
class(nci60)
names(nci60)  
class(nci60$data)
dim(nci60$data)
# normalize the variables before running pca
colMus = colMeans(nci60$data)  
colSds = apply(nci60$data, 2, sd)  
normalized = (nci60$data - colMus) / colSds  
# get the svd of the matrix
duv = svd(normalized) 

#VISUALIZING THE RESULTS OF PCA
qplot(1:length(duv$d), duv$d,  
      xlab = "principal component", ylab = "diagonal scale factor")
qplot(duv$u[,1], duv$u[,2], size=3,  
      xlab = "1st component", ylab = "2nd component",
      color=nci60$labs)
str(Smarket)

ggplot(Smarket) + aes(x = Direction, y = Volume) + geom_boxplot()
