forked from PMacDaSci/r-intermediate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fake-data.Rmd
303 lines (230 loc) · 8.07 KB
/
fake-data.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
---
title: "Data Example"
author: "Mark Dunning"
date: "2 February 2016"
output: html_document
---
```{r}
library(wakefield)
library(stringr)
library(dplyr)
set.seed(666)
random_patients <- function(n) {
as.data.frame(r_data_frame(
n,
id,
name,
race,
# age,
sex,
smokes,
height_cm(name = "Height"),
normal(name="Weight"),
birth(random = TRUE, x = NULL, start = Sys.Date() - 365 * 45, k = 365*2,by = "1 days"),
state,
pet,
grade_level(x=1:3),
died,
normal_round(name="Count",digits=2),
date_stamp)
)
}
patients <- random_patients(100)
patients$Height <- round(ifelse(patients$Sex == "Male",rnorm(n=100,mean=175.3,sd=6),rnorm(n=100,161.9,sd=3)),2)
patients$Weight <- round(ifelse(patients$Sex == "Male",rnorm(n=100,mean=84,sd=6),rnorm(n=100,69,sd=3)),2)
patients$Weight <- paste0(patients$Weight, "kg")
```
1. Change some values of `Smokes` to yes/no
```{r}
rand_rows <- sample(1:100,10)
patients$Smokes[rand_rows] <- sample(c("Yes","No"),10,replace=TRUE)
```
2. Add cm to the Height column
```{r}
patients$Height <- as.character(patients$Height)
patients$Height <- paste0(patients$Height,"cm")
```
3. Add whitespace to Male / Female columns
```{r}
rand_rows <- sample(1:100,10)
patients$Sex <- as.character(patients$Sex)
patients$Sex[rand_rows] <- paste0(" ", patients$Sex[rand_rows])
rand_rows <- sample(1:100,6)
patients$Sex[rand_rows] <- paste0(patients$Sex[rand_rows], " ")
```
4. upper-case convert some of the pets
```{r}
rand_rows <- sample(1:100,10)
patients$Pet <- as.character(patients$Pet)
patients$Pet[rand_rows] <- toupper(patients$Pet[rand_rows])
```
5. convert some of the `None` pets to NA or NULL
```{r}
patients$Race <- as.character(patients$Race)
error_row <- sample(which(patients$Pet != "None"),1)
patients$Race[error_row] <- patients$Pet[error_row]
no_pet <- which(patients$Pet == "None")
patients$Pet[sample(no_pet,2)] <- "NA"
patients$Pet[sample(no_pet,3)] <- "NULL"
```
6. Make some of the grades '99'
```{r}
rand_rows <- sample(1:100,7)
patients$Grade_Level <- as.numeric(patients$Grade_Level)
patients$Grade_Level[rand_rows] <- 99
```
7. Put in the IDs from the diabetes data
```{r}
diabetes <- read.delim("diabetes.txt")
patients$ID <- unique(diabetes$ID)
##First 57 IDs are from centre AH
```
8. Retain only the first occurence of each date
```{r}
dates <- unique(patients$Date)
firstDate <- match(dates, patients$Date)
patients$Date <- ""
patients$Date[firstDate] <- as.character(dates)
colnames(patients)[ncol(patients)] <- "Date Entered Study"
```
9. Change some names to upper case
```{r}
rand_rows <- sample(1:nrow(patients), 20)
patients$Name <- as.character(patients$Name)
patients$Name[rand_rows] <- str_to_upper(as.character(patients$Name[rand_rows]))
```
10. Introduce typos in 'California'
```{r}
cal <- sample(grep("California", patients$State),2)
patients$State <- as.character(patients$State)
patients$State[cal] <- "Californa"
car <- sample(grep("Carolina", patients$State),2)
patients$State[car] <- gsub("Carolina", "Caroline",patients$State[car])
```
```{r}
head(patients)
write.table(patients, file="patient-data.txt",sep="\t",row.names = FALSE)
```
Clean the data
```{r}
library(lubridate)
grds <- patients$Grade_Level
dob <- ymd(patients$Birth)
today <- ymd("20160509")
age <- interval(dob, today)
read.delim("patient-data.txt") %>%
tbl_df %>%
mutate(Age = year(as.period(age))) %>%
mutate(Sex = factor(str_trim(Sex))) %>%
mutate(Height= as.numeric(str_replace_all(Height,pattern = "cm",""))) %>%
mutate(Weight = as.numeric(str_replace_all(Weight,"kg",""))) %>%
mutate(BMI = round((Weight/(Height/100)^2),2), Overweight = BMI > 25) %>%
mutate(Smokes = str_replace_all(Smokes, "Yes|TRUE", "Smoker")) %>%
mutate(Smokes = str_replace_all(Smokes, "No|FALSE", "Non-Smoker")) %>%
mutate(Pet = str_to_title(Pet)) %>%
mutate(Name = str_to_title(Name)) %>%
mutate(Grade_Level = ifelse(Grade_Level == 99, NA,Grade_Level)) %>%
rename(Grade = Grade_Level) %>%
mutate(Pet = str_replace_all(Pet, "Null","NA")) %>%
mutate(Race = factor(Race,levels=c("Asian","Bi-Racial","Black","Hispanic","White",NA))) -> patients_clean
patients_clean
```
```{r}
write.table(patients_clean,file="patient-data-cleaned.txt",sep="\t",row.names = FALSE)
```
Now for a larger set
```{r}
random_patients <- function(n) {
as.data.frame(r_data_frame(
n,
id,
# name,
race,
# age,
sex,
smokes,
height_cm(name = "Height"),
normal(name="Weight"),
birth(random = TRUE, x = NULL, start = Sys.Date() - 365 * 45, k = 365*2,by = "1 days"),
state,
pet,
grade_level(x=1:3),
died,
normal_round(name="Count",digits=2),
date_stamp)
)
}
cohort <- random_patients(10023)
cohort$Height <- round(ifelse(cohort$Sex == "Male",rnorm(n=100,mean=175.3,sd=6),rnorm(n=100,161.9,sd=3)),2)
cohort$Weight <- round(ifelse(cohort$Sex == "Male",rnorm(n=100,mean=84,sd=6),rnorm(n=100,69,sd=3)),2)
cohort$Weight <- paste0(cohort$Weight, "kg")
cohort$Height <- as.character(cohort$Height)
cohort$Height <- paste0(cohort$Height,"cm")
rand_rows <- sample(1:1000,10)
cohort$Sex <- as.character(cohort$Sex)
cohort$Sex[rand_rows] <- paste0(" ", cohort$Sex[rand_rows])
rand_rows <- sample(1:1000,6)
cohort$Sex[rand_rows] <- paste0(cohort$Sex[rand_rows], " ")
rand_rows <- sample(1:100,10)
cohort$Pet <- as.character(cohort$Pet)
cohort$Pet[rand_rows] <- toupper(cohort$Pet[rand_rows])
no_pet <- which(patients$Pet == "None")
cohort$Pet[sample(no_pet,2)] <- "NA"
cohort$Pet[sample(no_pet,3)] <- "NULL"
rand_rows <- sample(1:100,7)
cohort$Grade_Level <- as.numeric(cohort$Grade_Level)
cohort$Grade_Level[rand_rows] <- 99
write.table(cohort, file="cohort-data.txt",sep="\t",row.names = FALSE)
```
```{r}
set.seed(20170120)
Placebo.1 <- rnorm(10,mean = 50, sd=3)
Placebo.2 <- rnorm(10, mean=48, sd=5)
Drug1.1 <- rnorm(10, mean=45, sd=3)
Drug1.2 <- rnorm(10, mean=43, sd=5)
Drug2.1 <- rnorm(10, 41,sd=2)
Drug2.2 <- rnorm(10, 37,sd=3)
clinicalData <- data.frame(Subject = paste0("Patient",1:10), Placebo.1, Placebo.2, Drug1.1,Drug1.2,Drug2.1,Drug2.2) %>%
mutate_each(funs(round(.,2)),-Subject)
write.table(clinicalData, file="clinicalData.txt",row.names=FALSE,sep="\t")
messyData <- read.delim("clinicalData.txt")
tidyData <- gather(messyData, Treatment, Value,-Subject)
png("images/tidy-boxplot.png",width=800,height=600)
boxplot(tidyData$Value ~ tidyData$Treatment)
dev.off()
```
Replace with better first names
```{r}
patients <- read.delim("patient-data.txt",stringsAsFactors = FALSE)
patients
patients_cleaned <- read.delim("patient-data-cleaned.txt",stringsAsFactors = FALSE)
patients_cleaned
if(!require(babynames)) devtools:::install_github("hadley/babynames")
data("babynames")
girls <- babynames %>% filter(year == 1973, sex=="F") %>% top_n(100,prop) %>% select(name) %>% unlist %>% as.character
boys <- babynames %>% filter(year == 1974, sex=="M") %>% top_n(100,prop) %>% select(name) %>% unlist %>% as.character
nboys <- table(patients_cleaned$Sex)[2]
ngirls <- table(patients_cleaned$Sex)[1]
set.seed("28032017")
newboys <- sample(boys,nboys,replace = TRUE)
newgirls <- sample(girls ,ngirls, replace = TRUE)
patients$Name[which(as.character(patients_cleaned$Sex) == "Male")] <- newboys
patients$Name[which(patients_cleaned$Sex == "Female")] <- newgirls
patients
write.table(patients, file="patient-data.txt",sep="\t",row.names = FALSE)
patients_cleaned$Name[which(as.character(patients_cleaned$Sex) == "Male")] <- newboys
patients_cleaned$Name[which(patients_cleaned$Sex == "Female")] <- newgirls
patients_cleaned
```
Also fill the Date Entered Study column
```{r}
library(tidyr)
## Turn the column into a character
patients_cleaned <- mutate(patients_cleaned, Date.Entered.Study = as.character(Date.Entered.Study))
## Replace the blank strings with an NA
patients_cleaned <- mutate(patients_cleaned, Date.Entered.Study = ifelse(Date.Entered.Study == "", NA,Date.Entered.Study))
## Now fill will happily fill the missing values for us
patients_cleaned <- fill(patients_cleaned,Date.Entered.Study)
patients_cleaned
write.table(patients_cleaned, file="patient-data-cleaned.txt",sep="\t",row.names = FALSE)
```