-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rhistory
512 lines (512 loc) · 21.4 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
setwd("/Users/kevinkuehn/Documents/Writing/YDSP collab/YDSP")
getwd()
write.csv(data, file = "full.random.csv")
write.csv(pdat2, file="pdat2.csv")
data<-read.csv("~/Documents/Writing/YDSP collab/YDSP/Daily data.csv")
data2<-read.spss("~/Documents/Writing/YDSP collab/YDSP/Baseline_variables.sav")
data<-as.data.frame(data)
data<-slide(data=data, Var="coping_sum_3avg",TimeVar="Day",GroupVar ="ID",NewVar="coping_sum_3avg.lag",slideBy=-1)
data<-slide(data=data, Var="all_coping_talkfamily",TimeVar="Day",GroupVar ="ID",NewVar="all_coping_talkfamily.lag",slideBy=-1)
data<-slide(data=data, Var="all_coping_talkMH",TimeVar="Day",GroupVar ="ID",NewVar="all_coping_talkMH.lag",slideBy=-1)
data<-slide(data=data, Var="all_coping_copingthought",TimeVar="Day",GroupVar ="ID",NewVar="all_coping_copingthought.lag",slideBy=-1)
data<-slide(data=data, Var="all_coping_thinking",TimeVar="Day",GroupVar ="ID",NewVar="all_coping_thinking.lag",slideBy=-1)
data<-slide(data=data, Var="all_coping_talkfriend",TimeVar="Day",GroupVar ="ID",NewVar="all_coping_talkfriend.lag",slideBy=-1)
data<-slide(data=data, Var="all_coping_dis_rel_combined",TimeVar="Day",GroupVar ="ID",NewVar="all_coping_dis_rel_combined.lag",slideBy=-1)
data<-slide(data=data, Var="all_coping_crisisline",TimeVar="Day",GroupVar ="ID",NewVar="all_coping_crisisline.lag",slideBy=-1)
data<-slide(data=data, Var="coping_sum",TimeVar="Day",GroupVar ="ID",NewVar="coping_sum.lag",slideBy=-1)
data<-slide(data=data, Var="all_coping_any",TimeVar="Day",GroupVar ="ID",NewVar="all_coping_any.lag",slideBy=-1)
data<-slide(data=data, Var="SIUrge",TimeVar="Day",GroupVar ="ID",NewVar="SIUrge.lag",slideBy=-1)
data<-slide(data=data, Var="Self_Efficacy",TimeVar="Day",GroupVar ="ID",NewVar="Self_Efficacy.lag",slideBy=-1)
pdat_split <- split(data, data$ID)
new_names<-as.character(c(unique(data$ID)))
for(i in seq_along(pdat_split)){
new_names[i] <- paste0("pdat", i, sep="")
}
for (i in 1:length(pdat_split)) {
assign(new_names[i], pdat_split[[i]])
}
MImodel = pdat2[c(-1,-2)]
MImodel[,1] = as.factor(MImodel[,1])
MImodel[,2] = as.factor(MImodel[,2])
MImodel[,3] = as.factor(MImodel[,3])
MImodel[,4] = as.factor(MImodel[,4])
MImodel[,5] = as.factor(MImodel[,5])
MImodel[,6] = as.factor(MImodel[,6])
MImodel[,7] = as.factor(MImodel[,7])
MImodel[,10] = as.factor(MImodel[,10])
MImodel[,14] = as.factor(MImodel[,14])
MImodel[,15] = as.factor(MImodel[,15])
MImodel[,16] = as.factor(MImodel[,16])
MImodel[,17] = as.factor(MImodel[,17])
MImodel[,18] = as.factor(MImodel[,18])
MImodel[,19] = as.factor(MImodel[,19])
MImodel[,20] = as.factor(MImodel[,20])
MImodel[,22] = as.factor(MImodel[,22])
m = 5
imp = mice(MImodel,m = m)
k=24
qhat = matrix(NA, nrow = m,ncol = k)
u = array(NA,dim = c(k,k,m))
for (i in 1:m) {
data.impute = mice::complete(imp,action = i)}
all_coping_talkfamily.imp = as.factor(as.factor(data.impute[,1]))
all_coping_talkMH.imp = as.factor(as.factor(data.impute[,2]))
all_coping_copingthought.imp = as.factor(as.factor(data.impute[,3]))
all_coping_thinking.imp = as.factor(as.factor(data.impute[,4]))
all_coping_talkfriend.imp = as.factor(as.factor(data.impute[,5]))
all_coping_dis_rel_combined.imp = as.factor(as.factor(data.impute[,6]))
all_coping_crisisline.imp = as.factor(as.factor(data.impute[,7]))
coping_sum.imp = data.impute[,8]
coping_sum_3avg.imp = data.impute[,9]
all_coping_any.imp = as.factor(as.factor(data.impute[,10]))
SIUrge.imp = data.impute[,11]
Self_Efficacy.imp = data.impute[,12]
pdat2.imp<-data.frame(all_coping_talkfamily.imp = all_coping_talkfamily.imp,
all_coping_talkMH.imp =all_coping_talkMH.imp,
all_coping_copingthought.imp = all_coping_copingthought.imp,
all_coping_thinking.imp = all_coping_thinking.imp,
all_coping_talkfriend.imp = all_coping_talkfriend.imp,
all_coping_dis_rel_combined.imp = all_coping_dis_rel_combined.imp,
all_coping_any.imp = all_coping_any.imp,
SIUrge.imp = SIUrge.imp,
Self_Efficacy.imp = Self_Efficacy.imp,
coping_sum_3avg.imp = coping_sum_3avg.imp,
coping_sum.imp = coping_sum.imp)
library(mice)
MImodel = pdat2[c(-1,-2)]
MImodel[,1] = as.factor(MImodel[,1])
MImodel[,2] = as.factor(MImodel[,2])
MImodel[,3] = as.factor(MImodel[,3])
MImodel[,4] = as.factor(MImodel[,4])
MImodel[,5] = as.factor(MImodel[,5])
MImodel[,6] = as.factor(MImodel[,6])
MImodel[,7] = as.factor(MImodel[,7])
MImodel[,10] = as.factor(MImodel[,10])
MImodel[,14] = as.factor(MImodel[,14])
MImodel[,15] = as.factor(MImodel[,15])
MImodel[,16] = as.factor(MImodel[,16])
MImodel[,17] = as.factor(MImodel[,17])
MImodel[,18] = as.factor(MImodel[,18])
MImodel[,19] = as.factor(MImodel[,19])
MImodel[,20] = as.factor(MImodel[,20])
MImodel[,22] = as.factor(MImodel[,22])
m = 5
imp = mice(MImodel,m = m)
k=24
qhat = matrix(NA, nrow = m,ncol = k)
u = array(NA,dim = c(k,k,m))
for (i in 1:m) {
data.impute = mice::complete(imp,action = i)}
all_coping_talkfamily.imp = as.factor(as.factor(data.impute[,1]))
all_coping_talkMH.imp = as.factor(as.factor(data.impute[,2]))
all_coping_copingthought.imp = as.factor(as.factor(data.impute[,3]))
all_coping_thinking.imp = as.factor(as.factor(data.impute[,4]))
all_coping_talkfriend.imp = as.factor(as.factor(data.impute[,5]))
all_coping_dis_rel_combined.imp = as.factor(as.factor(data.impute[,6]))
all_coping_crisisline.imp = as.factor(as.factor(data.impute[,7]))
coping_sum.imp = data.impute[,8]
coping_sum_3avg.imp = data.impute[,9]
all_coping_any.imp = as.factor(as.factor(data.impute[,10]))
SIUrge.imp = data.impute[,11]
Self_Efficacy.imp = data.impute[,12]
pdat2.imp<-data.frame(all_coping_talkfamily.imp = all_coping_talkfamily.imp,
all_coping_talkMH.imp =all_coping_talkMH.imp,
all_coping_copingthought.imp = all_coping_copingthought.imp,
all_coping_thinking.imp = all_coping_thinking.imp,
all_coping_talkfriend.imp = all_coping_talkfriend.imp,
all_coping_dis_rel_combined.imp = all_coping_dis_rel_combined.imp,
all_coping_any.imp = all_coping_any.imp,
SIUrge.imp = SIUrge.imp,
Self_Efficacy.imp = Self_Efficacy.imp,
coping_sum_3avg.imp = coping_sum_3avg.imp,
coping_sum.imp = coping_sum.imp)
na.df <- data.frame(SIUrge.imp = NA)
vars<-c("SIUrge.imp")
pdat2.1<-pdat2.imp[vars]
pdat2.2<- do.call(rbind, apply(pdat2.1, 1, function(x) {rbind(x, na.df)}))
na.df <- data.frame(all_coping_talkfamily.imp = NA,
all_coping_talkMH.imp = NA,
all_coping_copingthought.imp = NA,
all_coping_thinking.imp = NA,
all_coping_talkfriend.imp = NA,
all_coping_dis_rel_combined.imp = NA,
all_coping_any.imp = NA,
Self_Efficacy.imp = NA,
coping_sum.imp = NA,
coping_sum_3avg.imp = NA)
vars<-c("all_coping_talkfamily.imp",
"all_coping_talkMH.imp",
"all_coping_copingthought.imp",
"all_coping_thinking.imp",
"all_coping_talkfriend.imp",
"all_coping_dis_rel_combined.imp",
"all_coping_any.imp",
"Self_Efficacy.imp",
"coping_sum.imp",
"coping_sum_3avg.imp")
pdat2.3<-pdat2.imp[vars]
pdat2.4 <- do.call(rbind, apply(pdat2.3, 1, function(x) {rbind(na.df, x)}))
pdat2.imp<-cbind(pdat2.4, pdat2.2)
pdat2.imp$Day<-c(1:56)
pdat2.imp<-data.frame(na.spline(pdat2.imp))
pdat2.imp<-round(pdat2.imp)
pdat2.imp$SIUrge.imp[pdat2.imp$SIUrge.imp>7]<-7
pdat2.imp$SIUrge.imp[pdat2.imp$SIUrge.imp<0]<-0
pdat2.imp$Self_Efficacy.imp[pdat2.imp$Self_Efficacy.imp>10]<-10
pdat2.imp$Self_Efficacy.imp[pdat2.imp$Self_Efficacy.imp<0]<-0
pdat2.imp$all_coping_talkMH.imp[pdat2.imp$all_coping_talkMH.imp==-2]<-0
library(splines)
pdat2.imp<-data.frame(na.spline(pdat2.imp))
pdat2.imp<-round(pdat2.imp)
pdat2.imp$SIUrge.imp[pdat2.imp$SIUrge.imp>7]<-7
pdat2.imp$SIUrge.imp[pdat2.imp$SIUrge.imp<0]<-0
pdat2.imp$Self_Efficacy.imp[pdat2.imp$Self_Efficacy.imp>10]<-10
pdat2.imp$Self_Efficacy.imp[pdat2.imp$Self_Efficacy.imp<0]<-0
pdat2.imp$all_coping_talkMH.imp[pdat2.imp$all_coping_talkMH.imp==-2]<-0
library(zoo)
pdat2.imp<-data.frame(na.spline(pdat2.imp))
pdat2.imp<-round(pdat2.imp)
pdat2.imp$SIUrge.imp[pdat2.imp$SIUrge.imp>7]<-7
pdat2.imp$SIUrge.imp[pdat2.imp$SIUrge.imp<0]<-0
pdat2.imp$Self_Efficacy.imp[pdat2.imp$Self_Efficacy.imp>10]<-10
pdat2.imp$Self_Efficacy.imp[pdat2.imp$Self_Efficacy.imp<0]<-0
pdat2.imp$all_coping_talkMH.imp[pdat2.imp$all_coping_talkMH.imp==-2]<-0
cols <- c("all_coping_talkfamily.imp",
"all_coping_talkMH.imp",
"all_coping_copingthought.imp",
"all_coping_thinking.imp",
"all_coping_talkfriend.imp",
"all_coping_dis_rel_combined.imp",
"all_coping_any.imp")
pdat2.imp <- pdat2.imp %>%
mutate_at(c(cols),list(~recode(., `-3`=0,`-2`=0,`-1`=0,`2`= 1,`3`=1)))
pdat2.imp[cols] <- lapply(pdat2.imp[cols], factor)
library(dplyr)
cols <- c("all_coping_talkfamily.imp",
"all_coping_talkMH.imp",
"all_coping_copingthought.imp",
"all_coping_thinking.imp",
"all_coping_talkfriend.imp",
"all_coping_dis_rel_combined.imp",
"all_coping_any.imp")
pdat2.imp <- pdat2.imp %>%
mutate_at(c(cols),list(~recode(., `-3`=0,`-2`=0,`-1`=0,`2`= 1,`3`=1)))
pdat2.imp[cols] <- lapply(pdat2.imp[cols], factor)
mod1<-lm(coping_sum_3avg.imp~Day, data=pdat2.imp)
Coping_sum_3avg_resid = as.numeric(rstandard(mod1))
mod2<-glm(all_coping_talkfamily.imp~Day, data=pdat2.imp, family=binomial(link="logit"))
Coping_TalkFamily_resid<-as.numeric(rstandard(mod2))
mod3<-glm(all_coping_talkMH.imp~Day, data=pdat2.imp, family=binomial(link="logit"))
Coping_TalkMH_resid<-as.numeric(rstandard(mod3))
mod4<-glm(all_coping_any.imp~Day, data=pdat2.imp, family=binomial(link="logit"))
Coping_any_resid<-as.numeric(rstandard(mod4))
mod5<-glm(all_coping_dis_rel_combined.imp~Day, data=pdat2.imp, family=binomial(link="logit"))
Coping_dis_rel_resid<-as.numeric(rstandard(mod5))
mod6<-glm(all_coping_copingthought.imp~Day, data=pdat2.imp, family=binomial(link="logit"))
Coping_Thought_resid<-as.numeric(rstandard(mod6))
mod7<-glm(all_coping_thinking.imp~Day, data=pdat2.imp, family=binomial(link="logit"))
Coping_Thinking_resid<-as.numeric(rstandard(mod7))
mod8<-lm(coping_sum.imp~Day, data=pdat2.imp, na.action=na.exclude)
Coping_sum_resid<-as.numeric(rstandard(mod8))
mod9<-glm(all_coping_talkfriend.imp~Day, data=pdat2.imp, family=binomial(link="logit"))
Coping_TalkFriend_resid<-as.numeric(rstandard(mod9))
mod10<-lm(SIUrge.imp~Day, data=pdat2.imp)
SIUrge_resid<-as.numeric(rstandard(mod10))
mod11<-lm(Self_Efficacy.imp~Day, data=pdat2.imp)
Efficacy_resid<-as.numeric(rstandard(mod11))
pdat2<-as.data.frame(cbind(Coping_TalkFamily_resid, Coping_TalkMH_resid, Coping_dis_rel_resid, Coping_Thought_resid, Coping_Thinking_resid, Coping_TalkFriend_resid, Coping_sum_resid, Efficacy_resid, SIUrge_resid, Coping_any_resid, Coping_sum_3avg_resid))
mod2<-glm(all_coping_talkfamily.imp~Day, data=pdat2.imp, family=binomial(link="logit"))
Coping_TalkFamily_resid<-as.numeric(rstandard(mod2))
pdat2.imp$all_coping_talkfamily.imp
all_coping_talkfamily.imp
mod2<-glm(all_coping_talkfamily.imp~Day, data=pdat2.imp, family=binomial(link="logit"))
Coping_TalkFamily_resid<-as.numeric(rstandard(mod2))
pdat2.imp<-data.frame(all_coping_talkfamily.imp = all_coping_talkfamily.imp,
all_coping_talkMH.imp =all_coping_talkMH.imp,
all_coping_copingthought.imp = all_coping_copingthought.imp,
all_coping_thinking.imp = all_coping_thinking.imp,
all_coping_talkfriend.imp = all_coping_talkfriend.imp,
all_coping_dis_rel_combined.imp = all_coping_dis_rel_combined.imp,
all_coping_any.imp = all_coping_any.imp,
SIUrge.imp = SIUrge.imp,
Self_Efficacy.imp = Self_Efficacy.imp,
coping_sum_3avg.imp = coping_sum_3avg.imp,
coping_sum.imp = coping_sum.imp)
pdat2.imp$all_coping_talkfamily.imp
na.df <- data.frame(SIUrge.imp = NA)
vars<-c("SIUrge.imp")
pdat2.1<-pdat2.imp[vars]
pdat2.2<- do.call(rbind, apply(pdat2.1, 1, function(x) {rbind(x, na.df)}))
na.df <- data.frame(all_coping_talkfamily.imp = NA,
all_coping_talkMH.imp = NA,
all_coping_copingthought.imp = NA,
all_coping_thinking.imp = NA,
all_coping_talkfriend.imp = NA,
all_coping_dis_rel_combined.imp = NA,
all_coping_any.imp = NA,
Self_Efficacy.imp = NA,
coping_sum.imp = NA,
coping_sum_3avg.imp = NA)
vars<-c("all_coping_talkfamily.imp",
"all_coping_talkMH.imp",
"all_coping_copingthought.imp",
"all_coping_thinking.imp",
"all_coping_talkfriend.imp",
"all_coping_dis_rel_combined.imp",
"all_coping_any.imp",
"Self_Efficacy.imp",
"coping_sum.imp",
"coping_sum_3avg.imp")
pdat2.3<-pdat2.imp[vars]
pdat2.4 <- do.call(rbind, apply(pdat2.3, 1, function(x) {rbind(na.df, x)}))
pdat2.imp<-cbind(pdat2.4, pdat2.2)
pdat2.imp$Day<-c(1:56)
pdat2.imp$all_coping_talkfamily.imp
pdat2.imp<-data.frame(na.spline(pdat2.imp))
pdat2.imp<-round(pdat2.imp)
pdat2.imp$SIUrge.imp[pdat2.imp$SIUrge.imp>7]<-7
pdat2.imp$SIUrge.imp[pdat2.imp$SIUrge.imp<0]<-0
pdat2.imp$Self_Efficacy.imp[pdat2.imp$Self_Efficacy.imp>10]<-10
pdat2.imp$Self_Efficacy.imp[pdat2.imp$Self_Efficacy.imp<0]<-0
pdat2.imp$all_coping_talkMH.imp[pdat2.imp$all_coping_talkMH.imp==-2]<-0
pdat2.imp$all_coping_talkfamily.imp
cols <- c("all_coping_talkfamily.imp",
"all_coping_talkMH.imp",
"all_coping_copingthought.imp",
"all_coping_thinking.imp",
"all_coping_talkfriend.imp",
"all_coping_dis_rel_combined.imp",
"all_coping_any.imp")
pdat2.imp <- pdat2.imp %>%
mutate_at(c(cols),list(~recode(., `-3`=0,`-2`=0,`-1`=0,`2`= 1,`3`=1)))
pdat2.imp[cols] <- lapply(pdat2.imp[cols], factor)
pdat2.imp$all_coping_talkfamily.imp
mod1<-lm(coping_sum_3avg.imp~Day, data=pdat2.imp)
Coping_sum_3avg_resid = as.numeric(rstandard(mod1))
mod2<-glm(all_coping_talkfamily.imp~Day, data=pdat2.imp, family=binomial(link="logit"))
Coping_TalkFamily_resid<-as.numeric(rstandard(mod2))
mod3<-glm(all_coping_talkMH.imp~Day, data=pdat2.imp, family=binomial(link="logit"))
Coping_TalkMH_resid<-as.numeric(rstandard(mod3))
mod4<-glm(all_coping_any.imp~Day, data=pdat2.imp, family=binomial(link="logit"))
Coping_any_resid<-as.numeric(rstandard(mod4))
mod5<-glm(all_coping_dis_rel_combined.imp~Day, data=pdat2.imp, family=binomial(link="logit"))
Coping_dis_rel_resid<-as.numeric(rstandard(mod5))
mod6<-glm(all_coping_copingthought.imp~Day, data=pdat2.imp, family=binomial(link="logit"))
Coping_Thought_resid<-as.numeric(rstandard(mod6))
mod7<-glm(all_coping_thinking.imp~Day, data=pdat2.imp, family=binomial(link="logit"))
Coping_Thinking_resid<-as.numeric(rstandard(mod7))
mod8<-lm(coping_sum.imp~Day, data=pdat2.imp, na.action=na.exclude)
Coping_sum_resid<-as.numeric(rstandard(mod8))
mod9<-glm(all_coping_talkfriend.imp~Day, data=pdat2.imp, family=binomial(link="logit"))
Coping_TalkFriend_resid<-as.numeric(rstandard(mod9))
mod10<-lm(SIUrge.imp~Day, data=pdat2.imp)
SIUrge_resid<-as.numeric(rstandard(mod10))
mod11<-lm(Self_Efficacy.imp~Day, data=pdat2.imp)
Efficacy_resid<-as.numeric(rstandard(mod11))
pdat2<-as.data.frame(cbind(Coping_TalkFamily_resid, Coping_TalkMH_resid, Coping_dis_rel_resid, Coping_Thought_resid, Coping_Thinking_resid, Coping_TalkFriend_resid, Coping_sum_resid, Efficacy_resid, SIUrge_resid, Coping_any_resid, Coping_sum_3avg_resid))
attach(pdat2)
Coping_TalkFamily_resid=diff(Coping_TalkFamily_resid)
Coping_TalkMH_resid = diff(Coping_TalkMH_resid)
Coping_dis_rel_resid = diff(Coping_dis_rel_resid)
Coping_Thought_resid = diff(Coping_Thought_resid)
Coping_Thinking_resid = diff(Coping_Thinking_resid)
Coping_TalkFriend_resid = diff(Coping_TalkFriend_resid)
Coping_any_resid = diff(Coping_any_resid)
Coping_sum_resid = diff(Coping_sum_resid)
SIUrge_resid = diff(SIUrge_resid)
Efficacy_resid=diff(Efficacy_resid)
Coping_sum_3avg_resid = diff(Coping_sum_3avg_resid)
pdat2<-as.data.frame(cbind(Coping_TalkFamily_resid, Coping_TalkMH_resid, Coping_dis_rel_resid, Coping_Thought_resid, Coping_Thinking_resid, Coping_TalkFriend_resid, Coping_sum_resid, Efficacy_resid, SIUrge_resid, Coping_sum_3avg_resid))
pdat2$day<-c(1:55)
pdat2.2<-c("SIUrge_resid", "Coping_TalkFamily_resid")
pdat2.2<-pdat2[pdat2.2]
coping_var=VAR(pdat2.2,p=1, type="const")
causality(coping_var, cause="SIUrge_resid")$Granger
summary(coping_var)
library(vars)
pdat2.2<-c("SIUrge_resid", "Coping_TalkFamily_resid")
pdat2.2<-pdat2[pdat2.2]
coping_var=VAR(pdat2.2,p=1, type="const")
causality(coping_var, cause="SIUrge_resid")$Granger
summary(coping_var)
sout<-summary(coping_var)
nobs<-56
M <- sout$covres
L = chol(M)
nvars = dim(L)[1]
t(L)
t(L) %*% L
r = t(L) %*% matrix(rnorm(nvars*nobs), nrow=nvars, ncol=nobs)
r = t(r)
rdata = as.data.frame(r)
names(rdata) = c('Coping_TalkFamily', 'SIUrge')
write.csv(rdata, "/Users/kevinkuehn/Documents/Writing/YDSP collab/YDSP/Analysis/pdat2.var.csv")
rdata$Coping_TalkFamily
rdata$SIUrge
coping_var
sout$covres
nobs<-56
M <- sout$covres
L = chol(M)
nvars = dim(L)[1]
t(L)
t(L) %*% L
r = t(L) %*% matrix(rnorm(nvars*nobs), nrow=nvars, ncol=nobs)
r = t(r)
rdata = as.data.frame(r)
names(rdata) = c('SIUrge', 'Coping_TalkFamily')
write.csv(rdata, "/Users/kevinkuehn/Documents/Writing/YDSP collab/YDSP/Analysis/pdat2.var.csv")
rdata$SIUrge
write.csv(rdata, "/Users/kevinkuehn/Documents/Writing/YDSP collab/YDSP/pdat2.var.csv")
pdat2<-read.csv("~/pdat2.var.csv")
## Run model
pdat2.2<-c("SIUrge", "Coping_TalkFamily")
pdat2.2<-pdat2[pdat2.2]
coping_var=VAR(pdat2.2,p=1, type="const")
causality(coping_var, cause="SIUrge")$Granger
summary(coping_var)
getwd()
pdat2<-read.csv("~/pdat2.var.csv")
pdat2<-read.csv("~/pdat2.var.csv")
getwd()
pdat2<-read.csv("~/pdat2.var.csv")
rm(pdat2)
pdat2<-read.csv("~/pdat2.var.csv")
pdat2.1
pdat2
pdat2<-read.csv("~pdat2.var.csv")
pdat2<-read.csv("~/pdat2.var.csv")
pdat2<-read.csv("~/Writing/YDSP collab/YDSP/pdat2.var.csv")
pdat2<-read.csv("~/Documents/Writing/YDSP collab/YDSP/pdat2.var.csv")
pdat2.2<-c("SIUrge", "Coping_TalkFamily")
pdat2.2<-pdat2[pdat2.2]
coping_var=VAR(pdat2.2,p=1, type="const")
causality(coping_var, cause="SIUrge")$Granger
summary(coping_var)
data3<-read.csv("~/Documents/Writing/YDSP collab/Age at baseline.csv")
merge(data2, data3, by="ID")
data4<-merge(data2, data3, by="ID")
data4$age
subset(data4, ID=c(202,204,205,209,212,214,223,225,229,230,236))
data5<-subset(data4, ID=c(202,204,205,209,212,214,223,225,229,230,236))
data5<-subset(data4, ID==c(202,204,205,209,212,214,223,225,229,230,236))
data5<-subset(data4, data4$ID=c(202,204,205,209,212,214,223,225,229,230,236))
data5<-subset(data4, ID=c(202,204,205,209,212,214,223,225,229,230,236))
?subset
ID=c(202,204,205,209,212,214,223,225,229,230,236)
data5<-data4[which(data4$ID=ID)]
data5<-data4[which(data4$ID==ID)]
ID
data5<-data4[which(data4$ID==c(ID))]
data5<-data4[which(data4$ID=='ID']
data5<-data4[which(data4$ID=='ID')]
data5<-data4[which(data4$ID==c(202,204,205,209,212,214,223,225,229,230,236)]
data5<-data4[which(data4$ID==c(202,204,205,209,212,214,223,225,229,230,236))]
data5<-data4[which(data4$ID==c('202,204,205,209,212,214,223,225,229,230,236'))]
data5<-subset(data4, ID=202|204|205|209|209|212|214|223|225|229|230|236)
data5<-subset(data4, ID=202|ID=204|ID=205|ID=209|ID=209|ID=212|ID=214|ID=223|ID=225|ID=229|ID=230|ID=236)
data4$ID
data5<-data4[ID=c(202,204,205,209,212,214,223,225,229,230,236)]
data5<-data4[data4$ID=c(202,204,205,209,212,214,223,225,229,230,236)]
data5<-data4[data4$ID==c(202,204,205,209,212,214,223,225,229,230,236)]
data5<-data4[2,4,5, 8, 11,13, 21, 23,27,28,34]
data5<-data4[2,4,5,8,11,13, 21, 23,27,28,34,]
data5<-data4[2,4:5,8,11,13,21,23,27:28,34,]
data5<-data4[which(data4$ID==ID)]
ID
data5<-data4[which(data4$ID==c(ID)]
data5<-data4[which(data4$ID==c(ID))]
data5<-data4[which(data4$ID=c(202,204)]
data5<-data4[which(data4$ID==c(202,204)]
data5<-data4[which(data4$ID==c(202,204))]
data5$B_attemptyesno
data5<-data4[which(data4$ID==c(202,204,205))]
data5<-data4[which(data4$ID==c(202,204,205,))]
data5<-subset(data4, ID=ID)
data5$ID
data5<-subset(data4, ID=c(201))
data5<-subset(data4, ID=201)
data5<-subset(data4, ID==201)
data5$ID
data5<-subset(data4, ID==c(ID)
)
data5<-subset(data4, ID==c(ID))\
data5<-subset(data4, ID==c(202,204)
)
data5<-subset(data4, ID==c(202,204))
data5$ID
data5<-subset(data4, ID==c(202|204))
data5<-subset(data4, ID==202|204))
data5<-subset(data4, ID==202|204)
data5$ID
data5<-subset(data4, ID==202|ID==204)
ID
data5<-subset(data4, ID==202|ID==204|ID==205|ID==209|ID==212|ID==214|ID==223|ID==225|ID==229|ID==230|ID==236)
data5$age
mean(data5$age)
sd(data5$age)
table(data5$B_attemptyesno)
table(data5$B_multipleattempt)
6/11
table(data5$Race_dichotomous)
table(data5$Sex)
9/11
mean(data3$age)
sd(data3$age)
data4$group<-NA
data4$group[data4$ID==ID==202|ID==204|ID==205|ID==209|ID==212|ID==214|ID==223|ID==225|ID==229|ID==230|ID==236)]<-1
data4$group[data4$IDID==202|ID==204|ID==205|ID==209|ID==212|ID==214|ID==223|ID==225|ID==229|ID==230|ID==236)]<-1
data4$group[data4$ID==202|ID==204|ID==205|ID==209|ID==212|ID==214|ID==223|ID==225|ID==229|ID==230|ID==236]<-1
data4$group[data4$ID==202|data4$ID==204|data4$ID==205|data4$ID==209|data4$ID==212|data4$ID==214|data4$ID==223|data4$ID==225|data4$ID==229|data4$ID==230|data$ID==236]<-1
data4$group[data4$ID==202]<-1
data4$group
data4$ID
data4$group<-0
data4$gorup
data4$group
data4$group[data4$ID==202]<-1
data4$group
data4$group[data4$ID==204|205]<-1
data$group
data4$group
data4$group<-0
data4$group[data4$ID==202]<-1
data4$group[data4$ID==204]<-1
data4$group[data4$ID==205]<-1
data4$group[data4$ID==209]<-1
data4$group[data4$ID==212]<-1
data4$group[data4$ID==214]<-1
data4$group[data4$ID==223]<-1
data4$group[data4$ID==225]<-1
data4$group[data4$ID==229]<-1
data4$group[data4$ID==239]<-1
data4$group[data4$ID==230]<-1
data4$group[data4$ID==236]<-1
table(data4$group)
chisq.test(data4$group)
chisq.test(data4$group. data4$Sex)
chisq.test(data4$group, data4$Sex)
table(data4$group, data4$Sex)
chisq.test(data4$group!data4$Sex)
chisq.test(data4$group~data4$Sex)
str(data4$#Sex)
str(data4$Sex)
data4$group<-as.factor(data4$group)
chisq.test(data4$group~data4$Sex)
chisq.test(data4$group, data4$Sex)
tbl<-table(data4$group, data4$Sex)
chisq.test(tbl)
tbl<-table(data4$group, data4$B_attemptyesno)
chisq.test(tbl)
tbl<-table(data4$group, data4$B_multipleattempt)
chisq.test(tbl)
chisq.test(tbl, simulate.p.value = TRUE)
tbl<-table(data4$group, data4$B_attemptyesno)
chisq.test(tbl, simulate.p.value = TRUE)
tbl<-table(data4$group, data4$Sex)
chisq.test(tbl, simulate.p.value = TRUE)
t.test(data4$age~data4$group)
3+3+2+2+3+3+5+8+2
31/99
setwd("~/Documents/Writing/YDSP collab/YDSP")
remove(list=ls())