Last active
March 4, 2016 18:22
-
-
Save kylebaron/0aea16f97cf3265366d0 to your computer and use it in GitHub Desktop.
Manually pass the seed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(shiny) | |
library(mrgsolve) | |
library(dplyr) | |
library(magrittr) | |
library(parallel) | |
RNGkind("L'Ecuyer-CMRG") | |
set.seed(101) | |
mc.reset.stream() | |
##' A built in model | |
code <- ' | |
$OMEGA 0 0 | |
$TABLE | |
table(ETA1) = ETA(1); | |
table(ETA2) = ETA(2); | |
' | |
mod <- mread(code=code, "shinymc", tempdir(),warn=FALSE) | |
##' UI ########################################### | |
ui<- fluidPage( | |
titlePanel("Shiny MC"), | |
sidebarLayout( | |
sidebarPanel( | |
sliderInput("OM1", "OMEGA 1",0,4,1,0.25), | |
sliderInput("OM2", "OMEGA 2",0,4,1,.25), | |
sliderInput("n", "N rep", 1,100,10,1), | |
sliderInput("N", "N subj", 1, 100,10,1), | |
sliderInput("mccores", "mc.cores", 1,4,2,1), | |
checkboxInput("fix", "Implement workaround",FALSE) | |
), | |
mainPanel(tableOutput("table") | |
) | |
) | |
) | |
##' Simulate | |
##' if(fix) the seed will be set based on replicate number | |
sim <- function(i,x,idata,fix,SEEDS) { | |
RNGkind("L'Ecuyer-CMRG") | |
.Random.seed <- SEEDS[[i]] | |
x %>% idata_set(idata) %>% mrgsim(end=-1) %>% mutate(irep=i) | |
} | |
##' Summarize simulations | |
smry <- function(x) { | |
x %>% summarise(nETA1 = n_distinct(ETA1), | |
vETA1 = var(ETA1), | |
sETA1 = sum(ETA1), | |
nETA2 = n_distinct(ETA2), | |
vETA2 = var(ETA2), | |
sETA2 = sum(ETA2), | |
n=n()) | |
} | |
##' SERVER ########################################### | |
server<-function(input, output) { | |
RNGkind("L'Ecuyer-CMRG") | |
output$table <- renderTable({ | |
idata <- data_frame(ID=1:input$N) | |
mod %<>% omat(dmat(input$OM1,input$OM2)) | |
fix <- input$fix | |
RNGkind("L'Ecuyer-CMRG") | |
set.seed(2020229) | |
s <- .Random.seed | |
SEEDS <- as.list(vector("numeric",input$n)) | |
for(i in 1:input$n) { | |
s <- nextRNGStream(s) | |
SEEDS[[i]] <- s | |
} | |
mclapply(1:input$n, | |
mc.cores=input$mccores, | |
sim, | |
mod, | |
idata, | |
fix, | |
SEEDS) %>% bind_rows %>% smry | |
}) | |
} | |
##' Run the shiny app | |
shinyApp(ui = ui, server = server) | |
##' Run the shiny app simulation outside of shiny | |
RNGkind("L'Ecuyer-CMRG") | |
set.seed(101) | |
mc.reset.stream() | |
out <- mclapply(1:10,mc.cores=4,sim, | |
mod %>% omat(dmat(1,1)), | |
data_frame(ID=1:100),FALSE) %>% bind_rows | |
out %>% smry | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment