/
bw.R
443 lines (428 loc) · 12.9 KB
/
bw.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#' Bandwidth Selection by Cross-Validation
#'
#' Calculate bandwidth(s) by cross-validation for functions tvSURE, tvVAR and tvLM.
#'
#' @rdname bw
#' @param x An object used to select a method.
#' @param ... Other parameters passed to specific methods.
#' @return \code{bw} returns a vector or a scalar with the bandwith to estimate the mean or the covariance
#' residuals, fitted values.
#' @export
bw <- function(x, ...) UseMethod("bw", x)
#' @rdname bw
#' @param y A matrix or vector with the dependent variable(s).
#' @param z A vector with the variable over which coefficients are smooth over.
#' @param cv.block A positive scalar with the size of the block in leave-one block-out cross-validation.
#' By default 'cv.block=0' meaning leave-one-out cross-validation.
#' @param est The nonparametric estimation method, one of "lc" (default) for linear constant
#' or "ll" for local linear.
#' @param tkernel A character, either "Triweight" (default), "Epa" or "Gaussian" kernel function.
#' @param singular.ok Logical. If FALSE, a singular model is an error.
#'
#' @return A scalar or a vector of scalars.
#'
#' @examples
#' ##Generate data
#' tau <- seq(1:200)/200
#' beta <- data.frame(beta1 = sin(2*pi*tau), beta2 = 2*tau)
#' X <- data.frame(X1 = rnorm(200), X2 = rchisq(200, df = 4))
#' error <- rt(200, df = 10)
#' y <- apply(X*beta, 1, sum) + error
#'
#' ##Select bandwidth by cross-validation
#' bw <- bw(X, y, est = "ll", tkernel = "Gaussian")
#'
#' @method bw default
#' @export
#'
bw.default <- function(x, y, z = NULL, cv.block = 0, est = c("lc", "ll"), tkernel = c("Triweight", "Epa", "Gaussian"),
singular.ok = TRUE, ...)
{
if(!inherits(x, c("matrix", "data.frame", "vector", "numeric", "integer")))
stop("'x' should be a matrix, a vector or a data frame. \n")
if(is.null(y))
stop("Parameter 'y' missing. \n")
if(sum(is.na(y))>0 | sum(is.na(x))>0)
stop("There are NA values in your data, please enter only complete cases. \n")
tkernel <- match.arg(tkernel)
est <- match.arg(est)
cv.block <- floor(abs(cv.block))
y <- as.matrix(y)
x <- as.matrix(x)
neq <- NCOL(y)
obs <- NROW(y)
if( !identical(obs, NROW(x)))
stop("The number of equations in 'x' and 'y' are different \n")
if(is.null(z))
{
upper <- 20
lower <- 5/obs
top <- 1
}
else
{
top <- (max(z) - min (z))* 5
upper <- top
lower <- top * 0.001
}
bw <- numeric(neq)
for (j in 1:neq)
{
iter <- 0
value <- .Machine$double.xmax
while(value == .Machine$double.xmax)
{
if(iter == 10)
{
value <- 0
bw[j] <- top
warning("Maximum number of iterations reached in bandwidth calculation: either the function
is constant, no convergence of bandwidth, or cv.block is too big. \n")
break()
}
result <- try(stats::optim(stats::runif(1, lower, top), .tvOLS.cv, method = "Brent",
lower = lower, upper = upper, x = x, y = y[, j], z = z,
cv.block = cv.block, est = est, tkernel = tkernel,
singular.ok = singular.ok),
silent = TRUE)
if (!inherits(result, "list"))
value <- .Machine$double.xmax
else
{
if(is.na(result$value))
value <- .Machine$double.xmax
else
{
value <- result$value
bw[j] <- result$par
}
}
iter <- iter + 1
}
}
return(bw)
}
#' @examples
#' data( Kmenta, package = "systemfit" )
#'
#' ## x is a list of matrices containing the regressors, one matrix for each equation
#' x <- list()
#' x[[1]] <- Kmenta[, c("price", "income")]
#' x[[2]] <- Kmenta[, c("price", "farmPrice", "trend")]
#'
#' ## 'y' is a matrix with one column for each equation
#' y <- cbind(Kmenta$consump, Kmenta$consump)
#'
#' ## Select bandwidth by cross-validation
#' bw <- bw(x = x, y = y)
#'
#' ##One bandwidth per equation
#' print(bw)
#'
#' @rdname bw
#' @method bw list
#' @export
bw.list <- function(x, y, z = NULL, cv.block = 0, est = c("lc", "ll"),
tkernel = c("Triweight", "Epa", "Gaussian"),
singular.ok = TRUE, ...)
{
if(!inherits(x, "list"))
stop("'x' should be a list of matrices. \n")
neq <- length(x)
if(neq < 2)
stop("'x' should have at least two elements.\n")
if(sum(is.na(y)) > 0)
stop("There are NA values in your data, please enter only complete cases. \n")
if(is.null(y))
stop("Parameter 'y' is missing.\n")
obs <- NROW(x[[1]])
if(!identical(neq, NCOL(y)) | !identical(obs, NROW(y)))
stop("The number of equations in 'x' and 'y' are different \n")
tkernel <- match.arg(tkernel)
est <- match.arg(est)
bw <- numeric(neq)
cv.block <- floor(abs(cv.block))
if(is.null(z))
{
upper <- 20
lower <- 5/obs
top <- 0.5
}
else
{
top <- (max(z) - min(z))*5
upper <- top
lower <- top * 0.001
}
for (j in 1:neq)
{
iter <- 0
value <- .Machine$double.xmax
while(value == .Machine$double.xmax)
{
if(iter == 10)
{
value <- 0
bw[j] <- 100
warning("Maximum number of iterations reached in bandwidth calculation: either the function
is constant, or no convergence of bandwidth. \n")
break()
}
result <- try(stats::optim(stats::runif(1, lower, top), .tvOLS.cv,
method = "Brent", lower = lower,
upper = upper, x = x[[j]], y = y[, j], z = z,
cv.block = cv.block, est = est, tkernel = tkernel,
singular.ok = singular.ok), silent = TRUE)
if (!inherits(result, "list"))
value <- .Machine$double.xmax
else
{
if(is.na(result$value))
value <- .Machine$double.xmax
else
{
value <- result$value
bw[j] <- result$par
}
}
iter <- iter + 1
}
}
return(bw)
}
#' @rdname bw
#' @method bw tvlm
#' @export
bw.tvlm <- function(x, ...)
{
y <- x$y
z <- x$z
est <- x$est
tkernel <- x$tkernel
singular.ok <- x$singular.ok
cv.block <- floor(abs(x$cv.block))
x <- x$x
return(bw (x, y, z, cv.block, est, tkernel, singular.ok))
}
#' @rdname bw
#' @method bw tvar
#'
#' @export
bw.tvar <- bw.tvlm
#' @rdname bw
#' @method bw tvvar
#'
#' @export
bw.tvvar <- bw.tvlm
#' @rdname bw
#' @method bw tvsure
#'
#' @export
bw.tvsure <- bw.tvlm
#' @rdname bw
#' @method bw tvplm
#' @export
bw.tvplm <- function(x, ...)
{
if(!inherits(x, "tvplm"))
stop("'x' should be a 'tvplm' object. \n")
y <- x$y
z <- x$z
cv.block <- floor(abs(x$cv.block))
obs <- x$obs
neq <- x$neq
method <- x$method
est <- x$est
tkernel <- x$tkernel
x <- x$x
if(is.null(z))
{
upper <- 20
lower <- 5/obs
top <- 1
}
else
{
top <- (max(z) - min(z))*5
upper <- top
lower <- top * 0.001
}
iter <- 0
value <- .Machine$double.xmax
while(value == .Machine$double.xmax)
{
if(iter == 10)
{
value <- 0
bw <- 100
warning("Maximum number of iterations reached in bandwidth calculation: either the function
is constant, or no convergence of bandwidth. \n")
break()
}
if (method != "within")
result <- try(stats::optim(stats::runif(1, lower, top), .tvRE.cv, method = "Brent",
lower = lower, upper = upper, x = x, y = y, z = z,
neq = neq, obs = obs, cv.block = cv.block, est = est,
tkernel = tkernel),
silent = FALSE)
else
result <- try(stats::optim(stats::runif(1, lower, top), .tvFE.cv, method = "Brent",
lower = lower, upper = top, x = x, y = y, z = z,
neq = neq, obs = obs, cv.block = cv.block, est = est,
tkernel = tkernel),
silent = FALSE)
if (!inherits(result, "list"))
value <- .Machine$double.xmax
else
{
if(is.na(result$value))
value <- .Machine$double.xmax
else
{
value <- result$value
bw <- result$par
}
}
iter <- iter + 1
}
return(abs(bw))
}
#'
#' Panel Model Bandwidth Calculation by Cross-Validation
#' \emph{bwPanel} calculates a single bandwidth to estimate the time-varying
#' coefficients of a panel data modelc
#' @param method A character with the choice of panel model/estimation method:
#' If method = \code{tvPOLS} (default) then the data is pooled estimated with time-varying OLS.
#' No individual or time effects are estimated
#' If method = \code{tvFE} then individual effects which might be correlated with
#' the regressors are estimated.
#' If method = \code{tvRE} then individual effects are considered random and independent
#' of the regressors.
#' @return A scalar.
#' @method bw pdata.frame
#' @rdname bw
#' @export
bw.pdata.frame<-function(x, z = NULL, method, cv.block = 0,
est = c("lc", "ll"), tkernel = c("Triweight", "Epa", "Gaussian"), ...)
{
dimen <- plm::pdim(x)
neq <- dimen$nT$n
obs <- dimen$nT$T
y <- stats::model.extract(x, "response")
if (stats::is.empty.model(x))
stop ("No regressors in the model. \n")
else
{
terms <- attr(x, "terms")
x <- stats::model.matrix(terms, x)
var.names <- colnames(x)
}
nvar <- NCOL (x)
tkernel <- match.arg(tkernel)
est <- match.arg(est)
if(is.null(z))
{
upper <- 20
lower <- 5/obs
top <- 1
}
else
{
top <- (max(z) - min(z))*5
upper <- top
lower <- top * 0.001
}
value <- .Machine$double.xmax
iter <- 0
while(value == .Machine$double.xmax)
{
if(iter == 10)
{
value <- 0
bw <- top
warning("Maximum number of iterations reached in bandwidth calculation: either the function
is constant, or no convergence of bandwidth. \n")
break()
}
if (method != "within")
result <- try(stats::optim(stats::runif(1, lower, top), .tvRE.cv, method = "Brent",
lower = lower, upper = upper, x = x, y = y, z = z,
neq = neq, obs = obs, cv.block = cv.block, est = est,
tkernel = tkernel),
silent = FALSE)
else
result <- try(stats::optim(stats::runif(1, lower, top), .tvFE.cv, method = "Brent",
lower = lower, upper = top, x = x, y = y, z = z,
neq = neq, obs = obs, cv.block = cv.block, est = est,
tkernel = tkernel),
silent = FALSE)
if (!inherits(result, "list"))
value <- .Machine$double.xmax
else
{
if(is.na(result$value))
value <- .Machine$double.xmax
else
{
value <- result$value
bw <- result$par
}
}
iter <- iter + 1
}
return(abs(result$par))
}
#'
#' Covariance Bandwidth Calculation by Cross-Validation
#' \emph{bwCov} calculates a single bandwidth to estimate the time-varying variance-
#' covariance matrix.
#' @param x A matrix or a data frame.
#' @inheritParams bw
#' @return A scalar.
#' @examples
#'
#' data(CEES)
#' ## Using a shorter set for a quick example. Variable "Date" is removed.
#' mydata <- tail (CEES[, -1], 50)
#' bw.cov <- bwCov(mydata)
#' Sigma.hat <- tvCov(mydata, bw = bw.cov)
#'
#' @rdname bwCov
#' @export
bwCov <- function(x, cv.block = 0, est = c("lc", "ll"), tkernel = c("Triweight", "Epa", "Gaussian"))
{
if(!inherits(x, c("matrix", "data.frame")))
stop("'x' should be a matrix or a data.frame.\n")
tkernel <- match.arg(tkernel)
est <- match.arg(est)
x <- as.matrix(x)
cv.block <- abs(cv.block)
obs <- NROW(x)
neq <- NCOL(x)
value <- .Machine$double.xmax
iter <- 0
while(value == .Machine$double.xmax)
{
if(iter == 10)
{
value <- 0
bw <- 20
warning("Maximum number of iterations reached in bandwidth calculation: either the function
is constant, or no convergence of bandwidth. \n")
break()
}
result <- try(stats::optim(stats::runif(1, 5/obs, 1), .tvCov.cv, method = "Brent",
lower = 5/obs, upper = 20, x = x, cv.block = cv.block,
est = est, tkernel = tkernel),
silent = TRUE)
if (!inherits(result, "list"))
value <- .Machine$double.xmax
else
{
value <- result$value
bw <- result$par
}
iter <- iter + 1
}
return(bw)
}