Skip to content

Instantly share code, notes, and snippets.

@calpolystat
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save calpolystat/8f525780393a05e7d0fd to your computer and use it in GitHub Desktop.
Save calpolystat/8f525780393a05e7d0fd to your computer and use it in GitHub Desktop.
Performance of the Wilcoxon-Mann-Whitney vs. t-test: Shiny app at http://www.statistics.calpoly.edu/shiny
Performance of the Wilcoxon-Mann-Whitney vs. t-test Shiny App
Base R code created by Jimmy Wong
Shiny app files created by Jimmy Wong
Cal Poly Statistics Dept Shiny Series
http://statistics.calpoly.edu/shiny
Title: Performance of the Wilcoxon-Mann-Whitney vs. t-test
Author: Jimmy Wong
AuthorUrl: https://www.linkedin.com/in/jimmywong46
License: MIT
DisplayMode: Normal
Tags: Wilcoxon-Mann-Whitney, t-test
Type: Shiny
The MIT License (MIT)
Copyright (c) 2015 Jimmy Wong
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
# -----------------------------------------------------------------
# App Title: Performance of Wilcoxon-Mann-Whitney Test and t-test
# Author: Jimmy Wong
# -----------------------------------------------------------------
#####################################################################################################################
#####################################################################################################################
## Libraries
#####################################################################################################################
#####################################################################################################################
library(ggplot2)
library(gridExtra)
library(googleVis)
library(shinyIncubator)
#####################################################################################################################
#####################################################################################################################
## Global Functions
#####################################################################################################################
#####################################################################################################################
# Comparing type 1 error/power of t and wmw between two normal populations
comp.norm = function(n.norm1,n.norm2,mean.normal1,mean.normal2,alpha,nsim)
{
t.diff=NULL
wil.diff=NULL
tstat=NULL
wilstat=NULL
for (i in 1:nsim)
{
x = rnorm(n.norm1, mean=mean.normal1)
y = rnorm(n.norm2, mean=mean.normal2)
t = t.test(x, y)
t.diff[i] = (t$p.value<alpha)*1
tstat[i] = t$statistic
wil = wilcox.test(x, y)
wilstat[i] = wil$statistic
wil.diff[i] = (wil$p.value<alpha)*1
}
(t.percent = mean(t.diff))
(wil.percent = mean(wil.diff))
return(list(t.percent, wil.percent, tstat, wilstat, t.diff, wil.diff))
}
# Comparing type 1 error/power of t and wmw between two populations with
# different shapes
comp.diffshape = function(n.norm,n.gam,nsim,mean.normal,shape,scale,alpha)
{
t.diff=NULL
wil.diff=NULL
tstat=NULL
wilstat=NULL
for (i in 1:nsim)
{
x = rnorm(n.norm, mean=mean.normal)
y = rgamma(n.gam, shape=shape, scale=scale)
t = t.test(x, y)
t.diff[i] = (t$p.value<alpha)*1
tstat[i] = t$statistic
wil = wilcox.test(x, y)
wilstat[i] = wil$statistic
wil.diff[i] = (wil$p.value<alpha)*1
}
(t.percent = mean(t.diff))
(wil.percent = mean(wil.diff))
return(list(t.percent, wil.percent, tstat, wilstat, t.diff, wil.diff))
}
# Comparing type 1 error/power of t and wmw between two gamma populations
comp.skewed = function(n.gam1,n.gam2,nsim,shape,scale,alpha,far)
{
t.diff=NULL
wil.diff=NULL
tstat=NULL
wilstat=NULL
for (i in 1:nsim)
{
x = rgamma(n.gam1, shape=shape, scale=scale)
y = rgamma(n.gam2, shape=shape, scale=scale)+far
t = t.test(x, y)
t.diff[i] = (t$p.value<alpha)*1
tstat[i] = t$statistic
wil = wilcox.test(x, y)
wilstat[i] = wil$statistic
wil.diff[i] = (wil$p.value<alpha)*1
}
(t.percent = mean(t.diff))
(wil.percent = mean(wil.diff))
return(list(t.percent, wil.percent, tstat, wilstat, t.diff, wil.diff))
}
# Comparing type 1 error/power between two normals but allowing the mean of the normal
# distribution to vary
comp.vary.norm = function(norm.means,n.norm1,n.norm2,nsim,alpha,norm.mean2)
{
norm.means1 = seq(norm.means[1],norm.means[2],by=.1)
t.percent=NULL
wil.percent=NULL
i=0
while(i<length(norm.means1))
{
i=i+1
result = comp.norm(n.norm1=n.norm1,n.norm2=n.norm2,nsim=nsim,mean.normal1=norm.means1[i],
mean.normal2=norm.mean2,alpha=alpha)
t.percent[i] = result[[1]]
wil.percent[i] = result[[2]]
}
return(list(terror=t.percent,wmwerror=wil.percent))
}
# Comparing type 1 error/power between normal and gamma but allowing the mean of the normal
# distribution to vary
comp.vary.diffshape = function(norm.means,n.norm,n.gam,nsim,shape,scale,alpha)
{
norm.means1 = seq(norm.means[1],norm.means[2],by=.1)
t.percent=NULL
wil.percent=NULL
i=0
while(i<length(norm.means1))
{
i=i+1
result = comp.diffshape(n.norm=n.norm,n.gam=n.gam,nsim=nsim,mean.normal=norm.means1[i],
shape=shape,scale=scale,alpha)
t.percent[i] = result[[1]]
wil.percent[i] = result[[2]]
}
return(list(terror=t.percent,wmwerror=wil.percent))
}
# Comparing type 1 error/power between two gammas with same shape but allowing
# the mean of 1st gamma distribution to vary
comp.vary.skewed = function(farvary,n.gam1,n.gam2,nsim,shape,scale,alpha)
{
farvary1 = seq(farvary[1],farvary[2],by=.1)
t.percent=NULL
wil.percent=NULL
i=0
while(i<length(farvary1))
{
i=i+1
result = comp.skewed(n.gam1=n.gam1,n.gam2=n.gam2,nsim=nsim,shape=shape,
scale=scale,alpha=alpha,far=farvary1[i])
t.percent[i] = result[[1]]
wil.percent[i] = result[[2]]
}
return(list(terror=t.percent,wmwerror=wil.percent))
}
#####################################################################################################################
#####################################################################################################################
## Shiny Server
#####################################################################################################################
#####################################################################################################################
shinyServer(function(input, output, session) {
#####################################################################################################################
#####################################################################################################################
## Goal of Study Panel
#####################################################################################################################
#####################################################################################################################
output$intrograph = renderPlot({
x=seq(-3,3,length.out=200)
x1=seq(0,6,length.out=200)
g1 = ggplot() + geom_line(data.frame(x=x, y=dnorm(x,mean=0,sd=1)), mapping=aes(x=x, y=y), color="navy") +
geom_line(data.frame(x=x1, y=dnorm(x1,mean=3,sd=1)), mapping=aes(x=x, y=y), color="gold2") + xlim(-3,6) +
ggtitle("Two Normal populations\nwith different means") + xlab("") + ylab("") +
theme(axis.text = element_blank(), axis.ticks = element_blank()) + theme_bw()
x2=seq(0,13,length.out=200)
g2 = ggplot() + geom_line(data.frame(x=x2, y=dgamma(x2,shape=6,scale=.5)), mapping=aes(x=x, y=y), color="navy") +
geom_line(data.frame(x=x2+3, y=dgamma(x2,shape=6,scale=.5)), mapping=aes(x=x, y=y), color="gold2") + xlim(0,13) +
ggtitle("Two Gamma populations\nwith different means") + xlab("") + ylab("") +
theme(axis.text = element_blank(), axis.ticks = element_blank()) + theme_bw()
g3 = ggplot() + geom_line(data.frame(x=x2, y=dgamma(x2,shape=6,scale=.5)), mapping=aes(x=x, y=y), color="navy") +
geom_line(data.frame(x=x2, y=dnorm(x2,mean=3,sd=1)), mapping=aes(x=x, y=y), color="gold2") + xlim(0,13) +
ggtitle("Two populations with same mean\nbut different shapes") + xlab("") + ylab("") +
theme(axis.text = element_blank(), axis.ticks = element_blank()) + theme_bw()
g4 = ggplot() + geom_line(data.frame(x=x2, y=dgamma(x2,shape=6,scale=.5)), mapping=aes(x=x, y=y), color="navy") +
geom_line(data.frame(x=x2, y=dnorm(x2,mean=5,sd=1)), mapping=aes(x=x, y=y), color="gold2") + xlim(0,13) +
ggtitle("Two populations with different\nmeans and shapes") + xlab("") + ylab("") +
theme(axis.text = element_blank(), axis.ticks = element_blank()) + theme_bw()
gg = arrangeGrob(g1,g2,g3,g4,ncol=2)
return(gg)
})
#####################################################################################################################
#####################################################################################################################
## Guess the Population Panel
#####################################################################################################################
#####################################################################################################################
order = reactive({
input$resetgame
return(sample(c("normal1","normal2","gamma1","gamma2"),size=4))
})
output$guesspop = renderPlot({
input$resetgame
normal1 = ggplot() + geom_histogram(aes(x=rnorm(20,mean=3,sd=1)),fill="white",color="navy") + xlab("") + ylab("") +
theme(axis.text = element_blank(), axis.ticks = element_blank()) + theme_bw()
normal2 = ggplot() + geom_histogram(aes(x=rnorm(20,mean=3,sd=1)),fill="white",color="navy") + xlab("") + ylab("") +
theme(axis.text = element_blank(), axis.ticks = element_blank()) + theme_bw()
gamma1 = ggplot() + geom_histogram(aes(x=rgamma(20,shape=6,scale=.5)),fill="white",color="navy") + xlab("") + ylab("") +
theme(axis.text = element_blank(), axis.ticks = element_blank()) + theme_bw()
gamma2 = ggplot() + geom_histogram(aes(x=rgamma(20,shape=6,scale=.5)),fill="white",color="navy") + xlab("") + ylab("") +
theme(axis.text = element_blank(), axis.ticks = element_blank()) + theme_bw()
guesspop = arrangeGrob(get(order()[1]) + ggtitle("Random Data 1"),
get(order()[2]) + ggtitle("Random Data 2"),
get(order()[3]) + ggtitle("Random Data 3"),
get(order()[4]) + ggtitle("Random Data 4"),nrow=2)
print(guesspop)
})
output$answerinsert = renderUI({
input$resetgame
selectizeInput("answer","Your answer (from 1,2,3,4):",choices=c("normal"="normal_1","normal"="normal_2","gamma"="gamma_1","gamma"="gamma_2"),
multiple=TRUE,options=list(placeholder="select the order of distributions"))
})
output$answerreveal = renderUI({
input$resetgame
checkboxInput("reveal", "Reveal answer", FALSE)
})
output$answercheck = renderUI({
input$resetgame
if(length(input$answer)==4)
{
a1 = strsplit(input$answer[1],"_")[[1]][1]
a2 = strsplit(input$answer[2],"_")[[1]][1]
a3 = strsplit(input$answer[3],"_")[[1]][1]
a4 = strsplit(input$answer[4],"_")[[1]][1]
if(is.na(sum(pmatch(a1,order()[1]), pmatch(a2,order()[2]), pmatch(a3,order()[3]), pmatch(a4,order()[4]))))
{
return(code("Oops, try again!"))
} else if(sum(pmatch(a1,order()[1]), pmatch(a2,order()[2]), pmatch(a3,order()[3]), pmatch(a4,order()[4]))==4)
{
return(code("Congratulations, you are correct!"))
}
} else
{
return(code("Waiting for inputs..."))
}
})
output$answer = renderTable({
input$resetgame
tab = matrix(nrow=1,ncol=4)
colnames(tab) = c("Random Data 1","Random Data 2","Random Data 3","Random Data 4")
rownames(tab) = "Answer"
tab[1,1] = order()[1]
tab[1,2] = order()[2]
tab[1,3] = order()[3]
tab[1,4] = order()[4]
return(tab)
})
#####################################################################################################################
#####################################################################################################################
## Two Normal Populations Panel
#####################################################################################################################
#####################################################################################################################
res.comp = reactive({
input$start1
isolate({
return(comp.norm(n.norm1=input$nnorm1,n.norm2=input$nnorm2,mean.normal1=input$meannorm1,
mean.normal2=input$meannorm2,alpha=input$alpha1,nsim=input$nsim1))
})
})
output$normselected = renderUI({
p("You have selected", code(paste0("N(",input$meannorm1,",1)")), "and",
code(paste0("N(",input$meannorm2,",1)")))
})
output$satisfied1 = renderUI({
HTML("<li type=circle> The condition for the t-test is satisfied because the two population distributions are Normal. </li>
<li type=circle> The condition for the WMW test is satisfied because the two Normal population distributions have the same shape.</li>")
})
output$twonormplots = renderPlot({
x1=seq(input$meannorm1-3,input$meannorm1+3,length.out=200)
x2=seq(input$meannorm2-3,input$meannorm2+3,length.out=200)
ggplot() + geom_line(data.frame(x=x1, y=dnorm(x1,mean=input$meannorm1,sd=1)), mapping=aes(x=x, y=y), color="navy") +
geom_line(data.frame(x=x2, y=dnorm(x2,mean=input$meannorm2,sd=1)), mapping=aes(x=x, y=y), color="salmon") +
xlim(min(input$meannorm1-3,input$meannorm2-3),max(input$meannorm1+3,input$meannorm2+3)) +
ggtitle("Two Normal populations") + xlab("") + ylab("") + theme_bw() +
theme(axis.text.y = element_blank(), axis.ticks.y = element_blank())
})
output$twohistnorms = renderPlot({
input$start1
isolate({
dat = data.frame(t=res.comp()[[3]],wmw=res.comp()[[4]],t.ind=res.comp()[[5]],wmw.ind=res.comp()[[6]])
if(sum(dat$t.ind)==length(dat$t.ind) & input$meannorm1!=input$meannorm2)
{
dat$t.ind1 = factor(dat$t.ind,label="Rejected Ho")
lab1 = "seagreen3"
} else if(sum(dat$t.ind)==0 & input$meannorm1==input$meannorm2)
{
dat$t.ind1 = factor(dat$t.ind,label="Failed to reject Ho")
lab1 = "seagreen3"
} else if(sum(dat$t.ind)!=length(dat$t.ind) & input$meannorm1==input$meannorm2)
{
dat$t.ind1 = factor(dat$t.ind,label=c("Failed to reject Ho", "Rejected Ho"))
lab1 = c("seagreen3","tomato")
} else if(sum(dat$t.ind)!=0 & input$meannorm1!=input$meannorm2)
{
dat$t.ind1 = factor(dat$t.ind,label=c("Failed to reject Ho", "Rejected Ho"))
lab1 = c("tomato","seagreen3")
}
if(sum(dat$wmw.ind)==length(dat$wmw.ind) & input$meannorm1!=input$meannorm2)
{
dat$wmw.ind1 = factor(dat$wmw.ind,label="Rejected Ho")
lab2 = "seagreen3"
} else if(sum(dat$wmw.ind)==0 & input$meannorm1==input$meannorm2)
{
dat$wmw.ind1 = factor(dat$wmw.ind,label="Failed to reject Ho")
lab2 = "seagreen3"
} else if(sum(dat$wmw.ind)!=length(dat$wmw.ind) & input$meannorm1==input$meannorm2)
{
dat$wmw.ind1 = factor(dat$wmw.ind,label=c("Failed to reject Ho", "Rejected Ho"))
lab2 = c("seagreen3","tomato")
} else if(sum(dat$wmw.ind)!=0 & input$meannorm1!=input$meannorm2)
{
dat$wmw.ind1 = factor(dat$wmw.ind,label=c("Failed to reject Ho", "Rejected Ho"))
lab2 = c("tomato","seagreen3")
}
})
g1 = ggplot(dat) + geom_histogram(aes(x=t,fill=t.ind1), alpha=.7) + xlab("t-statistic") + ylab("Frequency") +
scale_fill_manual(name="Decision", values=lab1) + theme_bw() + theme(legend.position="bottom") +
ggtitle("Histogram of t-statistics")
g2 = ggplot(dat) + geom_histogram(aes(x=wmw,fill=wmw.ind1), alpha=.7) + xlab("WMW statistic") + ylab("Frequency") +
scale_fill_manual(name="Decision", values=lab2) + theme_bw() + theme(legend.position="bottom") +
ggtitle("Histogram of WMW statistics")
g3 = arrangeGrob(g1,g2,nrow=1)
return(g3)
})
output$text1 = renderUI({
input$start1
isolate({
p(HTML("<li type=square> Number of correct t-statistics ="),code(round(res.comp()[[1]]*input$nsim1)),HTML("</li>"),
HTML("<li type=square> Number of correct WMW statistics ="),code(round(res.comp()[[2]]*input$nsim1)),HTML("</li>"),
HTML("<li type=square> Number of simulations ="),code(input$nsim1))
})
})
# output$table1 = renderTable({
# input$start1
# isolate({
# if(input$start1>0 & input$meannorm1==input$meannorm2)
# {
# mat = matrix(c(res.comp()[[1]],res.comp()[[2]]),nrow=1,byrow=TRUE)
# rownames(mat) = "Type I error rate"
# colnames(mat) = c("t-test","WMW test")
# return(mat)
# } else if(input$start1>0 & input$meannorm1!=input$meannorm2)
# {
# mat = matrix(c(res.comp()[[1]],res.comp()[[2]]),nrow=1,byrow=TRUE)
# rownames(mat) = "Power"
# colnames(mat) = c("t-test","WMW test")
# return(mat)
# }
# })
# })
output$gauge1 = renderGvis({
input$start1
isolate({
dat = data.frame(test=c("t-test","WMW"),prob=round(c(res.comp()[[1]],res.comp()[[2]]),digits=3))
if(input$meannorm1!=input$meannorm2)
{
gauge=gvisGauge(dat, options=list(min=0,max=1,greenFrom=.75,greenTo=1,redFrom=0,redTo=.25,width=250,height=250))
} else if(input$meannorm1==input$meannorm2)
{
gauge=gvisGauge(dat, options=list(min=0,max=1,greenFrom=0,greenTo=input$alpha1,
yellowFrom=input$alpha1,yellowTo=input$alpha1*2,
redFrom=input$alpha1*2,redTo=1,width=250,height=250))
}
return(gauge)
})
})
res.vary = reactive({
input$start11
isolate({
comp.vary.norm(norm.means=input$varymeans1,n.norm1=input$nnorm11,n.norm2=input$nnorm21,nsim=input$nsim11,
norm.mean2=input$meannorm21,alpha=input$alpha11)
})
})
output$graph.norm = renderGvis({
input$start11
isolate({
if(input$start11>0)
{
res.dat = data.frame(mean = seq(input$varymeans1[1],input$varymeans1[2],by=.1),
t=round(res.vary()[[1]],digits=2),wmw=round(res.vary()[[2]],digits=2))
gvisLineChart(res.dat,
options=list(title="Comparing Type 1 error rate and power between t-test and WMW test",
hAxes="[{title:'Mean of 1st Normal distribution'}]",
vAxes="[{title:'P(rejecting Ho)'}]",
width=800,height=600,series="[{color:'#F08080'},{color:'navy'}]"))
# print(ggplot(data=res.dat) + geom_line(aes(x=mean,y=t.error,color="t-test")) +
# geom_line(aes(x=mean,y=wmw.error,color="WMW")) + xlab("Mean of Normal distribution") + ylab("P(rejecting Ho)") +
# scale_color_manual(name="Type of test",values=c("t-test"="navy","WMW"="gold2")) + theme(legend.position="bottom") +
# ggtitle("Comparing Type 1 error rate and\npower between t-test and WMW test"))
}
})
})
#####################################################################################################################
#####################################################################################################################
## Normal and Gamma Populations Panel
#####################################################################################################################
#####################################################################################################################
res.comp1 = reactive({
input$start2
isolate({
return(comp.diffshape(n.norm=input$nnorm3,n.gam=input$ngam1,nsim=input$nsim2,
mean.normal=input$meannorm3,shape=input$shape1,scale=input$scale1,alpha=input$alpha2))
})
})
gam.mean1 = reactive({
return(input$shape1*input$scale1)
})
output$normgamselected = renderUI({
p("You have selected", code(paste0("N(",input$meannorm3,",1)")), "and",
code(paste0("Gamma(",input$shape1,",",input$scale1,")")))
})
output$twonormgamplots = renderPlot({
x1=seq(min(input$meannorm3-3,0),max(input$meannorm3+3,gam.mean1()+7),length.out=200)
x2=seq(min(input$meannorm3-3,0),max(input$meannorm3+3,gam.mean1()+7),length.out=200)
ggplot() + geom_line(data.frame(x=x1, y=dnorm(x1,mean=input$meannorm3,sd=1)), mapping=aes(x=x, y=y), color="navy") +
geom_line(data.frame(x=x2, y=dgamma(x2,shape=input$shape1,scale=input$scale1)), mapping=aes(x=x, y=y), color="salmon") +
xlim(min(input$meannorm3-3,0),max(input$meannorm3+3,gam.mean1()+7)) +
ggtitle("Normal and Gamma populations") + xlab("") + ylab("") + theme_bw() +
theme(axis.text.y = element_blank(), axis.ticks.y = element_blank())
})
output$focusinfo1 = renderUI({
if(input$meannorm3==gam.mean1())
{
return(div("These two distributions have the same means; focus on", code("Type I error rate"), style = "color:navy"))
} else if(input$meannorm3!=gam.mean1())
{
return(div("These two distributions have different means; focus on", code("Power"), style = "color:navy"))
}
})
output$satisfied2 = renderUI({
HTML("<li type=circle> The condition for the t-test will be satisfied if the sample size for the Gamma population distribution is large enough (at least 20-30). </li>
<li type=circle> The condition for the WMW test is not satisfied because the two population distributions will never have the same shape.</li>")
})
output$twohistnormgams = renderPlot({
input$start2
isolate({
dat = data.frame(t=res.comp1()[[3]],wmw=res.comp1()[[4]],t.ind=res.comp1()[[5]],wmw.ind=res.comp1()[[6]])
if(sum(dat$t.ind)==length(dat$t.ind) & input$meannorm3!=gam.mean1())
{
dat$t.ind1 = factor(dat$t.ind,label="Rejected Ho")
lab1 = "seagreen3"
} else if(sum(dat$t.ind)==0 & input$meannorm3==gam.mean1())
{
dat$t.ind1 = factor(dat$t.ind,label="Failed to reject Ho")
lab1 = "seagreen3"
} else if(sum(dat$t.ind)!=length(dat$t.ind) & input$meannorm3==gam.mean1())
{
dat$t.ind1 = factor(dat$t.ind,label=c("Failed to reject Ho", "Rejected Ho"))
lab1 = c("seagreen3","tomato")
} else if(sum(dat$t.ind)!=0 & input$meannorm3!=gam.mean1())
{
dat$t.ind1 = factor(dat$t.ind,label=c("Failed to reject Ho", "Rejected Ho"))
lab1 = c("tomato","seagreen3")
}
if(sum(dat$wmw.ind)==length(dat$wmw.ind) & input$meannorm3!=gam.mean1())
{
dat$wmw.ind1 = factor(dat$wmw.ind,label="Rejected Ho")
lab2 = "seagreen3"
} else if(sum(dat$wmw.ind)==0 & input$meannorm3==gam.mean1())
{
dat$wmw.ind1 = factor(dat$wmw.ind,label="Failed to reject Ho")
lab2 = "seagreen3"
} else if(sum(dat$wmw.ind)!=length(dat$wmw.ind) & input$meannorm3==gam.mean1())
{
dat$wmw.ind1 = factor(dat$wmw.ind,label=c("Failed to reject Ho", "Rejected Ho"))
lab2 = c("seagreen3","tomato")
} else if(sum(dat$wmw.ind)!=0 & input$meannorm3!=gam.mean1())
{
dat$wmw.ind1 = factor(dat$wmw.ind,label=c("Failed to reject Ho", "Rejected Ho"))
lab2 = c("tomato","seagreen3")
}
})
g1 = ggplot(dat) + geom_histogram(aes(x=t,fill=t.ind1), alpha=.7) + xlab("t-statistic") + ylab("Frequency") +
scale_fill_manual(name="Decision", values=lab1) + theme_bw() + theme(legend.position="bottom") +
ggtitle("Histogram of t-statistics")
g2 = ggplot(dat) + geom_histogram(aes(x=wmw,fill=wmw.ind1), alpha=.7) + xlab("WMW statistic") + ylab("Frequency") +
scale_fill_manual(name="Decision", values=lab2) + theme_bw() + theme(legend.position="bottom") +
ggtitle("Histogram of WMW statistics")
g3 = arrangeGrob(g1,g2,nrow=1)
return(g3)
})
output$text2 = renderUI({
input$start2
isolate({
p(HTML("<li type=square> Number of correct t-statistics "),code(round(res.comp1()[[1]]*input$nsim2)),HTML("</li>"),
HTML("<li type=square> Number of correct WMW statistics ="),code(round(res.comp1()[[2]]*input$nsim2)),HTML("</li>"),
HTML("<li type=square> Number of simulations ="),code(input$nsim2))
})
})
# output$table2 = renderTable({
# input$start2
# isolate({
# if(input$start2>0 & input$meannorm3==gam.mean1())
# {
# mat = matrix(c(res.comp1()[[1]],res.comp1()[[2]]),nrow=1,byrow=TRUE)
# rownames(mat) = "Type I error rate"
# colnames(mat) = c("t-test","WMW test")
# return(mat)
# } else if(input$start2>0 & input$meannorm3!=gam.mean1())
# {
# mat = matrix(c(res.comp1()[[1]],res.comp1()[[2]]),nrow=1,byrow=TRUE)
# rownames(mat) = "Power"
# colnames(mat) = c("t-test","WMW test")
# return(mat)
# }
# })
# })
output$whichone = renderUI({
if(input$meannorm3==gam.mean1())
{
return(strong("Type I error rate:"))
} else if(input$meannorm3!=gam.mean1())
{
return(strong("Power:"))
}
})
output$gauge2 = renderGvis({
input$start2
isolate({
dat = data.frame(test=c("t-test","WMW"),prob=round(c(res.comp1()[[1]],res.comp1()[[2]]),digits=3))
if(input$meannorm3!=gam.mean1())
{
gauge=gvisGauge(dat, options=list(min=0,max=1,greenFrom=.75,greenTo=1,redFrom=0,redTo=.25,width=250,height=250))
} else if(input$meannorm3==gam.mean1())
{
gauge=gvisGauge(dat, options=list(min=0,max=1,greenFrom=0,greenTo=input$alpha2,
yellowFrom=input$alpha2,yellowTo=input$alpha2*2,
redFrom=input$alpha2*2,redTo=1,width=250,height=250))
}
return(gauge)
})
})
res.vary1 = reactive({
input$start21
isolate({
comp.vary.diffshape(norm.means=input$varymeans2,n.norm=input$nnorm31,n.gam=input$ngam11,
nsim=input$nsim21,shape=input$shape11,scale=input$scale11,alpha=input$alpha21)
})
})
output$graph.normgam = renderGvis({
input$start21
isolate({
if(input$start21>0)
{
res.dat = data.frame(mean = seq(input$varymeans2[1],input$varymeans2[2],by=.1),
t=round(res.vary1()[[1]],digits=2),wmw=round(res.vary1()[[2]],digits=2))
gvisLineChart(res.dat,
options=list(title="Comparing Type 1 error rate and power between t-test and WMW test",
hAxes="[{title:'Mean of Normal distribution'}]",
vAxes="[{title:'P(rejecting Ho)'}]",
width=800,height=600,series="[{color:'#F08080'},{color:'navy'}]"))
# print(ggplot(data=res.dat) + geom_line(aes(x=mean,y=t.error,color="t-test")) +
# geom_line(aes(x=mean,y=wmw.error,color="WMW")) + xlab("Mean of Normal distribution") + ylab("P(rejecting Ho)") +
# scale_color_manual(name="Type of test",values=c("t-test"="navy","WMW"="gold2")) + theme(legend.position="bottom") +
# ggtitle("Comparing Type 1 error rate and\npower between t-test and WMW test"))
}
})
})
#####################################################################################################################
#####################################################################################################################
## Two Gamma Populations Panel
#####################################################################################################################
#####################################################################################################################
res.comp2 = reactive({
input$start3
isolate({
return(comp.skewed(n.gam1=input$ngam2,n.gam2=input$ngam3,nsim=input$nsim3,shape=input$shape2,scale=input$scale2,
alpha=input$alpha3,far=input$far1))
})
})
gam.means = reactive({
return(c(input$shape2*input$scale2,(input$shape2*input$scale2)+input$far1))
})
output$gamselected = renderUI({
p("You have selected",code(paste0("Gamma(",input$shape2,",",input$scale2,")")), "and",
code(paste0("Gamma(",input$shape2,",",input$scale2,")+",input$far1)))
})
output$twogamplots = renderPlot({
x1=seq(0,max(gam.means()[1]*input$scale2*3+3,gam.means()[2]*input$scale2*3+3),length.out=1000)
ggplot() + geom_line(data.frame(x=x1, y=dgamma(x1,shape=input$shape2,scale=input$scale2)), mapping=aes(x=x, y=y), color="navy") +
geom_line(data.frame(x=x1+input$far1, y=dgamma(x1,shape=input$shape2,scale=input$scale2)), mapping=aes(x=x, y=y), color="salmon") +
xlim(0,max(gam.means()[1]*input$scale2*3+3,gam.means()[2]*input$scale2*3+3)) +
ggtitle("Two Gamma populations") + xlab("") + ylab("") + theme_bw() +
theme(axis.text.y = element_blank(), axis.ticks.y = element_blank())
})
output$focusinfo2 = renderUI({
if(gam.means()[1]==gam.means()[2])
{
return(div("These two Gamma distributions have the same means; focus on", code("Type I error rate"), style = "color:navy"))
} else if(gam.means()[1]!=gam.means()[2])
{
return(div("These two Gamma distributions have different means; focus on", code("Power"), style = "color:navy"))
}
})
output$satisfied3 = renderUI({
HTML("<li type=circle> The condition for the t-test will be satisfied if the sample sizes for the two Gamma population distributions is large enough (at least 20-30). </li>
<li type=circle> The condition for the WMW test is satisfied because the two population distributions have the same shape.</li>")
})
output$twohistgams = renderPlot({
input$start3
isolate({
dat = data.frame(t=res.comp2()[[3]],wmw=res.comp2()[[4]],t.ind=res.comp2()[[5]],wmw.ind=res.comp2()[[6]])
if(sum(dat$t.ind)==length(dat$t.ind) & gam.means()[1]!=gam.means()[2])
{
dat$t.ind1 = factor(dat$t.ind,label="Rejected Ho")
lab1 = "seagreen3"
} else if(sum(dat$t.ind)==0 & gam.means()[1]==gam.means()[2])
{
dat$t.ind1 = factor(dat$t.ind,label="Failed to reject Ho")
lab1 = "seagreen3"
} else if(sum(dat$t.ind)!=length(dat$t.ind) & gam.means()[1]==gam.means()[2])
{
dat$t.ind1 = factor(dat$t.ind,label=c("Failed to reject Ho", "Rejected Ho"))
lab1 = c("seagreen3","tomato")
} else if(sum(dat$t.ind)!=0 & gam.means()[1]!=gam.means()[2])
{
dat$t.ind1 = factor(dat$t.ind,label=c("Failed to reject Ho", "Rejected Ho"))
lab1 = c("tomato","seagreen3")
}
if(sum(dat$wmw.ind)==length(dat$wmw.ind) & gam.means()[1]!=gam.means()[2])
{
dat$wmw.ind1 = factor(dat$wmw.ind,label="Rejected Ho")
lab2 = "seagreen3"
} else if(sum(dat$wmw.ind)==0 & gam.means()[1]==gam.means()[2])
{
dat$wmw.ind1 = factor(dat$wmw.ind,label="Failed to reject Ho")
lab2 = "seagreen3"
} else if(sum(dat$wmw.ind)!=length(dat$wmw.ind) & gam.means()[1]==gam.means()[2])
{
dat$wmw.ind1 = factor(dat$wmw.ind,label=c("Failed to reject Ho", "Rejected Ho"))
lab2 = c("seagreen3","tomato")
} else if(sum(dat$wmw.ind)!=0 & gam.means()[1]!=gam.means()[2])
{
dat$wmw.ind1 = factor(dat$wmw.ind,label=c("Failed to reject Ho", "Rejected Ho"))
lab2 = c("tomato","seagreen3")
}
})
g1 = ggplot(dat) + geom_histogram(aes(x=t,fill=t.ind1), alpha=.7) + xlab("t-statistic") + ylab("Frequency") +
scale_fill_manual(name="Decision", values=lab1) + theme_bw() + theme(legend.position="bottom") +
ggtitle("Histogram of t-statistics")
g2 = ggplot(dat) + geom_histogram(aes(x=wmw,fill=wmw.ind1), alpha=.7) + xlab("WMW statistic") + ylab("Frequency") +
scale_fill_manual(name="Decision", values=lab2) + theme_bw() + theme(legend.position="bottom") +
ggtitle("Histogram of WMW statistics")
g3 = arrangeGrob(g1,g2,nrow=1)
return(g3)
})
output$text3 = renderUI({
input$start3
isolate({
p(HTML("<li type=square> Number of correct t-statistics "),code(round(res.comp2()[[1]]*input$nsim3)),HTML("</li>"),
HTML("<li type=square> Number of correct WMW statistics ="),code(round(res.comp2()[[2]]*input$nsim3)),HTML("</li>"),
HTML("<li type=square> Number of simulations ="),code(input$nsim3))
})
})
output$type1powertext = renderUI({
input$start3
isolate({
if(input$far1==0)
{
return(strong("Type I error rate:"))
} else if(input$far1>0)
{
return(strong("Power:"))
}
})
})
# output$table3 = renderTable({
# input$start3
# isolate({
# if(input$start3>0 & gam.means()[1]==gam.means()[2])
# {
# mat = matrix(c(res.comp2()[[1]],res.comp2()[[2]]),nrow=1,byrow=TRUE)
# rownames(mat) = "Type I error rate"
# colnames(mat) = c("t-test","WMW test")
# return(mat)
# } else if(input$start3>0 & (gam.means()[1]!=gam.means()[2]))
# {
# mat = matrix(c(res.comp2()[[1]],res.comp2()[[2]]),nrow=1,byrow=TRUE)
# rownames(mat) = "Power"
# colnames(mat) = c("t-test","WMW test")
# return(mat)
# }
# })
# })
output$gauge3 = renderGvis({
input$start3
isolate({
dat = data.frame(test=c("t-test","WMW"),prob=round(c(res.comp2()[[1]],res.comp2()[[2]]),digits=3))
if(gam.means()[1]!=gam.means()[2])
{
gauge=gvisGauge(dat, options=list(min=0,max=1,greenFrom=.75,greenTo=1,redFrom=0,redTo=.25,width=250,height=250))
} else if(gam.means()[1]==gam.means()[2])
{
gauge=gvisGauge(dat, options=list(min=0,max=1,greenFrom=0,greenTo=input$alpha3,
yellowFrom=input$alpha3,yellowTo=input$alpha3*2,
redFrom=input$alpha3*2,redTo=1,width=250,height=250))
}
return(gauge)
})
})
res.vary2 = reactive({
input$start31
isolate({
comp.vary.skewed(farvary=input$far2,n.gam1=input$ngam21,n.gam2=input$ngam31,
nsim=input$nsim31,shape=input$shape21,scale=input$scale21,alpha=input$alpha31)
})
})
output$graph.gamgam = renderGvis({
input$start31
isolate({
if(input$start31>0)
{
res.dat = data.frame(mean=seq((input$shape21*input$scale21)+input$far2[1],(input$shape21*input$scale21)+input$far2[2],length.out=length(res.vary2()[[1]])),
t=round(res.vary2()[[1]],digits=2),wmw=round(res.vary2()[[2]],digits=2))
gvisLineChart(res.dat,
options=list(title="Comparing Type 1 error rate and power between t-test and WMW test",
hAxes="[{title:'Mean of 2nd Gamma distribution'}]",
vAxes="[{title:'P(rejecting Ho)'}]",
width=800,height=600,series="[{color:'#F08080'},{color:'navy'}]"))
# print(ggplot(data=res.dat) + geom_line(aes(x=mean,y=t.error,color="t-test")) +
# geom_line(aes(x=mean,y=wmw.error,color="WMW")) + xlab("Mean of 2nd Gamma distribution") + ylab("P(rejecting Ho)") +
# scale_color_manual(name="Type of test",values=c("t-test"="navy","WMW"="gold2")) + theme(legend.position="bottom") +
# ggtitle("Comparing Type 1 error rate and\npower between t-test and WMW test"))
}
})
})
})
# -----------------------------------------------------------------
# App Title: Performance of Wilcoxon-Mann-Whitney Test and t-test
# Author: Jimmy Wong
# -----------------------------------------------------------------
if (!require("ggplot2")) install.packages("ggplot2")
if (!require("gridExtra")) install.packages("gridExtra")
if (!require("googleVis")) install.packages("googleVis")
if (!require("shinyBS")) install.packages("shinyBS")
if (!require("shinyIncubator")) install.packages("shinyIncubator")
shinyUI(fluidPage(
tags$head(tags$link(rel = "icon", type = "image/x-icon",
href = "https://webresource.its.calpoly.edu/cpwebtemplate/5.0.1/common/images_html/favicon.ico")),
titlePanel("Performance of Wilcoxon-Mann-Whitney Test and t-test"),
tabsetPanel(type="tabs",
tabPanel("Goal of Study",
column(1),
column(4,
br(),br(),
p("This study compares the", span("Type I error rate",style = "color:red"), "and", span("power",style = "color:blue"),
"between the two-sample t-test and the Wilcoxon-Mann-Whitney (WMW) test. The", strong("two-sample t-test"), "requires either the
two population distributions to be normal or the sample sizes to be large enough in order for the sampling distribution
to be normal. The", strong("WMW test"), "is a nonparametric test that requires the two population distributions to have the same shape.
When two populations have the same mean, Type I error rate is of interest. In contrast, when two populations have different
means, power is of interest"),
p("Different scenarios are analyzed in this study, such as comparing two Normal distributions, a Normal to a Gamma distribution, and two Gamma
distributions with small and large sample sizes. The better test is determined either through a lower Type I error rate or a higher power.")),
column(6,
plotOutput("intrograph"),br(),
bsPopover("intrograph", "Note","These 4 graphs provide sample scenarios. In each of these sample scenarios, would you focus on Type I error rate or power?",
placement="left",trigger="hover"),
div("Shiny app by",
a(href="http://www.linkedin.com/in/jimmywong46/",target="_blank","Jimmy Wong"),align="right", style = "font-size: 8pt"),
div("Base R code by",
a(href="http://www.linkedin.com/in/jimmywong46/",target="_blank","Jimmy Wong"),align="right", style = "font-size: 8pt"),
div("Shiny source files:",
a(href="https://gist.github.com/calpolystat/8f525780393a05e7d0fd",
target="_blank","GitHub Gist"),align="right", style = "font-size: 8pt"),
div(a(href="http://www.statistics.calpoly.edu/shiny",target="_blank",
"Cal Poly Statistics Dept Shiny Series"),align="right", style = "font-size: 8pt")),
column(1)),
tabPanel("Guess the Population!",
column(1),
column(4,
br(),
p("It is time to", strong("Guess the Population!"), "This game demonstrates the difficulty of identifying which
pair of sample data are from the same population. Below are 4 histograms of randomly generated data with sample
sizes of 20, where 2 are from", code("N(3,1)"), "(Normal distribution) and 2 are from", code("Gamma(6,.5)"), "(Gamma distribution)."),
div("Can you determine which pair came from the Normal distribution and which pair from the Gamma distribution?",style = "color:navy"),
tags$hr(),
uiOutput("answerinsert"),
uiOutput("answercheck"),br(),
uiOutput("answerreveal"),
conditionalPanel(
condition="input.reveal",
tableOutput("answer"))),
column(6,
plotOutput("guesspop"),
actionButton("resetgame", "Reset game"),
div("Shiny app by",
a(href="http://www.linkedin.com/in/jimmywong46/",target="_blank","Jimmy Wong"),align="right", style = "font-size: 8pt"),
div("Base R code by",
a(href="http://www.linkedin.com/in/jimmywong46/",target="_blank","Jimmy Wong"),align="right", style = "font-size: 8pt"),
div("Shiny source files:",
a(href="https://gist.github.com/calpolystat/8f525780393a05e7d0fd",
target="_blank","GitHub Gist"),align="right", style = "font-size: 8pt"),
div(a(href="http://www.statistics.calpoly.edu/shiny",target="_blank",
"Cal Poly Statistics Dept Shiny Series"),align="right", style = "font-size: 8pt")),
column(1)),
navbarMenu("Two Normal Populations",
tabPanel("Single comparison",
fluidPage(
absolutePanel(
right=110,width=220,draggable=TRUE,cursor="move",
wellPanel(
h5("Normal distribution 1:"),
sliderInput("nnorm1","Sample size:",min=10,max=100,step=10,value=20),
numericInput("meannorm1","Mean:",value=3),
tags$hr(),
h5("Normal distribution 2:"),
sliderInput("nnorm2","Sample size:",min=10,max=100,step=10,value=20),
numericInput("meannorm2","Mean:",value=4),
tags$hr(),
sliderInput("alpha1",HTML("Significance level &alpha;:"),min=.01,max=1,step=.01,value=.05),
sliderInput("nsim1","Number of simulations:",min=10,max=1000,value=50,step=10),br(),
actionButton("start1","Start simulation!"),
br(),br(),br(),
div("Shiny app by",
a(href="http://www.linkedin.com/in/jimmywong46/",target="_blank","Jimmy Wong"),align="right", style = "font-size: 8pt"),
div("Base R code by",
a(href="http://www.linkedin.com/in/jimmywong46/",target="_blank","Jimmy Wong"),align="right", style = "font-size: 8pt"),
div("Shiny source files:",
a(href="https://gist.github.com/calpolystat/8f525780393a05e7d0fd",
target="_blank","GitHub Gist"),align="right", style = "font-size: 8pt"),
div(a(href="http://www.statistics.calpoly.edu/shiny",target="_blank",
"Cal Poly Statistics Dept Shiny Series"),align="right", style = "font-size: 8pt"))),
column(8,
bsCollapse(multiple=TRUE, open=c("normnorm1","normnorm2","normcond"), id="normalcollapse",
bsCollapsePanel("Population Distributions", id="normnorm1",
bsPopover("normnorm1","Collapsible!","Click to collapse.",trigger="hover",placement="top"),
plotOutput("twonormplots"),
uiOutput("normselected"),
p("Note:"),
HTML("<ol> <li type=circle> Variances are fixed at 1 </li>
<li type=circle> P(rejecting Ho | &mu;<sub>1</sub>=&mu;<sub>2</sub>) = Type I error rate
</li> <li type=circle> P(rejecting Ho | &mu;<sub>1</sub>&ne;&mu;<sub>2</sub>) = Power </li> </ol>"), br(),
conditionalPanel(
condition="input.meannorm1==input.meannorm2",
div("These two Normal distributions have the same means; focus on", code("Type I error rate"),
style = "color:navy")),
conditionalPanel(
condition="input.meannorm1!=input.meannorm2",
div("These two Normal distributions have different means; focus on", code("Power"),
style = "color:navy"))),
bsCollapsePanel("Conditions", id="normcond",
bsPopover("normcond","Collapsible!","Click to collapse.",trigger="hover",placement="top"),
uiOutput("satisfied1")),
bsCollapsePanel("Simulation Results", id="normnorm2",
conditionalPanel(
condition="input.start1>0",
bsPopover("normnorm2","Collapsible!","Click to collapse.",trigger="hover",placement="top"),
plotOutput("twohistnorms"),
column(3,
#tableOutput("table1"),
conditionalPanel(
condition="input.meannorm1==input.meannorm2",
strong("Type I error rate:")),
conditionalPanel(
condition="input.meannorm1!=input.meannorm2",
strong("Power:")),
htmlOutput("gauge1")),
column(4,
br(),br(),
uiOutput("text1")))))),
column(4))),
tabPanel("Comparison over a range",
fluidPage(
absolutePanel(
right=110,width=220,draggable=TRUE,cursor="move",
wellPanel(
h5("Normal distribution 1:"),
sliderInput("nnorm11","Sample size:",min=10,max=100,step=10,value=20),
sliderInput("varymeans1","Range of means:",min=-10,max=10,step=1,value=c(0,6)),
tags$hr(),
h5("Normal distribution 2:"),
sliderInput("nnorm21","Sample size:",min=10,max=100,step=10,value=20),
sliderInput("meannorm21","Mean:",min=-10,max=10,step=1,value=3),
tags$hr(),
sliderInput("alpha11",HTML("Significance level &alpha;:"),min=.01,max=1,step=.01,value=.05),
sliderInput("nsim11","Number of simulations:",min=10,max=200,value=50,step=10),br(),
actionButton("start11","Start simulation!"),
br(),br(),br(),
div("Shiny app by",
a(href="http://www.linkedin.com/in/jimmywong46/",target="_blank","Jimmy Wong"),align="right", style = "font-size: 8pt"),
div("Base R code by",
a(href="http://www.linkedin.com/in/jimmywong46/",target="_blank","Jimmy Wong"),align="right", style = "font-size: 8pt"),
div("Shiny source files:",
a(href="https://gist.github.com/calpolystat/8f525780393a05e7d0fd",
target="_blank","GitHub Gist"),align="right", style = "font-size: 8pt"),
div(a(href="http://www.statistics.calpoly.edu/shiny",target="_blank",
"Cal Poly Statistics Dept Shiny Series"),align="right", style = "font-size: 8pt"))),
column(8,
bsCollapse(multiple=TRUE, open=c("c11","c12"), id="normalvarycollapse",
bsCollapsePanel("Simulation Information", id="c11",
bsPopover("c11","Collapsible!","Click to collapse.",trigger="hover",placement="top"),
span("In this scenario, the mean of the 1st Normal distribution varies according to the specified range, while the mean of the
2nd Normal distribution remains constant. The Type 1 error rate and power is compared between the t-test and the WMW test.",
style = "color:black")),
bsCollapsePanel("Simulation Results", id="c12",
p("In the generated graph, each point is either a Type I error rate or power;
there is at most 1 Type I error rate (when the two population means are the same)."),
conditionalPanel(
condition="input.start11>0",
bsPopover("c12","Collapsible!","Click to collapse.",trigger="hover",placement="top"),
htmlOutput("graph.norm"))))),
column(4)))),
navbarMenu("Normal and Gamma Populations",
tabPanel("Single comparison",
fluidPage(
absolutePanel(
right=110,width=220,draggable=TRUE,cursor="move",
wellPanel(
h5("Normal distribution:"),
sliderInput("nnorm3","Sample size:",min=10,max=100,step=10,value=10),
numericInput("meannorm3","Mean:",value=3),
tags$hr(),
h5("Gamma distribution:"),
sliderInput("ngam1","Sample size:",min=10,max=100,step=10,value=10),
sliderInput("shape1","Shape:",min=.5,max=20,step=.5,value=8),
sliderInput("scale1","Scale:",min=.5,max=10,step=.5,value=.5),
tags$hr(),
sliderInput("alpha2",HTML("Significance level &alpha;:"),min=.01,max=1,step=.01,value=.05),
sliderInput("nsim2","Number of simulations:",min=10,max=1000,value=50,step=10),br(),
actionButton("start2","Start simulation!"),
br(),br(),br(),
div("Shiny app by",
a(href="http://www.linkedin.com/in/jimmywong46/",target="_blank","Jimmy Wong"),align="right", style = "font-size: 8pt"),
div("Base R code by",
a(href="http://www.linkedin.com/in/jimmywong46/",target="_blank","Jimmy Wong"),align="right", style = "font-size: 8pt"),
div("Shiny source files:",
a(href="https://gist.github.com/calpolystat/8f525780393a05e7d0fd",
target="_blank","GitHub Gist"),align="right", style = "font-size: 8pt"),
div(a(href="http://www.statistics.calpoly.edu/shiny",target="_blank",
"Cal Poly Statistics Dept Shiny Series"),align="right", style = "font-size: 8pt"))),
column(8,
bsCollapse(multiple=TRUE, open=c("normgam1","normgam2","normgamcond"), id="normgamcollapse",
bsCollapsePanel("Population Distributions", id="normgam1",
bsPopover("normgam1","Collapsible!","Click to collapse.",trigger="hover",placement="top"),
plotOutput("twonormgamplots"),
uiOutput("normgamselected"),
p("Note:"),
HTML("<ol> <li type=circle> Variance of Normal is fixed at 1 </li>
<li type=circle> Gamma mean is the product of shape and scale </li>
<li type=circle> P(rejecting Ho | &mu;<sub>1</sub>=&mu;<sub>2</sub>) = Type I error rate
</li> <li type=circle> P(rejecting Ho | &mu;<sub>1</sub>&ne;&mu;<sub>2</sub>) = Power </li> </ol>"),br(),
uiOutput("focusinfo1")),
bsCollapsePanel("Conditions", id="normgamcond",
bsPopover("normgamcond","Collapsible!","Click to collapse.",trigger="hover",placement="top"),
uiOutput("satisfied2")),
bsCollapsePanel("Simulation Results", id="normgam2",
conditionalPanel(
condition="input.start2>0",
bsPopover("normgam2","Collapsible!","Click to collapse.",trigger="hover",placement="top"),
plotOutput("twohistnormgams"),
column(3,
#tableOutput("table2"),
uiOutput("whichone"),
htmlOutput("gauge2")),
column(4,
br(),br(),
uiOutput("text2")))))),
column(4))),
tabPanel("Comparison over a range",
fluidPage(
absolutePanel(
right=110,width=220,draggable=TRUE,cursor="move",
wellPanel(
h5("Normal distribution:"),
sliderInput("nnorm31","Sample size:",min=10,max=100,step=10,value=20),
sliderInput("varymeans2","Range of means:",min=0,max=50,step=1,value=c(0,6)),
tags$hr(),
h5("Gamma distribution:"),
sliderInput("ngam11","Sample size:",min=10,max=100,step=10,value=20),
sliderInput("shape11","Shape:",min=.5,max=10,step=.5,value=6),
sliderInput("scale11","Scale:",min=.5,max=2.5,step=.5,value=.5),
tags$hr(),
sliderInput("alpha21",HTML("Significance level &alpha;:"),min=.01,max=1,step=.01,value=.05),
sliderInput("nsim21","Number of simulations:",min=10,max=200,value=50,step=10),br(),
actionButton("start21","Start simulation!"),
br(),br(),br(),
div("Shiny app by",
a(href="http://www.linkedin.com/in/jimmywong46/",target="_blank","Jimmy Wong"),align="right", style = "font-size: 8pt"),
div("Base R code by",
a(href="http://www.linkedin.com/in/jimmywong46/",target="_blank","Jimmy Wong"),align="right", style = "font-size: 8pt"),
div("Shiny source files:",
a(href="https://gist.github.com/calpolystat/8f525780393a05e7d0fd",
target="_blank","GitHub Gist"),align="right", style = "font-size: 8pt"),
div(a(href="http://www.statistics.calpoly.edu/shiny",target="_blank",
"Cal Poly Statistics Dept Shiny Series"),align="right", style = "font-size: 8pt"))),
column(8,
bsCollapse(multiple=TRUE, open=c("c21","c22"), id="normalvarycollapse",
bsCollapsePanel("Simulation Information", id="c21",
bsPopover("c21","Collapsible!","Click to collapse.",trigger="hover",placement="top"),
span("In this scenario, the mean of the Normal distribution varies according to the specified range, while the mean of the
Gamma distribution remains constant. The Type 1 error rate and power is compared between the t-test and the WMW test.",
style = "color:black")),
bsCollapsePanel("Simulation Results", id="c22",
p("In the generated graph, each point is either a Type I error rate or power;
there is at most 1 Type I error rate (when the two population means are the same)."),
conditionalPanel(
condition="input.start21>0",
bsPopover("c22","Collapsible!","Click to collapse.",trigger="hover",placement="top"),
htmlOutput("graph.normgam"))))),
column(4)))),
navbarMenu("Two Gamma Populations",
tabPanel("Single comparison",
fluidPage(
absolutePanel(
right=110,width=220,draggable=TRUE,cursor="move",
wellPanel(
h5("Gamma distribution 1:"),
sliderInput("ngam2","Sample size:",min=10,max=100,step=10,value=10),
sliderInput("shape2","Shape:",min=.5,max=20,step=.5,value=6),
sliderInput("scale2","Scale:",min=.5,max=10,step=.5,value=.5),
tags$hr(),
h5("Gamma distribution 2:"),
sliderInput("ngam3","Sample size:",min=10,max=100,step=10,value=10),br(),
sliderInput("far1","Distance from first Gamma:",min=0,max=100,step=1,value=3),
tags$hr(),
sliderInput("alpha3",HTML("Significance level &alpha;:"),min=.01,max=1,step=.01,value=.05),
sliderInput("nsim3","Number of simulations:",min=10,max=1000,value=50,step=10),br(),
actionButton("start3","Start simulation!"),
br(),br(),br(),
div("Shiny app by",
a(href="http://www.linkedin.com/in/jimmywong46/",target="_blank","Jimmy Wong"),align="right", style = "font-size: 8pt"),
div("Base R code by",
a(href="http://www.linkedin.com/in/jimmywong46/",target="_blank","Jimmy Wong"),align="right", style = "font-size: 8pt"),
div("Shiny source files:",
a(href="https://gist.github.com/calpolystat/8f525780393a05e7d0fd",
target="_blank","GitHub Gist"),align="right", style = "font-size: 8pt"),
div(a(href="http://www.statistics.calpoly.edu/shiny",target="_blank",
"Cal Poly Statistics Dept Shiny Series"),align="right", style = "font-size: 8pt"))),
column(8,
bsCollapse(multiple=TRUE, open=c("gamgam1","gamgam2","gamcond"), id="gamgamcollapse",
bsCollapsePanel("Population Distributions", id="gamgam1",
bsPopover("gamgam1","Collapsible!","Click to collapse.",trigger="hover",placement="top"),
plotOutput("twogamplots"),
uiOutput("gamselected"),
p("Note:"),
HTML("<ol> <li type=circle> Gamma mean is the product of shape and scale </li>
<li type=circle> P(rejecting Ho | &mu;<sub>1</sub>=&mu;<sub>2</sub>) = Type I error rate
</li> <li type=circle> P(rejecting Ho | &mu;<sub>1</sub>&ne;&mu;<sub>2</sub>) = Power </li> </ol>"),br(),
uiOutput("focusinfo2")),
bsCollapsePanel("Conditions", id="gamcond",
bsPopover("gamcond","Collapsible!","Click to collapse.",trigger="hover",placement="top"),
uiOutput("satisfied3")),
bsCollapsePanel("Simulation Results", id="gamgam2",
conditionalPanel(
condition="input.start3>0",
bsPopover("gamgam2","Collapsible!","Click to collapse.",trigger="hover",placement="top"),
plotOutput("twohistgams"),
column(3,
#tableOutput("table3"),
uiOutput("type1powertext"),
htmlOutput("gauge3")),
column(4,
br(),br(),
uiOutput("text3")))))),
column(4))),
tabPanel("Comparison over a range",
absolutePanel(
right=110,width=220,draggable=TRUE,cursor="move",
wellPanel(
h5("Gamma distribution 1:"),
sliderInput("ngam21","Sample size:",min=10,max=100,step=10,value=20),
sliderInput("shape21","Shape:",min=.5,max=20,step=.5,value=12),
sliderInput("scale21","Scale:",min=.5,max=10,step=.5,value=.5),
tags$hr(),
h5("Gamma distribution 2:"),
sliderInput("ngam31","Sample size:",min=10,max=100,step=10,value=20),
sliderInput("far2","Range of distance from first Gamma:",min=0,max=20,step=1,value=c(0,6)),
tags$hr(),
sliderInput("alpha31",HTML("Significance level &alpha;:"),min=.01,max=1,step=.01,value=.05),
sliderInput("nsim31","Number of simulations:",min=10,max=200,value=50,step=10),br(),
actionButton("start31","Start simulation!"),
br(),br(),br(),
div("Shiny app by",
a(href="http://www.linkedin.com/in/jimmywong46/",target="_blank","Jimmy Wong"),align="right", style = "font-size: 8pt"),
div("Base R code by",
a(href="http://www.linkedin.com/in/jimmywong46/",target="_blank","Jimmy Wong"),align="right", style = "font-size: 8pt"),
div("Shiny source files:",
a(href="https://gist.github.com/calpolystat/8f525780393a05e7d0fd",
target="_blank","GitHub Gist"),align="right", style = "font-size: 8pt"),
div(a(href="http://www.statistics.calpoly.edu/shiny",target="_blank",
"Cal Poly Statistics Dept Shiny Series"),align="right", style = "font-size: 8pt"))),
column(8,
bsCollapse(multiple=TRUE, open=c("c31","c32"), id="gammavarycollapse",
bsCollapsePanel("Simulation Information", id="c31",
bsPopover("c31","Collapsible!","Click to collapse.",trigger="hover",placement="top"),
span("In this scenario, the mean of the 1st Gamma distribution remains constant, while the mean of the 2nd
Gamma distribution varies depending on the specified range of distance. The Type 1 error rate and power
is compared between the t-test and the WMW test.",
style = "color:black")),
bsCollapsePanel("Simulation Results", id="c32",
p("In the generated graph, each point is either a Type I error rate or power;
there is at most 1 Type I error rate (when the two population means are the same)."),
conditionalPanel(
condition="input.start31>0",
bsPopover("c32","Collapsible!","Click to collapse.",trigger="hover",placement="top"),
htmlOutput("graph.gamgam"))))),
column(3))))
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment