-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStock.R
148 lines (98 loc) · 2.99 KB
/
Stock.R
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
#install.packages("tidyquant")
library(tidyquant)
# Obtaining Stock Data
boxx <- tq_get('BOX',
from = '2020-01-01',
to = '2021-06-06',
get = 'stock.prices')
dropbox <- tq_get('DBX',
from = '2020-01-01',
to = '2021-06-06',
get = 'stock.prices')
head(boxx)
class(boxx)
# Graphing Stock Data
library(ggplot2)
boxx %>%
ggplot(aes(x = date, y = adjusted)) +
geom_line() +
theme_classic() +
labs(x = 'Date',
y = "Adjusted Price",
title = "Box price chart") +
scale_y_continuous(breaks = seq(0,300,10))
dropbox %>%
ggplot(aes(x = date, y = adjusted)) +
geom_line() +
theme_classic() +
labs(x = 'Date',
y = "Adjusted Price",
title = "Dropbox price chart") +
scale_y_continuous(breaks = seq(0,300,10))
# Merge data and rename columns
merged <- merge(boxx, dropbox, by=c("date"))
head(merged)
df = subset(merged, select=c("date","adjusted.x","adjusted.y"))
names(df)[names(df) == "adjusted.x"] <- "box"
names(df)[names(df) == "adjusted.y"] <- "dropbox"
head(df)
# Save the data
write.csv(df, "box_dropbox.csv")
# Load the data
df <- read.csv("box_dropbox.csv")
df <- df[,-1]
# Linear regression
attach(df)
reg <- lm(box~dropbox, data=df)
summary(reg)
reg <- lm(dropbox~box, data=df)
summary(reg)
par(mfrow=c(2,2))
plot(reg)
# Granger Test
library(lmtest)
grangertest(box ~ dropbox, order = 3, data=df)
grangertest(dropbox ~ box, order = 3, data=df)
# Serial Correlation Test
dwtest(reg)
residuals = reg$residuals
plot(residuals, type='l')
sub_box <- subset(df, select=c("date","box"))
sub_dropbox <- subset(df, select=c("date","dropbox"))
d_box = diff(as.numeric(unlist(sub_box["box"])))
d_dbox = diff(as.numeric(unlist(sub_dropbox["dropbox"])))
lagged_reg <- lm(d_box~d_dbox)
summary(lagged_reg)
lagged_reg_res = lagged_reg$residuals
plot(lagged_reg_res, type='l')
# Cochrane-Orcutt
#install.packages("orcutt")
library(orcutt)
co <- cochrane.orcutt(reg)
summary(co)
dwtest(co)
# ACF Plots
acf_residual_reg = acf(residuals)
acf_lag_residual_reg = acf(lagged_reg_res)
acf_residual_reg
acf_lag_residual_reg
# Stationarity
#install.packages("egcm")
#install.packages("tseries")
library(egcm)
library(tseries)
adf.test(as.numeric(unlist(sub_box["box"])))
adf.test(as.numeric(unlist(sub_dropbox["dropbox"])))
adf.test(d_box)
adf.test(d_dbox)
pp.test(as.numeric(unlist(sub_box["box"])))
pp.test(as.numeric(unlist(sub_dropbox["dropbox"])))
pp.test(d_box)
pp.test(d_dbox)
kpss.test(as.numeric(unlist(sub_box["box"])), null="Trend")
kpss.test(as.numeric(unlist(sub_dropbox["dropbox"])), null="Trend")
# Cointegration
egcm(as.numeric(unlist(sub_box["box"])), as.numeric(unlist(sub_dropbox["dropbox"])))
egcm(d_box, d_dbox)
plot(egcm(as.numeric(unlist(sub_box["box"])), as.numeric(unlist(sub_dropbox["dropbox"]))))
plot(egcm(d_box, d_dbox))