This is an R notebook based on old code written for the paper:
Data files: https://osf.io/69pcd/files/
library(pacman)
p_load(kirkegaard, QuantPsyc)
#load mega
m = read_mega("data/Megadataset_v2.0j.csv")
#get data for crime and predictors
c = cbind(m[365:383], #main crime variables
m["LV2012estimatedIQ"],
m["IslamPewResearch2010"],
m["International.S.Factor.Kirkegaard2014"])
#12 to 24 year olds
c2 = m[c(374:375, 379:380)]
colnames(c2) = c("Age12_17_1gen",
"Age18_24_1gen",
"Age12_17_2gen",
"Age18_24_2gen"
)
# zero-order correlations -------------------------------------------------
#within and between generations
c2_cor = wtd.cors(c2)
write_clipboard(c2_cor)
## Age12 17 1gen Age18 24 1gen Age12 17 2gen Age18 24 2gen
## Age12 17 1gen 1.00 0.85 0.69 0.70
## Age18 24 1gen 0.85 1.00 0.83 0.70
## Age12 17 2gen 0.69 0.83 1.00 0.83
## Age18 24 2gen 0.70 0.70 0.83 1.00
#general crime factor
crime_fa = fa(c2)
#stability
fa_stabil = fa_all_methods(c2, messages = F)
wtd.cors(fa_stabil$scores)
## regression_minres Thurstone_minres tenBerge_minres
## regression_minres 1.0000000 1.0000000 1.0000000
## Thurstone_minres 1.0000000 1.0000000 1.0000000
## tenBerge_minres 1.0000000 1.0000000 1.0000000
## Bartlett_minres 0.9998536 0.9998536 0.9998536
## regression_ols 1.0000000 1.0000000 1.0000000
## Thurstone_ols 1.0000000 1.0000000 1.0000000
## tenBerge_ols 1.0000000 1.0000000 1.0000000
## Bartlett_ols 0.9998535 0.9998535 0.9998535
## regression_wls 0.9999922 0.9999922 0.9999922
## Thurstone_wls 0.9999922 0.9999922 0.9999922
## tenBerge_wls 0.9999922 0.9999922 0.9999922
## Bartlett_wls 0.9998508 0.9998508 0.9998508
## regression_gls 0.9977944 0.9977944 0.9977944
## Thurstone_gls 0.9977944 0.9977944 0.9977944
## tenBerge_gls 0.9977944 0.9977944 0.9977944
## Bartlett_gls 0.9993517 0.9993517 0.9993517
## regression_pa 0.9999916 0.9999916 0.9999916
## Thurstone_pa 0.9999916 0.9999916 0.9999916
## tenBerge_pa 0.9999916 0.9999916 0.9999916
## Bartlett_pa 0.9998449 0.9998449 0.9998449
## regression_ml 0.9981789 0.9981789 0.9981789
## Thurstone_ml 0.9981789 0.9981789 0.9981789
## tenBerge_ml 0.9981789 0.9981789 0.9981789
## Bartlett_ml 0.9981789 0.9981789 0.9981789
## regression_minchi 0.9996420 0.9996420 0.9996420
## Thurstone_minchi 0.9996420 0.9996420 0.9996420
## tenBerge_minchi 0.9996420 0.9996420 0.9996420
## Bartlett_minchi 0.9995535 0.9995535 0.9995535
## regression_minrank 0.9997455 0.9997455 0.9997455
## Thurstone_minrank 0.9997455 0.9997455 0.9997455
## tenBerge_minrank 0.9997455 0.9997455 0.9997455
## Bartlett_minrank 0.9997505 0.9997505 0.9997505
## Bartlett_minres regression_ols Thurstone_ols
## regression_minres 0.9998536 1.0000000 1.0000000
## Thurstone_minres 0.9998536 1.0000000 1.0000000
## tenBerge_minres 0.9998536 1.0000000 1.0000000
## Bartlett_minres 1.0000000 0.9998536 0.9998536
## regression_ols 0.9998536 1.0000000 1.0000000
## Thurstone_ols 0.9998536 1.0000000 1.0000000
## tenBerge_ols 0.9998536 1.0000000 1.0000000
## Bartlett_ols 1.0000000 0.9998536 0.9998536
## regression_wls 0.9998154 0.9999923 0.9999923
## Thurstone_wls 0.9998154 0.9999923 0.9999923
## tenBerge_wls 0.9998154 0.9999923 0.9999923
## Bartlett_wls 0.9999979 0.9998508 0.9998508
## regression_gls 0.9981930 0.9977942 0.9977942
## Thurstone_gls 0.9981930 0.9977942 0.9977942
## tenBerge_gls 0.9981930 0.9977942 0.9977942
## Bartlett_gls 0.9995740 0.9993516 0.9993516
## regression_pa 0.9998851 0.9999916 0.9999916
## Thurstone_pa 0.9998851 0.9999916 0.9999916
## tenBerge_pa 0.9998851 0.9999916 0.9999916
## Bartlett_pa 0.9999980 0.9998449 0.9998449
## regression_ml 0.9987024 0.9981795 0.9981795
## Thurstone_ml 0.9987024 0.9981795 0.9981795
## tenBerge_ml 0.9987024 0.9981795 0.9981795
## Bartlett_ml 0.9987023 0.9981794 0.9981794
## regression_minchi 0.9999213 0.9996421 0.9996421
## Thurstone_minchi 0.9999213 0.9996421 0.9996421
## tenBerge_minchi 0.9999213 0.9996421 0.9996421
## Bartlett_minchi 0.9998697 0.9995537 0.9995537
## regression_minrank 0.9998740 0.9997454 0.9997454
## Thurstone_minrank 0.9998740 0.9997454 0.9997454
## tenBerge_minrank 0.9998740 0.9997454 0.9997454
## Bartlett_minrank 0.9998744 0.9997505 0.9997505
## tenBerge_ols Bartlett_ols regression_wls Thurstone_wls
## regression_minres 1.0000000 0.9998535 0.9999922 0.9999922
## Thurstone_minres 1.0000000 0.9998535 0.9999922 0.9999922
## tenBerge_minres 1.0000000 0.9998535 0.9999922 0.9999922
## Bartlett_minres 0.9998536 1.0000000 0.9998154 0.9998154
## regression_ols 1.0000000 0.9998536 0.9999923 0.9999923
## Thurstone_ols 1.0000000 0.9998536 0.9999923 0.9999923
## tenBerge_ols 1.0000000 0.9998536 0.9999923 0.9999923
## Bartlett_ols 0.9998536 1.0000000 0.9998153 0.9998153
## regression_wls 0.9999923 0.9998153 1.0000000 1.0000000
## Thurstone_wls 0.9999923 0.9998153 1.0000000 1.0000000
## tenBerge_wls 0.9999923 0.9998153 1.0000000 1.0000000
## Bartlett_wls 0.9998508 0.9999979 0.9998191 0.9998191
## regression_gls 0.9977942 0.9981927 0.9978468 0.9978468
## Thurstone_gls 0.9977942 0.9981927 0.9978468 0.9978468
## tenBerge_gls 0.9977942 0.9981927 0.9978468 0.9978468
## Bartlett_gls 0.9993516 0.9995739 0.9993589 0.9993589
## regression_pa 0.9999916 0.9998850 0.9999864 0.9999864
## Thurstone_pa 0.9999916 0.9998850 0.9999864 0.9999864
## tenBerge_pa 0.9999916 0.9998850 0.9999864 0.9999864
## Bartlett_pa 0.9998449 0.9999980 0.9998098 0.9998098
## regression_ml 0.9981795 0.9987028 0.9981523 0.9981523
## Thurstone_ml 0.9981795 0.9987028 0.9981523 0.9981523
## tenBerge_ml 0.9981795 0.9987028 0.9981523 0.9981523
## Bartlett_ml 0.9981794 0.9987027 0.9981522 0.9981522
## regression_minchi 0.9996421 0.9999214 0.9996189 0.9996189
## Thurstone_minchi 0.9996421 0.9999214 0.9996189 0.9996189
## tenBerge_minchi 0.9996421 0.9999214 0.9996189 0.9996189
## Bartlett_minchi 0.9995537 0.9998699 0.9995245 0.9995245
## regression_minrank 0.9997454 0.9998739 0.9996752 0.9996752
## Thurstone_minrank 0.9997454 0.9998739 0.9996752 0.9996752
## tenBerge_minrank 0.9997454 0.9998739 0.9996752 0.9996752
## Bartlett_minrank 0.9997505 0.9998744 0.9996617 0.9996617
## tenBerge_wls Bartlett_wls regression_gls Thurstone_gls
## regression_minres 0.9999922 0.9998508 0.9977944 0.9977944
## Thurstone_minres 0.9999922 0.9998508 0.9977944 0.9977944
## tenBerge_minres 0.9999922 0.9998508 0.9977944 0.9977944
## Bartlett_minres 0.9998154 0.9999979 0.9981930 0.9981930
## regression_ols 0.9999923 0.9998508 0.9977942 0.9977942
## Thurstone_ols 0.9999923 0.9998508 0.9977942 0.9977942
## tenBerge_ols 0.9999923 0.9998508 0.9977942 0.9977942
## Bartlett_ols 0.9998153 0.9999979 0.9981927 0.9981927
## regression_wls 1.0000000 0.9998191 0.9978468 0.9978468
## Thurstone_wls 1.0000000 0.9998191 0.9978468 0.9978468
## tenBerge_wls 1.0000000 0.9998191 0.9978468 0.9978468
## Bartlett_wls 0.9998191 1.0000000 0.9982928 0.9982928
## regression_gls 0.9978468 0.9982928 1.0000000 1.0000000
## Thurstone_gls 0.9978468 0.9982928 1.0000000 1.0000000
## tenBerge_gls 0.9978468 0.9982928 1.0000000 1.0000000
## Bartlett_gls 0.9993589 0.9996206 0.9995111 0.9995111
## regression_pa 0.9999864 0.9998883 0.9980571 0.9980571
## Thurstone_pa 0.9999864 0.9998883 0.9980571 0.9980571
## tenBerge_pa 0.9999864 0.9998883 0.9980571 0.9980571
## Bartlett_pa 0.9998098 0.9999992 0.9983119 0.9983119
## regression_ml 0.9981523 0.9986926 0.9956140 0.9956140
## Thurstone_ml 0.9981523 0.9986926 0.9956140 0.9956140
## tenBerge_ml 0.9981523 0.9986926 0.9956140 0.9956140
## Bartlett_ml 0.9981522 0.9986925 0.9956139 0.9956139
## regression_minchi 0.9996189 0.9999305 0.9982428 0.9982428
## Thurstone_minchi 0.9996189 0.9999305 0.9982428 0.9982428
## tenBerge_minchi 0.9996189 0.9999305 0.9982428 0.9982428
## Bartlett_minchi 0.9995245 0.9998740 0.9979750 0.9979750
## regression_minrank 0.9996752 0.9998640 0.9983316 0.9983316
## Thurstone_minrank 0.9996752 0.9998640 0.9983316 0.9983316
## tenBerge_minrank 0.9996752 0.9998640 0.9983316 0.9983316
## Bartlett_minrank 0.9996617 0.9998402 0.9973206 0.9973206
## tenBerge_gls Bartlett_gls regression_pa Thurstone_pa
## regression_minres 0.9977944 0.9993517 0.9999916 0.9999916
## Thurstone_minres 0.9977944 0.9993517 0.9999916 0.9999916
## tenBerge_minres 0.9977944 0.9993517 0.9999916 0.9999916
## Bartlett_minres 0.9981930 0.9995740 0.9998851 0.9998851
## regression_ols 0.9977942 0.9993516 0.9999916 0.9999916
## Thurstone_ols 0.9977942 0.9993516 0.9999916 0.9999916
## tenBerge_ols 0.9977942 0.9993516 0.9999916 0.9999916
## Bartlett_ols 0.9981927 0.9995739 0.9998850 0.9998850
## regression_wls 0.9978468 0.9993589 0.9999864 0.9999864
## Thurstone_wls 0.9978468 0.9993589 0.9999864 0.9999864
## tenBerge_wls 0.9978468 0.9993589 0.9999864 0.9999864
## Bartlett_wls 0.9982928 0.9996206 0.9998883 0.9998883
## regression_gls 1.0000000 0.9995111 0.9980571 0.9980571
## Thurstone_gls 1.0000000 0.9995111 0.9980571 0.9980571
## tenBerge_gls 1.0000000 0.9995111 0.9980571 0.9980571
## Bartlett_gls 0.9995111 1.0000000 0.9994909 0.9994909
## regression_pa 0.9980571 0.9994909 1.0000000 1.0000000
## Thurstone_pa 0.9980571 0.9994909 1.0000000 1.0000000
## tenBerge_pa 0.9980571 0.9994909 1.0000000 1.0000000
## Bartlett_pa 0.9983119 0.9996308 0.9998840 0.9998840
## regression_ml 0.9956140 0.9975228 0.9981686 0.9981686
## Thurstone_ml 0.9956140 0.9975228 0.9981686 0.9981686
## tenBerge_ml 0.9956140 0.9975228 0.9981686 0.9981686
## Bartlett_ml 0.9956139 0.9975228 0.9981685 0.9981685
## regression_minchi 0.9982428 0.9995215 0.9996924 0.9996924
## Thurstone_minchi 0.9982428 0.9995215 0.9996924 0.9996924
## tenBerge_minchi 0.9982428 0.9995215 0.9996924 0.9996924
## Bartlett_minchi 0.9979750 0.9993534 0.9995946 0.9995946
## regression_minrank 0.9983316 0.9996050 0.9997908 0.9997908
## Thurstone_minrank 0.9983316 0.9996050 0.9997908 0.9997908
## tenBerge_minrank 0.9983316 0.9996050 0.9997908 0.9997908
## Bartlett_minrank 0.9973206 0.9990977 0.9997367 0.9997367
## tenBerge_pa Bartlett_pa regression_ml Thurstone_ml
## regression_minres 0.9999916 0.9998449 0.9981789 0.9981789
## Thurstone_minres 0.9999916 0.9998449 0.9981789 0.9981789
## tenBerge_minres 0.9999916 0.9998449 0.9981789 0.9981789
## Bartlett_minres 0.9998851 0.9999980 0.9987024 0.9987024
## regression_ols 0.9999916 0.9998449 0.9981795 0.9981795
## Thurstone_ols 0.9999916 0.9998449 0.9981795 0.9981795
## tenBerge_ols 0.9999916 0.9998449 0.9981795 0.9981795
## Bartlett_ols 0.9998850 0.9999980 0.9987028 0.9987028
## regression_wls 0.9999864 0.9998098 0.9981523 0.9981523
## Thurstone_wls 0.9999864 0.9998098 0.9981523 0.9981523
## tenBerge_wls 0.9999864 0.9998098 0.9981523 0.9981523
## Bartlett_wls 0.9998883 0.9999992 0.9986926 0.9986926
## regression_gls 0.9980571 0.9983119 0.9956140 0.9956140
## Thurstone_gls 0.9980571 0.9983119 0.9956140 0.9956140
## tenBerge_gls 0.9980571 0.9983119 0.9956140 0.9956140
## Bartlett_gls 0.9994909 0.9996308 0.9975228 0.9975228
## regression_pa 1.0000000 0.9998840 0.9981686 0.9981686
## Thurstone_pa 1.0000000 0.9998840 0.9981686 0.9981686
## tenBerge_pa 1.0000000 0.9998840 0.9981686 0.9981686
## Bartlett_pa 0.9998840 1.0000000 0.9986514 0.9986514
## regression_ml 0.9981686 0.9986514 1.0000000 1.0000000
## Thurstone_ml 0.9981686 0.9986514 1.0000000 1.0000000
## tenBerge_ml 0.9981686 0.9986514 1.0000000 1.0000000
## Bartlett_ml 0.9981685 0.9986513 1.0000000 1.0000000
## regression_minchi 0.9996924 0.9999220 0.9991496 0.9991496
## Thurstone_minchi 0.9996924 0.9999220 0.9991496 0.9991496
## tenBerge_minchi 0.9996924 0.9999220 0.9991496 0.9991496
## Bartlett_minchi 0.9995946 0.9998626 0.9993450 0.9993450
## regression_minrank 0.9997908 0.9998823 0.9978674 0.9978674
## Thurstone_minrank 0.9997908 0.9998823 0.9978674 0.9978674
## tenBerge_minrank 0.9997908 0.9998823 0.9978674 0.9978674
## Bartlett_minrank 0.9997367 0.9998479 0.9985388 0.9985388
## tenBerge_ml Bartlett_ml regression_minchi
## regression_minres 0.9981789 0.9981789 0.9996420
## Thurstone_minres 0.9981789 0.9981789 0.9996420
## tenBerge_minres 0.9981789 0.9981789 0.9996420
## Bartlett_minres 0.9987024 0.9987023 0.9999213
## regression_ols 0.9981795 0.9981794 0.9996421
## Thurstone_ols 0.9981795 0.9981794 0.9996421
## tenBerge_ols 0.9981795 0.9981794 0.9996421
## Bartlett_ols 0.9987028 0.9987027 0.9999214
## regression_wls 0.9981523 0.9981522 0.9996189
## Thurstone_wls 0.9981523 0.9981522 0.9996189
## tenBerge_wls 0.9981523 0.9981522 0.9996189
## Bartlett_wls 0.9986926 0.9986925 0.9999305
## regression_gls 0.9956140 0.9956139 0.9982428
## Thurstone_gls 0.9956140 0.9956139 0.9982428
## tenBerge_gls 0.9956140 0.9956139 0.9982428
## Bartlett_gls 0.9975228 0.9975228 0.9995215
## regression_pa 0.9981686 0.9981685 0.9996924
## Thurstone_pa 0.9981686 0.9981685 0.9996924
## tenBerge_pa 0.9981686 0.9981685 0.9996924
## Bartlett_pa 0.9986514 0.9986513 0.9999220
## regression_ml 1.0000000 1.0000000 0.9991496
## Thurstone_ml 1.0000000 1.0000000 0.9991496
## tenBerge_ml 1.0000000 1.0000000 0.9991496
## Bartlett_ml 1.0000000 1.0000000 0.9991496
## regression_minchi 0.9991496 0.9991496 1.0000000
## Thurstone_minchi 0.9991496 0.9991496 1.0000000
## tenBerge_minchi 0.9991496 0.9991496 1.0000000
## Bartlett_minchi 0.9993450 0.9993450 0.9999861
## regression_minrank 0.9978674 0.9978673 0.9996397
## Thurstone_minrank 0.9978674 0.9978673 0.9996397
## tenBerge_minrank 0.9978674 0.9978673 0.9996397
## Bartlett_minrank 0.9985388 0.9985387 0.9996875
## Thurstone_minchi tenBerge_minchi Bartlett_minchi
## regression_minres 0.9996420 0.9996420 0.9995535
## Thurstone_minres 0.9996420 0.9996420 0.9995535
## tenBerge_minres 0.9996420 0.9996420 0.9995535
## Bartlett_minres 0.9999213 0.9999213 0.9998697
## regression_ols 0.9996421 0.9996421 0.9995537
## Thurstone_ols 0.9996421 0.9996421 0.9995537
## tenBerge_ols 0.9996421 0.9996421 0.9995537
## Bartlett_ols 0.9999214 0.9999214 0.9998699
## regression_wls 0.9996189 0.9996189 0.9995245
## Thurstone_wls 0.9996189 0.9996189 0.9995245
## tenBerge_wls 0.9996189 0.9996189 0.9995245
## Bartlett_wls 0.9999305 0.9999305 0.9998740
## regression_gls 0.9982428 0.9982428 0.9979750
## Thurstone_gls 0.9982428 0.9982428 0.9979750
## tenBerge_gls 0.9982428 0.9982428 0.9979750
## Bartlett_gls 0.9995215 0.9995215 0.9993534
## regression_pa 0.9996924 0.9996924 0.9995946
## Thurstone_pa 0.9996924 0.9996924 0.9995946
## tenBerge_pa 0.9996924 0.9996924 0.9995946
## Bartlett_pa 0.9999220 0.9999220 0.9998626
## regression_ml 0.9991496 0.9991496 0.9993450
## Thurstone_ml 0.9991496 0.9991496 0.9993450
## tenBerge_ml 0.9991496 0.9991496 0.9993450
## Bartlett_ml 0.9991496 0.9991496 0.9993450
## regression_minchi 1.0000000 1.0000000 0.9999861
## Thurstone_minchi 1.0000000 1.0000000 0.9999861
## tenBerge_minchi 1.0000000 1.0000000 0.9999861
## Bartlett_minchi 0.9999861 0.9999861 1.0000000
## regression_minrank 0.9996397 0.9996397 0.9995318
## Thurstone_minrank 0.9996397 0.9996397 0.9995318
## tenBerge_minrank 0.9996397 0.9996397 0.9995318
## Bartlett_minrank 0.9996875 0.9996875 0.9996648
## regression_minrank Thurstone_minrank tenBerge_minrank
## regression_minres 0.9997455 0.9997455 0.9997455
## Thurstone_minres 0.9997455 0.9997455 0.9997455
## tenBerge_minres 0.9997455 0.9997455 0.9997455
## Bartlett_minres 0.9998740 0.9998740 0.9998740
## regression_ols 0.9997454 0.9997454 0.9997454
## Thurstone_ols 0.9997454 0.9997454 0.9997454
## tenBerge_ols 0.9997454 0.9997454 0.9997454
## Bartlett_ols 0.9998739 0.9998739 0.9998739
## regression_wls 0.9996752 0.9996752 0.9996752
## Thurstone_wls 0.9996752 0.9996752 0.9996752
## tenBerge_wls 0.9996752 0.9996752 0.9996752
## Bartlett_wls 0.9998640 0.9998640 0.9998640
## regression_gls 0.9983316 0.9983316 0.9983316
## Thurstone_gls 0.9983316 0.9983316 0.9983316
## tenBerge_gls 0.9983316 0.9983316 0.9983316
## Bartlett_gls 0.9996050 0.9996050 0.9996050
## regression_pa 0.9997908 0.9997908 0.9997908
## Thurstone_pa 0.9997908 0.9997908 0.9997908
## tenBerge_pa 0.9997908 0.9997908 0.9997908
## Bartlett_pa 0.9998823 0.9998823 0.9998823
## regression_ml 0.9978674 0.9978674 0.9978674
## Thurstone_ml 0.9978674 0.9978674 0.9978674
## tenBerge_ml 0.9978674 0.9978674 0.9978674
## Bartlett_ml 0.9978673 0.9978673 0.9978673
## regression_minchi 0.9996397 0.9996397 0.9996397
## Thurstone_minchi 0.9996397 0.9996397 0.9996397
## tenBerge_minchi 0.9996397 0.9996397 0.9996397
## Bartlett_minchi 0.9995318 0.9995318 0.9995318
## regression_minrank 1.0000000 1.0000000 1.0000000
## Thurstone_minrank 1.0000000 1.0000000 1.0000000
## tenBerge_minrank 1.0000000 1.0000000 1.0000000
## Bartlett_minrank 0.9998357 0.9998357 0.9998357
## Bartlett_minrank
## regression_minres 0.9997505
## Thurstone_minres 0.9997505
## tenBerge_minres 0.9997505
## Bartlett_minres 0.9998744
## regression_ols 0.9997505
## Thurstone_ols 0.9997505
## tenBerge_ols 0.9997505
## Bartlett_ols 0.9998744
## regression_wls 0.9996617
## Thurstone_wls 0.9996617
## tenBerge_wls 0.9996617
## Bartlett_wls 0.9998402
## regression_gls 0.9973206
## Thurstone_gls 0.9973206
## tenBerge_gls 0.9973206
## Bartlett_gls 0.9990977
## regression_pa 0.9997367
## Thurstone_pa 0.9997367
## tenBerge_pa 0.9997367
## Bartlett_pa 0.9998479
## regression_ml 0.9985388
## Thurstone_ml 0.9985388
## tenBerge_ml 0.9985388
## Bartlett_ml 0.9985387
## regression_minchi 0.9996875
## Thurstone_minchi 0.9996875
## tenBerge_minchi 0.9996875
## Bartlett_minchi 0.9996648
## regression_minrank 0.9998357
## Thurstone_minrank 0.9998357
## tenBerge_minrank 0.9998357
## Bartlett_minrank 1.0000000
#add to the right place in the DF
c = cbind(c[1:19],
as.vector(crime_fa$scores),
c[20:22]
)
colnames(c) = c("12_17men", #new names
"12_17women",
"18_24men",
"18_24women",
"25_44men",
"25_44women",
"45_79men",
"45_79women",
"All",
"12_17_1gen",
"18_24_1gen",
"25_44_1gen",
"45_79_1gen",
"All_1gen",
"12_17_2gen",
"18_24_2gen",
"25_44_2gen",
"45_79_2gen",
"All_2gen",
"YoungPersonsCrimeFactor",
"IQ",
"Islam",
"Int.S.factor"
)
c_cor = wtd.cors(c) #correlation
pred_r = round(c_cor[1:20, 21:23], 2) #correlations
pred_n = pairwiseCount(c)[1:20, 21:23]
write_clipboard(pred_r)
## IQ Islam Int S factor
## 12 17men -0.49 0.31 -0.43
## 12 17women -0.43 0.13 -0.40
## 18 24men -0.53 0.43 -0.41
## 18 24women -0.53 0.05 -0.58
## 25 44men -0.52 0.20 -0.52
## 25 44women -0.55 0.11 -0.65
## 45 79men -0.50 0.12 -0.54
## 45 79women -0.47 0.12 -0.57
## All -0.59 0.24 -0.61
## 12 17 1gen -0.73 0.17 -0.45
## 18 24 1gen -0.74 0.29 -0.45
## 25 44 1gen -0.63 0.11 -0.47
## 45 79 1gen -0.50 -0.04 -0.21
## All 1gen -0.66 0.12 -0.48
## 12 17 2gen -0.75 0.39 -0.34
## 18 24 2gen -0.75 0.50 -0.37
## 25 44 2gen -0.83 0.48 -0.43
## 45 79 2gen 0.05 0.20 -0.13
## All 2gen -0.81 0.54 -0.49
## YoungPersonsCrimeFactor -0.80 0.36 -0.42
write_clipboard(pred_n)
## IQ Islam Int S factor
## 12 17men 55.00 54.00 46.00
## 12 17women 55.00 54.00 46.00
## 18 24men 55.00 54.00 46.00
## 18 24women 55.00 54.00 46.00
## 25 44men 55.00 54.00 46.00
## 25 44women 55.00 54.00 46.00
## 45 79men 54.00 53.00 45.00
## 45 79women 54.00 53.00 45.00
## All 55.00 54.00 46.00
## 12 17 1gen 21.00 20.00 16.00
## 18 24 1gen 21.00 20.00 16.00
## 25 44 1gen 21.00 20.00 16.00
## 45 79 1gen 21.00 20.00 16.00
## All 1gen 21.00 20.00 16.00
## 12 17 2gen 21.00 20.00 16.00
## 18 24 2gen 21.00 20.00 16.00
## 25 44 2gen 21.00 20.00 16.00
## 45 79 2gen 19.00 18.00 15.00
## All 2gen 21.00 20.00 16.00
## YoungPersonsCrimeFactor 21.00 20.00 16.00
# weights -----------------------------------------------------------------
#this is somewhat difficult because the weights vary by sample
pred_names = c("IQ", "Islam", "Int.S.factor")
var_names = colnames(c)[1:9]
weight_names = colnames(m)[396:404]
wtd_res = matrix(nrow = length(var_names), ncol = length(pred_names))
colnames(wtd_res) = pred_names;rownames(wtd_res) = var_names
for (pred in pred_names) { #for each predictor
for (var_idx in seq_along(var_names)) { #for each variable
#fetch the names
var_name = var_names[var_idx]
weight_name = weight_names[var_idx]
#correlate
r_tmp = wtd.cors(c[, var_name], c[, pred], weight = sqrt(m[, weight_name]))
wtd_res[var_name, pred] = r_tmp
}
}
write_clipboard(wtd_res)
## IQ Islam Int S factor
## 12 17men -0.48 0.36 -0.42
## 12 17women -0.41 0.04 -0.43
## 18 24men -0.48 0.47 -0.36
## 18 24women -0.51 -0.04 -0.53
## 25 44men -0.47 0.18 -0.49
## 25 44women -0.41 -0.03 -0.57
## 45 79men -0.44 0.05 -0.50
## 45 79women -0.37 -0.13 -0.38
## All -0.51 0.12 -0.57
# controlling for income --------------------------------------------------
i = m[c(384:395)] #income crime rates
colnames(i) = c("1g, less than 9.5€",
"1g, 9.5 to 12.5€",
"1g, 12.5 to 20€",
"1g, 20€ to 27.5€",
"1g, above 27.5€",
"1g, total",
"2g, less than 9.5€",
"2g, 9.5 to 12.5€",
"2g, 12.5 to 20€",
"2g, 20€ to 27.5€",
"2g, above 27.5€",
"2g, total"
)
#predictors
i$IQ = c$IQ
i$Islam = c$Islam
i$Int.S = c$Int.S.factor
#correlations
cors = wtd.cors(i)[1:12, 13:15]
write_clipboard(cors)
## IQ Islam Int S
## 1g, less than 9 5€ -0.61 0.07 -0.31
## 1g, 9 5 to 12 5€ -0.76 0.14 -0.41
## 1g, 12 5 to 20€ -0.69 0.28 -0.58
## 1g, 20€ to 27 5€ -0.77 0.52 -0.81
## 1g, above 27 5€ -0.65 0.36 -0.62
## 1g, total -0.66 0.12 -0.48
## 2g, less than 9 5€ -0.81 0.50 -0.45
## 2g, 9 5 to 12 5€ -0.77 0.42 -0.49
## 2g, 12 5 to 20€ -0.72 0.66 -0.40
## 2g, 20€ to 27 5€ -0.80 0.29 -0.48
## 2g, above 27 5€ -0.79 0.31 -0.47
## 2g, total -0.81 0.54 -0.49
pairwiseCount(i)
## 1g, less than 9.5€ 1g, 9.5 to 12.5€ 1g, 12.5 to 20€
## 1g, less than 9.5€ 22 21 22
## 1g, 9.5 to 12.5€ 21 21 21
## 1g, 12.5 to 20€ 22 21 22
## 1g, 20€ to 27.5€ 22 21 22
## 1g, above 27.5€ 22 21 22
## 1g, total 22 21 22
## 2g, less than 9.5€ 22 21 22
## 2g, 9.5 to 12.5€ 22 21 22
## 2g, 12.5 to 20€ 22 21 22
## 2g, 20€ to 27.5€ 22 21 22
## 2g, above 27.5€ 22 21 22
## 2g, total 22 21 22
## IQ 21 20 21
## Islam 20 19 20
## Int.S 16 15 16
## 1g, 20€ to 27.5€ 1g, above 27.5€ 1g, total
## 1g, less than 9.5€ 22 22 22
## 1g, 9.5 to 12.5€ 21 21 21
## 1g, 12.5 to 20€ 22 22 22
## 1g, 20€ to 27.5€ 22 22 22
## 1g, above 27.5€ 22 22 22
## 1g, total 22 22 22
## 2g, less than 9.5€ 22 22 22
## 2g, 9.5 to 12.5€ 22 22 22
## 2g, 12.5 to 20€ 22 22 22
## 2g, 20€ to 27.5€ 22 22 22
## 2g, above 27.5€ 22 22 22
## 2g, total 22 22 22
## IQ 21 21 21
## Islam 20 20 20
## Int.S 16 16 16
## 2g, less than 9.5€ 2g, 9.5 to 12.5€ 2g, 12.5 to 20€
## 1g, less than 9.5€ 22 22 22
## 1g, 9.5 to 12.5€ 21 21 21
## 1g, 12.5 to 20€ 22 22 22
## 1g, 20€ to 27.5€ 22 22 22
## 1g, above 27.5€ 22 22 22
## 1g, total 22 22 22
## 2g, less than 9.5€ 22 22 22
## 2g, 9.5 to 12.5€ 22 22 22
## 2g, 12.5 to 20€ 22 22 22
## 2g, 20€ to 27.5€ 22 22 22
## 2g, above 27.5€ 22 22 22
## 2g, total 22 22 22
## IQ 21 21 21
## Islam 20 20 20
## Int.S 16 16 16
## 2g, 20€ to 27.5€ 2g, above 27.5€ 2g, total IQ Islam
## 1g, less than 9.5€ 22 22 22 21 20
## 1g, 9.5 to 12.5€ 21 21 21 20 19
## 1g, 12.5 to 20€ 22 22 22 21 20
## 1g, 20€ to 27.5€ 22 22 22 21 20
## 1g, above 27.5€ 22 22 22 21 20
## 1g, total 22 22 22 21 20
## 2g, less than 9.5€ 22 22 22 21 20
## 2g, 9.5 to 12.5€ 22 22 22 21 20
## 2g, 12.5 to 20€ 22 22 22 21 20
## 2g, 20€ to 27.5€ 22 22 22 21 20
## 2g, above 27.5€ 22 22 22 21 20
## 2g, total 22 22 22 21 20
## IQ 21 21 21 203 198
## Islam 20 20 20 198 232
## Int.S 16 16 16 141 142
## Int.S
## 1g, less than 9.5€ 16
## 1g, 9.5 to 12.5€ 15
## 1g, 12.5 to 20€ 16
## 1g, 20€ to 27.5€ 16
## 1g, above 27.5€ 16
## 1g, total 16
## 2g, less than 9.5€ 16
## 2g, 9.5 to 12.5€ 16
## 2g, 12.5 to 20€ 16
## 2g, 20€ to 27.5€ 16
## 2g, above 27.5€ 16
## 2g, total 16
## IQ 141
## Islam 142
## Int.S 142
#table output
write_clipboard(i[miss_by_case(i) < 12, ])
## 1g, less than 9 5€ 1g, 9 5 to 12 5€ 1g, 12 5 to 20€ 1g, 20€ to 27 5€
## ANT 7.60 3.50 2.30 1.70
## AUS 0.60 0.50 0.70 0.60
## AUT 1.70 0.80 0.30
## BEL 2.00 1.30 1.20 0.60
## CAN 0.60 0.30 0.50 0.30
## CHN 0.90 0.70 1.40 1.40
## CPV 3.50 3.00 2.30 2.60
## DEU 1.20 0.90 0.90 0.70
## ESP 1.20 0.80 0.80 1.10
## FRA 1.80 1.30 0.30 0.80
## GBR 1.60 0.80 0.60 0.50
## HKG 0.70 0.50 0.60 0.50
## HUN 0.70 0.50 0.40 1.50
## IDN 0.90 0.70 0.50 1.20
## ITA 2.10 0.90 0.90 0.60
## MAR 3.30 2.20 2.30 2.60
## POL 1.40 1.10 1.00 0.60
## SUN 3.90 1.70 0.40 0.50
## SUR 4.50 2.60 2.10 1.60
## TUR 2.10 1.50 1.60 2.00
## USA 1.00 0.80 0.70 0.40
## YUG 2.90 1.50 1.00 0.60
## 1g, above 27 5€ 1g, total 2g, less than 9 5€ 2g, 9 5 to 12 5€
## ANT 1.20 6.40 3.50 2.10
## AUS 0.50 0.60 3.90 0.40
## AUT 1.00 1.30 2.10 0.80
## BEL 0.70 1.60 1.70 0.80
## CAN 0.10 0.40 2.60 2.50
## CHN 1.40 1.00 1.50 0.90
## CPV 2.60 3.30 7.00 5.50
## DEU 0.60 1.10 1.60 1.00
## ESP 1.10 1.00 2.90 1.30
## FRA 0.60 1.30 1.50 1.40
## GBR 0.50 1.10 2.00 1.00
## HKG 0.40 0.60 0.70 1.20
## HUN 1.70 0.80 2.40 1.40
## IDN 0.40 0.80 1.90 1.00
## ITA 0.80 1.70 2.20 1.20
## MAR 2.10 3.20 8.00 4.40
## POL 0.70 1.30 2.10 0.50
## SUN 0.80 3.30 1.80 1.10
## SUR 1.70 3.70 4.40 2.30
## TUR 2.20 2.00 5.20 3.40
## USA 0.30 0.70 1.70 0.80
## YUG 1.40 2.40 3.30 3.00
## 2g, 12 5 to 20€ 2g, 20€ to 27 5€ 2g, above 27 5€ 2g, total IQ
## ANT 1.60 1.80 1.10 2.60 87.00
## AUS 1.70 0.20 0.30 2.10 99.20
## AUT 0.70 0.50 0.70 1.40 99.00
## BEL 0.60 0.50 0.70 1.20 99.30
## CAN 1.30 0.90 0.40 1.90 100.40
## CHN 0.10 0.00 0.20 0.90 105.80
## CPV 2.60 9.20 5.70 6.20 76.00
## DEU 0.80 0.90 0.80 1.30 98.80
## ESP 0.90 1.10 0.30 1.90 96.60
## FRA 0.60 0.30 0.50 1.10 98.10
## GBR 1.20 1.00 1.10 1.50 99.10
## HKG 0.40 0.30 0.30 0.70 105.70
## HUN 1.00 1.00 0.60 1.70 98.10
## IDN 0.80 0.70 0.60 1.20 85.80
## ITA 0.90 0.90 1.10 1.60 96.10
## MAR 4.20 4.10 2.70 7.00 82.40
## POL 0.80 0.20 0.90 1.30 96.10
## SUN 0.50 2.10 2.00 1.40
## SUR 1.60 1.60 1.30 3.20 89.00
## TUR 3.50 4.00 2.80 4.70 89.40
## USA 1.10 0.90 0.70 1.30 97.50
## YUG 1.40 0.90 0.70 2.60 92.33
## Islam Int S
## ANT 0.00
## AUS 0.02 1.56
## AUT 0.06 1.37
## BEL 0.06 1.38
## CAN 0.03 1.44
## CHN 0.02 -0.25
## CPV 0.00
## DEU 0.05 1.46
## ESP 0.02 1.21
## FRA 0.08 1.28
## GBR 0.05 1.38
## HKG 0.01
## HUN 0.00 0.77
## IDN 0.88 -0.30
## ITA 0.03 0.96
## MAR 1.00 -0.37
## POL 0.00 0.84
## SUN
## SUR 0.16
## TUR 0.99 0.15
## USA 0.01 1.43
## YUG
# plots -------------------------------------------------------------------
all_plotter = function (x) {
#model
mod = str_c("All ~ ", x)
#fits
fit = lm(mod, data = c)
fit_wtd = lm(mod, data = c, weights = sqrt(m$DutchPopImmiTotal))
#plot
g = ggplot(c, aes_string(x, "All")) + geom_point(aes(size = sqrt(m$DutchPopImmiTotal))) + geom_text(aes(label = rownames(c)), vjust = 1.5, size = 4) + scale_size(guide = F) + geom_abline(intercept = coef(fit)[1], slope = coef(fit)[2]) + geom_abline(intercept = coef(fit_wtd)[1], slope = coef(fit_wtd)[2], linetype = "dashed")
return(g)
}
all_plotter("IQ")
## Warning: Removed 225 rows containing missing values (geom_point).
## Warning: Removed 225 rows containing missing values (geom_text).
all_plotter("Islam")
## Warning: Removed 226 rows containing missing values (geom_point).
## Warning: Removed 226 rows containing missing values (geom_text).
all_plotter("Int.S.factor")
## Warning: Removed 234 rows containing missing values (geom_point).
## Warning: Removed 234 rows containing missing values (geom_text).
# modeling ----------------------------------------------------------------
#try all models
beta_mat = MOD_APSLM("All", c("IQ", "Islam", "Int.S.factor"), data = c, standardized = T)
##
|
| | 0%
|
|=========== | 17%
|
|====================== | 33%
|
|================================ | 50%
|
|=========================================== | 67%
|
|====================================================== | 83%
|
|=================================================================| 100%
#matrix
View(beta_mat$beta_matrix)
#best model
beta_mat$all_models[[5]] %>% summary()
##
## Call:
## lm(formula = models[model.idx], data = data, weights = .weights)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.67617 -0.31883 -0.05178 0.17563 2.80138
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.1498 0.1186 1.263 0.213
## IQ -0.5185 0.2309 -2.245 0.030 *
## Int.S.factor -0.1460 0.2117 -0.690 0.494
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.7344 on 43 degrees of freedom
## (234 observations deleted due to missingness)
## Multiple R-squared: 0.4329, Adjusted R-squared: 0.4065
## F-statistic: 16.41 on 2 and 43 DF, p-value: 5.058e-06
plot(beta_mat$all_models[[5]])
# Crime by generation -----------------------------------------------------
c2_nomiss = na.omit(c2)
rownames(c2_nomiss) = m[miss_by_case(c2)==0, "Names"]
write_clipboard(c2_nomiss)
## Age12 17 1gen Age18 24 1gen Age12 17 2gen
## Netherlands Antilles 9.20 10.10 4.40
## Australia 1.00 0.50 1.80
## Austria 0.70 3.40 2.90
## Belgium 2.60 3.20 1.80
## Canada 0.90 1.60 1.40
## China 0.90 1.40 0.90
## Cabo Verde 5.50 6.80 5.00
## Germany 1.80 2.10 1.80
## Spain 2.20 2.10 3.10
## France 1.40 2.40 1.70
## United Kingdom 1.80 2.50 2.00
## Hong Kong SAR China 0.00 1.20 1.10
## Hungary 3.50 1.90 2.10
## Indonesia 1.80 1.60 1.70
## Italy 3.90 2.40 2.90
## Morocco 6.20 9.60 6.90
## Poland 2.70 2.40 1.50
## USSR 2.50 6.80 3.60
## Suriname 4.20 7.00 4.40
## Turkey 2.60 3.60 3.10
## United States 1.30 1.20 2.80
## Former Yugoslavia 2.90 5.00 6.00
## Age18 24 2gen
## Netherlands Antilles 5.00
## Australia 3.60
## Austria 3.10
## Belgium 3.20
## Canada 3.40
## China 0.80
## Cabo Verde 6.60
## Germany 3.20
## Spain 3.30
## France 2.20
## United Kingdom 3.00
## Hong Kong SAR China 1.30
## Hungary 5.00
## Indonesia 2.40
## Italy 4.10
## Morocco 10.70
## Poland 2.60
## USSR 2.10
## Suriname 6.50
## Turkey 5.60
## United States 2.30
## Former Yugoslavia 5.30
#means by group
apply(c2, 2, mean, na.rm=TRUE)
## Age12_17_1gen Age18_24_1gen Age12_17_2gen Age18_24_2gen
## 2.709091 3.581818 2.859091 3.877273
write_sessioninfo()
## R version 3.6.1 (2019-07-05)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Linux Mint 19.1
##
## Matrix products: default
## BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] QuantPsyc_1.5 MASS_7.3-51.4 boot_1.3-23
## [4] kirkegaard_2018.05 metafor_2.1-0 Matrix_1.2-17
## [7] psych_1.8.12 magrittr_1.5 assertthat_0.2.1
## [10] weights_1.0 mice_3.6.0 gdata_2.18.0
## [13] Hmisc_4.2-0 Formula_1.2-3 survival_2.44-1.1
## [16] lattice_0.20-38 forcats_0.4.0 stringr_1.4.0
## [19] dplyr_0.8.3 purrr_0.3.2 readr_1.3.1
## [22] tidyr_0.8.3 tibble_2.1.3 ggplot2_3.2.1
## [25] tidyverse_1.2.1 pacman_0.5.1
##
## loaded via a namespace (and not attached):
## [1] httr_1.4.1 jsonlite_1.6 splines_3.6.1
## [4] modelr_0.1.5 gtools_3.8.1 latticeExtra_0.6-28
## [7] cellranger_1.1.0 yaml_2.2.0 pillar_1.4.2
## [10] backports_1.1.4 glue_1.3.1 digest_0.6.20
## [13] RColorBrewer_1.1-2 checkmate_1.9.4 rvest_0.3.4
## [16] minqa_1.2.4 colorspace_1.4-1 plyr_1.8.4
## [19] htmltools_0.3.6 pkgconfig_2.0.2 broom_0.5.2
## [22] haven_2.1.1 scales_1.0.0 lme4_1.1-21
## [25] htmlTable_1.13.1 generics_0.0.2 withr_2.1.2
## [28] pan_1.6 nnet_7.3-12 lazyeval_0.2.2
## [31] mnormt_1.5-5 cli_1.1.0 crayon_1.3.4
## [34] readxl_1.3.1 mitml_0.3-7 evaluate_0.14
## [37] nlme_3.1-141 xml2_1.2.2 foreign_0.8-72
## [40] tools_3.6.1 data.table_1.12.2 hms_0.5.0
## [43] munsell_0.5.0 cluster_2.1.0 compiler_3.6.1
## [46] rlang_0.4.0 grid_3.6.1 nloptr_1.2.1
## [49] rstudioapi_0.10 htmlwidgets_1.3 labeling_0.3
## [52] base64enc_0.1-3 rmarkdown_1.14 gtable_0.3.0
## [55] R6_2.4.0 gridExtra_2.3 lubridate_1.7.4
## [58] knitr_1.24 Rcsdp_0.1.55 zeallot_0.1.0
## [61] jomo_2.6-9 stringi_1.4.3 parallel_3.6.1
## [64] Rcpp_1.0.2 vctrs_0.2.0 rpart_4.1-15
## [67] acepack_1.4.1 tidyselect_0.2.5 xfun_0.8
#data out
write_rds(c, "data/data_for_reuse.rds")