Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Deleetdk/b9182f088ef8a1eb34ffb0e95e6f1026 to your computer and use it in GitHub Desktop.
Save Deleetdk/b9182f088ef8a1eb34ffb0e95e6f1026 to your computer and use it in GitHub Desktop.
### re-analyze Beaver and Wright's correlation matrix
library(magrittr)
# load data ---------------------------------------------------------------
#load matrix
d_bw = structure(list(IQ = c(1, -0.4, -0.44, -0.29, -0.51, -0.58, -0.54, -0.52, -0.53, -0.49, -0.43, -0.56, -0.27, -0.46, -0.38), Property.crime = c(-0.4, 1, 0.95, 0.97, 0.76, 0.77, 0.69, 0.72, 0.93, 0.32, 0.44, 0.56, 0.12, 0.13, 0.16), Burglary = c(-0.44, 0.95, 1, 0.88, 0.7, 0.79, 0.68, 0.75, 0.92, 0.46, 0.56, 0.64, 0.25, 0.27, 0.27), Larceny = c(-0.29, 0.97, 0.88, 1, 0.59, 0.64, 0.53, 0.62, 0.83, 0.27, 0.42, 0.5, 0.13, 0.05, 0.09), Motor.vehicle.theft = c(-0.51, 0.76, 0.7, 0.59, 1, 0.84, 0.88, 0.69, 0.89, 0.19, 0.18, 0.44, -0.09, 0.13, 0.14), Violent.crime = c(-0.58, 0.77, 0.79, 0.64, 0.84, 1, 0.91, 0.93, 0.94, 0.57, 0.6, 0.76, 0.23, 0.46, 0.39), Robbery = c(-0.54, 0.69, 0.68, 0.53, 0.88, 0.91, 1, 0.7, 0.87, 0.36, 0.38, 0.62, 0.03, 0.28, 0.23), Aggravated.assault = c(-0.52, 0.72, 0.75, 0.62, 0.69, 0.93, 0.7, 1, 0.86, 0.66, 0.69, 0.76, 0.36, 0.55, 0.46), Composite.crime = c(-0.53, 0.93, 0.92, 0.83, 0.89, 0.94, 0.87, 0.86, 1, 0.44, 0.51, 0.68, 0.15, 0.29, 0.27), Concentrated.dis. = c(-0.49, 0.32, 0.46, 0.27, 0.19, 0.57, 0.36, 0.66, 0.44, 1, 0.82, 0.84, 0.83, 0.92, 0.84), African.American = c(-0.43, 0.44, 0.56, 0.42, 0.18, 0.6, 0.38, 0.69, 0.51, 0.82, 1, 0.85, 0.56, 0.63, 0.49), Female.headed.house = c(-0.56, 0.56, 0.64, 0.5, 0.44, 0.76, 0.62, 0.76, 0.68, 0.84, 0.85, 1, 0.49, 0.7, 0.56), Less.than..15.000 = c(-0.27, 0.12, 0.25, 0.13, -0.09, 0.23, 0.03, 0.36, 0.15, 0.83, 0.56, 0.49, 1, 0.76, 0.73), Public.assistance = c(-0.46, 0.13, 0.27, 0.05, 0.13, 0.46, 0.28, 0.55, 0.29, 0.92, 0.63, 0.7, 0.76, 1, 0.8), Unemployment.rate = c(-0.38, 0.16, 0.27, 0.09, 0.14, 0.39, 0.23, 0.46, 0.27, 0.84, 0.49, 0.56, 0.73, 0.8, 1)), .Names = c("IQ", "Property.crime", "Burglary", "Larceny", "Motor.vehicle.theft", "Violent.crime", "Robbery", "Aggravated.assault", "Composite.crime", "Concentrated.dis.", "African.American", "Female.headed.house", "Less.than..15.000", "Public.assistance", "Unemployment.rate" ), class = "data.frame", row.names = c("IQ", "Property.crime", "Burglary", "Larceny", "Motor.vehicle.theft", "Violent.crime", "Robbery", "Aggravated.assault", "Composite.crime", "Concentrated.dis.", "African.American", "Female.headed.house", "Less.than..15.000", "Public.assistance", "Unemployment.rate"))
# factor analyze ----------------------------------------------------------
fa_bw1 = fa(d_bw[c(3:5, 7:8, 12:15), c(3:5, 7:8, 12:15)] %>% as.matrix())
fa_bw2 = fa(d_bw[c(2, 6, 12:15), c(2, 6, 12:15)] %>% as.matrix())
fa_bw3 = fa(d_bw[c(9, 12:15), c(9, 12:15)] %>% as.matrix())
m_bw <- as.matrix(d_bw)
# make keys ----------------------------------------------------------
keys.list = list(
IQ = c("IQ"),
AA = "African.American",
S1 = c("Burglary", "Larceny", "Motor.vehicle.theft", "Robbery", "Aggravated.assault", "Female.headed.house", "Less.than..15.000", "Public.assistance", "Unemployment.rate"),
S2 = c("Property.crime", "Aggravated.assault", "Female.headed.house", "Less.than..15.000", "Public.assistance", "Unemployment.rate"),
S3 = c("Composite.crime", "Female.headed.house", "Less.than..15.000", "Public.assistance", "Unemployment.rate"))
keys5 <- make.keys(d_bw, keys.list)
# get scores --------------------------------------------------------------
# score the items using the keys
x <- scoreOverlap(keys5, m_bw, impute="none")
# see the correlations between the items and loadings based scales
print(x, short=FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment