uhcsim

Function that samples, randomly, locations from non-stratified test data. Will return an array of dimension nsims x nused_test x p (where p is the number of predictors to be validated)

importFrom = MASS

uhcsim <- function(nsims, nused_test, xmat, fit_rsf, z){
  # array to store chosen x's for each simulation
  x_sim_choice <- array(NA,dim=c(nsims,nused_test,ncol(z))) 
  p <- length(coef(fit_rsf)) # number of predictors in the model

  # new beta^ for each simulation
  beta.hats <- mvrnorm(nsims,coef(fit_rsf)[-1], vcov(fit_rsf)[2:p,2:p])  
  ntot.test <- nrow(xmat) # total number of test observations
  z <- as.matrix(z) # z has to be a matrix for the code to work

  for(i in 1:nsims){
    # probability of choosing each location
    wx.pred <- exp(as.matrix(xmat)%*%beta.hats[i,]) 
    # choices
    inds <- sample(1:ntot.test, nused_test, replace=FALSE, prob=wx.pred) 
    # covariates at chosen locations
    x_sim_choice[i,,] <- z[inds,] 
  }
  return(x_sim_choice)
}

spun with ezspin(“uhcplots/functions/uhcsim.R”, out_dir = “uhcplots/output”, fig_dir=“uhcplots/figures”, keep_md=F)