-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTPP.R
70 lines (61 loc) · 1.62 KB
/
TPP.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
#' True positive proportion (TPP)
#'
#' Computes the TPP based on the estimated and the true regression coefficient vectors.
#'
#' @param beta_hat Estimated regression coefficient vector.
#' @param beta True regression coefficient vector.
#' @param eps Numerical zero.
#'
#' @return True positive proportion (TPP).
#'
#' @export
#'
#' @examples
#' data("Gauss_data")
#' X <- Gauss_data$X
#' y <- c(Gauss_data$y)
#' beta <- Gauss_data$beta
#'
#' set.seed(1234)
#' res <- trex(X, y)
#' beta_hat <- res$selected_var
#'
#' TPP(beta_hat = beta_hat, beta = beta)
TPP <- function(beta_hat,
beta,
eps = .Machine$double.eps) {
# Remove all dimension attributes of length one
beta_hat <- drop(beta_hat)
beta <- drop(beta)
# Error control
if (!is.vector(beta_hat)) {
stop("'beta_hat' must be a vector.")
}
if (!is.numeric(beta_hat)) {
stop("'beta_hat' only allows numerical values.")
}
if (anyNA(beta_hat)) {
stop("'beta_hat' contains NAs. Please remove or impute them before proceeding.")
}
if (!is.vector(drop(beta))) {
stop("'beta' must be a vector.")
}
if (!is.numeric(beta)) {
stop("'beta' only allows numerical values.")
}
if (anyNA(beta)) {
stop("'beta' contains NAs. Please remove or impute them before proceeding.")
}
if (length(beta_hat) != length(beta)) {
stop("Length of beta_hat does not match length of beta.")
}
# Compute TPP
num_actives <- sum(abs(beta) > eps)
num_true_positives <- sum(abs(beta) > eps & abs(beta_hat) > eps)
if (num_actives == 0) {
tpp <- 0
} else {
tpp <- (num_true_positives / num_actives)
}
return(tpp)
}