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/fad8ef712fc6f726640c to your computer and use it in GitHub Desktop.
Save calpolystat/fad8ef712fc6f726640c to your computer and use it in GitHub Desktop.
Testing Violation of the Constant Variance Condition for ANOVA: Shiny app at http://www.statistics.calpoly.edu/shiny
Testing Violation of the Constant Variance Condition for ANOVA Shiny App
Base R code created by Gail Potter
Shiny app files created by Gail Potter
Cal Poly Statistics Dept Shiny Series
http://statistics.calpoly.edu/shiny
Title: Testing Violation of the Constant Variance Condition for ANOVA
Author: Gail Potter
AuthorUrl: http://www.gailpotter.org
License: MIT
DisplayMode: Normal
Tags: ANOVA, Constant Variance, Robustness
Type: Shiny
The MIT License (MIT)
Copyright (c) 2015 Gail Potter
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: Robustness of ANOVA F-test to violation of constant variance
# Author: Gail Potter
# ------------------
simulate.response = function(nsim, sample.sizes, s1=2, s2=2, s3=2, m1=0, m2=0, m3=0){
## user inputs number of samples to draw (nsim),
## sample size, and standard deviation of each sample (s1,s2,s3)
## The function outputs a matrix of nsim samples of size sample.size
## drawn from normal distributions with mean 0 and specified SDs.
## Each column is a vector of the 3 simulated output.
matrix(rnorm(nsim * sum(sample.sizes), mean=rep(c(m1,m2,m3), sample.sizes),
sd=rep(c(s1,s2,s3), sample.sizes)), ncol=nsim)
}
get.f.stat = function(y,x) return(anova(lm(y~x))[[4]][1])
create.predictor = function(sample.sizes)
factor(rep(paste("Group", 1:3),sample.sizes))
shinyServer(function(input, output, session) {
draw.sample <- reactiveValues()
observe({
x = isolate(create.predictor(sample.sizes=c(input$n1, input$n2, input$n3)))
if(input$go==0) {
y = simulate.response(
nsim=input$nsim, sample.sizes=c(input$n1, input$n2, input$n3),
s1=input$sigma1, s2=input$sigma2, s3=input$sigma3,
m1=input$mu1, m2=input$mu2, m3=input$mu3)
f.stats = apply(y, 2, get.f.stat, x=x)
draw.sample$f.stats <- c(f.stats, isolate(draw.sample$f.stats))
draw.sample$y = y[1:isolate(input$n1+input$n2+input$n3)]
} else {
input$go
y = simulate.response(nsim=isolate(input$nsim),
sample.sizes=c(isolate(input$n1), isolate(input$n2), isolate(input$n3)),
s1=isolate(input$sigma1), s2=isolate(input$sigma2), s3=isolate(input$sigma3),
m1=isolate(input$mu1), m2=isolate(input$mu2), m3=isolate(input$mu3))
withProgress(message = "Calculating, please wait.",
detail = " ", value=.5, {
f.stats = isolate(apply(y, 2, get.f.stat, x=x))
draw.sample$f.stats <- c(f.stats, isolate(draw.sample$f.stats))
draw.sample$y = y[1:isolate(input$n1+input$n2+input$n3)]
})
}
})
observe({
input$sigma1
input$sigma2
input$sigma3
input$mu1
input$mu2
input$mu3
input$n1
input$n2
input$n3
input$n
input$clear
draw.sample$y<-NULL
draw.sample$f.stats=NULL
})
output$y <- renderText({y})
output$dotplot <- renderPlot({
input$sigma1
input$sigma2
input$sigma3
input$n1
input$n2
input$n3
input$n
x = create.predictor(sample.sizes=c(input$n1, input$n2, input$n3))
y = draw.sample$y
par(mfrow=c(1,2))
if (!is.null(y)){
stripchart(y ~ x,
vertical = TRUE, method="jitter" , main =paste("Sampled data"),
pch = 21, col = "darkblue", bg = "lightblue")
crit = qf(0.95, df1=2, df2=(sum(c(input$n1, input$n2, input$n3))-1))
type.I.error = mean(draw.sample$f.stats>=crit)
#xmax = max(15, round(max(draw.sample$f.stats[draw.sample$f.stats<20]))+1)
xmax = max(15, round(max(draw.sample$f.stats))+1)
ymax = max(hist(draw.sample$f.stats,
breaks=seq(0,xmax,1), plot=FALSE)$counts+2, 15)
hist(draw.sample$f.stats, col="lightblue",
main="Sampling distribution",
xlim=c(0,xmax), breaks=seq(0,xmax,1),
ylim=c(0,ymax), xlab="F-statistics")
abline(v=crit,col="red", lty=2)
text(x=crit+1, y=ymax*.7, expression(F[0.05]), col="red")
}
})
output$typeI = renderUI({
if (input$mu1!=input$mu2 | input$mu1!=input$mu3 | input$mu2!=input$mu3){
typeofstudy="Power = "
} else {
typeofstudy="Type I error rate = "
}
if (!is.null(draw.sample$y)) {
crit = qf(0.95, df1=2, df2=(input$n1+input$n2+input$n3-1))
type.I.error = sum(draw.sample$f.stats>=crit)/length(draw.sample$f.stats)
n.samples = length(draw.sample$f.stats)
str1 = paste("Total samples drawn =", n.samples)
str2 = paste(typeofstudy, sum(draw.sample$f.stats>=crit), "/",
length(draw.sample$f.stats), " = ", round(type.I.error,3))
HTML(paste(str1, str2, sep = '<br/>'))
}
})
})
.shiny-progress {
top: 50% !important;
left: 50% !important;
margin-top: -220px !important;
margin-left: 50px !important;
}
# ------------------
# App Title: Robustness of ANOVA F-test to violation of constant variance
# Author: Gail Potter
# ------------------
if (!require("devtools"))
install.packages("devtools")
if (!require("shinyBS")) install.packages("shinyBS")
library(shinyBS)
if (!require("digest")) install.packages("digest")
if (!require("shinyIncubator")) devtools::install_github("rstudio/shiny-incubator")
library(shinyIncubator)
shinyUI(fluidPage(
tags$title("Robustness of ANOVA"),
includeCSS('styles.css'),
#progressInit(),
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")),
h3("How robust is the ANOVA F-test to violation of constant variance?"),
fluidRow(
column(3,
wellPanel(
h5("Specifications for ANOVA", style="color:brown"),
h5("Population standard deviations:"),
sliderInput("sigma1", label="Group 1", value=6, min=1, max=20),
sliderInput("sigma2", label="Group 2", value=6, min=1, max=20),
sliderInput("sigma3", label="Group 3", value=6, min=1, max=20),
br(),
h5("Sample sizes"),
sliderInput("n1", label="Group 1", value=20, min=2, max=100),
sliderInput("n2", label="Group 2", value=20, min=2, max=100),
sliderInput("n3", label="Group 3", value=20, min=2, max=100),
br(),
br(),
h5("Population means:"),
sliderInput("mu1", label="Group 1", value=0, min=-5, max=5),
sliderInput("mu2", label="Group 2", value=0, min=-5, max=5),
sliderInput("mu3", label="Group 3", value=0, min=-5, max=5),
div("Shiny app by",
a(href="http://www.gailpotter.org",target="_blank",
"Gail Potter"),align="right", style = "font-size: 8pt"),
div("Base R code by",
a(href="http://www.gailpotter.org",target="_blank",
"Gail Potter"),align="right", style = "font-size: 8pt"),
div("Shiny source files:",
a(href="https://gist.github.com/calpolystat/fad8ef712fc6f726640c",
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"))
),
tags$style(type="text/css",
".shiny-output-error { visibility: hidden; }",
".shiny-output-error:before { visibility: hidden; }"
),
column(9, wellPanel(
p("The ANOVA F-test is used to test for difference in means between groups, and requires
the conditions of normality (or large sample size), independence, and constant variance in order to be valid. This
app evaluates robustness of the ANOVA F-test to violation of the constant variance condition.
At left, specify the sample sizes and standard deviations for each group. Below left are simulated
data from normal distributions with the specified standard deviations and mean zero.
In the right plot, the F-statistic for the simulated data is added to the sampling distribution.
The critical value for a 0.05 significance test is shown in red."),
sliderInput("nsim", label="Number of samples", value=1, min=1, max=1000),
actionButton("go", label = "Draw samples"),
actionButton("clear",label="Clear"),
plotOutput("dotplot"),
conditionalPanel(
condition="input.mu1==input.mu2 & input.mu2==input.mu3",
div("You have selected identical population means; you will analyze ", code("Type I error"))),
conditionalPanel(
condition="input.mu1!=input.mu2 || input.mu2!=input.mu3 || input.mu1!=input.mu3",
div("You have selected different population means; you will analyze ", code("power"))),
htmlOutput("typeI"),
div(h4("Explorations"), style="color:brown"),
p("1. If conditions for ANOVA are satisfied, the Type I Error rate should be equal to 0.05. Simulate data
that satisfy conditions and verify that this is true. Perform several hundred simulations to get a good estimate
for the error rate."),
p("2. Simulate samples of size 20 from populations with equal means and standard deviations 6, 6, and 6. What is your Type I error rate?"),
p("3. Simulate samples of size 20 from populations with equal means and standard deviations 4, 6, and 8. Now what is your Type I error rate?"),
p("4. Simulate samples of size 20 from populations with equal means and standard deviations 1, 6, and 11. Now what is your Type I error rate?"),
p("5. Do the error rates you found in 2, 3, or 4 vary by sample size, when sample sizes are equal?"),
p("6. Next repeat your simulation study with sample sizes 10, 20, and 30. How do results differ?"),
p("7. Finally, repeat the above simulation studies, but specify population means to be -3, 0, and 3, so that you study the power of the test under different conditions.")
))
)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment