-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_partialeffects_repeats.Rmd
203 lines (157 loc) · 7.08 KB
/
3_partialeffects_repeats.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
---
title: "Toxic metal mixtures in wells and PTB: partial effects modeling, repeats"
author: "Lauren Eaves"
date: "4/13/2022"
output: word_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
sessionInfo()
rm(list=ls())
#install.packages("yaml")
#install.packages("rmarkdown")
#install.packages("broom")
#install.packages("devtools")
#library(devtools)
#devtools::install_github("alexpkeil1/qgcompint")
library(tidyverse)
library(ggplot2)
library(knitr)
library(yaml)
library(rmarkdown)
library(broom)
library(ggpubr)
library(qgcomp)
library(qgcompint)
#Create a current date variable to name outputfiles
cur_date <- str_replace_all(Sys.Date(),"-","")
#Create an output folder
Output_Folder <- ("/Users/lauren_eaves/IEHS Dropbox/lauren Eaves/2_Dissertation/2_Analysis/Aim 2/1_analysis/output")
#Load cohort data - generated in script: 1_20220131_cohortdataprep
cohort <- read.csv(file="output/20220405_NCbirths_NAsingletons_GAexclusions_tractlevelmetals_data_withexposurevars.csv")
colnames(cohort)
```
#Covariate variables generation
```{r}
#ensure covariates are in correct variable type form
cohort <- cohort %>%
mutate(racegp = as.factor(racegp)) %>%
mutate(mage = as.numeric(mage)) %>%
mutate(mage_sq = as.numeric(mage_sq)) %>%
mutate(smoke = as.numeric(smoke)) %>%
mutate(season_concep = as.factor(season_concep)) %>%
mutate(mothed = as.numeric(mothed)) %>%
mutate(Nitr_perc = as.numeric(Nitr_perc)) %>%
mutate(sex = as.factor(sex))%>%
mutate(pov_perc = as.numeric(pov_perc))
cohort <- cohort %>%
mutate(preterm = as.factor(preterm)) %>%
mutate(verypreterm = as.factor(verypreterm)) %>%
mutate(extremepreterm = as.factor(extremepreterm))
cohort$preterm <- relevel(cohort$preterm, ref = "0")
cohort$verypreterm <- relevel(cohort$verypreterm, ref = "0")
cohort$extremepreterm <- relevel(cohort$extremepreterm, ref = "0")
```
#PTB mixture models: partial effects, repeats
```{r}
Xnm <- c('Arsenic.Mean_avg', 'Cadmium.Mean_avg', 'Lead.Mean_avg', 'Manganese.Mean_avg', 'Chromium.Mean_avg', 'Copper.Mean_avg', 'Zinc.Mean_avg')
covars = c('mage','mage_sq','racegp','smoke','season_concep','mothed','Nitr_perc','pov_perc')
for (i in 1:12) {
round <- i
round <- as.name(round)
print(round)
n <- runif(1,1,3000000)
set.seed(n)
print(n)
trainidx <- sample(1:nrow(cohort), round(nrow(cohort)*0.3))
valididx <- setdiff(1:nrow(cohort),trainidx)
traindata <- cohort[trainidx,]
validdata <- cohort[valididx,]
PTB_splitres <- qgcomp.partials(fun="qgcomp.noboot",
f=preterm~.,
q=4,
traindata=traindata[,c(Xnm,covars,'preterm')],
validdata=validdata[,c(Xnm,covars,'preterm')],
expnms=Xnm,
family=binomial())
PTB_splitres
plot(PTB_splitres$pos.fit)
plot(PTB_splitres$neg.fit)
PTB_splitres$posmix
posfit_posweight <- as.data.frame(PTB_splitres$pos.fit$pos.weights) %>%
mutate("round"=paste0(round))
posfit_negweight <- as.data.frame(PTB_splitres$pos.fit$neg.weights)%>%
mutate("round"=paste0(round))
PTB_splitres$negmix
negfit_posweight <- as.data.frame(PTB_splitres$neg.fit$pos.weights)%>%
mutate("round"=paste0(round))
negfit_negweight <- as.data.frame(PTB_splitres$neg.fit$neg.weights)%>%
mutate("round"=paste0(round))
assign(paste0("round",round,"_PTB_splitres"),PTB_splitres)
assign(paste0("round",round,"_posfit"),PTB_splitres$pos.fit)
assign(paste0("round",round,"_negfit"),PTB_splitres$neg.fit)
assign(paste0("round",round,"_posfit_posweight"),posfit_posweight)
assign(paste0("round",round,"_posfit_negweight"),posfit_negweight)
assign(paste0("round",round,"_negfit_posweight"),negfit_posweight)
assign(paste0("round",round,"_negfit_negweight"),negfit_negweight)
}
#export weights from each round
negfit_posweight<-grep("_negfit_posweight",names(.GlobalEnv),value=TRUE)
negfit_posweight_list_results<-do.call("list",mget(negfit_posweight))
print(names(negfit_posweight_list_results))
negfit_negweight<-grep("_negfit_negweight",names(.GlobalEnv),value=TRUE)
negfit_negweight_list_results<-do.call("list",mget(negfit_negweight))
print(names(negfit_negweight_list_results))
posfit_posweight<-grep("_posfit_posweight",names(.GlobalEnv),value=TRUE)
posfit_posweight_list_results<-do.call("list",mget(posfit_posweight))
print(names(posfit_posweight_list_results))
posfit_negweight<-grep("_posfit_negweight",names(.GlobalEnv),value=TRUE)
posfit_negweight_list_results<-do.call("list",mget(posfit_negweight))
print(names(posfit_negweight_list_results))
negfit<-grep("_negfit",names(.GlobalEnv),value=TRUE)
negfit_list_results<-do.call("list",mget(negfit))
print(names(negfit_list_results))
posfit<-grep("_posfit",names(.GlobalEnv),value=TRUE)
posfit_list_results<-do.call("list",mget(posfit))
print(names(posfit_list_results))
splitres<-grep("splitres",names(.GlobalEnv),value=TRUE)
splitres_list_results<-do.call("list",mget(splitres))
print(names(splitres_list_results))
combined_negfit_posweight <- bind_rows(negfit_posweight_list_results, .id=NULL)
combined_negfit_negweight <- bind_rows(negfit_negweight_list_results, .id=NULL)
combined_posfit_posweight <- bind_rows(posfit_posweight_list_results, .id=NULL)
combined_posfit_negweight <- bind_rows(posfit_negweight_list_results, .id=NULL)
rm(list=c(names(splitres_list_results),names(negfit_list_results), names(posfit_list_results), names(negfit_posweight_list_results),names(negfit_negweight_list_results),names(posfit_posweight_list_results),names(posfit_negweight_list_results)))
negfit_list_results$round1_negfit
negfit_list_results$round2_negfit
negfit_list_results$round3_negfit
negfit_list_results$round4_negfit
negfit_list_results$round5_negfit
negfit_list_results$round6_negfit
negfit_list_results$round7_negfit
negfit_list_results$round8_negfit
negfit_list_results$round9_negfit
negfit_list_results$round10_negfit
negfit_list_results$round11_negfit
negfit_list_results$round12_negfit
posfit_list_results$round1_posfit
posfit_list_results$round2_posfit
posfit_list_results$round3_posfit
posfit_list_results$round4_posfit
posfit_list_results$round5_posfit
posfit_list_results$round6_posfit
posfit_list_results$round7_posfit
posfit_list_results$round8_posfit
posfit_list_results$round9_posfit
posfit_list_results$round10_posfit
posfit_list_results$round11_posfit
posfit_list_results$round12_posfit
```
#Export results
```{r}
#bind all weights outputs from previous runs
write.csv(combined_negfit_posweight, paste0(Output_Folder,"/", cur_date, "_qgcomp_Results_weights_partialeffectsrepeats_negfitposweight.csv"), row.names=TRUE)
write.csv(combined_negfit_negweight, paste0(Output_Folder,"/", cur_date, "_qgcomp_Results_weights_partialeffectsrepeats_negfitnegweight.csv"), row.names=TRUE)
write.csv(combined_posfit_posweight, paste0(Output_Folder,"/", cur_date, "_qgcomp_Results_weights_partialeffectsrepeats_posfitposweight.csv"), row.names=TRUE)
write.csv(combined_posfit_negweight, paste0(Output_Folder,"/", cur_date, "_qgcomp_Results_weights_partialeffectsrepeats_posfitnegweight.csv"), row.names=TRUE)
```