-
Notifications
You must be signed in to change notification settings - Fork 1
/
APHS_script.Rmd
149 lines (84 loc) · 4.57 KB
/
APHS_script.Rmd
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
---
title: "APHS_nonbinary"
author: "Varun Warrier"
date: "30 May 2019"
output:
html_document: default
pdf_document: default
---
```{r}
library(ggplot2)
library(epitools)
library(ggplot2)
library(data.table)
library(logistf)
load("APHS.RData")
```
## Autism: case-control
###Calculates odds ratio by median-unbiased estimation (mid-p), conditional maximum likelihood estimation (Fisher), unconditional maximum likelihood estimation (Wald), and small sample adjustment (small). Confidence intervals are calculated using exact methods (mid-p and Fisher), normal approximation (Wald), and normal approximation with small sample adjustment (small). This function expects the following table struture:
disease=0 disease=1
exposed=0 (ref) n00 n01
exposed=1 n10 n11
exposed=2 n20 n21
exposed=3 n30 n31
The reason for this is because each level of exposure is compared to the reference level. If you are providing a 2x2 table the following table is preferred:
disease=0 disease=1
exposed=0 (ref) n00 n01
exposed=1 n10 n11
###
Controls Cases
exposed=Males n00 n01
exposed=Nonbin n10 n11
```{r case-control chi-square}
males = subset(APHS, Binary_nonbinary == "Binary_Male")
autism_males = subset(males, Autism.Y.N == "1")
control_males = subset(males, Autism.Y.N == "0")
females = subset(APHS, Binary_nonbinary == "Binary_Female")
autism_females = subset(females, Autism.Y.N == "1")
control_females = subset(females, Autism.Y.N == "0")
nonbinary = subset(APHS, Binary_nonbinary == "Nonbinary")
autism_nonbinary = subset(nonbinary, Autism.Y.N == "1")
control_nonbinary = subset(nonbinary, Autism.Y.N == "0")
sex_3_way = matrix(c(nrow(control_males), nrow(autism_males), nrow(control_females), nrow(autism_females), nrow(control_nonbinary), nrow(autism_nonbinary) ), ncol = 2, byrow = TRUE)
colnames(sex_3_way) = c("Controls", "Cases")
rownames(sex_3_way) = c("Males", "Females", "Nonbinary")
chisq.test(sex_3_way)
#############
male_nonbinary = matrix(c(nrow(control_males), nrow(autism_males), nrow(control_nonbinary), nrow(autism_nonbinary) ), ncol = 2, byrow = TRUE)
colnames(male_nonbinary) = c("Controls", "Cases")
rownames(male_nonbinary) = c("Males", "Nonbinary")
male_nonbinary
chisq.test(male_nonbinary)
oddsratio(male_nonbinary)
##########
female_nonbinary = matrix(c(nrow(control_females), nrow(autism_females), nrow(control_nonbinary), nrow(autism_nonbinary) ), ncol = 2, byrow = TRUE)
colnames(female_nonbinary) = c("Controls", "Cases")
rownames(female_nonbinary) = c("Females", "Nonbinary")
female_nonbinary
chisq.test(female_nonbinary)
oddsratio(female_nonbinary)
##########
binary_nonbinary = matrix(c(nrow(control_females) + nrow(control_males), nrow(autism_females) + nrow(autism_males), nrow(control_nonbinary), nrow(autism_nonbinary) ), ncol = 2, byrow = TRUE)
colnames(binary_nonbinary) = c("Controls", "Cases")
rownames(binary_nonbinary) = c("Binary", "Nonbinary")
binary_nonbinary
chisq.test(binary_nonbinary)
oddsratio(binary_nonbinary)
### Regressing age effects
summary(glm(Autism.Y.N ~ relevel(as.factor(Binary_nonbinary), ref = "Nonbinary") + scale(Age) + Education, data = APHS, family = "binomial"))
summary(glm(Autism.Y.N ~ binary2 + scale(Age) + Education, data = APHS, family = "binomial"))
```
## Create plots
```{R, Plot}
ggplot(APHS, aes(x=Age, colour=Binary_nonbinary)) + geom_density() + theme_classic() + xlab("Age")
summary(aov(Age ~ Binary_nonbinary, data = APHS))
APHS$edu2 = ifelse(APHS$Education == "University Postgraduate level qualifications (MA, MSc, PhD, Certificate, etc.)", "1 - Postgraduate", 0)
APHS$edu2 = ifelse(APHS$Education == "University Undergraduate level qualifications (BA, BSc, etc.)", "2 - Undergraduate", APHS$edu2)
APHS$edu2 = ifelse(APHS$Education == "Further vocational qualifications", "3 - Vocational", APHS$edu2)
APHS$edu2 = ifelse(APHS$Education == "Secondary School/ High School level qualifications", "4 - Highschool", APHS$edu2)
APHS$edu2 = ifelse(APHS$Education == "No formal qualifications", "5 - No Highschool", APHS$edu2)
APHS$edu2 = ifelse(APHS$Education == "", "Missing", APHS$edu2)
summary_edu = setDT(APHS)[, list(count=.N) , list(Binary_nonbinary, edu2)]
summary_edu
ggplot(APHS, aes(Binary_nonbinary, fill = edu2)) + geom_bar(position = 'fill') + theme_classic()
```