-
Notifications
You must be signed in to change notification settings - Fork 7
/
Helper_Functions_.R
139 lines (110 loc) · 3.76 KB
/
Helper_Functions_.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
myrename <- function(x,y){
names(x) <- y
return(x)
}
formatN2 <- function(x, y = 3){
sprintf(paste("%.",y,"f", sep=""), round(x, y))
}
ComRet <- function(R, RF, H, type = 1){
data <- merge(R, RF, join='left')
#data <- data[complete.cases]
data$Add <- data[,1] + data[, 2]
if(type == 1){
R_c01 = rollapply(data$Add, width = H, FUN = function(x){prod(1 + x)}, align = "left")
R_c02 = rollapply(data$RF, width = H, FUN = function(x){prod(1 + x)}, align = "left")
R_c = R_c01 - R_c02
}
if(type == 2){
R_c = rollapply(data$R, H, function(x){
sum(x[, 1])})}
return(R_c)
}
olsNW02 <- function(reg = reg,lag){
# %Syntax: olsnwsdbeta(Y,X,Const,lag)
# %
# %Calculates the Newey-West standard deviation of the
# %estimated OLS betas an OLS regression of Y on X, with
# %lag lengt lag.
# %Y may contain more than one variable; Beta assumes that
# %each column in Y refers to a different variable. If you
# %wantto include a constant in your regression, then the
# %variable Const should be equal to 1; If it is zero then
# %Beta assumes that you do not want to include a constant.
Y <- matrix(as.matrix(reg$model)[, 1])
if(ncol(reg$model) > 2){
X <- as.matrix(reg$model)[, 2:ncol(reg$model)]
} else {
X <- matrix(as.matrix(reg$model)[, 2:ncol(reg$model)])
}
Const <- 0
if(length(names(reg$coefficients)) > I(ncol(reg$model)-1)){
Const = 1
}
Y.dim = dim(Y)
X.dim = dim(X)
if(Const == 1){
c = array(1, c(Y.dim[1],1))
X = cbind(c, X)
X.dim[2] = X.dim[2] + 1
}
Omega = array( 0, c(Y.dim[2]*X.dim[2],Y.dim[2]*X.dim[2]))
b = solve(t(X)%*%(X))%*%(t(X)%*%(Y))
e = Y - X%*%b
d = 0.5
for(k in 0:lag){
if(k > 0){
d = 1
}
for(i in 1:Y.dim[2]){
Zi = array(0, c(Y.dim[1], X.dim[2]))
for(ii in 1:X.dim[2]){
Zi[ ,ii] = e[ ,i]*X[ ,ii]
}
for(j in 1:Y.dim[2]){
Z1j = array(0, c(Y.dim[1], X.dim[2]))
Z2j = array(0, c(Y.dim[1], X.dim[2]))
for(jj in 1:X.dim[2]){
Z1j[I(1+k):Y.dim[1], jj] = e[1:I(Y.dim[1]-k), j]*X[1:I(Y.dim[1]-k), jj]
Z2j[1:I(Y.dim[1]-k), jj] = e[I(1+k):Y.dim[1], j]*X[I(1+k):Y.dim[1], jj]
}
Omega[I(I(i-1)*X.dim[2] + 1):I(i*X.dim[2]), I(I(j-1)*X.dim[2]+1):I(j*X.dim[2])] = Omega[
I(I(i-1)*X.dim[2]+1):I(i*X.dim[2]), I(I(j-1)*X.dim[2]+1):I(j*X.dim[2])] + (d*(1-k/(1+lag)))*(
solve(t(X)%*%X)%*%(t(Zi)%*%Z1j)%*%solve(t(X)%*%X) + solve(t(X)%*%X)%*%t(t(Z2j)%*%Zi)%*%solve(t(X)%*%X))
}
}
}
V = Omega*(Y.dim[1]/(Y.dim[1]-X.dim[2]))
var = diag(V)
se = sqrt(var)
return(V)
}
hodrickse <- function(reg,lags){
# single equation Hodrick (1992) standard errors
# construct time series of (uncorrelated) et+1 from regression of ln rt+1 on constant
Y <- matrix(as.matrix(reg$model)[, 1])
X <- X <- as.matrix(reg$model)[, 2:ncol(reg$model)]
# if(ncol(reg$model) > 2){
# X <- as.matrix(reg$model)[, 2:ncol(reg$model)]
# } else {
# X <- matrix(as.matrix(reg$model)[, 2:ncol(reg$model)])
# }
Tn = nrow(as.matrix(Y))
model = lm(Y ~ 1);
resid = as.matrix(model$residuals)
T2 = nrow(as.matrix(X))
X = cbind(1, X)
N = ncol(X)
A = solve(t(X)%*%X)
SbT = matrix(0, nrow = N, ncol = N)
for(t in (1+lags - 1):(Tn - 1)){
if(is.null(dim(X[(t- (lags - 1)):t, ]))){
wkt = resid[t+1 , 1]%*%X[(t- (lags - 1)):t, ];
SbT = SbT + t(wkt)%*%wkt
} else {
wkt = resid[t+1 , 1]%*%apply(X[(t- (lags - 1)):t, ], 2, sum);
SbT = SbT + t(wkt)%*%wkt
}
}
omega = A%*%SbT%*%A;
return(unname(omega))
}