-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathData Clean with Example.R
More file actions
131 lines (117 loc) · 4.59 KB
/
Copy pathData Clean with Example.R
File metadata and controls
131 lines (117 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
library(shiny)
library(bslib)
library(pwr)
ui <- page_sidebar(
title = "Sample Size Calculator",
sidebar = sidebar(
selectInput("test_type", "Select Test Type",
choices = c("t-test (Two Sample)" = "t_test",
"Proportion Test" = "prop_test",
"Correlation Test" = "correlation")),
# Parameters for t-test
conditionalPanel(
condition = "input.test_type == 't_test'",
numericInput("mean1", "Mean of Group 1",
value = 10, step = 0.1),
numericInput("mean2", "Mean of Group 2",
value = 12, step = 0.1),
numericInput("sd_pooled", "Pooled Standard Deviation",
value = 4, min = 0.1, step = 0.1),
numericInput("power_t", "Statistical Power",
value = 0.8, min = 0.1, max = 0.99, step = 0.05),
numericInput("sig_level_t", "Significance Level (α)",
value = 0.05, min = 0.01, max = 0.1, step = 0.01)
),
# Parameters for proportion test
conditionalPanel(
condition = "input.test_type == 'prop_test'",
numericInput("prop1", "Proportion 1",
value = 0.5, min = 0, max = 1, step = 0.05),
numericInput("prop2", "Proportion 2",
value = 0.6, min = 0, max = 1, step = 0.05),
numericInput("power_p", "Statistical Power",
value = 0.8, min = 0.1, max = 0.99, step = 0.05),
numericInput("sig_level_p", "Significance Level (α)",
value = 0.05, min = 0.01, max = 0.1, step = 0.01)
),
# Parameters for correlation test
conditionalPanel(
condition = "input.test_type == 'correlation'",
numericInput("correlation", "Expected Correlation",
value = 0.3, min = -0.99, max = 0.99, step = 0.05),
numericInput("power_c", "Statistical Power",
value = 0.8, min = 0.1, max = 0.99, step = 0.05),
numericInput("sig_level_c", "Significance Level (α)",
value = 0.05, min = 0.01, max = 0.1, step = 0.01)
)
),
layout_columns(
fill = FALSE,
value_box(
title = "Required Sample Size",
value = textOutput("sample_size"),
theme = "primary"
),
value_box(
title = "Effect Size (Cohen's d)",
value = textOutput("cohens_d"),
theme = "secondary"
),
card(
card_header("Description"),
textOutput("description")
)
)
)
server <- function(input, output) {
# Calculate Cohen's d for t-test
cohens_d <- reactive({
if (input$test_type == "t_test") {
d <- abs(input$mean1 - input$mean2) / input$sd_pooled
return(d)
}
})
sample_size <- reactive({
if (input$test_type == "t_test") {
result <- pwr.t.test(d = cohens_d(),
power = input$power_t,
sig.level = input$sig_level_t,
type = "two.sample")
ceiling(result$n) # n per group for t-test
} else if (input$test_type == "prop_test") {
h <- ES.h(input$prop1, input$prop2) # Calculate effect size
result <- pwr.2p.test(h = h,
power = input$power_p,
sig.level = input$sig_level_p)
ceiling(result$n) # n per group for proportion test
} else if (input$test_type == "correlation") {
result <- pwr.r.test(r = input$correlation,
power = input$power_c,
sig.level = input$sig_level_c)
ceiling(result$n) # total n for correlation
}
})
output$sample_size <- renderText({
paste0(sample_size(), " participants",
if(input$test_type %in% c("t_test", "prop_test")) " per group" else "")
})
output$cohens_d <- renderText({
if (input$test_type == "t_test") {
sprintf("%.3f", cohens_d())
} else {
"N/A"
}
})
output$description <- renderText({
if (input$test_type == "t_test") {
paste0("This calculation provides the required sample size per group for a two-sample t-test. ",
"Cohen's d is calculated from the means and pooled standard deviation. ",
"Current effect size (d) = ", sprintf("%.3f", cohens_d()))
} else if (input$test_type == "prop_test") {
"This calculation provides the required sample size per group for comparing two proportions. Enter the expected proportions for each group."
} else {
"This calculation provides the total sample size needed to detect the specified correlation coefficient with the given power and significance level."
}
})
}
shinyApp(ui, server)