-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEDA .Rmd
169 lines (109 loc) · 3.88 KB
/
EDA .Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
---
title: "EDA S&P 500 index"
date: "`r Sys.Date()`"
output:
html_document:
code_folding: hide
number_sections: false
toc: yes
toc_depth: 3
toc_float: yes
pdf_document:
toc: yes
toc_depth: '3'
---
```{r init, include=FALSE}
# Load all required packagess
library(xts)
library(ezids)
library(dplyr)
library(tseries)
library(ggplot2)
library(forecast)
library(corrplot)
library(lubridate)
knitr::opts_chunk$set(warning = F, results = "markup", message = F)
options(scientific=T, digits = 3)
```
```{r}
df <- data.frame(read.csv('sp500.csv'))
df$Date <- as.Date(df$Date)
str(df)
```
# finding unique values in Symbols column
```{r}
length(df$Symbol)
unique(df$GICS )
unique(df$Symbol)
```
# count plot for different sectors
```{r}
library(ggplot2)
ggplot(df, aes(x = GICS)) + geom_bar() + theme(axis.text.x = element_text(angle = 90, hjust = 1))
```
In the overall dataset, the sector with the highest number of symbols is the Industrials sector, followed by the Financials sector. the least number of symbols is in the communication Services sector.
# sort dataframe by date column
```{r}
df <- df[order(df$Date),]
head(df)
```
# finding unique values in the Date column and get year from date column
```{r}
library(lubridate)
year <- unique(year(df$Date))
year
```
# plot countplot for each year
```{r}
ggplot(df, aes(x = year(df$Date))) + geom_bar() + theme(axis.text.x = element_text(angle = 90, hjust = 1))
```
the number of symbols in the dataset has increased over the years. The year with the highest number of symbols is 2020, followed by 2021. The year with the least number of symbols is 2000. there was a significant increase in companies because of the fluctuations in the stock market.
# nrows where the year is 2020
```{r}
(nrow(df[year(df$Date) == 2020,]))
unique(df$Symbol[year(df$Date) == 2023])
```
#plot countplot for each year and sector
```{r}
ggplot(df, aes(x = year(df$Date), fill = GICS)) + geom_bar() + theme(axis.text.x = element_text(angle = 90, hjust = 1))
```
the plot tells us the contribution of each sector to the total number of symbols in the dataset.
# plot sum of volume for each year
```{r}
ggplot(df, aes(x = year(df$Date), y = Volume)) + geom_bar(stat = "identity") + theme(axis.text.x = element_text(angle = 90, hjust = 1))
```
the plot tells us the total volume of stocks traded in each year. The year with the highest volume of stocks traded is 2008. The year with the least volume of stocks traded is 2000.
# sum of volume for each year
```{r}
df_sum <- df %>% group_by(year(df$Date)) %>% summarise(sum(Volume))
df_sum
```
volume of stocks traded in numeric values for each year.
# plot sum of volume for each year and sector
```{r}
ggplot(df, aes(x = year(df$Date), y = Volume, fill = GICS)) + geom_bar(stat = "identity") + theme(axis.text.x = element_text(angle = 90, hjust = 1))
```
the plot tells us the contribution of each sector to the total volume of stocks traded in the dataset.
# sum of volume for each year and sector
```{r}
library(dplyr)
library(lubridate)
df_sum_grouped <- df %>% group_by(year(df$Date), GICS) %>% summarise(sum(Volume))
(df_sum_grouped)
```
the contribution of each sector to the total volume of stocks traded in the dataset in numeric values.
# sort dataframe by close column
```{r}
df_ <- df[order(df$Close),]
head(df_)
```
# plot highest closing price for each year for each sector
```{r}
ggplot(df, aes(x = year(df$Date), y = Close, color = GICS)) + geom_point() + theme(axis.text.x = element_text(angle = 90, hjust = 1))
```
the plot tells us the highest closing price for each sector in each year.
# top ten symbols with highest close price each year
```{r}
top_ten <- df %>% group_by(year(df$Date)) %>% top_n(10, Close)
top_ten
```