Skip to content

Instantly share code, notes, and snippets.

@phewson
Created November 11, 2014 08:37
Show Gist options
  • Save phewson/9996a1e532d6aecfa9d1 to your computer and use it in GitHub Desktop.
Save phewson/9996a1e532d6aecfa9d1 to your computer and use it in GitHub Desktop.
Illustration of Randomisation Tests
library(shiny)
# Define server logic required to generate and plot a random distribution
shinyServer(function(input, output) {
x1 <- c(3.36, 3.24, 5.16, 6.09, 3.08, 6.97, 4.85, 5.42, 5.15, 3.61, 4.95, 2.22)
x2 <- c(2.72, 3.09, 1.49, 1.98, 0.85, 2.56, 2.14, 1.80, 0.57, 3.16, 0.90, 0.15)
x <- c(x1, x2)
mycol = c(rep("red", 12), rep("blue", 12))
ind <- c(rep(1,12), rep(2,12))
X <- matrix("numeric", 24, 1001)
X[,1] <- c(1:24)
for (j in 2:1001){
X[,j] <- sample(c(1:24))
}
Y <- vector("numeric", 1001)
for (j in 1:1001){
Y[j] <- mean(x[as.numeric(X[1:12,j])]) - mean(x[as.numeric(X[13:24,j])])
}
hp <- hist(Y, plot = FALSE)
#
output$DataShuffle <- reactivePlot(function() {
# this gives the numbers
r <- as.numeric(input$replicates)
par(oma =c(0,0,0,0), xpd = NA)
##par(mfrow = c(2,1))
layout(matrix(c(1,1,2,2), 2, 2, byrow = TRUE), widths=lcm(5), heights=lcm(10))
#layout(matrix(c(1,1,2,3), 2, 2, byrow = TRUE))
##j <- 1
plot(c(1,6), c(0,1), xaxt="n", yaxt="n", bty="n", type = "n", xlab = "", ylab = "", main = "")
if (r ==1) {text(3,1.3,"Original Data", cex = 1.5)} else{text(3,1.3,paste("Shuffled Data:",(r-1)), cex = 1.5)}
text(1:6, rep(1,6), x[as.numeric(X[c(1:6),r])], col = mycol[as.numeric(X[c(1:6),r])], cex = 1.5)
text(1:6, rep(0.8,6), x[as.numeric(X[c(7:12),r])], col = mycol[as.numeric(X[c(7:12),r])], cex = 1.5)
text(1:6, rep(0,6), x[as.numeric(X[c(13:18),r])], col = mycol[as.numeric(X[c(13:18),r])], cex = 1.5)
text(1:6, rep(0.2,6), x[as.numeric(X[c(19:24),r])], col = mycol[as.numeric(X[c(19:24),r])], cex = 1.5)
t1 <- bquote(bar(x)[1] - bar(x)[2] ==.(round(mean(x[as.numeric(X[c(1:12),r])]) - mean(x[as.numeric(X[c(13:24),r])]),2)) )
text(3.5, 0.5, t1, cex=2)
})
output$NullHypHist <- reactivePlot(function(){
r <- as.numeric(input$replicates)
par(oma =c(0,0,0,0), las = 1)
##par(mfrow = c(2,1))
hist(Y[1:r], xlim = c(-5, 5), breaks = hp$breaks, ylim = c(0, max(hp$counts)), main = "Null Hypothesis Distribution")
points(Y[r], 0, pch = 16, col = "red")
#acplot(1,1)
})
})
library(shiny)
# Define UI for application that plots random distributions
shinyUI(pageWithSidebar(
# Application title
headerPanel("Simple randomisation test"),
# Sidebar with a slider input for number of observations
sidebarPanel(
sliderInput("replicates",
"Number of shuffles:",
min = 1,
max = 1000,
value = 1)
),
# Show a plot of the generated distribution
mainPanel(
#conditionalPanel(condition = "input.DataShuffle",
plotOutput(outputId = "DataShuffle"),
#conditionalPanel(condition = "input.NullHypHist",
plotOutput(outputId = "NullHypHist")
)
))
@phewson
Copy link
Author

phewson commented Nov 11, 2014

Actually, the slider bar doesn't work too well (used to work fine as a tkPanel app) and you don't get to see it moving in steps of 1. Maybe this is better as an animation in any case and you set the delay time/?????

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment