Skip to content

Commit d0fb624

Browse files
committed
working on cov_flag option
1 parent 90c55d3 commit d0fb624

File tree

5 files changed

+47
-12
lines changed

5 files changed

+47
-12
lines changed

R/lpcde/R/lpcde.R

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,15 @@
4545
#' @param grid_spacing String, If equal to "quantile" will generate quantile-spaced grid evaluation points, otherwise will generate equally spaced points.
4646
#' @param normalize Boolean, False (default) returns original estimator, True normalizes estimates to integrate to 1.
4747
#' @param nonneg Boolean, False (default) returns original estimator, True returns maximum of estimate and 0.
48+
#' @param cov_flag String, specifies covariance computation. Must be one of "full" (default), "diag" or "off".
4849
#' @return
4950
#' \item{Estimate}{ A matrix containing (1) \code{grid} (grid points),\cr
5051
#' (2) \code{bw} (bandwidths),\cr
5152
#' (3) \code{est} (point estimates with p-th and q-th order local polynomial),\cr
5253
#' (4) \code{est_RBC} (point estimates with p_RBC-th and q_RBC-th order local polynomial),\cr
53-
#' (5) \code{se} (standard error corresponding to \code{est}).
54-
#' (6) \code{se_RBC} (standard error corresponding to \code{est_RBC}).}
55-
#' \item{CovMat}{The variance-covariance matrix corresponding to \code{est}.}
54+
#' (5) \code{se} (standard error corresponding to \code{est}. Set to NA if cov_flag="off").
55+
#' (6) \code{se_RBC} (standard error corresponding to \code{est_RBC}). Set to NA if cov_flag="off"}
56+
#' \item{CovMat}{The variance-covariance matrix corresponding to \code{est}. Will be 0 if cov_flag="off" or a diagonal matrix if cov_flag="diag".}
5657
#' \item{opt}{A list containing options passed to the function.}
5758
#' @details Bias correction is only used for the construction of confidence intervals/bands, but not for point estimation.
5859
#' The point estimates, denoted by \code{est}, are constructed using local polynomial estimates of order \code{p} and \code{q},
@@ -108,6 +109,7 @@
108109
#' @export
109110
lpcde = function(x_data, y_data, y_grid=NULL, x=NULL, bw=NULL, p=NULL, q=NULL,
110111
p_RBC=NULL, q_RBC=NULL, mu=NULL, nu=NULL, rbc = TRUE, ng=NULL,
112+
cov_flag=c("full", "diag", "off"),
111113
normalize=FALSE, nonneg=FALSE, grid_spacing="",
112114
kernel_type=c("epanechnikov", "triangular", "uniform"),
113115
bw_type=NULL){
@@ -138,6 +140,15 @@ lpcde = function(x_data, y_data, y_grid=NULL, x=NULL, bw=NULL, p=NULL, q=NULL,
138140
stop("Data should be numeric, and cannot be empty.\n")
139141
}
140142

143+
if (length(cov_flag) == 0) {
144+
cov_flag = "full"
145+
} else {
146+
cov_flag = cov_flag[1]
147+
if (!cov_flag%in%c("full", "diag", "off")){
148+
stop("Incorrect covariance estimation flag provided. Please see the documentation on available options.")
149+
}
150+
}
151+
141152
#sd_y = stats::sd(y_data)
142153
#sd_x = apply(x_data, 2, stats::sd)
143154
#mx = apply(x_data, 2, mean)
@@ -293,7 +304,7 @@ lpcde = function(x_data, y_data, y_grid=NULL, x=NULL, bw=NULL, p=NULL, q=NULL,
293304

294305
lpcdest = lpcde_fn(y_data = y_data, x_data = x_data, y_grid = y_grid,
295306
x = x, p = p, q = q, p_RBC = p_RBC, q_RBC = q_RBC,
296-
bw = bw, mu = mu, nu = nu,
307+
bw = bw, mu = mu, nu = nu, cov_flag = cov_flag,
297308
kernel_type = kernel_type, rbc = rbc)
298309
rownames(lpcdest$est) = 1:ng
299310

@@ -319,7 +330,7 @@ lpcde = function(x_data, y_data, y_grid=NULL, x=NULL, bw=NULL, p=NULL, q=NULL,
319330
opt=list(
320331
p=p, q=q, p_RBC = p_RBC, q_RBC = q_RBC,
321332
mu=mu, nu=nu, kernel=kernel_type, n=length(y_data), ng=ng,
322-
bw_type=bw_type, bw = bw, xeval = x,
333+
bw_type=bw_type, bw = bw, xeval = x, cov_flag = cov_flag,
323334
y_data_min=min(y_data), y_data_max=max(y_data),
324335
x_data_min=min(x_data), x_data_max=max(x_data),
325336
grid_min=min(y_grid), grid_max=max(y_grid)

R/lpcde/R/lpcde_fns.R

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@
1818
#' @param mu Degree of derivative with respect to y.
1919
#' @param nu Degree of derivative with respect to x.
2020
#' @param kernel_type Kernel function choice.
21+
#' @param cov_flag Flag for covariance estimation option.
2122
#' @param rbc Boolean for whether to return RBC estimate and standard errors.
2223
# @param var_type String. type of variance estimator to implement.
2324
# choose from "ustat" and "asymp".
2425
#' @return Conditional density estimate at all grid points.
2526
#' @keywords internal
2627
#' @importFrom Rdpack reprompt
2728
lpcde_fn = function(y_data, x_data, y_grid, x, p, q, p_RBC, q_RBC, bw, mu, nu,
28-
kernel_type, rbc = FALSE){
29+
cov_flag, kernel_type, rbc = FALSE){
2930
sd_y = stats::sd(y_data)
3031
sd_x = apply(x_data, 2, stats::sd)
3132
mx = apply(x_data, 2, mean)
@@ -48,12 +49,19 @@ lpcde_fn = function(y_data, x_data, y_grid, x, p, q, p_RBC, q_RBC, bw, mu, nu,
4849
est_flag = f_hat_val$singular_flag
4950

5051
# standard errors
51-
# if(var_type=="ustat"){
52+
if(cov_flag=="full"){
5253
covmat = cov_hat(x_data=x_data, y_data=y_data, x=x, y_grid=y_grid, p=p, q=q,
5354
mu=mu, nu=nu, h=bw, kernel_type=kernel_type)
5455
covMat = covmat$cov
5556
c_flag = covmat$singular_flag
5657
se = sqrt(abs(diag(covMat)))*sd_y*mean(sd_x)
58+
} else if (cov_flag == "diag"){
59+
60+
} else if (cov_flag == "off"){
61+
covMat = NA
62+
c_flag = FALSE
63+
se = rep(NA, length(y_grid))
64+
}
5765

5866
if (rbc){
5967
est_rbc = matrix(0L, nrow = length(y_grid), ncol = 1)
@@ -75,13 +83,21 @@ lpcde_fn = function(y_data, x_data, y_grid, x, p, q, p_RBC, q_RBC, bw, mu, nu,
7583
# covariance matrix
7684

7785
# standard errors
78-
# if(var_type=="ustat"){
86+
if(cov_flag=="full"){
7987
covmat_rbc = cov_hat(x_data=x_data, y_data=y_data, x=x, y_grid=y_grid,
8088
p=p_RBC, q=q_RBC, mu=mu, nu=nu, h=bw,
8189
kernel_type=kernel_type)
8290
covMat_rbc = covmat_rbc$cov
8391
c_rbc_flag = covmat_rbc$singular_flag
8492
se_rbc = sqrt(abs(diag(covMat_rbc)))*sd_y*mean(sd_x)
93+
} else if (cov_flag == "diag"){
94+
95+
} else if (cov_flag == "off"){
96+
covMat_rbc = NA
97+
c_rbc_flag = FALSE
98+
se_rbc = rep(NA, length(y_grid))
99+
100+
}
85101
}
86102

87103
# with rbc results

R/lpcde/man/lpcde.Rd

Lines changed: 6 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

R/lpcde/man/lpcde_fn.Rd

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

todo.org

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
* JOSS Revision
55
** DONE bandwidth selection error example
66
*** DONE typo in documentation
7-
** TODO check grid redundancy
7+
** DONE check grid redundancy
8+
** TODO Implement diag cov estimation
9+
** TODO multiple x inputs
810
** TODO adding predict method
911
** DONE add more unit tests
1012
*** DONE checking integrates to 1

0 commit comments

Comments
 (0)