Skip to content

Commit bd90c4c

Browse files
committed
first subgroup analyses
1 parent da5e82e commit bd90c4c

File tree

5 files changed

+111
-29
lines changed

5 files changed

+111
-29
lines changed

p-hacker/global.R

+8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ DV_ALL <- 'DV_all' # name of average DV
1616
##
1717

1818

19+
readFile <- function(filename) {
20+
fileConnection <- file(filename, encoding="UTF-8")
21+
text <- readChar(fileConnection, file.info(filename)$size, useBytes = TRUE)
22+
Encoding(text) <- "UTF-8"
23+
close(fileConnection)
24+
text
25+
}
26+
1927
# simple wrapper: formats a number in f.2 format
2028
f2 <- function(x, digits=2, prepoint=0, skipZero=FALSE) {
2129
if (skipZero == TRUE) {zero <- "."} else {zero <- "0."}

p-hacker/server.R

+72-12
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@ shinyServer(function(input, output, session) {
1717
save_dv_observers = list(), # list of observer objects dynamically produced when displaying table
1818
counter = 0,
1919
chosen = '',
20-
control_for_gender = F,
21-
control_for_interaction = F,
20+
DV_selector_sg.chosen = DV_ALL,
21+
control_for_gender = FALSE,
22+
control_for_interaction = FALSE,
2223
next_seed = sample(1:5000,1,replace=TRUE),
2324
current_seed = NULL,
2425
dv_names = c(),
2526
dv_names_all = c(),
2627
TEST = NULL,
2728
blub = 0,
28-
flag_auto_selected = F,
29-
flag_point_already_excluded = F
29+
flag_auto_selected = FALSE,
30+
flag_point_already_excluded = FALSE
3031
)
3132

3233

@@ -138,12 +139,15 @@ shinyServer(function(input, output, session) {
138139
})
139140

140141

141-
# dv chosen
142+
# dvs chosen
142143
observeEvent(input$DV_selector, {
143-
#Print(paste0("DV selector chosen option: ", input$DV_selector))
144144
dat$chosen <- input$DV_selector
145145
})
146146

147+
observeEvent(input$DV_selector_sg, {
148+
dat$DV_selector_sg.chosen <- input$DV_selector_sg
149+
})
150+
147151
# clear stack pressed
148152
observeEvent(input$clear_stack, {
149153
dat$n_studies <- 0
@@ -234,7 +238,7 @@ shinyServer(function(input, output, session) {
234238
dat$allData$group <- factor(rep_len(c(input$label_group1, input$label_group2), n))
235239

236240
# add column with randomized ages
237-
dat$allData$age <- round(rgamma(n, 4, 0.5) + 18)
241+
dat$allData$age <- round(rgamma(n, 5, 0.5) + 18)
238242

239243
# add column with randomized genders
240244
dat$allData$gender <- factor(sample(0:1, n, replace=TRUE), labels=c("male", "female"))
@@ -380,7 +384,9 @@ shinyServer(function(input, output, session) {
380384

381385

382386

383-
# render plot overview
387+
# ---------------------------------------------------------------------
388+
# render plot overview
389+
384390
output$plotoverview <- renderUI({
385391
if(is.null(dat$chosen) || dat$counter < 1) {
386392
return()
@@ -412,15 +418,17 @@ shinyServer(function(input, output, session) {
412418
})
413419

414420

415-
# render main plot
421+
# ---------------------------------------------------------------------
422+
# render main plot
423+
416424
output$mainplot <- renderPlot({
417425

418426
# react on changes in dat$TEST[[1]], dat$currentData, dat$chosen
419427

420428
if (is.null(dat$TEST) || is.null(dat$selected) || is.null(dat$chosen) || nrow(dat$selected) == 0) return()
421429

422430
isolate({
423-
# TODO: Interaction plot when interaction with gender is chosen
431+
424432
p_overview <- NULL
425433
dv <- dat$chosen
426434

@@ -448,7 +456,7 @@ shinyServer(function(input, output, session) {
448456

449457
p_overview <- ggplot(dat$allData[1:dat$n,], aes_string(x="group", y=dv)) +
450458
stat_boxplot(geom ='errorbar', data = includedData, color = "grey", width = 0.5) + # draw vertical lines at lower and upper end
451-
geom_boxplot(data = includedData, fill="grey", colour = "grey", alpha = 0.25) + # draw boxplot
459+
geom_boxplot(data = includedData, fill="grey", colour = "grey", alpha = 0.25, outlier.color="red") + # draw boxplot
452460
geom_point(data = includedData, shape = 16, size=4, fill = NA) + # show data points
453461
geom_point(data = excludedData, shape = 21, size=4, fill = NA, colour = "black", alpha = 0.5) + # show excluded points
454462
theme_bw()
@@ -469,7 +477,10 @@ shinyServer(function(input, output, session) {
469477
}
470478
})
471479

472-
# render study stack panel
480+
481+
# ---------------------------------------------------------------------
482+
# render study stack panel
483+
473484
output$studystack<- renderUI({
474485
pchecker_link <- paste0("http://shinyapps.org/apps/p-checker/?syntax=", URLencode(dat$studystack, reserved=TRUE))
475486

@@ -522,4 +533,53 @@ shinyServer(function(input, output, session) {
522533
div(class="alert alert-danger",role="alert",dat$last_error_msg)
523534
})
524535

536+
537+
# ---------------------------------------------------------------------
538+
# Subgroup analysis
539+
540+
output$subgroupOutput <- renderUI({
541+
542+
if (nrow(dat$allData) == 0) return()
543+
544+
# split into 6 groups
545+
includedData <- getSelectedRows(dat$allData, dat$selected, dat$DV_selector_sg.chosen)
546+
includedData$ageGroup <- cut(includedData$age, breaks=c(0, median(includedData$age), max(includedData$age)), labels=c("young", "old"))
547+
548+
print(includedData)
549+
550+
subgroupTests <- data.frame()
551+
for (ag in levels(includedData$ageGroup)) {
552+
for (g in levels(includedData$gender)){
553+
iD2 <- includedData[includedData$ageGroup == ag & includedData$gender == g, ]
554+
555+
print(iD2)
556+
557+
if (sum(iD2$group == input$label_group1)>2 & sum(iD2$group == input$label_group2)>2) {
558+
559+
sg.aov <- aov(formula(paste0(dat$DV_selector_sg.chosen, " ~ group")), iD2)
560+
561+
subgroupTests <- rbind(subgroupTests, data.frame(
562+
agegroup = ag,
563+
gender = g,
564+
p.value = summary(sg.aov)[[1]]$Pr[1]
565+
))
566+
}
567+
}
568+
}
569+
570+
p1 <- ggplot(includedData, aes_string(x="group", y=dat$DV_selector_sg.chosen)) + geom_point() + facet_grid(gender~ageGroup) + stat_boxplot(geom ='errorbar', data = includedData, color = "grey", width = 0.5) + # draw vertical lines at lower and upper end
571+
geom_boxplot(data = includedData, fill="grey", colour = "grey", alpha = 0.25, outlier.color="red") + # draw boxplot
572+
geom_point(data = includedData, shape = 16, size=4, fill = NA) + # show data points
573+
theme_bw()
574+
575+
576+
return(list(
577+
HTML("PLOT"),
578+
selectInput("DV_selector_sg", label="Choose DV for analysis", c(paste0(DV_PREFIX, 1:input$dv_n), isolate(dat$DV_selector_sg.chosen))),
579+
renderTable(subgroupTests),
580+
renderPlot(p1)
581+
))
582+
})
583+
584+
525585
})

p-hacker/snippets/quick_start.R renamed to p-hacker/snippets/quick_start.html

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
qs_panel <- '
21
<!-- ######### QUICK START PANEL ######### -->
32
<div class="col-sm-9">
43
<div class="panel-group" id="accordion">
@@ -80,5 +79,4 @@ <h4>References</h4>
8079
</div>
8180
</div>
8281
</div>
83-
</div>
84-
'
82+
</div>

p-hacker/ui.R

+28-14
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ library(shinythemes)
33
library(shinyBS) # Additional Bootstrap Controls
44

55
# Load the panels with the manual etc.
6-
source("snippets/quick_start.R")
76
source("snippets/about.R")
87

98
shinyUI(fluidPage(theme = shinytheme("spacelab"),
@@ -14,15 +13,15 @@ shinyUI(fluidPage(theme = shinytheme("spacelab"),
1413
titlePanel("p-hacker: Train your p-hacking skills!"),
1514

1615
div(class="row",
17-
HTML(qs_panel),
16+
HTML(readFile("snippets/quick_start.html")),
1817
HTML(about_panel)
1918
),
2019

2120
# ---------------------------------------------------------------------
2221
# The actual app ...
2322

2423
fluidRow(
25-
column(width=3,
24+
column(width=4,
2625
tabsetPanel(id ="tabs1",
2726
tabPanel("New study",
2827
h3("Settings for initial data collection:"),
@@ -41,29 +40,44 @@ shinyUI(fluidPage(theme = shinytheme("spacelab"),
4140
HTML("</div>")
4241
),
4342
tabPanel("Now: p-hack!", class="disabled",
44-
h3("Tools to improve your p-value:"),
43+
h3("Basic tools to improve your p-value:"),
4544
checkboxInput("cov_age", "Control for age", FALSE),
4645
checkboxInput("cov_gender", "Control for gender", FALSE),
4746
checkboxInput("cov_gender_IA", "Interaction with gender", FALSE),
48-
div(class="btn-group-vertical",
47+
div(class="btn-group-vertical",
4948
actionButton('add5','Add 5 new participants'),
5049
actionButton('add10','Add 10 new participants')
51-
)
52-
)
50+
)
51+
)# ,
52+
# tabPanel("Expert feature: Subgroup analysis", class="disabled",
53+
# h3("Unlock the expert feature: Subgroup analysis!"),
54+
# checkboxInput("subgroups", "Do an expert subgroup analysis", FALSE)
55+
# )
5356
)
5457
),
5558

5659

5760
# ---------------------------------------------------------------------
5861
# The output panels, on the right side
5962

60-
column(width=6,
61-
htmlOutput("testoverview"),
62-
htmlOutput("plotoverview"),
63-
plotOutput("mainplot",
64-
click="mainplot_clicked"
65-
),
66-
htmlOutput("plothints")
63+
column(width=5,
64+
conditionalPanel(
65+
condition = "input.tabs1 == 'New study' | input.tabs1 == 'Now: p-hack!'",
66+
htmlOutput("testoverview"),
67+
htmlOutput("plotoverview"),
68+
plotOutput("mainplot",
69+
click="mainplot_clicked"
70+
),
71+
htmlOutput("plothints")
72+
)#,
73+
# conditionalPanel(
74+
# condition = "input.tabs1 == 'Expert feature: Subgroup analysis'",
75+
# HTML("<h3>Subgroup analysis: Age groups by gender</h3>"),
76+
# conditionalPanel(
77+
# condition = "input.subgroups == 1",
78+
# htmlOutput("subgroupOutput")
79+
# )
80+
# )
6781
),
6882

6983
column(width=3,

run.R

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
library(shiny)
2+
runApp("p-hacker")

0 commit comments

Comments
 (0)