Skip to content

Instantly share code, notes, and snippets.

@wch
Created November 6, 2012 06:52
Show Gist options
  • Select an option

  • Save wch/4023134 to your computer and use it in GitHub Desktop.

Select an option

Save wch/4023134 to your computer and use it in GitHub Desktop.
Anorexia experiment data
library(shiny)
# Data preparation ----------------------------------------------------------
library(MASS) # For the anorexia data set
library(reshape2)
# Load the anorexia data set
data(anorexia)
# Give each person a unique ID
anorexia$ID <- as.factor(1:nrow(anorexia))
# Rename columns
names(anorexia)[names(anorexia) == "Prewt"] <- "Pre"
names(anorexia)[names(anorexia) == "Postwt"] <- "Post"
names(anorexia)[names(anorexia) == "Treat"] <- "Treatment"
# Change order of Treatment factor levels
anorexia$Treatment <- factor(anorexia$Treatment, levels = c("Cont", "CBT", "FT"))
# Rename Treatment values
levels(anorexia$Treatment)[levels(anorexia$Treatment) == "Cont"] <- "Control"
levels(anorexia$Treatment)[levels(anorexia$Treatment) == "CBT"] <- "Cognitive behavioral"
levels(anorexia$Treatment)[levels(anorexia$Treatment) == "FT"] <- "Family treatment"
# Convert to long format
anorexia <- melt(anorexia,
id.vars = c("ID", "Treatment"),
value.vars = c("Pre", "Post"),
variable.name = "Time",
value.name = "Weight")
# Make sure factor levels Pre and Post are in correct order for plotting
anorexia$Time <- factor(anorexia$Time, levels = c("Pre", "Post"))
# Calculate mean, standard deviation, and count
library(plyr)
anorexia_summary <- ddply(anorexia, c("Treatment", "Time"), summarise,
mean = mean(Weight),
sd = sd(Weight),
n = length(Treatment))
names(anorexia_summary)[names(anorexia_summary) == "mean"] <- "Weight"
# Define server logic -------------------------------------------------------
shinyServer(function(input, output) {
# Generate a plot of the requested variable against mpg and only
# include outliers if requested
output$main_plot <- reactivePlot(function() {
# Options:
# x Color by group
# x Facet by group
# Overall means and error bars
if (input$color_treatment) {
aes_mapping <- aes(x=Time, y=Weight, colour=Treatment, shape=Treatment)
} else {
aes_mapping <- aes(x=Time, y=Weight)
}
# Save this dodging specification
pd <- position_dodge(0.1)
p <- ggplot(anorexia, mapping = aes_mapping) +
geom_point(alpha = 0.3) +
geom_line(aes(group = ID), alpha = 0.2) +
scale_shape_manual(values = c(21, 22, 24)) +
scale_colour_hue(l = 40) +
theme_bw()
if (input$facet_treatment) {
p <- p + facet_grid(. ~ Treatment)
}
if (input$show_means) {
# Add mean points
p <- p +
geom_line(data = anorexia_summary, aes(group = Treatment),
size = 1.5, position = pd) +
geom_point(data = anorexia_summary, aes(group = Treatment),
size = 5, fill = "white", position = pd)
}
print(p)
})
# Text output options
# t-test overall (paired)
# t-test for each group
# ANOVA between groups
output$t_test_output <- reactivePrint(function() {
# If we're distinguishing groups, do separate t-tests for each group, and
# put them in a list
if (input$color_treatment || input$facet_treatment) {
list(
"Control" =
t.test(Weight ~ Time,
data = subset(anorexia, Treatment == "Control"),
paired = input$t_tests_paired),
"Cognitive behavioral" =
t.test(Weight ~ Time,
data = subset(anorexia, Treatment == "Cognitive behavioral"),
paired = input$t_tests_paired),
"Family treatment" =
t.test(Weight ~ Time,
data = subset(anorexia, Treatment == "Family treatment"),
paired = input$t_tests_paired))
} else {
t.test(Weight ~ Time, data = anorexia, paired = input$t_tests_paired)
}
})
output$anova_output <- reactivePrint(function() {
res <- aov(Weight ~ Treatment * Time + Error(ID/Time), data=anorexia)
summary(res)
})
})
library(shiny)
library(ggplot2)
# Define UI for miles per gallon application
shinyUI(pageWithSidebar(
# Application title
headerPanel("Anorexia treatment experiment"),
# Sidebar with controls to select the variable to plot against mpg
# and to specify whether outliers should be included
sidebarPanel(
wellPanel(
p(strong("Show different treatment groups")),
checkboxInput("color_treatment",
"With colors and shapes",
value = FALSE),
checkboxInput("facet_treatment",
"With facets",
value = FALSE),
conditionalPanel(
condition = "input.color_treatment || input.facet_treatment",
checkboxInput("show_means",
"Show means",
value = FALSE)
)
),
wellPanel(
p(strong("Statistical tests")),
checkboxInput("t_tests",
"t-test for pre/post weight",
value = FALSE),
# TODO: Indent this somehow?
conditionalPanel(
condition = "input.t_tests",
checkboxInput("t_tests_paired",
"Use paired t-test",
value = TRUE)
),
checkboxInput("anova",
"ANOVA between treatment conditions",
value = FALSE)
)
),
# Show the caption and plot of the requested variable against mpg
mainPanel(
plotOutput("main_plot"),
conditionalPanel(
condition = "input.t_tests",
h3("t-test for weight before and after treatment"),
verbatimTextOutput("t_test_output")
),
conditionalPanel(
condition = "input.anova",
h3("ANOVA for weight before and after treatment"),
p("3x2 mixed design: Treatment (between) and Time (within)."),
verbatimTextOutput("anova_output")
)
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment