## See help("example_reftable") for SLRT() examples;
## continuing from there, after refine() steps for good results:
# set.seed(123);mean(get_LRboot(slik_j, nsim=500, reset=TRUE)) # close to df=2
# mean(get_LRboot(slik_j, h0_pars = "s2", nsim=500, reset=TRUE)) # close to df=1
if (FALSE) {
### *Old* simulation study of performance of the corrected LRTs:
## Same toy example as in help("example_reftable"):
blurred <- function(mu,s2,sample.size) {
s <- rnorm(n=sample.size,mean=mu,sd=sqrt(s2))
s <- exp(s/4)
return(c(mean=mean(s),var=var(s)))
}
## First build a largish reference table and projections to be used in all replicates
# Only the 600 first rows will be used as initial reference table for each "data"
#
set.seed(123)
#
parsp_j <- data.frame(mu=runif(6000L,min=2.8,max=5.2),
s2=runif(6000L,min=0.4,max=2.4),sample.size=40)
dsimuls <- add_reftable(,Simulate="blurred", parsTable=parsp_j,verbose=FALSE)
#
mufit <- project("mu",stats=c("mean","var"),data=dsimuls,verbose=TRUE)
s2fit <- project("s2",stats=c("mean","var"),data=dsimuls,verbose=TRUE)
dprojectors <- list(MEAN=mufit,VAR=s2fit)
dprojSimuls <- project(dsimuls,projectors=dprojectors,verbose=FALSE)
## Function for single-data analysis:
#
foo <- function(y, refine_maxit=0L, verbose=FALSE) {
dSobs <- blurred(mu=4,s2=1,sample.size=40)
## ----Inference workflow-----------------------------------------------
dprojSobs <- project(dSobs,projectors=dprojectors)
dslik <- infer_SLik_joint(dprojSimuls[1:600,],stat.obs=dprojSobs,verbose=FALSE)
dslik <- MSL(dslik, verbose=verbose, eval_RMSEs=FALSE)
if (refine_maxit) dslik <- refine(dslik, maxit=refine_maxit)
## ---- LRT-----------------------------------------------
lrt <- SLRT(dslik, h0=c(s2=1), nsim=200)
c(basic=lrt$basicLRT$p_value,raw=lrt$rawBootLRT$p_value,
bart=lrt$BartBootLRT$p_value,safe=lrt$safeBartBootLRT$p_value)
}
## Simulations using convenient parallelization interface:
#
# library(doSNOW) # optional
#
bootreps <- spaMM::dopar(matrix(1,ncol=200,nrow=1), # 200 replicates of foo()
fn=foo, fit_env=list(blurred=blurred, dprojectors=dprojectors, dprojSimuls=dprojSimuls),
control=list(.errorhandling = "pass", .packages = "Infusion"),
refine_maxit=5L,
nb_cores=parallel::detectCores()-1L, iseed=123)
#
plot(ecdf(bootreps["basic",]))
abline(0,1)
plot(ecdf(bootreps["bart",]), add=TRUE, col="blue")
plot(ecdf(bootreps["safe",]), add=TRUE, col="red")
plot(ecdf(bootreps["raw",]), add=TRUE, col="green")
#
# Note that refine() iterations are important for good performance.
# Without them, even a larger reftable of 60000 lines
# may exhibit poor results for some of the CI types.
}
Run the code above in your browser using DataLab