Difference between revisions of "Convest.R"

From Organic Design wiki
m
(rm comment, tolerance arg was added to examine convergence vs speed)
 
(10 intermediate revisions by the same user not shown)
Line 1: Line 1:
# {{R}}
+
# ---------------------- Modified convest function ---------------------------- # {{R}}
 +
# f.hat.diff <- (f.hat.p - f.theta.hat.p) computed for efficiency
 +
# Second redundant pi.0.hat <- f.hat[101] removed
 +
# !--------------------- Modified convest function ---------------------------- #
 +
 
 +
 
 +
 
 
`convest` <-
 
`convest` <-
   function (p, niter = 100, doplot = FALSE, doreport = FALSE)  
+
   function (p, niter = 100, doplot = FALSE, doreport = FALSE, file="", tol=1e-06) {
{
 
 
   if (!length(p))  
 
   if (!length(p))  
 
     return(NA)
 
     return(NA)
Line 10: Line 15:
 
     stop("All p-values must be between 0 and 1")
 
     stop("All p-values must be between 0 and 1")
 
   k <- niter
 
   k <- niter
   ny <- 1e-06
+
   ny <- tol
 
   p <- sort(p)
 
   p <- sort(p)
 
   m <- length(p)
 
   m <- length(p)
Line 20: Line 25:
 
   f.hat <- rep(1, 101)
 
   f.hat <- rep(1, 101)
 
   f.hat.p <- rep(1, m)
 
   f.hat.p <- rep(1, m)
   theta.hat <- 0.01 * which.max(apply(t.grid.mat, 1, function(theta) sum((2 *  
+
 
                                                                          (theta - p) * (p < theta)/theta^2))))
+
   theta.hat <- 0.01 * which.max(apply(t.grid.mat, 1,
 +
                                      function(theta) sum((2 * (theta - p) * (p < theta)/theta^2))))
 
   f.theta.hat <- 2 * (theta.hat - x.grid) * (x.grid < theta.hat)/theta.hat^2
 
   f.theta.hat <- 2 * (theta.hat - x.grid) * (x.grid < theta.hat)/theta.hat^2
 
   f.theta.hat.p <- 2 * (theta.hat - p) * (p < theta.hat)/theta.hat^2
 
   f.theta.hat.p <- 2 * (theta.hat - p) * (p < theta.hat)/theta.hat^2
Line 27: Line 33:
 
   j <- 0
 
   j <- 0
 
   thetas <- numeric()
 
   thetas <- numeric()
 +
 +
  if (doreport == TRUE) {
 +
    cat("j\tpi0\ttheta.hat\t\tepsilon\tD\n", file=file, append=FALSE)
 +
  }
 
   for (j in 1:k) {
 
   for (j in 1:k) {
     if (sum((f.hat.p - f.theta.hat.p)/f.hat.p) > 0)  
+
     f.hat.diff <- (f.hat.p - f.theta.hat.p)
 +
    if (sum(f.hat.diff/f.hat.p) > 0)  
 
       eps <- 0
 
       eps <- 0
 
     else {
 
     else {
Line 35: Line 46:
 
       while (abs(u - l) > ny) {
 
       while (abs(u - l) > ny) {
 
         eps <- (l + u)/2
 
         eps <- (l + u)/2
         if (sum(((f.hat.p - f.theta.hat.p)/((1 - eps) *  
+
         if (sum((f.hat.diff/((1 - eps) *  
                                            f.hat.p + eps * f.theta.hat.p))[f.hat.p > 0]) <  
+
                              f.hat.p + eps * f.theta.hat.p))[f.hat.p > 0]) < 0) {
            0)  
 
 
           l <- eps
 
           l <- eps
         else u <- eps
+
         } else {
 +
          u <- eps
 +
        }
 
       }
 
       }
 
     }
 
     }
 
     f.hat <- (1 - eps) * f.hat + eps * f.theta.hat
 
     f.hat <- (1 - eps) * f.hat + eps * f.theta.hat
 
     pi.0.hat <- f.hat[101]
 
     pi.0.hat <- f.hat[101]
     d <- -sum((f.theta.hat.p - f.hat.p)/f.hat.p)
+
     d <- sum((f.hat.diff)/f.hat.p)
 
     if (doreport == TRUE) {
 
     if (doreport == TRUE) {
       cat("j:", j, "\tpi0:", pi.0.hat, "\ttheta.hat:",  
+
       cat(j, "\t",pi.0.hat, "\t",theta.hat,"\t",eps, "\t",d, "\n", file=file, append=TRUE)
          theta.hat, "\t\tepsilon:", eps, "\tD:", d, "\n")
 
 
     }
 
     }
 
     f.hat.p <- 100 * (f.hat[100 * p.f + 1] - f.hat[100 *  
 
     f.hat.p <- 100 * (f.hat[100 * p.f + 1] - f.hat[100 *  
Line 65: Line 76:
 
       i <- i + 1
 
       i <- i + 1
 
     }
 
     }
    pi.0.hat <- f.hat[101]
+
#    pi.0.hat <- f.hat[101]
 
     if (doplot == TRUE) {
 
     if (doplot == TRUE) {
 
       plot(x.grid, f.hat, type = "l", main = paste(format(round(pi.0.hat,  
 
       plot(x.grid, f.hat, type = "l", main = paste(format(round(pi.0.hat,  

Latest revision as of 23:29, 24 June 2007

  1. ---------------------- Modified convest function ---------------------------- #

Code snipits and programs written in R, S or S-PLUS

  1. f.hat.diff <- (f.hat.p - f.theta.hat.p) computed for efficiency
  2. Second redundant pi.0.hat <- f.hat[101] removed
  3.  !--------------------- Modified convest function ---------------------------- #


`convest` <-

 function (p, niter = 100, doplot = FALSE, doreport = FALSE, file="", tol=1e-06) {
 if (!length(p)) 
   return(NA)
 if (any(is.na(p))) 
   stop("Missing values in p not allowed")
 if (any(p < 0 | p > 1)) 
   stop("All p-values must be between 0 and 1")
 k <- niter
 ny <- tol
 p <- sort(p)
 m <- length(p)
 p.c <- ceiling(100 * p)/100
 p.f <- floor(100 * p)/100
 t.grid <- (1:100)/100
 x.grid <- (0:100)/100
 t.grid.mat <- matrix(t.grid, ncol = 1)
 f.hat <- rep(1, 101)
 f.hat.p <- rep(1, m)
 
 theta.hat <- 0.01 * which.max(apply(t.grid.mat, 1,
                                     function(theta) sum((2 * (theta - p) * (p < theta)/theta^2))))
 f.theta.hat <- 2 * (theta.hat - x.grid) * (x.grid < theta.hat)/theta.hat^2
 f.theta.hat.p <- 2 * (theta.hat - p) * (p < theta.hat)/theta.hat^2
 i <- 1
 j <- 0
 thetas <- numeric()

  if (doreport == TRUE) {
    cat("j\tpi0\ttheta.hat\t\tepsilon\tD\n", file=file, append=FALSE)
  }
 for (j in 1:k) {
   f.hat.diff <- (f.hat.p - f.theta.hat.p)
   if (sum(f.hat.diff/f.hat.p) > 0) 
     eps <- 0
   else {
     l <- 0
     u <- 1
     while (abs(u - l) > ny) {
       eps <- (l + u)/2
       if (sum((f.hat.diff/((1 - eps) * 
                              f.hat.p + eps * f.theta.hat.p))[f.hat.p > 0]) < 0) {
         l <- eps
       } else {
         u <- eps
       }
     }
   }
   f.hat <- (1 - eps) * f.hat + eps * f.theta.hat
   pi.0.hat <- f.hat[101]
   d <- sum((f.hat.diff)/f.hat.p)
   if (doreport == TRUE) {
     cat(j, "\t",pi.0.hat, "\t",theta.hat,"\t",eps, "\t",d, "\n", file=file, append=TRUE)
   }
   f.hat.p <- 100 * (f.hat[100 * p.f + 1] - f.hat[100 * 
                                                  p.c + 1]) * (p.c - p) + f.hat[100 * p.c + 1]
   theta.hat <- 0.01 * which.max(apply(t.grid.mat, 1, function(theta) sum((2 * 
                                                                           (theta - p) * (p < theta)/theta^2)/f.hat.p)))
   f.theta.hat <- 2 * (theta.hat - x.grid) * (x.grid < theta.hat)/theta.hat^2
   f.theta.hat.p <- 2 * (theta.hat - p) * (p < theta.hat)/theta.hat^2
   if (sum(f.theta.hat.p/f.hat.p) < sum(1/f.hat.p)) {
     theta.hat <- 0
     f.theta.hat <- rep(1, 101)
     f.theta.hat.p <- rep(1, m)
   }
   if (sum(thetas == theta.hat) == 0) {
     thetas[i] <- theta.hat
     thetas <- sort(thetas)
     i <- i + 1
   }
  1. pi.0.hat <- f.hat[101]
   if (doplot == TRUE) {
     plot(x.grid, f.hat, type = "l", main = paste(format(round(pi.0.hat, 
                                       5), digits = 5)), ylim = c(0, 1.2))
     points(thetas, f.hat[100 * thetas + 1], pch = 20, 
            col = "blue")
   }
 }
 return(pi.0.hat)

}