-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
512 lines (512 loc) · 25.8 KB
/
.Rhistory
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
data$scode[data$sex=="(2) FEMALE"] <- "2"
ds00_nomissing$scode[data$B1PGENDER.x=="(1) MALE"] <- "1"
ds00_nomissing$scode[data$B1PGENDER.x=="(2) FEMALE"] <- "2"
ds00_nomissing$scode[ds00_nomissing$B1PGENDER.x=="(1) MALE"] <- "1"
ds00_nomissing$scode[ds00_nomissing$B1PGENDER.x=="(2) FEMALE"] <- "2"
View(ds00_nomissing)
ds00_nomissing$revalue[ds00_nomissing$B1PGENDER.x=="(1) MALE"] <- "1"
ds00_nomissing$revalue[ds00_nomissing$B1PGENDER.x=="(2) FEMALE"] <- "2"
View(ds00_nomissing)
data$scode <- factor(data$scode)
ds00_nomissing$scode[ds00_nomissing$B1PGENDER.x=="(1) MALE"] <- "1"
ds00_nomissing$scode[ds00_nomissing$B1PGENDER.x=="(1) MALE"] <- "1"
ds00_nomissing$scode[ds00_nomissing$B1PGENDER.x=="(2) FEMALE"] <- "2"
ds00_nomissing$scode<- factor(ds00_nomissing$scode)
View(ds00_nomissing)
ds00_nomissing$B1PGENDER.x[ds00_nomissing$B1PGENDER.x=="(1) MALE"] <- "1"
ds00_nomissing$B1PGENDER.x[ds00_nomissing$B1PGENDER.x=="(2) FEMALE"] <- "2"
scode <- B1PGENDER.x
B1PGENDER.x <-scode
test <- c(B1PGENDER.x)
test <- ds00_nomissing[B1PGENDER.x]
View(test)
ds00_nomissing$GENDER[ds00_nomissing$B1PGENDER.x=="(1) MALE"] <- "1"
ds00_nomissing$GENDER[ds00_nomissing$B1PGENDER.x=="(2) FEMALE"] <- "2"
View(ds00_nomissing)
ds00_nomissing$GENDER[ds00_nomissing$B1PGENDER.x=="(1) MALE"] <- "1"
ds00_nomissing$GENDER[ds00_nomissing$B1PGENDER.x=="(2) FEMALE"] <- "2"
# Convert the column to a factor
ds00_nomissing$GENDER<- factor(ds00_nomissing$GENDER)
View(ds00_nomissing)
# remove all elements for a clean start
rm(list=ls(all=TRUE))
#####LOADING DATA#####
## @knitr LoadData
data_BIOMARKER <- load("./Data/Raw/data_BIOMARKER.rda")
data_COGNITION <- load("./Data/Raw/data_COGNITIVE.rda")
data_BIOMARKER <- da29282.0001
data_COGNITION<- da25281.0001
#Load data with control variables
library(Hmisc)
library(foreign)
data_CONTROLS <- spss.get("./Data/Raw/04652-0001-Data.sav", use.value.labels=TRUE)
d0 <- merge(data_BIOMARKER,data_COGNITION,by="M2ID", all=TRUE)
MIDUS <- merge(d0,data_CONTROLS,by="M2ID", all=TRUE)
#####VARIABLES#####
#Object with MIDUS II participant variables (ID, age, gender)
MIDUS_pvars <- c('M2ID', 'B1PAGE_M2.x', 'B1PGENDER.x')
#Object with MIDUS II cognitive variables (episodic memory, EF)
MIDUS_cogvars <- c('M2ID', 'B3TEMZ3', 'B3TEFZ3')
#Object with MIDUS II variables to control for
MIDUS_controls <- c('M2ID', 'B1PB1', 'B1SA11T', 'B1SA1', 'B1PA1', 'B1PA2')
##codes:
#education B1PB1
#depression - B1SA11T
#health status - B1SA1
#physical health - B1PA1
#psychological health - B1PA2
#Object with MIDUS II participant, cognitive, and control variables
MIDUSvars <- c(
'M2ID', 'B1PAGE_M2.x', 'B1PGENDER.x',
'B3TEMZ3', 'B3TEFZ3',
'B1PB1', 'B1SA11T', 'B1SA1', 'B1PA1', 'B1PA2')
#Object with MIDUS II PSQ variables (for MIDUS_PSQ)
MIDUS_PSQ <- c(
'M2ID',
'B4S4', 'B4S5', 'B4S7',
'B4S11A', 'B4S11B', 'B4S11C', 'B4S11D', 'B4S11E',
'B4S11F', 'B4S11G', 'B4S11H', 'B4S11I', 'B4S11J')
#object with MIDUS daily sleep variables
MIDUS_sleepdaily <- c(
'M2ID',
## Sleep medications (item 7, MED):
'B4AD17', 'B4AD27', 'B4AD37', 'B4AD47', 'B4AD57', 'B4AD67', 'B4AD77',
## Sleep difficulty (item 10, SDIFF)
'B4AD110', 'B4AD210', 'B4AD310', 'B4AD410', 'B4AD510', 'B4AD610', 'B4AD710',
## No. times woke last night (item 11, WAKE)
'B4AD111', 'B4AD211', 'B4AD311', 'B4AD411', 'B4AD511', 'B4AD611', 'B4AD711',
## Trouble getting back to sleep if woke (item 13, TRSLEEP)
'B4AD113', 'B4AD213', 'B4AD313', 'B4AD413', 'B4AD513', 'B4AD613', 'B4AD713',
## Overall sleep quality (item 20, DSSQUAL)
'B4AD120', 'B4AD220', 'B4AD320', 'B4AD420', 'B4AD520', 'B4AD620', 'B4AD720',
## Sleep start time (item 18)
'B4AD18', 'B4AD28', 'B4AD38', 'B4AD48', 'B4AD58', 'B4AD68', 'B4AD78',
'B4AD18A', 'B4AD28A', 'B4AD38A', 'B4AD48A', 'B4AD58A', 'B4AD68A', 'B4AD78A',
## Minutes to sleep (item 19)
'B4AD19', 'B4AD29', 'B4AD39', 'B4AD49', 'B4AD59', 'B4AD69', 'B4AD79',
## Sleep end time (item 15)
'B4AD115', 'B4AD215', 'B4AD315', 'B4AD415', 'B4AD515', 'B4AD615', 'B4AD715',
'B4AD115A', 'B4AD215A', 'B4AD315A', 'B4AD415A', 'B4AD515A', 'B4AD615A', 'B4AD715A')
#Variables pulled out of MIDUS
d_MIDUS <- MIDUS[MIDUSvars]
d_MIDUS_PSQ<- MIDUS[MIDUS_PSQ]
d_MIDUS_sleepdaily <-MIDUS[MIDUS_sleepdaily]
#####DATA SETS#####
d0_MIDUS_PSQ <- merge(d_MIDUS,d_MIDUS_PSQ,by="M2ID")
d0_MIDUS_sleepdaily<- merge(d_MIDUS,d_MIDUS_sleepdaily,by="M2ID")
#Create new dataset without missing data
d0_PSQ <- na.omit(d0_MIDUS_PSQ)
d0_sleepdaily <- na.omit(d0_MIDUS_sleepdaily)
#####CONVERTING FACTOR TO NUMERIC IN d0_PSQ#####
#Convert GENDER to numeric in d0_PSQ
d0_PSQ$GENDER[d0_PSQ$B1PGENDER.x=="(1) MALE"] <- "1"
d0_PSQ$GENDER[d0_PSQ$B1PGENDER.x=="(2) FEMALE"] <- "2"
d0_PSQ$GENDER = as.numeric(d0_PSQ$GENDER)
#checks to see if it is numeric:
is.numeric(d0_PSQ$GENDER)
#Convert Sleep Quality to numeric in d0_PSQ
#Fairly, Good, Fairly Bad, Very Bad is condensed into "Bad" containing both groups
d0_PSQ$SQUAL[d0_PSQ$B4S5=="(1) 1=VERY GOOD"] <- "1"
d0_PSQ$SQUAL[d0_PSQ$B4S5=="(2) 2=FAIRLY GOOD"] <- "2"
d0_PSQ$SQUAL[d0_PSQ$B4S5=="(3) 3=FAIRLY BAD"] <- "2"
d0_PSQ$SQUAL[d0_PSQ$B4S5=="(4) 4=VERY BAD"] <- "2"
d0_PSQ$SQUAL = as.numeric(d0_PSQ$SQUAL)
#checks to see if it is numeric:
is.numeric(d0_PSQ$SQUAL)
#Convert Sleep Disturbance to numeric in d0_PSQ
d0_PSQ$SDISA[d0_PSQ$B4S11A=="(1) 1=NOT DURING PAST MONTH"] <- "1"
d0_PSQ$SDISA[d0_PSQ$B4S11A=="(2) 2=LESS THAN 1 X WEEK"] <- "2"
d0_PSQ$SDISA[d0_PSQ$B4S11A=="(3) 3=1-2 X WEEK"] <- "3"
d0_PSQ$SDISA[d0_PSQ$B4S11A=="(4) 4=3+ WEEK"] <- "4"
d0_PSQ$SDISA = as.numeric(d0_PSQ$SDISA)
#####
d0_PSQ$SDISB[d0_PSQ$B4S11B=="(1) 1=NOT DURING PAST MONTH"] <- "1"
d0_PSQ$SDISB[d0_PSQ$B4S11B=="(2) 2=LESS THAN 1 X WEEK"] <- "2"
d0_PSQ$SDISB[d0_PSQ$B4S11B=="(3) 3=1-2 X WEEK"] <- "3"
d0_PSQ$SDISB[d0_PSQ$B4S11B=="(4) 4=3+ WEEK"] <- "4"
d0_PSQ$SDISB = as.numeric(d0_PSQ$SDISB)
#####
d0_PSQ$SDISC[d0_PSQ$B4S11C=="(1) 1=NOT DURING PAST MONTH"] <- "1"
d0_PSQ$SDISC[d0_PSQ$B4S11C=="(2) 2=LESS THAN 1 X WEEK"] <- "2"
d0_PSQ$SDISC[d0_PSQ$B4S11C=="(3) 3=1-2 X WEEK"] <- "3"
d0_PSQ$SDISC[d0_PSQ$B4S11C=="(4) 4=3+ WEEK"] <- "4"
d0_PSQ$SDISC = as.numeric(d0_PSQ$SDISC)
#####
d0_PSQ$SDISD[d0_PSQ$B4S11D=="(1) 1=NOT DURING PAST MONTH"] <- "1"
d0_PSQ$SDISD[d0_PSQ$B4S11D=="(2) 2=LESS THAN 1 X WEEK"] <- "2"
d0_PSQ$SDISD[d0_PSQ$B4S11D=="(3) 3=1-2 X WEEK"] <- "3"
d0_PSQ$SDISD[d0_PSQ$B4S11D=="(4) 4=3+ WEEK"] <- "4"
d0_PSQ$SDISD = as.numeric(d0_PSQ$SDISD)
#####
d0_PSQ$SDISE[d0_PSQ$B4S11E=="(1) 1=NOT DURING PAST MONTH"] <- "1"
d0_PSQ$SDISE[d0_PSQ$B4S11E=="(2) 2=LESS THAN 1 X WEEK"] <- "2"
d0_PSQ$SDISE[d0_PSQ$B4S11E=="(3) 3=1-2 X WEEK"] <- "3"
d0_PSQ$SDISE[d0_PSQ$B4S11E=="(4) 4=3+ WEEK"] <- "4"
d0_PSQ$SDISE = as.numeric(d0_PSQ$SDISE)
#####
d0_PSQ$SDISF[d0_PSQ$B4S11F=="(1) 1=NOT DURING PAST MONTH"] <- "1"
d0_PSQ$SDISF[d0_PSQ$B4S11F=="(2) 2=LESS THAN 1 X WEEK"] <- "2"
d0_PSQ$SDISF[d0_PSQ$B4S11F=="(3) 3=1-2 X WEEK"] <- "3"
d0_PSQ$SDISF[d0_PSQ$B4S11F=="(4) 4=3+ WEEK"] <- "4"
d0_PSQ$SDISF = as.numeric(d0_PSQ$SDISF)
#####
d0_PSQ$SDISG[d0_PSQ$B4S11G=="(1) 1=NOT DURING PAST MONTH"] <- "1"
d0_PSQ$SDISG[d0_PSQ$B4S11G=="(2) 2=LESS THAN 1 X WEEK"] <- "2"
d0_PSQ$SDISG[d0_PSQ$B4S11G=="(3) 3=1-2 X WEEK"] <- "3"
d0_PSQ$SDISG[d0_PSQ$B4S11G=="(4) 4=3+ WEEK"] <- "4"
d0_PSQ$SDISG = as.numeric(d0_PSQ$SDISG)
#####
d0_PSQ$SDISH[d0_PSQ$B4S11H=="(1) 1=NOT DURING PAST MONTH"] <- "1"
d0_PSQ$SDISH[d0_PSQ$B4S11H=="(2) 2=LESS THAN 1 X WEEK"] <- "2"
d0_PSQ$SDISH[d0_PSQ$B4S11H=="(3) 3=1-2 X WEEK"] <- "3"
d0_PSQ$SDISH[d0_PSQ$B4S11H=="(4) 4=3+ WEEK"] <- "4"
d0_PSQ$SDISH = as.numeric(d0_PSQ$SDISH)
#####
d0_PSQ$SDISI[d0_PSQ$B4S11I=="(1) 1=NOT DURING PAST MONTH"] <- "1"
d0_PSQ$SDISI[d0_PSQ$B4S11I=="(2) 2=LESS THAN 1 X WEEK"] <- "2"
d0_PSQ$SDISI[d0_PSQ$B4S11I=="(3) 3=1-2 X WEEK"] <- "3"
d0_PSQ$SDISI[d0_PSQ$B4S11I=="(4) 4=3+ WEEK"] <- "4"
d0_PSQ$SDISI = as.numeric(d0_PSQ$SDISI)
#####
d0_PSQ$SDISJ[d0_PSQ$B4S11J=="(1) 1=NOT DURING PAST MONTH"] <- "1"
d0_PSQ$SDISJ[d0_PSQ$B4S11J=="(2) 2=LESS THAN 1 X WEEK"] <- "2"
d0_PSQ$SDISJ[d0_PSQ$B4S11J=="(3) 3=1-2 X WEEK"] <- "3"
d0_PSQ$SDISJ[d0_PSQ$B4S11J=="(4) 4=3+ WEEK"] <- "4"
d0_PSQ$SDISJ = as.numeric(d0_PSQ$SDISJ)
#Syntax to SUM Sleep Disturbance scores
d0_PSQ$SDis <- d0_PSQ$SDISA + d0_PSQ$SDISB + d0_PSQ$SDISC + d0_PSQ$SDISD + d0_PSQ$SDISE + d0_PSQ$SDISF + d0_PSQ$SDISG + d0_PSQ$SDISH + d0_PSQ$SDISI + d0_PSQ$SDISJ
#####CONVERTING FACTOR TO NUMERIC IN d0_sleepdaily#####
d0_sleepdaily$MEDsum <- ((d0_sleepdaily$MED1 +
d0_sleepdaily$MED2 +
d0_sleepdaily$MED3 +
d0_sleepdaily$MED4 +
d0_sleepdaily$MED5 +
d0_sleepdaily$MED6 +
d0_sleepdaily$MED7))
d0_sleepdaily$MED1[d0_sleepdaily$B4AD17=="(1) YES"] <- "1"
d0_sleepdaily$MED1[d0_sleepdaily$B4AD17=="(2) NO"] <- "0"
d0_sleepdaily$MED1 = as.numeric(d0_sleepdaily$MED1)
d0_sleepdaily$MED2[d0_sleepdaily$B4AD27=="(1) YES"] <- "1"
d0_sleepdaily$MED2[d0_sleepdaily$B4AD27=="(2) NO"] <- "0"
d0_sleepdaily$MED2 = as.numeric(d0_sleepdaily$MED2)
d0_sleepdaily$MED3[d0_sleepdaily$B4AD37=="(1) YES"] <- "1"
d0_sleepdaily$MED3[d0_sleepdaily$B4AD37=="(2) NO"] <- "0"
d0_sleepdaily$MED3 = as.numeric(d0_sleepdaily$MED3)
d0_sleepdaily$MED4[d0_sleepdaily$B4AD47=="(1) YES"] <- "1"
d0_sleepdaily$MED4[d0_sleepdaily$B4AD47=="(2) NO"] <- "0"
d0_sleepdaily$MED4 = as.numeric(d0_sleepdaily$MED4)
d0_sleepdaily$MED5[d0_sleepdaily$B4AD57=="(1) YES"] <- "1"
d0_sleepdaily$MED5[d0_sleepdaily$B4AD57=="(2) NO"] <- "0"
d0_sleepdaily$MED5 = as.numeric(d0_sleepdaily$MED5)
d0_sleepdaily$MED6[d0_sleepdaily$B4AD67=="(1) YES"] <- "1"
d0_sleepdaily$MED6[d0_sleepdaily$B4AD67=="(2) NO"] <- "0"
d0_sleepdaily$MED6 = as.numeric(d0_sleepdaily$MED6)
d0_sleepdaily$MED7[d0_sleepdaily$B4AD77=="(1) YES"] <- "1"
d0_sleepdaily$MED7[d0_sleepdaily$B4AD77=="(2) NO"] <- "0"
d0_sleepdaily$MED7 = as.numeric(d0_sleepdaily$MED7)
d0_sleepdaily$SDIFF1[d0_sleepdaily$B4AD110=="(1) VERY EASY"] <- "1"
d0_sleepdaily$SDIFF1[d0_sleepdaily$B4AD110=="(2) 2"] <- "2"
d0_sleepdaily$SDIFF1[d0_sleepdaily$B4AD110=="(3) 3"] <- "3"
d0_sleepdaily$SDIFF1[d0_sleepdaily$B4AD110=="(4) 4"] <- "4"
d0_sleepdaily$SDIFF1[d0_sleepdaily$B4AD110=="(5) VERY DIFFICULT"] <- "5"
d0_sleepdaily$SDIFF1 = as.numeric(d0_sleepdaily$SDIFF1)
d0_sleepdaily$SDIFF2[d0_sleepdaily$B4AD210=="(1) VERY EASY"] <- "1"
d0_sleepdaily$SDIFF2[d0_sleepdaily$B4AD210=="(2) 2"] <- "2"
d0_sleepdaily$SDIFF2[d0_sleepdaily$B4AD210=="(3) 3"] <- "3"
d0_sleepdaily$SDIFF2[d0_sleepdaily$B4AD210=="(4) 4"] <- "4"
d0_sleepdaily$SDIFF2[d0_sleepdaily$B4AD210=="(5) VERY DIFFICULT"] <- "5"
d0_sleepdaily$SDIFF2 = as.numeric(d0_sleepdaily$SDIFF2)
d0_sleepdaily$SDIFF3[d0_sleepdaily$B4AD310=="(1) VERY EASY"] <- "1"
d0_sleepdaily$SDIFF3[d0_sleepdaily$B4AD310=="(2) 2"] <- "2"
d0_sleepdaily$SDIFF3[d0_sleepdaily$B4AD310=="(3) 3"] <- "3"
d0_sleepdaily$SDIFF3[d0_sleepdaily$B4AD310=="(4) 4"] <- "4"
d0_sleepdaily$SDIFF3[d0_sleepdaily$B4AD310=="(5) VERY DIFFICULT"] <- "5"
d0_sleepdaily$SDIFF3 = as.numeric(d0_sleepdaily$SDIFF3)
d0_sleepdaily$SDIFF4[d0_sleepdaily$B4AD410=="(1) VERY EASY"] <- "1"
d0_sleepdaily$SDIFF4[d0_sleepdaily$B4AD410=="(2) 2"] <- "2"
d0_sleepdaily$SDIFF4[d0_sleepdaily$B4AD410=="(3) 3"] <- "3"
d0_sleepdaily$SDIFF4[d0_sleepdaily$B4AD410=="(4) 4"] <- "4"
d0_sleepdaily$SDIFF4[d0_sleepdaily$B4AD410=="(5) VERY DIFFICULT"] <- "5"
d0_sleepdaily$SDIFF4 = as.numeric(d0_sleepdaily$SDIFF4)
d0_sleepdaily$SDIFF5[d0_sleepdaily$B4AD510=="(1) VERY EASY"] <- "1"
d0_sleepdaily$SDIFF5[d0_sleepdaily$B4AD510=="(2) 2"] <- "2"
d0_sleepdaily$SDIFF5[d0_sleepdaily$B4AD510=="(3) 3"] <- "3"
d0_sleepdaily$SDIFF5[d0_sleepdaily$B4AD510=="(4) 4"] <- "4"
d0_sleepdaily$SDIFF5[d0_sleepdaily$B4AD510=="(5) VERY DIFFICULT"] <- "5"
d0_sleepdaily$SDIFF5 = as.numeric(d0_sleepdaily$SDIFF5)
d0_sleepdaily$SDIFF6[d0_sleepdaily$B4AD610=="(1) VERY EASY"] <- "1"
d0_sleepdaily$SDIFF6[d0_sleepdaily$B4AD610=="(2) 2"] <- "2"
d0_sleepdaily$SDIFF6[d0_sleepdaily$B4AD610=="(3) 3"] <- "3"
d0_sleepdaily$SDIFF6[d0_sleepdaily$B4AD610=="(4) 4"] <- "4"
d0_sleepdaily$SDIFF6[d0_sleepdaily$B4AD610=="(5) VERY DIFFICULT"] <- "5"
d0_sleepdaily$SDIFF6 = as.numeric(d0_sleepdaily$SDIFF6)
d0_sleepdaily$SDIFF7[d0_sleepdaily$B4AD710=="(1) VERY EASY"] <- "1"
d0_sleepdaily$SDIFF7[d0_sleepdaily$B4AD710=="(2) 2"] <- "2"
d0_sleepdaily$SDIFF7[d0_sleepdaily$B4AD710=="(3) 3"] <- "3"
d0_sleepdaily$SDIFF7[d0_sleepdaily$B4AD710=="(4) 4"] <- "4"
d0_sleepdaily$SDIFF7[d0_sleepdaily$B4AD710=="(5) VERY DIFFICULT"] <- "5"
d0_sleepdaily$SDIFF7 = as.numeric(d0_sleepdaily$SDIFF7)
d0_sleepdaily$SDIFF <- (d0_sleepdaily$SDIFF1 +
d0_sleepdaily$SDIFF2 +
d0_sleepdaily$SDIFF3 +
d0_sleepdaily$SDIFF4 +
d0_sleepdaily$SDIFF5 +
d0_sleepdaily$SDIFF6 +
d0_sleepdaily$SDIFF7)/7
d0_sleepdaily$WAKE <- (d0_sleepdaily$B4AD111 +
d0_sleepdaily$B4AD211 +
d0_sleepdaily$B4AD311 +
d0_sleepdaily$B4AD411 +
d0_sleepdaily$B4AD511 +
d0_sleepdaily$B4AD611 +
d0_sleepdaily$B4AD711)/7
d0_sleepdaily$DSQUAL1[d0_sleepdaily$B4AD120=="(1) VERY GOOD"] <- "1"
d0_sleepdaily$DSQUAL1[d0_sleepdaily$B4AD120=="(2) 2"] <- "2"
d0_sleepdaily$DSQUAL1[d0_sleepdaily$B4AD120=="(3) 3"] <- "3"
d0_sleepdaily$DSQUAL1[d0_sleepdaily$B4AD120=="(4) 4"] <- "4"
d0_sleepdaily$DSQUAL1[d0_sleepdaily$B4AD120=="(5) VERY POOR"] <- "5"
d0_sleepdaily$DSQUAL1 = as.numeric(d0_sleepdaily$DSQUAL1)
d0_sleepdaily$DSQUAL2[d0_sleepdaily$B4AD220=="(1) VERY GOOD"] <- "1"
d0_sleepdaily$DSQUAL2[d0_sleepdaily$B4AD220=="(2) 2"] <- "2"
d0_sleepdaily$DSQUAL2[d0_sleepdaily$B4AD220=="(3) 3"] <- "3"
d0_sleepdaily$DSQUAL2[d0_sleepdaily$B4AD220=="(4) 4"] <- "4"
d0_sleepdaily$DSQUAL2[d0_sleepdaily$B4AD220=="(5) VERY POOR"] <- "5"
d0_sleepdaily$DSQUAL2 = as.numeric(d0_sleepdaily$DSQUAL2)
d0_sleepdaily$DSQUAL3[d0_sleepdaily$B4AD320=="(1) VERY GOOD"] <- "1"
d0_sleepdaily$DSQUAL3[d0_sleepdaily$B4AD320=="(2) 2"] <- "2"
d0_sleepdaily$DSQUAL3[d0_sleepdaily$B4AD320=="(3) 3"] <- "3"
d0_sleepdaily$DSQUAL3[d0_sleepdaily$B4AD320=="(4) 4"] <- "4"
d0_sleepdaily$DSQUAL3[d0_sleepdaily$B4AD320=="(5) VERY POOR"] <- "5"
d0_sleepdaily$DSQUAL3 = as.numeric(d0_sleepdaily$DSQUAL3)
d0_sleepdaily$DSQUAL4[d0_sleepdaily$B4AD420=="(1) VERY GOOD"] <- "1"
d0_sleepdaily$DSQUAL4[d0_sleepdaily$B4AD420=="(2) 2"] <- "2"
d0_sleepdaily$DSQUAL4[d0_sleepdaily$B4AD420=="(3) 3"] <- "3"
d0_sleepdaily$DSQUAL4[d0_sleepdaily$B4AD420=="(4) 4"] <- "4"
d0_sleepdaily$DSQUAL4[d0_sleepdaily$B4AD420=="(5) VERY POOR"] <- "5"
d0_sleepdaily$DSQUAL4 = as.numeric(d0_sleepdaily$DSQUAL4)
d0_sleepdaily$DSQUAL5[d0_sleepdaily$B4AD520=="(1) VERY GOOD"] <- "1"
d0_sleepdaily$DSQUAL5[d0_sleepdaily$B4AD520=="(2) 2"] <- "2"
d0_sleepdaily$DSQUAL5[d0_sleepdaily$B4AD520=="(3) 3"] <- "3"
d0_sleepdaily$DSQUAL5[d0_sleepdaily$B4AD520=="(4) 4"] <- "4"
d0_sleepdaily$DSQUAL5[d0_sleepdaily$B4AD520=="(5) VERY POOR"] <- "5"
d0_sleepdaily$DSQUAL5 = as.numeric(d0_sleepdaily$DSQUAL5)
d0_sleepdaily$DSQUAL6[d0_sleepdaily$B4AD620=="(1) VERY GOOD"] <- "1"
d0_sleepdaily$DSQUAL6[d0_sleepdaily$B4AD620=="(2) 2"] <- "2"
d0_sleepdaily$DSQUAL6[d0_sleepdaily$B4AD620=="(3) 3"] <- "3"
d0_sleepdaily$DSQUAL6[d0_sleepdaily$B4AD620=="(4) 4"] <- "4"
d0_sleepdaily$DSQUAL6[d0_sleepdaily$B4AD620=="(5) VERY POOR"] <- "5"
d0_sleepdaily$DSQUAL6 = as.numeric(d0_sleepdaily$DSQUAL6)
d0_sleepdaily$DSQUAL7[d0_sleepdaily$B4AD720=="(1) VERY GOOD"] <- "1"
d0_sleepdaily$DSQUAL7[d0_sleepdaily$B4AD720=="(2) 2"] <- "2"
d0_sleepdaily$DSQUAL7[d0_sleepdaily$B4AD720=="(3) 3"] <- "3"
d0_sleepdaily$DSQUAL7[d0_sleepdaily$B4AD720=="(4) 4"] <- "4"
d0_sleepdaily$DSQUAL7[d0_sleepdaily$B4AD720=="(5) VERY POOR"] <- "5"
d0_sleepdaily$DSQUAL7 = as.numeric(d0_sleepdaily$DSQUAL7)
d0_sleepdaily$DSQUAL <- (d0_sleepdaily$DSQUAL1 +
d0_sleepdaily$DSQUAL2 +
d0_sleepdaily$DSQUAL3 +
d0_sleepdaily$DSQUAL4 +
d0_sleepdaily$DSQUAL5 +
d0_sleepdaily$DSQUAL6 +
d0_sleepdaily$DSQUAL7)/7
d0_sleepdaily$TRSLEEP1[d0_sleepdaily$B4AD113=="(1) YES"] <- "1"
d0_sleepdaily$TRSLEEP1[d0_sleepdaily$B4AD113=="(2) NO"] <- "2"
d0_sleepdaily$TRSLEEP1 = as.numeric(d0_sleepdaily$TRSLEEP1)
d0_sleepdaily$TRSLEEP3[d0_sleepdaily$B4AD313=="(1) YES"] <- "1"
d0_sleepdaily$TRSLEEP3[d0_sleepdaily$B4AD313=="(2) NO"] <- "2"
d0_sleepdaily$TRSLEEP3 = as.numeric(d0_sleepdaily$TRSLEEP3)
d0_sleepdaily$TRSLEEP4[d0_sleepdaily$B4AD413=="(1) YES"] <- "1"
d0_sleepdaily$TRSLEEP4[d0_sleepdaily$B4AD413=="(2) NO"] <- "2"
d0_sleepdaily$TRSLEEP4 = as.numeric(d0_sleepdaily$TRSLEEP4)
d0_sleepdaily$TRSLEEP5[d0_sleepdaily$B4AD513=="(1) YES"] <- "1"
d0_sleepdaily$TRSLEEP5[d0_sleepdaily$B4AD513=="(2) NO"] <- "2"
d0_sleepdaily$TRSLEEP5 = as.numeric(d0_sleepdaily$TRSLEEP5)
d0_sleepdaily$TRSLEEP6[d0_sleepdaily$B4AD613=="(1) YES"] <- "1"
d0_sleepdaily$TRSLEEP6[d0_sleepdaily$B4AD613=="(2) NO"] <- "2"
d0_sleepdaily$TRSLEEP6 = as.numeric(d0_sleepdaily$TRSLEEP6)
d0_sleepdaily$TRSLEEP7[d0_sleepdaily$B4AD713=="(1) YES"] <- "1"
d0_sleepdaily$TRSLEEP7[d0_sleepdaily$B4AD713=="(2) NO"] <- "2"
d0_sleepdaily$TRSLEEP7 = as.numeric(d0_sleepdaily$TRSLEEP7)
d0_sleepdaily$START1 = d0_sleepdaily(myData$B4AD18,"%Y-%m-%d %H:%M:%S")
#####RESHAPING DATA#####
# Create squared Sleep Duration term to use in regression for curvilinear (quadratic) relationship with cognition
# Get the mean SleepDuration, then subtract it from each person's score, then multiply the centered variables together
#Drop outlier EF
PSQ <- d0_PSQ[ which(d0_PSQ$B3TEFZ3 > -4.00), ]
#Sleep duration term
PSQ$DurationC <- PSQ$B4S4 - 6.888
PSQ$Duration2 <- PSQ$DurationC * PSQ$DurationC
View(`d0_sleepdaily`)
d0_sleepdaily$START1 = d0_sleepdaily(myData$B4AD18,"%Y-%m-%d %H:%M:%S")
d0_sleepdaily$START1 <- as.POSIXct(d0_sleepdaily$START1, format = "%H:%M)
d0_sleepdaily$START1 <- as.POSIXct(d0_sleepdaily$B4AD18, format = "%H:%M)
is.numeric(d0_sleepdaily$TRSLEEP7)
is.numeric(d0_sleepdaily$B4AD18)
is.factor(d0_sleepdaily$B4AD18)
is.interval(do_sleepdaily$B4AD18)
d0_sleepdaily$day1_t1 = as.numeric(d0_sleepdaily$B4AD18)
View(`d0_sleepdaily`)
list(d0_sleepdaily)
is.integer((d0_sleepdaily$B4AD18)
is.integer(d0_sleepdaily$B4AD18)
is.numeric(d0_sleepdaily$B4AD18)
is.integer(d0_sleepdaily$B4AD18)
d0_sleepdaily$day1_t1 = as.integer(d0_sleepdaily$B4AD18)
list(d0_sleepdaily)
as.POSIXct(d0_sleepdaily$B4AD18)
as.POSIXlt(d0_sleepdaily$B4AD18)
is.POSIXlt(d0_sleepdaily$B4AD18)
is.POSIXct(d0_sleepdaily$B4AD18)
d0_sleepdaily$day1_t1 = strptime( paste(d0_sleepdaily$B4AD18[,1], d0_sleepdaily$B4AD18[,2]), "%Y-%m-%d %H:%M:%S")
d0_sleepdaily$day1_t1 = as.numeric(d0_sleepdaily$B4AD18, strsplit(time, ':')[[1]][1])
is.character(d0_sleepdaily$B4AD18)
d0_sleepdaily$day1_t1 = as.character(d0_sleepdaily$B4AD18)
list(d0_sleepdaily)
d0_sleepdaily$test<-strsplit(as.character(d0_sleepdaily$day1_t1), " ")[[1]][2]
hr<-as.numeric(strsplit(d0_sleepdaily$test, ":")[[1]][1])
min<-as.numeric(strsplit(d0_sleepdaily$test, ":")[[1]][2])
list(d0_sleepdaily)
hr<-as.numeric(strsplit(d0_sleepdaily$day1_t1, ":")[[1]][1])
min<-as.numeric(strsplit(d0_sleepdaily$day1_t1, ":")[[1]][2])
list(d0_sleepdaily)
d0_sleepdaily$day1_t1_hr<-as.numeric(strsplit(d0_sleepdaily$day1_t1, ":")[[1]][1])
d0_sleepdaily$day1_t1_min<-as.numeric(strsplit(d0_sleepdaily$day1_t1, ":")[[1]][2])
list(d0_sleepdaily)
aracter(d0_sleepdaily$B4AD18)
d0_sleepdaily$day1_t1_hr<-as.numeric(strsplit(d0_sleepdaily$day1_t1, ":")[[1]][1])
d0_sleepdaily$day1_t1 = as.character(d0_sleepdaily$B4AD18)
d0_sleepdaily$day1_t1_hr<-as.numeric(strsplit(d0_sleepdaily$day1_t1, ":")[[1]][1])
list(d0_sleepdaily$day1_t1_hr)
d0_sleepdaily$day1_t1_hr<-as.numeric(strsplit(d0_sleepdaily$day1_t1, ":")[[1]][2])
list(d0_sleepdaily$day1_t1_hr)
d0_sleepdaily$day1_t1 = as.factor(d0_sleepdaily$B4AD18)
d0_sleepdaily$day1_t1_hr<-as.numeric(strsplit(d0_sleepdaily$day1_t1, ":")[[1]][1])
d0_sleepdaily$day1_t1_hr<-as.numeric(strsplit(d0_sleepdaily$day1_t1, ":")[[1]][1])
d0_sleepdaily$day1_t1 = as.character(d0_sleepdaily$B4AD18)
d0_sleepdaily$day1_t1_hr<-as.numeric(strsplit(d0_sleepdaily$day1_t1, ":")[[1]][1])
d0_sleepdaily$day1_t1_hr<-as.factor(strsplit(d0_sleepdaily$day1_t1, ":")[[1]][1])
list(d0_sleepdaily$day1_t1_hr)
d0_sleepdaily$day1_t1 = as.character(d0_sleepdaily$B4AD18)
d0_sleepdaily$day1_t1_hr<-as.numeric(strsplit(d0_sleepdaily$day1_t1, ":")
list(d0_sleepdaily$day1_t1_hr)
list(d0_sleepdaily$day1_t1_hr)
d0_sleepdaily$day1_t1 = as.character(d0_sleepdaily$B4AD18)
d0_sleepdaily$day1_t1_hr<-as.numeric(strsplit(d0_sleepdaily$day1_t1, ":")
list(d0_sleepdaily)
list(d0_sleepdaily)
d0_sleepdaily$day1_t1_hr<-as.numeric(strsplit(d0_sleepdaily$day1_t1, ":")[[1]][1])
head(d0_sleepdaily$B4AD18)
head(d0_sleepdaily$day1_t1_hr)
d0_sleepdaily$day1_t1_hr<-as.numeric(strsplit(d0_sleepdaily$day1_t1, ":")[[1]][1])
head(d0_sleepdaily$day1_t1_hr)
d0_sleepdaily$day1_t1 = as.character(d0_sleepdaily$B4AD18)
d0_sleepdaily$day1_t1_hr<-as.numeric(strsplit(d0_sleepdaily$day1_t1, ":")[[1]][1])
head(d0_sleepdaily$day1_t1_hr)
d0_sleepdaily$day1_t1 = as.character(d0_sleepdaily$B4AD18), " ")[[1]][2]
d0_sleepdaily$day1_t1 = as.character(d0_sleepdaily$B4AD18) " ")[[1]][2]
d0_sleepdaily$day1_t1 = as.character(d0_sleepdaily$B4AD18),":")[[1]][2]
d0_sleepdaily$day1_t1 = as.character(d0_sleepdaily$B4AD18,":")[[1]][2]
d0_sleepdaily$day1_t1_hr<-as.numeric(strsplit(d0_sleepdaily$day1_t1, ":")[[1]][1])
head(day1_t1_hr)
head(d0_sleepdaily$day1_t1_hr)
d0_sleepdaily$day1_t1 = as.character(d0_sleepdaily$B4AD18, " ")[[1]][2]
d0_sleepdaily$day1_t1_hr<-as.numeric(strsplit(d0_sleepdaily$day1_t1, ":")[[1]][1])
head(d0_sleepdaily$day1_t1_hr)
head(d0_sleepdaily$day1_t1)
d0_sleepdaily$day1_t1 = as.character(d0_sleepdaily$B4AD18)
head(d0_sleepdaily$day1_t1)
d0_sleepdaily$dt11<-strsplit(as.character(d0_sleepdaily$day1_t1), " ")[[1]][2]
list(d0_sleepdaily$dt11)
d0_sleepdaily$dt11<-strsplit(as.character(d0_sleepdaily$day1_t1), ":")[[1]][2]
list(d0_sleepdaily$dt11)
d0_sleepdaily$dt11<-strsplit(as.character(d0_sleepdaily$day1_t1), ":")[[2]][2]
list(d0_sleepdaily$dt11)
d0_sleepdaily$dt11<-strsplit(as.character(d0_sleepdaily$day1_t1), ":")[[1]][1]
d0_sleepdaily$dt11<-strsplit(as.character(d0_sleepdaily$day1_t1), ":")[[1]][1]
list(d0_sleepdaily$dt11)
d0_sleepdaily$dt11<-strsplit(as.character(d0_sleepdaily$day1_t1), ":")[[1]]
d0_sleepdaily$dt11<-strsplit(as.character(d0_sleepdaily$day1_t1), " ")
list(d0_sleepdaily$dt11)
d0_sleepdaily$day1_t1_hr<-as.numeric(strsplit(d0_sleepdaily$day1_t1, ":")[[1]][1])
list(d0_sleepdaily$day1_t1_hr)
d0_sleepdaily$day1_t1_hr<-as.numeric(strsplit(d0_sleepdaily$dt11, ":")[[1]][1])
d0_sleepdaily$dt11<-strsplit(as.character(d0_sleepdaily$day1_t1), " ")[[1]][2]
d0_sleepdaily$day1_t1_hr<-as.numeric(strsplit(d0_sleepdaily$dt11, ":")[[1]][1])
list(d0_sleepdaily$day1_t1_hr)
list(d0_sleepdaily$dt11)
list(d0_sleepdaily$day1_t1)
library("stringr", lib.loc="~/R/win-library/3.1")
d0_sleepdaily$day1_t1 = as.character(d0_sleepdaily$B4AD18)
d0_sleepdaily$dt11<-strsplit(as.character(d0_sleepdaily$day1_t1), " ")[[1]][2]
d0_sleepdaily$day1_t1_hr<-as.numeric(strsplit(d0_sleepdaily$dt11, ":")[[1]][1])
list(d0_sleepdaily$day1_t1_hr)
d0_sleepdaily$dt11<-str_split(d0_sleepdaily$day1_t1, ":")
list(dt11)
list(d0_sleepdaily$dt11)
d0_sleepdaily$day1_t1_hr<-str_split(d0_sleepdaily$dt11, ":")[[1]][1])
d0_sleepdaily$day1_t1_hr<-str_split(d0_sleepdaily$dt11, ":", 1)
d0_sleepdaily$day1_t1_hr<-str_split(d0_sleepdaily$dt11, ":", [1])
d0_sleepdaily$day1_t1_hr<-str_split(d0_sleepdaily$dt11, " ",)
is.atomic(d0_sleepdaily$dt11<-str_split)
is.atomic(d0_sleepdaily$dt11)
d0_sleepdaily$dt11<-as.atomic(str_split(d0_sleepdaily$day1_t1, ":"))
d0_sleepdaily$day1_t1 = as.atomic(d0_sleepdaily$B4AD18)
d0_sleepdaily$dt11<-str_split(d0_sleepdaily$day1_t1, ":")
d0_sleepdaily$day1_t1 = as.character(d0_sleepdaily$B4AD18)
d0_sleepdaily$dt11<-str_split(d0_sleepdaily$day1_t1, ":")
d0_sleepdaily$day1_t1_min<-as.numeric(strsplit(d0_sleepdaily$dt11, " ")[[1]][2])
d0_sleepdaily$day1_t1_min<-as.numeric(strsplit(d0_sleepdaily$dt11, ":")[[1]][2])
list(d0_sleepdaily$dt11)
detach("package:stringr", unload=TRUE)
d0_sleepdaily$day1_t1_min<-as.numeric(strsplit(d0_sleepdaily$dt11, ":")[[1]][2])
list(day1_t1_hr)
list(d0_sleepdaily$day1_t1_hr)
list(d0_sleepdaily$dt11)
d0_sleepdaily$day1_start1 <-strsplit(as.character(timeStamp), " ")[[1]][2]
d0_sleepdaily$day1_start1 <-strsplit(as.character(d0_sleepdaily$dt11), " ")[[1]][2]
list(d0_sleepdaily$day1_start1)
d0_sleepdaily$day1_start1_hr<-as.numeric(strsplit(d0_sleepdaily$day1_start1, ":")[[1]][1])
d0_sleepdaily$day1_start1_hr<-as.numeric(strsplit(d0_sleepdaily$day1_start1, " ")[[1]][1])
d0_sleepdaily$day1_start1_hr<-as.numeric(strsplit(d0_sleepdaily$dt11, " ")[[1]][1])
d0_sleepdaily$dt11<-str_split(d0_sleepdaily$day1_t1, ":")
library("stringr", lib.loc="~/R/win-library/3.1")
d0_sleepdaily$dt11<-str_split(d0_sleepdaily$day1_t1, ":")
d0_sleepdaily$day1_start1_hr<-as.numeric(strsplit(d0_sleepdaily$dt11, " ")[[1]][1])
d0_sleepdaily$dt11<-str_split_fixed(d0_sleepdaily$day1_t1, ":")
d0_sleepdaily$day1_t1 = as.character(d0_sleepdaily$B4AD18)
d0_sleepdaily$dt11<-str_split_fixed(d0_sleepdaily$day1_t1, ":")
d0_sleepdaily$day1_t1 = as.character(d0_sleepdaily$B4AD18)
d0_sleepdaily$dt11<-str_split(d0_sleepdaily$day1_t1, ":")
list(d0_sleepdaily$dt11)
list(d0_sleepdaily$dt11)
d0_sleepdaily$dt11[[1]]
d0_sleepdaily$dt11[[2]]
d0_sleepdaily$dt11[[3]]
d0_sleepdaily$dt11 = class(d0_sleepdaily$dt11)
d0_sleepdaily$dt11[[1]]
d0_sleepdaily$day1_start1_hr<-as.numeric(strsplit(d0_sleepdaily$dt11, " ")[[1]][1])
d0_sleepdaily$day1_start1_hr<-as.numeric(strsplit(d0_sleepdaily$dt11, " ")[[1]][1])
d0_sleepdaily$dt1[[1]]
d0_sleepdaily$dt1[[1]][1]
list(d0_sleepdaily$dt1[[1]][1])
head(d0_sleepdaily$dt1)
d0_sleepdaily$day1_start1_hr<-str_split(d0_sleepdaily$dt11, " ", 1)
head(d0_sleepdaily$day1_start1_hr)
d0_sleepdaily$dt11<-str_split(d0_sleepdaily$day1_t1, ":")
d0_sleepdaily$day1_start1_hr<-str_split(d0_sleepdaily$dt11, " ", 1)
d0_sleepdaily$day1_start1_hr<-str_split(d0_sleepdaily$dt11, ":", 1)
d0_sleepdaily$dt11<-str_split(d0_sleepdaily$day1_t1, ":", 1)
head(d0_sleepdaily$dt11)
d0_sleepdaily$dt11<-str_split(d0_sleepdaily$day1_t1, ":", 1, 2)
d0_sleepdaily$dt11<-str_split(d0_sleepdaily$day1_t1, ":", 2)
list(d0_sleepdaily$dt11)
d0_sleepdaily$dt11_hr <- strsplit(d0_sleepdaily$dt11, " ")[[1]][2])
d0_sleepdaily$dt11_hr <- (strsplit(d0_sleepdaily$dt11, " ")[[1]][2])
d0_sleepdaily$dt11_hr <- (strsplit(d0_sleepdaily$dt11, ":")[[1]][2])
d0_sleepdaily$dt11_hr <- (strsplit(d0_sleepdaily$dt11, ":")[[1]][2])
d0_sleepdaily$dt11_hr <- (strsplit(d0_sleepdaily$dt11, ":")[[1]][2])
d0_sleepdaily$dt11_hr <- (strsplit(d0_sleepdaily$dt11, ":")[[1]][2])
d0_sleepdaily$dt11_hr <- (strsplit(d0_sleepdaily$dt11, ":")[[1]][2])
d0_sleepdaily$dt11_hr <- (strsplit(d0_sleepdaily$dt11, ":")[[1]][2])
d0_sleepdaily$dt11_hr <- (strsplit(d0_sleepdaily$dt11, ":")[[1]][2])
d0_sleepdaily$dt11_hr <- (strsplit(d0_sleepdaily$dt11, ":")[[1]][2])