-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreanalyses.R
1606 lines (1440 loc) · 59.7 KB
/
reanalyses.R
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
library("Hmisc") # for spss.get()
# library("OpenMx") # for SEM model fitting
# library("umx") # for umxExpCov and other cool stuff
library("semTools") # for reliability()
library("semPlot") # for path diagrams
library("lavaan") # for SEMs
# run this to make semPlot play nice with OpenMx
source("/Users/Jake/Desktop/Google Drive/SEM_class/rewriteSemplotFunctions.R")
# read in panel dataset ---------------------------------------------------
path <- "/Users/Jake/Desktop/Dropbox/Dropbox/SIC/"
panel <- read.csv(paste0(path,"anes2008_2009panel/anes2008_2009panel_rawdata.txt"))
# examine vars ------------------------------------------------------------
table(panel$derw10a1) # for whom will R vote?
# 1 = mccain, 2 = obama, 3 = someone else
# -6 -5 -1 1 2 3
# 80 3 38 518 536 58
table(panel$der16) # For whom did R vote for president in 2008 election
# 13 = mccain, 18 = obama, 3 = someone else
# -6 -2 -1 1 2 3
# 27 28 113 497 542 26
# good agreement between prospective and retrospective
with(panel, table(der16, derw10a1))
# derw10a1
# der16 -6 -5 -1 1 2 3
# -6 27 0 0 0 0 0
# -2 6 0 0 12 6 4
# -1 4 0 0 57 43 9
# 1 22 1 19 425 13 17
# 2 21 2 18 16 470 15
# 3 0 0 1 8 4 13
# grab names of AMP response variables
n <- nchar(names(panel))
name <- names(panel)[substr(names(panel), n-6, n) == "_choice"]
# verify that respondents complete only 1 of the AMPs, w9 or w10
apply(panel[,name], 1, function(x) sum(!x %in% 1:2))
### explicit attitude measures
# wave 2 warm/cold
table(panel$w2d11) # do you feel warm, cold, or neither toward Blacks?
# 1 = warm, 2 = cold, 3 = neither
table(panel$w2d12) # How warm does R feel to blacks
# 1 = extremely warm, 2 = moderately warm, 3 = a little warm
table(panel$w2d13) # How cold does R feel to blacks
# 1 = extremely cold, 2 = moderately cold, 3 = a little cold
# wave 10 warm/cold
table(panel$w10d11) # do you feel warm, cold, or neither toward Blacks?
table(panel$w10d11_warm) # How warm does R feel to blacks
table(panel$w10d11_cold) # How cold does R feel to blacks
# wave 20 warm/cold
table(panel$w20d11) # do you feel warm, cold, or neither toward Blacks?
table(panel$w20d12) # How warm does R feel to blacks
table(panel$w20d13) # How cold does R feel to blacks
# sympathy for blacks
# 1 = always, 2 = most time, 3 = half time, 4 = once in a while, 5 = never
table(panel$w9zb24)
table(panel$w11zb24)
table(panel$w17x24)
# admiration for blacks
# 1 = always, 2 = most time, 3 = half time, 4 = once in a while, 5 = never
table(panel$w9zb25)
table(panel$w11zb25)
table(panel$w17x25)
# Do blacks have too much or too little pol influence
# 1 = too much, 2 = just about right, 3 = too little
table(panel$w9zb23) # factor, not numeric!
table(panel$w11zb23)
table(panel$w17x26)
### control variables:
# party identification
table(panel$der08w1)
table(panel$der08w9)
table(panel$der08w10)
table(panel$der08w11)
table(panel$der08w17)
table(panel$der08w19)
# liberalism/conservatism
table(panel$der09w1)
table(panel$der09w2)
table(panel$der09w6)
table(panel$der09w10)
table(panel$der09w11)
# gender
table(panel$rgenderr)
# age
hist(panel$cpyourself_age, col="gray")
# race
table(panel$der04)
# education
table(panel$der05)
# income
table(panel$der06)
# build dataset -----------------------------------------------------------
# grab names of AMP response variables
n <- nchar(names(panel))
name <- names(panel)[substr(names(panel), n-6, n) == "_choice"]
# construct dataset with only variables relevant to AMP analysis
clean <- panel[,c(
"caseid","der16", # president vote
"w2d11","w2d12","w2d13", # wave 2 warm/cold
"w10d11","w10d11_warm","w10d11_cold", # wave 10 warm/cold
"w20d11","w20d12","w20d13", # wave 20 warm/cold
"w9zb24","w11zb24","w17x24", # sympathy
"w9zb25","w11zb25","w17x25", # admiration
"w9zb23","w11zb23","w17x26", # influence
# party identification
"der08w1","der08w9","der08w10","der08w11","der08w17","der08w19",
# liberal/conservative
"der09w1","der09w2","der09w6","der09w10","der09w11",
"rgenderr","cpyourself_age","der04","der05","der06")] # demographics
names(clean) <- c(
"caseid","vote",
"warmCold_w2","howWarm_w2","howCold_w2",
"warmCold_w10","howWarm_w10","howCold_w10",
"warmCold_w20","howWarm_w20","howCold_w20",
"sympathy_w9","sympathy_w11","sympathy_w17",
"admiration_w9","admiration_w11","admiration_w17",
"influence_w9","influence_w11","influence_w17",
"party_w1","party_w9","party_w10","party_w11","party_w17","party_w19",
"libCon_w1","libCon_w2","libCon_w6","libCon_w10","libCon_w11",
"gender","age","race","education","income")
# not sure why this one is a factor, but cast to numeric
clean$influence_w9 <- as.numeric(as.character(clean$influence_w9))
# replace missing codes (negative values) with NA
clean[clean < 0] <- NA
# and remove 2.5% of Rs who voted for other than mccain/obama
clean$vote[clean$vote==3] <- NA
# grab the AMP responses
choices <- panel[,name]
choices <- data.frame(lapply(choices, function(x) as.numeric(as.character(x))))
choices[choices < 1] <- NA
# compute new variables
key <- c("1"=1,"2"=-1,"3"=0)
clean <- within(clean, {
vote <- vote - 1
black <- rowMeans(choices[c(1:24, 49:72)]==1, na.rm=TRUE)
white <- rowMeans(choices[c(25:48, 73:96)]==1, na.rm=TRUE)
diff <- black - white
sympathy <- rowMeans(cbind(sympathy_w9, sympathy_w11, sympathy_w17), na.rm=TRUE)
sympathy <- 6 - sympathy
admiration <- rowMeans(cbind(admiration_w9, admiration_w11, admiration_w17), na.rm=TRUE)
admiration <- 6 - admiration
influence <- rowMeans(cbind(influence_w9, influence_w11, influence_w17), na.rm=TRUE)
warm_w2 <- key[as.character(warmCold_w2)]
warm_w2 <- warm_w2*cbind(howCold_w2,0,howWarm_w2)[cbind(seq(nrow(clean)),warm_w2+2)]
warm_w10 <- key[as.character(warmCold_w10)]
warm_w10 <- warm_w10*cbind(howCold_w10,0,howWarm_w10)[cbind(seq(nrow(clean)),warm_w10+2)]
warm_w20 <- key[as.character(warmCold_w20)]
warm_w20 <- warm_w20*cbind(howCold_w20,0,howWarm_w20)[cbind(seq(nrow(clean)),warm_w20+2)]
warm <- rowMeans(cbind(warm_w2, warm_w10, warm_w20), na.rm=TRUE)
explicit <- rowMeans(scale(cbind(warm,sympathy,admiration,influence)),
na.rm=TRUE)
party <- rowMeans(cbind(party_w1,party_w9,party_w10,party_w11,party_w17,party_w19), na.rm=TRUE)
libCon <- rowMeans(cbind(libCon_w1,libCon_w2,libCon_w6,libCon_w10,libCon_w11), na.rm=TRUE)
})
# merge in pre-computed D-scores
iat <- spss.get(paste0(path,"IATscores/ANES0809Panel_IAT.por"))
names(iat)[match("CASEID", names(iat))] <- "caseid"
clean <- merge(clean, iat[,c("caseid","IAT.D")], all.x=TRUE)
# mean center most predictors
index <- match(c("vote","diff","race","IAT.D"), names(clean))
clean[,-index] <- scale(clean[,-index], scale=FALSE)
# dummy code race
dummy <- contr.treatment(4)
colnames(dummy) <- c("race1","race2","race3")
clean <- data.frame(clean, dummy[clean$race,])
# check number of missing obs by variable
apply(clean, 2, function(x) sum(is.na(x)))
# check simple correlations
round(cor(clean[,c("diff","IAT.D","explicit","party","libCon","gender",
"age","education","income")], use="pairwise.complete.obs"), 2)
# AMP regression models ---------------------------------------------------
# note two simplifications compared to Payne et al.:
# single implicit predictor (% diff) rather than black % + white %
# single vote outcome (mccain vs. obama) rather than 2 vote dummies
# model using only AMP scores as predictors
summary(glm(vote ~ diff, family=binomial, data=clean))
# control for explicit measures
summary(glm(vote ~ diff + explicit, family=binomial, data=clean))
# w/ demographics, without controlling for explicit
summary(glm(vote ~ diff +
party + libCon + gender + age + race + education + income,
family=binomial, data=clean))
# w/ demographics, controlling for explicit
summary(glm(vote ~ diff + explicit +
party + libCon + gender + age + race + education + income,
family=binomial, data=clean))
# AMP SEM -----------------------------------------------------------------
ind <- c("warm","sympathy","admiration","influence")
demo <- c("party","libCon","gender","age","education","income",
"race1","race2","race3")
# the model has a hard time with missing data!
include <- !is.na(clean$diff) & !is.na(clean$vote)
ampdat <- within(na.omit(clean[include, c("vote","diff",ind,demo)]), {
# scale variables to have similar SDs
diff <- diff*3
influence <- influence*2
party <- party/2
libCon <- libCon/2
age <- age/10
income <- income/4
race1 <- race1*3
race2 <- race2*3
race3 <- race3*3
vote <- mxFactor(vote, levels=0:1)
})
# subsample
ampdat <- ampdat[sample(seq(nrow(ampdat)), 400),]
# check SDs
sqrt(diag(var(ampdat, na.rm=TRUE)))
# check number of missing obs by variable (should be none)
apply(ampdat, 2, function(x) sum(is.na(x)))
# test out with reliability = .4 (no demographic controls)
alpha <- .55
modA <- mxTryHard(mxOption(model=mxModel(name="modA",
type="RAM",
manifestVars=c("vote","diff",ind),
latentVars=c("implicit","explicit"),
# indicators
mxPath(from="explicit", to=ind),
mxPath(from="implicit", to="diff", free=FALSE, value=1),
# residual variances
mxPath(from=ind, arrows=2),
mxPath(from="diff", arrows=2, free=FALSE,
value=(1-alpha)*var(ampdat$diff, na.rm=TRUE)),
mxPath(from="vote", arrows=2, free=FALSE, values=1),
# fix scale of latents
mxPath(from=c("implicit","explicit"), arrows=2, free=FALSE, value=1),
# allow latents to covary
mxPath(from="implicit", to="explicit", arrows=2),
# regress Y on Ts
mxPath(from=c("implicit","explicit"), to="vote"),
# means/intercepts and thresholds
mxPath(from="one", to=c("implicit","explicit","vote"), free=FALSE, values=0),
mxPath(from="one", to=c("diff",ind)),
mxThreshold(vars="vote", nThresh=1, values=0),
# data source
mxData(observed=ampdat, type="raw")),
# enable parallel (arguments to mxOption)
key="Number of Threads", value=(omxDetectCores()-1)),
# number of times to try new starting values (argument to mxTryHard)
extraTries=99)
summary(modA)
umxSummary(modA)
modC <- mxTryHard(mxOption(model=mxModel(name="modC",
type="RAM",
manifestVars=c("vote","diff",ind),
latentVars=c("attitude"),
# indicators
mxPath(from="attitude", to=ind),
mxPath(from="attitude", to="diff", free=FALSE, value=1),
# residual variances
mxPath(from=ind, arrows=2),
mxPath(from="diff", arrows=2, free=FALSE,
value=(1-alpha)*var(ampdat$diff, na.rm=TRUE)),
mxPath(from="vote", arrows=2, free=FALSE, values=1),
# fix scale of latent
mxPath(from=c("attitude"), arrows=2, free=FALSE, value=1),
# regress Y on latent
mxPath(from=c("attitude"), to="vote"),
# means/intercepts and thresholds
mxPath(from="one", to=c("attitude","vote"), free=FALSE, values=0),
mxPath(from="one", to=c("diff",ind)),
mxThreshold(vars="vote", nThresh=1, values=0),
# data source
mxData(observed=ampdat, type="raw")),
# enable parallel (arguments to mxOption)
key="Number of Threads", value=(omxDetectCores()-1)),
# number of times to try new starting values (argument to mxTryHard)
extraTries = 99)
summary(modC)
umxCompare(modA,modC)
loads <- modA@matrices$A$values[ind,"explicit"]
errors <- diag(modA@matrices$S$values[ind,ind])
round(sum(loads)^2/(sum(loads)^2 + sum(errors)), 2)
# reliability = 0.86
# define function to get chi-square differences as f(alpha)
getX2 <- function(alpha){
modA <- mxTryHard(mxOption(model=mxModel(name="modA",
type="RAM",
manifestVars=c("vote","diff",ind),
latentVars=c("implicit","explicit"),
mxPath(from="explicit", to=ind),
mxPath(from="implicit", to="diff", free=FALSE, value=1),
mxPath(from=ind, arrows=2),
mxPath(from="diff", arrows=2, free=FALSE,
value=(1-alpha)*var(ampdat$diff, na.rm=TRUE)),
mxPath(from="vote", arrows=2, free=FALSE, values=1),
mxPath(from=c("implicit","explicit"), arrows=2, free=FALSE, value=1),
mxPath(from="implicit", to="explicit", arrows=2),
mxPath(from=c("implicit","explicit"), to="vote"),
mxPath(from="one", to=c("implicit","explicit","vote"), free=FALSE, values=0),
mxPath(from="one", to=c("diff",ind)),
mxThreshold(vars="vote", nThresh=1, values=0),
mxData(observed=ampdat, type="raw")),
key="Number of Threads", value=(omxDetectCores()-1)),
extraTries=99)
modC <- mxTryHard(mxOption(model=mxModel(name="modC",
type="RAM",
manifestVars=c("vote","diff",ind),
latentVars=c("attitude"),
mxPath(from="attitude", to=ind),
mxPath(from="attitude", to="diff", free=FALSE, value=1),
mxPath(from=ind, arrows=2),
mxPath(from="diff", arrows=2, free=FALSE,
value=(1-alpha)*var(ampdat$diff, na.rm=TRUE)),
mxPath(from="vote", arrows=2, free=FALSE, values=1),
mxPath(from=c("attitude"), arrows=2, free=FALSE, value=1),
mxPath(from=c("attitude"), to="vote"),
mxPath(from="one", to=c("attitude","vote"), free=FALSE, values=0),
mxPath(from="one", to=c("diff",ind)),
mxThreshold(vars="vote", nThresh=1, values=0),
mxData(observed=ampdat, type="raw")),
key="Number of Threads", value=(omxDetectCores()-1)),
extraTries = 99)
umxCompare(modA,modC)[2,3]
}
system.time({
X2diffs <- sapply(seq(from=.01, to=.99, length.out=11), getX2)
}) # elapsed = 1.5 minutes
png("/Users/Jake/Desktop/AMP_LR_as_fAlpha_subsample.png",
units="in", height=8, width=8, res=200, pointsize=25)
plot(y=log(X2diffs), x=seq(from=.01, to=.99, length.out=11),
type="o", pch=19, yaxt="n", lwd=2, ylim=c(0,max(log(X2diffs))),
ylab=expression(paste(chi^2," difference (log scale)")),
main="AMP", mgp=c(2.5,1,0),
xlab="Assumed reliability of implicit measure")
axis(side=2, at=log(c(1,7,50,400,3000)), labels=c(1,7,50,400,3000))
abline(h=c(0, log(qchisq(.975, df=2))), lty=2)
dev.off()
### this time with demographic controls
alpha <- .4
modA_demo <- mxTryHard(mxOption(model=mxModel(name="modA_demo",
type="RAM",
manifestVars=c("vote","diff",ind,demo),
latentVars=c("implicit","explicit"),
# indicators
mxPath(from="explicit", to=ind),
mxPath(from="implicit", to="diff", free=FALSE, value=1),
# residual variances
mxPath(from=ind, arrows=2),
mxPath(from="diff", arrows=2, free=FALSE,
value=(1-alpha)*var(ampdat$diff, na.rm=TRUE)),
mxPath(from="vote", arrows=2, free=FALSE, values=1),
# fix scale of latents
mxPath(from=c("implicit","explicit"), arrows=2, free=FALSE, value=1),
# allow latents to covary
#mxPath(from="implicit", to="explicit", arrows=2),
# regress Y on Ts
mxPath(from=c("implicit","explicit"), to="vote"),
# means/intercepts and thresholds
mxPath(from="one", to=c("implicit","explicit","vote"), free=FALSE, values=0),
mxPath(from="one", to=c("diff",ind)),
mxThreshold(vars="vote", nThresh=1, values=0),
# demographic controls
mxPath(from=demo, to="vote"),
mxPath(from=demo, arrows=2),
mxPath(from=c(demo,"implicit","explicit"), arrows=2, connect="unique.bivariate"),
# data source
mxData(observed=ampdat, type="raw")),
# enable parallel (arguments to mxOption)
key="Number of Threads", value=(omxDetectCores()-1)),
# number of times to try new starting values (argument to mxTryHard)
extraTries=99)
# define function to get chi-square differences as f(alpha)
getX2_demo <- function(alpha){
modA <- mxTryHard(mxOption(model=mxModel(name="modA",
type="RAM",
manifestVars=c("vote","diff",ind,demo),
latentVars=c("implicit","explicit"),
mxPath(from="explicit", to=ind),
mxPath(from="implicit", to="diff", free=FALSE, value=1),
mxPath(from=ind, arrows=2),
mxPath(from="diff", arrows=2, free=FALSE,
value=(1-alpha)*var(ampdat$diff, na.rm=TRUE)),
mxPath(from="vote", arrows=2, free=FALSE, values=1),
mxPath(from=c("implicit","explicit"), arrows=2, free=FALSE, value=1),
mxPath(from="implicit", to="explicit", arrows=2),
mxPath(from=c("implicit","explicit"), to="vote"),
mxPath(from="one", to=c("implicit","explicit","vote"), free=FALSE, values=0),
mxPath(from="one", to=c("diff",ind)),
mxThreshold(vars="vote", nThresh=1, values=0),
mxPath(from=demo, to="vote"),
mxPath(from=demo, arrows=2),
mxData(observed=ampdat, type="raw")),
key="Number of Threads", value=(omxDetectCores()-1)),
extraTries=99)
modC <- mxTryHard(mxOption(model=mxModel(name="modC",
type="RAM",
manifestVars=c("vote","diff",ind,demo),
latentVars=c("attitude"),
mxPath(from="attitude", to=ind),
mxPath(from="attitude", to="diff", free=FALSE, value=1),
mxPath(from=ind, arrows=2),
mxPath(from="diff", arrows=2, free=FALSE,
value=(1-alpha)*var(ampdat$diff, na.rm=TRUE)),
mxPath(from="vote", arrows=2, free=FALSE, values=1),
mxPath(from=c("attitude"), arrows=2, free=FALSE, value=1),
mxPath(from=c("attitude"), to="vote"),
mxPath(from="one", to=c("attitude","vote"), free=FALSE, values=0),
mxPath(from="one", to=c("diff",ind)),
mxThreshold(vars="vote", nThresh=1, values=0),
mxPath(from=demo, to="vote"),
mxPath(from=demo, arrows=2),
mxData(observed=ampdat, type="raw")),
key="Number of Threads", value=(omxDetectCores()-1)),
extraTries = 99)
umxCompare(modA,modC)[2,3]
}
system.time({
X2diffs_demo <- sapply(seq(from=.01, to=.99, length.out=11), getX2_demo)
}) # elapsed = 1.5 minutes
# path diagrams -----------------------------------------------------------
# for plotting purposes, must change ordinal outcome to continuous
modA2 <- mxTryHard(mxOption(model=mxModel(name="modA2",
type="RAM",
manifestVars=c("vote2","diff",ind),
latentVars=c("implicit","explicit"),
# indicators
mxPath(from="explicit", to=ind),
mxPath(from="implicit", to="diff", free=FALSE, value=1),
# residual variances
mxPath(from=ind, arrows=2),
mxPath(from="diff", arrows=2, free=FALSE,
value=(1-alpha)*var(ampdat$diff, na.rm=TRUE)),
mxPath(from="vote2", arrows=2),
# fix scale of latents
mxPath(from=c("implicit","explicit"), arrows=2, free=FALSE, value=1),
# allow latents to covary
mxPath(from="implicit", to="explicit", arrows=2),
# regress Y on Ts
mxPath(from=c("implicit","explicit"), to="vote2"),
# means/intercepts and thresholds
mxPath(from="one", to=c("implicit","explicit"), free=FALSE, values=0),
mxPath(from="one", to=c("diff",ind,"vote2")),
# data source
mxData(observed=ampdat[,-match("vote",names(ampdat))], type="raw")),
# enable parallel (arguments to mxOption)
key="Number of Threads", value=(omxDetectCores()-1)))
modC2 <- mxTryHard(mxOption(model=mxModel(name="modC2",
type="RAM",
manifestVars=c("vote2","diff",ind),
latentVars=c("attitude"),
# indicators
mxPath(from="attitude", to=ind),
mxPath(from="attitude", to="diff", free=FALSE, value=1),
# residual variances
mxPath(from=ind, arrows=2),
mxPath(from="diff", arrows=2, free=FALSE,
value=(1-alpha)*var(ampdat$diff, na.rm=TRUE)),
mxPath(from="vote2", arrows=2),
# fix scale of latent
mxPath(from=c("attitude"), arrows=2, free=FALSE, value=1),
# regress Y on latent
mxPath(from=c("attitude"), to="vote2"),
# means/intercepts and thresholds
mxPath(from="one", to=c("attitude"), free=FALSE, values=0),
mxPath(from="one", to=c("diff",ind,"vote2")),
# data source
mxData(observed=ampdat[,-match("vote",names(ampdat))], type="raw")),
# enable parallel (arguments to mxOption)
key="Number of Threads", value=(omxDetectCores()-1)))
# plot path diagram
semPaths(semPlotModel_MxRAMModel(modA2), whatLabels=c("par"),
sizeMan=6, sizeLat=9, intercepts=FALSE, style="lisrel",
edge.color="black", edge.label.cex=1)
semPaths(semPlotModel_MxRAMModel(modC2), whatLabels=c("par"),
sizeMan=6, sizeLat=9, intercepts=FALSE, style="lisrel",
edge.color="black", edge.label.cex=1)
# IAT regression models ---------------------------------------------------
# model using only D scores as predictors
summary(glm(vote ~ IAT.D, family=binomial, data=clean))
# control for explicit measures
summary(glm(vote ~ IAT.D + explicit, family=binomial, data=clean))
# w/ demographics, without controlling for explicit
summary(glm(vote ~ IAT.D +
party + libCon + gender + age + race + education + income,
family=binomial, data=clean))
# w/ demographics, controlling for explicit
summary(glm(vote ~ IAT.D + explicit +
party + libCon + gender + age + race + education + income,
family=binomial, data=clean))
# IAT SEM -----------------------------------------------------------------
ind <- c("warm","sympathy","admiration","influence")
# the model has a hard time with missing data!
include <- !is.na(clean$IAT.D) & !is.na(clean$vote)
iatdat <- within(na.omit(clean[include, c("vote","IAT.D",ind)]), {
# scale variables to have similar SDs
D <- IAT.D
IAT.D <- NULL
influence <- influence
vote <- mxFactor(vote, levels=0:1)
})
# check SDs
sqrt(diag(var(iatdat, na.rm=TRUE)))
# check number of missing obs by variable (should be none)
apply(iatdat, 2, function(x) sum(is.na(x)))
# test out with reliability = .5
alpha <- .4
modA <- mxTryHard(mxOption(model=mxModel(name="modA",
type="RAM",
manifestVars=c("vote","D",ind),
latentVars=c("implicit","explicit"),
# indicators
mxPath(from="explicit", to=ind),
mxPath(from="implicit", to="D", free=FALSE, value=1),
# residual variances
mxPath(from=ind, arrows=2),
mxPath(from="D", arrows=2, free=FALSE,
value=(1-alpha)*var(iatdat$D, na.rm=TRUE)),
mxPath(from="vote", arrows=2, free=FALSE, values=1),
# fix scale of latents
mxPath(from=c("implicit","explicit"), arrows=2, free=FALSE, value=1),
# allow latents to covary
mxPath(from="implicit", to="explicit", arrows=2),
# regress Y on Ts
mxPath(from=c("implicit","explicit"), to="vote"),
# means/intercepts and thresholds
mxPath(from="one", to=c("implicit","explicit","vote"), free=FALSE, values=0),
mxPath(from="one", to=c("D",ind)),
mxThreshold(vars="vote", nThresh=1, values=0),
# data source
mxData(observed=iatdat, type="raw")),
# enable parallel (arguments to mxOption)
key="Number of Threads", value=(omxDetectCores()-1)),
# number of times to try new starting values (argument to mxTryHard)
extraTries=99)
summary(modA)
umxSummary(modA)
modC <- mxTryHard(mxOption(model=mxModel(name="modC",
type="RAM",
manifestVars=c("vote","D",ind),
latentVars=c("attitude"),
# indicators
mxPath(from="attitude", to=ind),
mxPath(from="attitude", to="D", free=FALSE, value=1),
# residual variances
mxPath(from=ind, arrows=2),
mxPath(from="D", arrows=2, free=FALSE,
value=(1-alpha)*var(iatdat$D, na.rm=TRUE)),
mxPath(from="vote", arrows=2, free=FALSE, values=1),
# fix scale of latent
mxPath(from=c("attitude"), arrows=2, free=FALSE, value=1),
# regress Y on latent
mxPath(from=c("attitude"), to="vote"),
# means/intercepts and thresholds
mxPath(from="one", to=c("attitude","vote"), free=FALSE, values=0),
mxPath(from="one", to=c("D",ind)),
mxThreshold(vars="vote", nThresh=1, values=0),
# data source
mxData(observed=iatdat, type="raw")),
# enable parallel (arguments to mxOption)
key="Number of Threads", value=(omxDetectCores()-1)),
# number of times to try new starting values (argument to mxTryHard)
extraTries = 99)
summary(modC)
umxCompare(modA,modC)
loads <- modA@matrices$A$values[ind,"explicit"]
errors <- diag(modA@matrices$S$values[ind,ind])
round(sum(loads)^2/(sum(loads)^2 + sum(errors)), 2)
# reliability = 0.77
# define function to get chi-square differences as f(alpha)
getX2 <- function(alpha){
modA <- mxTryHard(mxOption(model=mxModel(name="modA",
type="RAM",
manifestVars=c("vote","D",ind),
latentVars=c("implicit","explicit"),
mxPath(from="explicit", to=ind),
mxPath(from="implicit", to="D", free=FALSE, value=1),
mxPath(from=ind, arrows=2),
mxPath(from="D", arrows=2, free=FALSE,
value=(1-alpha)*var(iatdat$D, na.rm=TRUE)),
mxPath(from="vote", arrows=2, free=FALSE, values=1),
mxPath(from=c("implicit","explicit"), arrows=2, free=FALSE, value=1),
mxPath(from="implicit", to="explicit", arrows=2),
mxPath(from=c("implicit","explicit"), to="vote"),
mxPath(from="one", to=c("implicit","explicit","vote"), free=FALSE, values=0),
mxPath(from="one", to=c("D",ind)),
mxThreshold(vars="vote", nThresh=1, values=0),
mxData(observed=iatdat, type="raw")),
key="Number of Threads", value=(omxDetectCores()-1)),
extraTries=99)
modC <- mxTryHard(mxOption(model=mxModel(name="modC",
type="RAM",
manifestVars=c("vote","D",ind),
latentVars=c("attitude"),
mxPath(from="attitude", to=ind),
mxPath(from="attitude", to="D", free=FALSE, value=1),
mxPath(from=ind, arrows=2),
mxPath(from="D", arrows=2, free=FALSE,
value=(1-alpha)*var(iatdat$D, na.rm=TRUE)),
mxPath(from="vote", arrows=2, free=FALSE, values=1),
mxPath(from=c("attitude"), arrows=2, free=FALSE, value=1),
mxPath(from=c("attitude"), to="vote"),
mxPath(from="one", to=c("attitude","vote"), free=FALSE, values=0),
mxPath(from="one", to=c("D",ind)),
mxThreshold(vars="vote", nThresh=1, values=0),
mxData(observed=iatdat, type="raw")),
key="Number of Threads", value=(omxDetectCores()-1)),
extraTries = 99)
umxCompare(modA,modC)[2,3]
}
system.time({
X2diffs2 <- sapply(seq(from=.01, to=.99, length.out=11), getX2)
}) # elapsed = 1 minute
png("/Users/Jake/Desktop/IAT_LR_as_fAlpha.png",
units="in", height=8, width=8, res=200, pointsize=25)
plot(y=log(X2diffs2), x=seq(from=.01, to=.99, length.out=11),
type="o", pch=19, yaxt="n", lwd=2, ylim=c(0,max(log(X2diffs2))),
ylab=expression(paste(chi^2," difference (log scale)")),
main="IAT", mgp=c(2.5,1,0),
xlab="Assumed reliability of implicit measure")
axis(side=2, at=log(c(1,7,50,400,3000)), labels=c(1,7,50,400,3000))
abline(h=c(0, log(qchisq(.975, df=2))), lty=2)
dev.off()
# IPIP, all 11 factors ----------------------------------------------------
list.files(paste0(path,"ipip_data/"))
bri <- read.table(paste0(path,"ipip_data/bri.dat"), sep="\t", header=TRUE)
bri_clus <- read.table(paste0(path,"ipip_data/bri_clus.dat"), sep="\t", header=TRUE)
hexaco <- read.table(paste0(path,"ipip_data/hexaco.dat"), sep="\t", header=TRUE)
neo <- read.table(paste0(path,"ipip_data/neo.dat"), sep="\t", header=TRUE)
ipip <- Reduce(merge, list(bri_clus, neo[,1:36], hexaco))
# scale NEO variables so all manifests have similar SDs
index <- apply(expand.grid(c("n","e","o","a","c"), 1:6), 1, paste, collapse="")
ipip[,index] <- ipip[,index]/6
# multiple regression of BRI drugs on neo + hexaco
modA <- lm(drugs ~ n + e + o + a + c +
hones + emoti + extra + agree + consc + openn, data=ipip)
modC <- lm(drugs ~ n + e + o + a + c, data=ipip)
anova(modC, modA)
# Res.Df RSS Df Sum of Sq F Pr(>F)
# 1 598 209.14
# 2 592 190.25 6 18.892 9.7974 2.611e-10 ***
# again w/ SEM
h <- names(hexaco)[2:25]
modA <- OpenMx::mxTryHard(mxOption(model=mxModel(name="modA",
type="RAM",
manifestVars=c("drugs", names(neo)[7:36], h),
latentVars=c("n","e","o","a","c",
"hones","emoti","extra","agree","consc","openn"),
# indicators
mxPath(from="n", to=paste0("n",1:6)),
mxPath(from="e", to=paste0("e",1:6)),
mxPath(from="o", to=paste0("o",1:6)),
mxPath(from="a", to=paste0("a",1:6)),
mxPath(from="c", to=paste0("c",1:6)),
mxPath(from="hones", to=h[substr(h,1,1)=="h"]),
mxPath(from="emoti", to=h[substr(h,1,1)=="e"]),
mxPath(from="extra", to=h[substr(h,1,1)=="x"]),
mxPath(from="agree", to=h[substr(h,1,1)=="a"]),
mxPath(from="consc", to=h[substr(h,1,1)=="c"]),
mxPath(from="openn", to=h[substr(h,1,1)=="o"]),
# residual variances
mxPath(from=c("drugs", names(neo)[7:36], h), arrows=2),
# fix scale of latents
mxPath(from=c("n","e","o","a","c","hones","emoti","extra","agree",
"consc","openn"), arrows=2, free=FALSE, value=1),
# allow latents to covary
mxPath(from=c("n","e","o","a","c","hones","emoti","extra","agree",
"consc","openn"), arrows=2, connect="unique.bivariate"),
# regress Y on Ts
mxPath(from=c("n","e","o","a","c","hones","emoti","extra","agree",
"consc","openn"), to="drugs"),
# data source
mxData(observed=cov(ipip[,c("drugs", names(neo)[7:36], h)]),
type="cov", numObs=nrow(ipip))),
# enable parallel (arguments to mxOption)
key="Number of Threads", value=(omxDetectCores()-1)),
# number of times to try new starting values (argument to mxTryHard)
extraTries=99)
# WON'T CONVERGE
summary(modA)
# check SDs
sqrt(diag(cov(ipip[,c("drugs", names(neo)[7:36], h)])))
semPaths(semPlotModel_MxRAMModel(modA), whatLabels=c("par"),
sizeMan=3, sizeLat=9, sizeInt=1, mar=c(5,5,5,5),
edge.color="black", edge.label.cex=1, structural=TRUE,
color=list(man="pink", lat="lightblue"))
omxGraphviz(modA, "/Users/Jake/Desktop/test.dot")
# IPIP, subsets of factors ------------------------------------------------
# multiple regression of BRI drugs on neo + hexaco
summary(lm(drugs ~ n + e + o + a + c + hones, data=ipip))
summary(lm(drugs ~ hones + emoti + extra + agree + consc + openn, data=ipip))
# NEO variables + HEXACO honesty
h <- names(hexaco)[2:5]
modA <- mxTryHard(mxOption(model=mxModel(name="modA",
type="RAM",
manifestVars=c("drugs", names(neo)[7:36], h),
latentVars=c("n","e","o","a","c","hones"),
# indicators
mxPath(from="n", to=paste0("n",1:6)),
mxPath(from="e", to=paste0("e",1:6)),
mxPath(from="o", to=paste0("o",1:6)),
mxPath(from="a", to=paste0("a",1:6)),
mxPath(from="c", to=paste0("c",1:6)),
mxPath(from="hones", to=h),
# residual variances
mxPath(from=c("drugs", names(neo)[7:36], h), arrows=2),
# fix scale of latents
mxPath(from=c("n","e","o","a","c","hones"),
arrows=2, free=FALSE, value=1),
# allow latents to covary
mxPath(from=c("n","e","o","a","c","hones"),
arrows=2, connect="unique.bivariate"),
# regress Y on Ts
mxPath(from=c("n","e","o","a","c","hones"), to="drugs"),
# data source
mxData(observed=cov(ipip[,c("drugs", names(neo)[7:36], h)]),
type="cov", numObs=nrow(ipip))),
# enable parallel (arguments to mxOption)
key="Number of Threads", value=(omxDetectCores()-1)),
# number of times to try new starting values (argument to mxTryHard)
extraTries=99)
# WON'T CONVERGE
summary(modA)
# HEXACO variables only
h <- names(hexaco)[2:25]
modA <- mxTryHard(mxOption(model=mxModel(name="modA",
type="RAM",
manifestVars=c("drugs", h),
latentVars=c("hones","emoti","extra","agree","consc","openn"),
# indicators
mxPath(from="hones", to=h[substr(h,1,1)=="h"]),
mxPath(from="emoti", to=h[substr(h,1,1)=="e"]),
mxPath(from="extra", to=h[substr(h,1,1)=="x"]),
mxPath(from="agree", to=h[substr(h,1,1)=="a"]),
mxPath(from="consc", to=h[substr(h,1,1)=="c"]),
mxPath(from="openn", to=h[substr(h,1,1)=="o"]),
# residual variances
mxPath(from=c("drugs", h), arrows=2),
# fix scale of latents
mxPath(from=c("hones","emoti","extra","agree","consc","openn"),
arrows=2, free=FALSE, value=1),
# allow latents to covary
mxPath(from=c("hones","emoti","extra","agree","consc","openn"),
arrows=2, connect="unique.bivariate"),
# regress Y on Ts
mxPath(from=c("hones","emoti","extra","agree","consc","openn"), to="drugs"),
# data source
mxData(observed=cov(ipip[,c("drugs", h)]), type="cov", numObs=nrow(ipip))),
# enable parallel (arguments to mxOption)
key="Number of Threads", value=(omxDetectCores()-1)),
# number of times to try new starting values (argument to mxTryHard)
extraTries=99)
# WON'T CONVERGE
# still won't converge if "drugs" outcome changed to "religion"
summary(modA)
# NEO-PI variables only
h <- names(hexaco)[2:25]
modA <- mxTryHard(mxOption(model=mxModel(name="modA",
type="RAM",
manifestVars=c("drugs", names(neo)[7:36]),
latentVars=c("n","e","o","a","c"),
# indicators
mxPath(from="n", to=paste0("n",1:6)),
mxPath(from="e", to=paste0("e",1:6)),
mxPath(from="o", to=paste0("o",1:6)),
mxPath(from="a", to=paste0("a",1:6)),
mxPath(from="c", to=paste0("c",1:6)),
# residual variances
mxPath(from=c("drugs", names(neo)[7:36]), arrows=2),
# fix scale of latents
mxPath(from=c("n","e","o","a","c"), arrows=2, free=FALSE, value=1),
# allow latents to covary
mxPath(from=c("n","e","o","a","c"), arrows=2, connect="unique.bivariate"),
# regress Y on Ts
mxPath(from=c("n","e","o","a","c"), to="drugs"),
# data source
mxData(observed=cov(ipip[,c("drugs", names(neo)[7:36])]),
type="cov", numObs=nrow(ipip))),
# enable parallel (arguments to mxOption)
key="Number of Threads", value=(omxDetectCores()-1)),
# number of times to try new starting values (argument to mxTryHard)
extraTries=99)
# WON'T CONVERGE
summary(modA)
# NEO extraversion + HEXACO extraversion
h <- names(hexaco)[2:25]
h <- h[substr(h,1,1)=="x"]
modA <- mxTryHard(mxOption(model=mxModel(name="modA",
type="RAM",
manifestVars=c("drugs", paste0("e",1:6), h),
latentVars=c("e","extra"),
# indicators
mxPath(from="e", to=paste0("e",1:6)),
mxPath(from="extra", to=h),
# residual variances
mxPath(from=c("drugs", paste0("e",1:6), h), arrows=2),
# fix scale of latents
mxPath(from=c("e","extra"), arrows=2, free=FALSE, value=1),
# allow latents to covary
mxPath(from=c("e","extra"), arrows=2, connect="unique.bivariate"),
# regress Y on Ts
mxPath(from=c("e","extra"), to="drugs"),
# data source
mxData(observed=cov(ipip[,c("drugs", paste0("e",1:6), h)]),
type="cov", numObs=nrow(ipip))),
# enable parallel (arguments to mxOption)
key="Number of Threads", value=(omxDetectCores()-1)),
# number of times to try new starting values (argument to mxTryHard)
extraTries=99)
# WON'T CONVERGE
summary(modA)
# NEO extraversion + HEXACO extraversion, measurement model only
h <- names(hexaco)[2:25]
h <- h[substr(h,1,1)=="x"]
modA <- mxTryHard(mxOption(model=mxModel(name="modA",
type="RAM",
manifestVars=c(paste0("e",1:6), h),
latentVars=c("e","extra"),
# indicators
mxPath(from="e", to=paste0("e",1:6)),
mxPath(from="extra", to=h),
# residual variances
mxPath(from=c(paste0("e",1:6), h), arrows=2),
# fix scale of latents
mxPath(from=c("e","extra"), arrows=2, free=FALSE, value=1),
# allow latents to covary
mxPath(from=c("e","extra"), arrows=2, connect="unique.bivariate"),
# data source
mxData(observed=cov(ipip[,c(paste0("e",1:6), h)]),
type="cov", numObs=nrow(ipip))),
# enable parallel (arguments to mxOption)
key="Number of Threads", value=(omxDetectCores()-1)),
# number of times to try new starting values (argument to mxTryHard)
extraTries=99)
# WON'T CONVERGE
summary(modA)
# NEO variables only (no AC), measurement model only
modA <- mxTryHard(mxOption(model=mxModel(name="modA",
type="RAM",
manifestVars=c(names(neo)[7:26]),
latentVars=c("n","e","o"),
# indicators
mxPath(from="n", to=paste0("n",1:6)),
mxPath(from="e", to=paste0("e",1:6)),
mxPath(from="o", to=paste0("o",1:6)),
# residual variances
mxPath(from=c(names(neo)[7:26]), arrows=2),
# fix scale of latents
mxPath(from=c("n","e","o"), arrows=2, free=FALSE, value=1),
# allow latents to covary
mxPath(from=c("n","e","o"), arrows=2, connect="unique.bivariate"),
# data source
# mxData(observed=cov(ipip[,c(names(neo)[7:26])]),
# type="cov", numObs=nrow(ipip))),
mxData(observed=ipip[,c(names(neo)[7:26])],
type="raw"),
# means/intercepts and thresholds
mxPath(from="one", to=c("n","e","o"), free=FALSE, values=0),
mxPath(from="one", to=names(neo)[7:26])),
# enable parallel (arguments to mxOption)
key="Number of Threads", value=(omxDetectCores()-1)),
# number of times to try new starting values (argument to mxTryHard)
extraTries=99)
# WON'T CONVERGE
### try with lavaan
model <- "n =~ n1 + n2 + n3 + n4 + n5 + n6
e =~ e1 + e2 + e3 + e4 + e5 + e6
o =~ o1 + o2 + o3 + o4 + o5 + o6"
fit <- cfa(model, std.lv=TRUE, data=ipip[,c(names(neo)[7:26])])
summary(fit)
# IPIP in lavaan ----------------------------------------------------------
# NEO variables + HEXACO honesty
summary(lm(drugs ~ n + e + o + a + c + hones, data=ipip))
mod <- sem("
# measurement models
n =~ n1 + n2 + n3 + n4 + n5 + n6
e =~ e1 + e2 + e3 + e4 + e5 + e6
o =~ o1 + o2 + o3 + o4 + o5 + o6
a =~ a1 + a2 + a3 + a4 + a5 + a6
c =~ c1 + c2 + c3 + c4 + c5 + c6
honesty =~ hsinc + hfair + hgree + hmode
# regressions
drugs ~ n + e + o + a + c + honesty
", data=ipip)
summary(mod)
semPaths(mod, whatLabels=c("par"), layout="tree",
sizeMan=3, sizeLat=5, sizeInt=1, mar=c(3,3,3,3),
edge.color="black", edge.label.cex=1, # structural=TRUE,
color=list(man="pink", lat="lightblue"))
# HEXACO variables only
summary(lm(drugs ~ emoti + extra + agree + consc + openn + hones, data=ipip))
mod <- sem("
# measurement models
x =~ xexpr + xsocb + xsoci + xlive
e =~ efear + eanxi + edepe + esent
o =~ oaesa + oinqu + ocrea + ounco
a =~ aforg + agent + aflex + apati
c =~ corga + cdili + cperf + cprud