Skip to content

Instantly share code, notes, and snippets.

@cwickham
Created November 5, 2012 05:19
Show Gist options
  • Select an option

  • Save cwickham/4015489 to your computer and use it in GitHub Desktop.

Select an option

Save cwickham/4015489 to your computer and use it in GitHub Desktop.
anova
library(shiny)
library(ggplot2)
# Define server logic required to generate and plot a random distribution
shinyServer(function(input, output) {
generate_data <- reactive(
function(){
mu <- c(input$mu_1, input$mu_2, input$mu_3)
tmp <- data.frame(len = rnorm(length(mu) * input$n,
mean = rep(mu, each = input$n),
sd = input$sigma),
dose = rep(c("0.5","1","2"),
each = input$n))
tmp$group_avg <- with(tmp, ave(len, dose))
tmp$overall_avg <- with(tmp, mean(len))
tmp
})
output$rawPlot <- reactivePlot(function() {
print(qplot(len, factor(dose), data = generate_data(),
colour = dose) +
facet_wrap(~ dose, scale = "free_y", ncol =1) +
geom_vline(aes(xintercept = overall_avg), colour = "grey50") +
geom_vline(aes(xintercept = group_avg,colour = dose)) +
xlim(limits = range(generate_data()$len)))
})
output$total_res <- reactivePlot(function(){
print(qplot(len - overall_avg, data = generate_data(), fill = dose)+
xlim(limits = range(generate_data()$len) - generate_data()$overall_avg[1]))
})
output$within_res <- reactivePlot(function(){
print(qplot(len - group_avg, data = generate_data(), fill = dose)+
xlim(limits = range(generate_data()$len) - generate_data()$overall_avg[1]))
})
output$between_res <- reactivePlot(function(){
print(qplot(group_avg - overall_avg, data = generate_data(), fill = dose)+
xlim(limits = range(generate_data()$len) - generate_data()$overall_avg[1]))
})
calc_ss <- reactive(function(){
with(generate_data(),
list(total_ss = sum((len - overall_avg)^2),
total_df = length(len) - 1,
within_ss = sum((len - group_avg)^2),
within_df = length(len) - length(unique(dose)),
between_ss = sum((group_avg - overall_avg)^2),
between_df = length(unique(dose)) - 1)
)
})
output$total_ss <- reactiveText(function(){
paste("Total sum of squares: ", round(calc_ss()$total_ss,2),
"Total df: ", calc_ss()$total_df,
"MSS total: ", round(calc_ss()$total_ss/calc_ss()$total_df,2))
})
output$within_ss <- reactiveText(function(){
paste("Within sum of squares: ", round(calc_ss()$within_ss,2),
"Within df: ", calc_ss()$within_df,
"MSS within: ", round(calc_ss()$within_ss/calc_ss()$within_df,2))
})
output$between_ss <- reactiveText(function(){
paste("Between sum of squares: ", round(calc_ss()$between_ss,2),
"Between df: ", calc_ss()$between_df,
"MSS between: ", round(calc_ss()$between_ss/calc_ss()$between_df,2))
})
output$F_stat <- reactiveText(function(){
paste("F-statistic", round((calc_ss()$between_ss/calc_ss()$between_df)/(calc_ss()$within_ss/calc_ss()$within_df),2))
})
})
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("Sums of squares in ANOVA"),
sidebarPanel(
sliderInput("mu_1", "mu_1:", value = 13,
min = 10, max = 40),
sliderInput("mu_2", "mu_2:", value = 23,
min = 10, max = 40),
sliderInput("mu_3", "mu_3:", value = 26,
min = 10, max = 40),
sliderInput("sigma", "sigma:", value = 3.75,
min = 1, max = 20),
numericInput("n", "sample size", 10),
submitButton("Update View")
),
mainPanel(
h3("Raw data"),
plotOutput("rawPlot", height = "200px", width = "400px"),
h3("Residuals from equal means model"),
plotOutput("total_res", height = "200px", width = "400px"),
textOutput("total_ss"),
h3("Residuals from separate means model"),
plotOutput("within_res", height = "200px", width = "400px"),
textOutput("within_ss"),
h3("Deviation of groups averages from overall averages"),
plotOutput("between_res", height = "200px", width = "400px"),
textOutput("between_ss"),
h3(textOutput("F_stat"))
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment