-
Notifications
You must be signed in to change notification settings - Fork 1
/
CARDnonbinary_script.Rmd
227 lines (119 loc) · 6.26 KB
/
CARDnonbinary_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
---
title: "Non_binary_CARD"
author: "Varun Warrier"
date: "21 April 2019"
output:
html_document: default
pdf_document: default
---
# Libraries and read in the data
```{r setup, include=FALSE}
library(ggplot2)
library(epitools)
library(ggplot2)
library(data.table)
load("IMAGEnonbinary.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}
CARD2$key = ifelse(CARD2$Gender_final == "Male" & CARD2$ASCDiagnosis == "Y" , "autism_males", "other")
CARD2$key = ifelse(CARD2$Gender_final == "Male" & CARD2$ASCDiagnosis == "N" , "control_males", CARD2$key)
CARD2$key = ifelse(CARD2$Gender_final == "Female" & CARD2$ASCDiagnosis == "N" , "control_females", CARD2$key)
CARD2$key = ifelse(CARD2$Gender_final == "Female" & CARD2$ASCDiagnosis == "Y" , "autism_females", CARD2$key)
CARD2$key = ifelse(CARD2$Gender_final == "Nonbinary" & CARD2$ASCDiagnosis == "Y" , "autism_nonbinary", CARD2$key)
CARD2$key = ifelse(CARD2$Gender_final == "Nonbinary" & CARD2$ASCDiagnosis == "N" , "control_nonbinary", CARD2$key)
autism_males = subset(CARD2, key == "autism_males")
autism_females = subset(CARD2, key == "autism_females")
autism_nonbinary = subset(CARD2, key == "autism_nonbinary")
control_males = subset(CARD2, key == "control_males")
control_females = subset(CARD2, key == "control_females")
control_nonbinary = subset(CARD2, key == "control_nonbinary")
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)
oddsratio(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
CARD2$autism2 = ifelse(CARD2$ASCDiagnosis == "Y", 1, 0)
summary(glm(autism2 ~ Gender_3 + scale(AQ_Age) + Education_recode + Category, data = CARD2, family = "binomial"))
summary(glm(autism2 ~ relevel(as.factor(Gender_final), ref = "Nonbinary") + scale(AQ_Age) + Education_recode + Category, data = CARD2, family = "binomial" ))
```
## Let's do suspected autism now
```{R, suspected autism}
####Suspected autism
suspectall = matrix(c(589, 216, 490, 97, 10, 16 ), ncol = 2, byrow = TRUE)
colnames(suspectall) = c("Controls", "Autism_suspect")
rownames(suspectall) = c("Males", "Females", "Nonbinary")
suspectall
chisq.test(suspectall)
oddsratio(suspectall)
### Suspect_males
suspectall_males = matrix(c(589, 216, 10, 16 ), ncol = 2, byrow = TRUE)
colnames(suspectall_males) = c("Controls", "Autism_suspect")
rownames(suspectall_males) = c("Males", "Nonbinary")
suspectall_males
chisq.test(suspectall_males)
oddsratio(suspectall_males)
### Suspect_females
suspectall_females = matrix(c(490, 97, 10, 16 ), ncol = 2, byrow = TRUE)
colnames(suspectall_females) = c("Controls", "Autism_suspect")
rownames(suspectall_females) = c("Females", "Nonbinary")
suspectall_females
chisq.test(suspectall_females)
oddsratio(suspectall_females)
### Suspect_binary
suspectall_binary = matrix(c(490 + 589 , 97 + 216, 10, 16 ), ncol = 2, byrow = TRUE)
colnames(suspectall_binary) = c("Controls", "Autism_suspect")
rownames(suspectall_binary) = c("Binary", "Nonbinary")
suspectall_binary
chisq.test(suspectall_binary)
oddsratio(suspectall_binary)
```
## Autistic traits regression
```{R, Autistic traits}
summary(lm(scale(AQ_TestScore) ~ as.factor(ASCDiagnosis) + Education_recode + Gender_final + scale(AQ_Age) + Category, data = CARD2))
summary(lm(scale(AQ_TestScore) ~ as.factor(ASCDiagnosis) + Education_recode + Gender_3 + scale(AQ_Age) + Category, data = CARD2))
```
## Make plot
```{R, Plot}
ggplot(CARD2, aes(x=AQ_TestScore, colour=key)) + geom_density() + theme_classic() + xlab("AQ_full")
summary_edu = setDT(CARD2)[, list(count=.N) , list(Gender_final, Education_recode)]
summary_edu
ggplot(CARD2, aes(Gender_final, fill = Education_recode)) + geom_bar(position = 'fill') + theme_classic()
ggplot(CARD2, aes(x=AQ_Age, colour=Gender_final)) + geom_density() + theme_classic() + xlab("AQ_full")
```