R/MSE.mode.R

#' Calculate mean squared error based on the mode of the posterior
#'
#' @param prior Prior to calculate posterior. Specify posterior instead if available.
#' @param prob.range Range of values to calculate MSE over
#' @param length Number of values to calculate MSE for
#' @param n.binom Number of patients in new trial
#' @param mc.cores Number of cores for parallel
#' @param posterior Posterior density 
#'
#' @return A vector of error values
#' @export
#'
calc.MSE.mode <- function(prior, prob.range=c(.5,1), length=20, n.binom=30, mc.cores=1, posterior){

  P <- seq(prob.range[1],prob.range[2],len=length)



  if(missing(posterior)){
  MSE.for.x <- parallel::mclapply(0:n.binom, function(Xs){


      if(inherits(prior, "function")){
        post <- function(p,g=1) prior(p,Xs)*dbinom(x=Xs, size=n.binom, prob=p)/g
        #mode
         p <- optimise(post, interval=c(0,1), maximum=TRUE)
        #   square error
        sq.err <- (p$maximum-P)^2
        return(sq.err)
      } else if(inherits(prior, "mixture.prior")){
        post.list <- posterior.mixture.prior(Xs, n.binom, prior)
        return((mean.mixture.prior(post.list)-P)^2 + var.mixture.prior(post.list))
      } else if(inherits(prior, "list")){
        post.list <- posterior.mixture.prior(Xs, n.binom, prior[[Xs+1]])
        return((mean.mixture.prior(post.list)-P)^2 + var.mixture.prior(post.list))
      }
    }, mc.cores = mc.cores)
  } else if(!missing(posterior)){
    MSE.for.x <- parallel::mclapply(posterior, function(post){
      p <- optimise(post, interval=c(0,1), maximum=TRUE)
      sq.err <- (p$maximum-P)^2
      return(sq.err)
      }, mc.cores = mc.cores)
  }


  MSE.for.x <- matrix(unlist(MSE.for.x), nrow=length)


  sapply(seq_along(P), function(i){
    sum(MSE.for.x[i,] * dbinom(0:n.binom, size=n.binom, prob=P[i]))})
}

Try the StudyPrior package in your browser

Any scripts or data that you put into this service are public.

StudyPrior documentation built on May 2, 2019, 5:54 p.m.