Professional Documents
Culture Documents
Baba
Baba
getSymbols("BABA",src="yahoo",return.class="xts")
##
##
##
##
##
##
##
##
##
## [1] "BABA"
price<-BABA[,4]
plot(price)
60
70
80
90
100
120
price
Sep 19
2014
Dec 01
2014
Feb 02
2015
Apr 01
2015
plot(log(price))
Jun 01
2015
Aug 03
2015
Oct 01
2015
4.2
4.4
4.6
4.8
log(price)
Sep 19
2014
Dec 01
2014
Feb 02
2015
Apr 01
2015
Jun 01
2015
Aug 03
2015
Regression
SIN <- COS <- matrix(nr = length(price), nc = 3)
TIME <- as.numeric(time(price) - mean(time(price)))/sd(time(price))
for (i in 1:3) {
COS[, i] <- cos(2 * pi * i * TIME)
SIN[, i] <- sin(2 * pi * i * TIME)
}
price.lm1 <- lm(log(price) ~ TIME + I(TIME^2) + I(TIME^3)+ I(TIME^4) +
COS[,1] + SIN[,1] + COS[,2] + SIN[,2] + COS[,3] + SIN[,3])
summary(price.lm1)
##
## Call:
## lm(formula = log(price) ~ TIME + I(TIME^2) + I(TIME^3) + I(TIME^4) +
##
COS[, 1] + SIN[, 1] + COS[, 2] + SIN[, 2] + COS[, 3] + SIN[,
##
3])
##
## Residuals:
##
Min
1Q
Median
3Q
Max
Oct 01
2015
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
-0.201408 -0.060745
0.001487
0.062786
0.164778
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 4.462123
0.009331 478.208 < 2e-16 ***
TIME
-0.200395
0.012738 -15.732 < 2e-16 ***
I(TIME^2)
0.003860
0.020235
0.191
0.8488
I(TIME^3)
0.044712
0.006593
6.782 7.07e-11 ***
I(TIME^4)
-0.008779
0.007883 -1.114
0.2664
COS[, 1]
0.009162
0.007776
1.178
0.2397
SIN[, 1]
-0.036725
0.007208 -5.095 6.44e-07 ***
COS[, 2]
-0.014320
0.007097 -2.018
0.0446 *
SIN[, 2]
0.003134
0.007205
0.435
0.6639
COS[, 3]
0.006717
0.007135
0.941
0.3473
SIN[, 3]
-0.008378
0.007078 -1.184
0.2376
--Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.08442 on 279 degrees of freedom
Multiple R-squared: 0.7152, Adjusted R-squared: 0.705
F-statistic: 70.05 on 10 and 279 DF, p-value: < 2.2e-16
Call:
lm(formula = log(price) ~ TIME + I(TIME^2) + I(TIME^4) + COS[,
2] + COS[, 3] + SIN[, 3])
Residuals:
Min
1Q
Median
-0.236863 -0.065282 -0.003812
3Q
0.070358
Max
0.240086
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 4.460274
0.010583 421.438
<2e-16 ***
TIME
-0.120930
0.005658 -21.372
<2e-16 ***
I(TIME^2)
0.013471
0.022451
0.600
0.5490
I(TIME^4)
-0.014030
0.008497 -1.651
0.0998 .
COS[, 2]
-0.014360
0.008064 -1.781
0.0760 .
COS[, 3]
0.008117
0.008089
1.004
0.3165
SIN[, 3]
-0.012496
0.008010 -1.560
0.1199
--Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.09607 on 283 degrees of freedom
Multiple R-squared: 0.6259, Adjusted R-squared: 0.618
F-statistic: 78.91 on 6 and 283 DF, p-value: < 2.2e-16
par(mfrow=c(2,2))
plot(price.lm2)
+ COS[,3] + SIN[,3])
286
4.2
4.3
4.4
4.5
4.6
Normal QQ
258
286
4.5
4.6
Fitted values
286
Cook's distance
0.00
0.02
0.04
Leverage
Plot
par(mfrow=c(1,1))
plot(TIME,log(price),type="l")
lines(TIME,predict(price.lm2),col="red",type="l")
289290
1.0
4.4
Residuals vs Leverage
Standardized residuals
ScaleLocation
285258
286
4.3
Theoretical Quantiles
0.0
Standardized residuals
Fitted values
4.2
285
2 0
Standardized residuals
0.2 0.1
Residuals
Residuals vs Fitted
285258
0.06
4.8
4.6
4.4
4.2
log(price)
1.5
1.0
0.5
0.0
TIME
Residuals
acf(resid(price.lm2))
0.5
1.0
1.5
0.4
0.0
0.2
ACF
0.6
0.8
1.0
Series resid(price.lm2)
10
15
Lag
pacf(resid(price.lm2))
20
0.6
0.4
0.2
0.0
Partial ACF
0.8
1.0
Series resid(price.lm2)
10
15
Lag
Prediction
new.t<-TIME[length(price)]+cumsum(diff(TIME)[1:20])
SIN <- COS <- matrix(nr = length(new.t), nc = 3)
for (i in 1:3) {
COS[, i] <- cos(2 * pi * i * new.t)
SIN[, i] <- sin(2 * pi * i * new.t)
}
new.dat <- data.frame(TIME = as.vector(new.t), SIN = SIN, COS = COS)
price.pred <- predict(price.lm2, new.dat)
plot(new.t,exp(price.pred),type="l",col="red")
20
1.75
1.80
1.85
new.t
1.90
60
61
62
63
exp(price.pred)
64