File: bandwidth.R

package info (click to toggle)
r-cran-gmm 1.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,592 kB
  • sloc: fortran: 131; ansic: 25; makefile: 2
file content (154 lines) | stat: -rw-r--r-- 5,332 bytes parent folder | download | duplicates (4)
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
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  A copy of the GNU General Public License is available at
#  http://www.r-project.org/Licenses/


bwWilhelm <- function(x, order.by = NULL, kernel = c("Quadratic Spectral",
  "Bartlett", "Parzen", "Tukey-Hanning"), approx = c("AR(1)", "ARMA(1,1)"),
  weights = NULL, prewhite = 1, ar.method = "ols", data=list()) 
{
  ## ensure that NAs are omitted
  if(is.list(x) && !is.null(x$na.action)) class(x$na.action) <- "omit"

  # ensure that x is a gmm object
  stopifnot(attr(x,"class") == "gmm")

  kernel <- match.arg(kernel)
  approx <- match.arg(approx)
  prewhite <- as.integer(prewhite)

  # the following line applies only to the case class(res)=="gmm"
  umat <- x$gt
  n <- nrow(umat)
  k <- ncol(umat)

  # the following line applies only to the case class(res)=="gmm"
  G <- x$G
  p <- ncol(G)

  # return Andrews' bandwidth in the just-identified case
  
  if (p==k)
      {
          class(umat) <- "gmmFct"
          return(bwAndrews(umat, order.by=order.by, kernel=kernel,
                           approx=approx, weights=weights, prewhite=prewhite,
                           ar.method=ar.method, data=data))
      }

  if(!is.null(order.by))
  {
    if(inherits(order.by, "formula")) {
      z <- model.matrix(order.by, data = data)
      z <- as.vector(z[,ncol(z)])
    } else {
      z <- order.by
    }
    index <- order(z)
  } else {
    index <- 1:n
  }

  umat <- umat[index, , drop = FALSE]

  ## compute weights (w_a, see p. 834)
  ## (try to set the intercept weight to 0)
  #### could be ignored by using: weights = 1
  
  if(is.null(weights)) {
    weights <- rep(1, p)
    unames <- colnames(umat)
    if(!is.null(unames) && "(Intercept)" %in% unames) 
      weights[which(unames == "(Intercept)")] <- 0
    else {
      res <- try(as.vector(rowMeans(estfun(x)/model.matrix(x), na.rm = TRUE)), silent = TRUE)
      if(inherits(res, "try-error")) res <- try(residuals(x), silent = TRUE)
      if(!inherits(res, "try-error")) weights[which(colSums((umat - res)^2) < 1e-16)] <- 0
    }
    if(isTRUE(all.equal(weights, rep(0, p)))) weights <- rep(1, p)
  } else {
    weights <- rep(weights, length.out = p)
  }
  if(length(weights) < 2) weights <- 1

  ## pre-whitening
  if(prewhite > 0) {
    var.fit <- ar(umat, order.max = prewhite, demean = FALSE, aic = FALSE, method = ar.method)
    if(inherits(var.fit, "try-error")) stop(sprintf("VAR(%i) prewhitening of estimating functions failed", prewhite))
    umat <- as.matrix(na.omit(var.fit$resid))
    n <- n - prewhite
  }
 
  # define kernel constants
  kernConst <- switch(kernel,
    "Bartlett"           = list(q = 1, g_q = 1,        mu1 = 1,       mu2 = 2/3),
    "Parzen"             = list(q = 2, g_q = 6,        mu1 = 0.75,    mu2 = 0.539286),
    "Tukey-Hanning"      = list(q = 2, g_q = pi^2/4,   mu1 = 1,       mu2 = 0.75),
    "Quadratic Spectral" = list(q = 2, g_q = 1.421223, mu1 = 1.25003, mu2 = 0.999985))

  # fit approximating model to moments
  if(approx == "AR(1)") {

    fitAR1 <- function(x) {
      rval <- ar(x, order.max = 1, aic = FALSE, method = "ols")
      rval <- c(rval$ar, sqrt(rval$var.pred))
      names(rval) <- c("rho", "sigma")
      return(rval)
    }
    
    ar.coef <- apply(umat, 2, fitAR1)

    Omega0 <- ar.coef["sigma", ]^2 / (1-ar.coef["rho", ])^2
    Omega_q <- if(kernConst$q == 1) {
      diag(2 * ar.coef["sigma", ]^2 * ar.coef["rho", ] / ((1 - ar.coef["rho", ])^3 * (1 + ar.coef["rho", ])))
    } else {
      diag(2 * ar.coef["sigma", ]^2 * ar.coef["rho", ] / (1 - ar.coef["rho", ])^4) 
    }

  } else {

    fitARMA11 <- function(x) {
      rval <- arima(x, order = c(1, 0, 1), include.mean = FALSE)
      rval <- c(rval$coef, sqrt(rval$sigma2))
      names(rval) <- c("rho", "psi", "sigma")
      return(rval)
    }
    
    arma.coef <- apply(umat, 2, fitARMA11)

    Omega0 <- (1 + ar.coef["psi", ])^2 * ar.coef["sigma", ]^2 / (1 - ar.coef["rho", ])^2
    Omega_q <- if(kernConst$q == 1) {
      diag(2 * (1 + ar.coef["psi", ] * ar.coef["rho", ]) * (ar.coef["psi", ] + ar.coef["rho", ]) * ar.coef["sigma", ]^2 / ((1 - ar.coef["rho", ])^3 * (1 + ar.coef["rho", ])))
    } else {
      diag(2 * (1 + ar.coef["psi", ] * ar.coef["rho", ]) * (ar.coef["psi", ] + ar.coef["rho", ]) * ar.coef["sigma", ]^2 / (1 - ar.coef["rho", ])^4)
    }

  }

  # compute remaining bandwidth components
  W <- diag(weights)
  Omega0inv <- diag(1/Omega0)
  Sigma0 <- solve( crossprod(Omega0inv%*%G, G) )
  H0 <- Sigma0 %*% t(G) %*% Omega0inv
  P0 <- Omega0inv - Omega0inv %*% G %*% H0
  
  # compute optimal bandwidth
  nu2 <- (2 * kernConst$mu1 + kernConst$mu2) * (k - p) * sum(diag(Sigma0 %*% W))
  nu3 <- kernConst$g_q^2 * sum(diag(t(Omega_q) %*% t(H0) %*% W %*% H0 %*% Omega_q %*% P0))
  c0 <- if(nu2 * nu3 > 0) 2 * kernConst$q else -1
  rval <- (c0 * nu3/nu2 * n)^(1/(1 + 2 * kernConst$q))

  return(rval)
}