# R-programming-statistics

R is a programming language possesses an extensive catalog of statistical and graphical methods. It includes machine learning algorithm, linear regression, time series, statistical inference to name a few. Most of the R libraries are written in R,but many large companies also use R programming language, including Uber, Google, Airbnb, Facebook and so on

## Email subscriptions

Delivered by FeedBurner

## Data has been collected HANG SENG INDEX (^HSI) from yahoo finance

getSymbols("^HSI", from="2004-01-01", to=Sys.Date())
‘getSymbols’ currently uses auto.assign=TRUE\

[1] "^HSI"
Warning message:
^HSI contains missing values. Some functions will not work if objects contain missing values in the middle of the series. Consider using na.omit(), na.approx(), na.fill(), etc to remove or replace them.
> chartSeries(Cl(HSI))

> ret <- dailyReturn(Cl(HSI), type='log')
Warning message:
In to_period(xx, period = on.opts[[period]], ...) :
missing values removed from data
> par(mfrow=c(2,2))
> acf(ret, main="Return ACF");
> pacf(ret, main="Return PACF");
> acf(ret^2, main="Squared return ACF");
> pacf(ret^2, main="Squared return PACF")

> par(mfrow=c(1,1))
> m=mean(ret);s=sd(ret);
> par(mfrow=c(1,2))
> hist(ret, nclass=40, freq=FALSE, main='Return histogram');curve(dnorm(x,mean=m,sd=s), from = -0.3, to = 0.2, add=TRUE, col="red")
> plot(density(ret), main='Return empirical distribution');curve(dnorm(x,mean=m,sd=s), from = -0.3, to = 0.2, add=TRUE, col="red")

> par(mfrow=c(1,1))
> library("moments", lib.loc="~/R/win-library/3.6")

Attaching package: ‘moments’

The following objects are masked from ‘package:timeDate’:

kurtosis, skewness

> kurtosis(ret)
daily.returns
12.72668
> plot(density(ret), main='Return EDF - upper tail', xlim = c(0.1, 0.2),
+      ylim=c(0,2));
> curve(dnorm(x, mean=m,sd=s), from = -0.3, to = 0.2, add=TRUE, col="red")
> plot(density(ret), xlim=c(-5*s,5*s),log='y', main='Density on log-scale')

Warning message:
In xy.coords(x, y, xlabel, ylabel, log) :
1 y value <= 0 omitted from logarithmic plot
> curve(dnorm(x, mean=m,sd=s), from=-5*s, to=5*s, log="y", add=TRUE,
+       col="red")
> qqnorm(ret);qqline(ret);
> library("rugarch", lib.loc="~/R/win-library/3.6")

Attaching package: ‘rugarch’

The following object is masked from ‘package:stats’:

sigma

> ret.hsi <- dailyReturn(Cl(HSI), type='log')

> chartSeries(ret.hsi)
> garch11.spec = ugarchspec(variance.model = list(model="sGARCH",garchOrder=c(1,1)), mean.model = list(armaOrder=c(0,0)))
> hsi.garch11.fit = ugarchfit(spec=garch11.spec, data=ret.hsi)
> coef(hsi.garch11.fit)
mu        omega       alpha1        beta1
4.901825e-04 1.552876e-06 5.928304e-02 9.312759e-01
> vcov(hsi.garch11.fit)
[,1]          [,2]          [,3]
[1,]  2.644461e-08 -1.163920e-10 -1.135825e-06
[2,] -1.163920e-10 -9.074665e-12 -9.114218e-08
[3,] -1.135825e-06 -9.114218e-08 -8.802228e-04
[4,]  1.249661e-06  9.971735e-08  9.720115e-04
[,4]
[1,]  1.249661e-06
[2,]  9.971735e-08
[3,]  9.720115e-04
[4,] -1.068850e-03
infocriteria(hsi.garch11.fit)

### #calculate news impact curve

newsimpact(hsi.garch11.fit)
\$zy
[1] 0.0054902042 0.0052768070 0.0050677649
[4] 0.0048630778 0.0046627457 0.0044667687
[7] 0.0042751468 0.0040878799 0.0039049680
[10] 0.0037264112 0.0035522094 0.0033823626
[13] 0.0032168710 0.0030557343 0.0028989527
[16] 0.0027465261 0.0025984546 0.0024547382
[19] 0.0023153767 0.0021803704 0.0020497190
[22] 0.0019234227 0.0018014815 0.0016838953
[25] 0.0015706641 0.0014617880 0.0013572669
[28] 0.0012571009 0.0011612899 0.0010698340
[31] 0.0009827331 0.0008999873 0.0008215965
[34] 0.0007475607 0.0006778800 0.0006125543
[37] 0.0005515837 0.0004949681 0.0004427076
[40] 0.0003948021 0.0003512517 0.0003120563
[43] 0.0002772159 0.0002467306 0.0002206003
[46] 0.0001988251 0.0001814049 0.0001683398
[49] 0.0001596297 0.0001552747 0.0001552747
[52] 0.0001596297 0.0001683398 0.0001814049
[55] 0.0001988251 0.0002206003 0.0002467306
[58] 0.0002772159 0.0003120563 0.0003512517
[61] 0.0003948021 0.0004427076 0.0004949681
[64] 0.0005515837 0.0006125543 0.0006778800
[67] 0.0007475607 0.0008215965 0.0008999873
[70] 0.0009827331 0.0010698340 0.0011612899
[73] 0.0012571009 0.0013572669 0.0014617880
[76] 0.0015706641 0.0016838953 0.0018014815
[79] 0.0019234227 0.0020497190 0.0021803704
[82] 0.0023153767 0.0024547382 0.0025984546
[85] 0.0027465261 0.0028989527 0.0030557343
[88] 0.0032168710 0.0033823626 0.0035522094
[91] 0.0037264112 0.0039049680 0.0040878799
[94] 0.0042751468 0.0044667687 0.0046627457
[97] 0.0048630778 0.0050677649 0.0052768070
[100] 0.0054902042

\$zx
[1] -0.300000000 -0.293939394 -0.287878788
[4] -0.281818182 -0.275757576 -0.269696970
[7] -0.263636364 -0.257575758 -0.251515152
[10] -0.245454545 -0.239393939 -0.233333333
[13] -0.227272727 -0.221212121 -0.215151515
[16] -0.209090909 -0.203030303 -0.196969697
[19] -0.190909091 -0.184848485 -0.178787879
[22] -0.172727273 -0.166666667 -0.160606061
[25] -0.154545455 -0.148484848 -0.142424242
[28] -0.136363636 -0.130303030 -0.124242424
[31] -0.118181818 -0.112121212 -0.106060606
[34] -0.100000000 -0.093939394 -0.087878788
[37] -0.081818182 -0.075757576 -0.069696970
[40] -0.063636364 -0.057575758 -0.051515152
[43] -0.045454545 -0.039393939 -0.033333333
[46] -0.027272727 -0.021212121 -0.015151515
[49] -0.009090909 -0.003030303  0.003030303
[52]  0.009090909  0.015151515  0.021212121
[55]  0.027272727  0.033333333  0.039393939
[58]  0.045454545  0.051515152  0.057575758
[61]  0.063636364  0.069696970  0.075757576
[64]  0.081818182  0.087878788  0.093939394
[67]  0.100000000  0.106060606  0.112121212
[70]  0.118181818  0.124242424  0.130303030
[73]  0.136363636  0.142424242  0.148484848
[76]  0.154545455  0.160606061  0.166666667
[79]  0.172727273  0.178787879  0.184848485
[82]  0.190909091  0.196969697  0.203030303
[85]  0.209090909  0.215151515  0.221212121
[88]  0.227272727  0.233333333  0.239393939
[91]  0.245454545  0.251515152  0.257575758
[94]  0.263636364  0.269696970  0.275757576
[97]  0.281818182  0.287878788  0.293939394
[100]  0.300000000

\$yexpr
expression(sigma[t]^2)

\$xexpr
expression(epsilon[t - 1])
#obtain the residuals\
residuals(msft.garch11.fit)
///////
signbias(hsi.garch11.fit)
t-value         prob sig
Sign Bias           0.1171965 9.067105e-01
Negative Sign Bias  2.4015250 1.637379e-02  **
Positive Sign Bias  3.3608760 7.844921e-04 ***
Joint Effect       32.1001392 4.985218e-07 ***

#### #unconditional (long-run) variance

> uncvariance(hsi.garch11.fit)
[1] 0.0001644812\
#unconditional (long-run) mean
uncmean(hsi.garch11.fit)
[1] 0.0004901825
ni.garch11 <- newsimpact(hsi.garch11.fit)
> plot(ni.garch11\$zx, ni.garch11\$zy, type="l", lwd=2, col="blue",
+      main="GARCH(1,1) - News Impact", ylab=ni.garch11\$yexpr, xlab=ni.garch11\$xexpr)
> egarch11.spec = ugarchspec(variance.model = list(model="eGARCH",garchOrder=c(1,1)), mean.model = list(armaOrder=c(0,0)))
> hsi.egarch11.fit = ugarchfit(spec=egarch11.spec, data=ret.hsi)
> coef(hsi.egarch11.fit)
mu         omega        alpha1
0.0002988673 -0.1349264062 -0.0556000850
beta1        gamma1
0.9843495645  0.1249733123
> ni.egarch11 <- newsimpact(hsi.egarch11.fit)
> plot(ni.egarch11\$zx, ni.egarch11\$zy, type="l", lwd=2, col="blue",
+      main="EGARCH(1,1) - News Impact",
+      ylab=ni.egarch11\$yexpr, xlab=ni.egarch11\$xexpr)

> ni.tgarch11 <- newsimpact(hsi.tgarch11.fit)
Error in newsimpact(hsi.tgarch11.fit) :
> tgarch11.spec = ugarchspec(variance.model = list(model="fGARCH",submodel="TGARCH", garchOrder=c(1,1)),
+ mean.model = list(armaOrder=c(0,0)))
> hsi.tgarch11.fit = ugarchfit(spec=tgarch11.spec, data=ret.hsi)
> coef(hsi.tgarch11.fit)
mu        omega       alpha1        beta1
0.0002866974 0.0002251146 0.0673026379 0.9297468601
eta11
0.5022077363
> garch11.sim = ugarchpath(garch11.spec, n.sim=1000)
> hsi.garch11.fit = ugarchfit(spec=garch11.spec, data=ret.hsi, out.sample=20)