-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclinical_trials.R
91 lines (58 loc) · 2.8 KB
/
clinical_trials.R
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
# Global Options
options(stringsAsFactors = FALSE)
# Required Packages
library(sas7bdat)
library(ggplot2)
library(dplyr)
# Load Data
clinical_data = data.frame(read.sas7bdat(file = "/chapter15_example.sas7bdat"))
save(clinical_data, file = "clinical_data.RData")
# Summary Checks on Data
str(clinical_data)
summary(clinical_data)
# Adjust Data
clinical_data = clinical_data %>%
mutate(GENDER = factor(GENDER, levels = c("F", "M")),
THERAPY = factor(THERAPY, levels = c("DRUG", "PLACEBO")))
# Summaries
summary_1 = clinical_data %>%
group_by(THERAPY, GENDER) %>%
summarize(average_change = mean(change)) %>%
ungroup()
# Create Plots
plot_1 = ggplot(clinical_data, aes(x = change/basval, fill = THERAPY)) +
geom_density(alpha = 0.4) +
scale_x_continuous(labels = scales::percent,
breaks = scales::pretty_breaks(n = 10)) +
scale_y_continuous(labels = scales::percent,
breaks = scales::pretty_breaks(n = 10)) +
ggtitle("Drug VS Placebo Change") +
xlab("Change")
plot_2 = ggplot(clinical_data, aes(x = basval, y = change/basval, color = GENDER, shape = THERAPY)) +
geom_point() +
geom_hline(yintercept = 0, linetype = "dashed", color = "blue2") +
scale_x_continuous(labels = scales::comma,
breaks = scales::pretty_breaks(n = 10)) +
scale_y_continuous(labels = scales::percent,
breaks = scales::pretty_breaks(n = 10)) +
ggtitle("Drug VS Placebo Change Scatter") +
xlab("basval") +
ylab("percentage changed") +
theme_minimal()
# Statistical Test - For Boys
boys_data = clinical_data %>%
filter(GENDER == "M")
N_drug = nrow(boys_data%>%filter(THERAPY == "DRUG"))
N_placebo = nrow(boys_data%>%filter(THERAPY == "PLACEBO"))
mu_drug = mean((boys_data%>%filter(THERAPY == "DRUG"))$change)
mu_placebo = mean((boys_data%>%filter(THERAPY == "PLACEBO"))$change)
sd_drug = sd((boys_data%>%filter(THERAPY == "DRUG"))$change)
sd_placebo = sd((boys_data%>%filter(THERAPY == "PLACEBO"))$change)
mu_difference = 0
sd_difference = sqrt((sd_drug^2)/N_drug + (sd_placebo^2)/N_placebo)
actual_difference = mu_drug - mu_placebo
p_value = pnorm(actual_difference, mu_difference, sd_difference)
final_plot = ggplot(data.frame(simulation = rnorm(1e5 ,mu_difference, sd_difference)), aes(x = simulation)) +
geom_density() +
geom_vline(xintercept = qnorm(0.05), linetype = "dashed", color = "blue2", size = 1.5) +
geom_vline(xintercept = actual_difference, linetype = "dashed", color = "red2", size = 1.5)