library(car)
library(stargazer)
library(survey)
library(ggplot2)
library(pander)
library(knitr)
library(haven)
dat<-read_xpt("C:/Users/Monica/Documents/CSparks_StatsII/CNTY05.xpt")
My idea for a latent construct is “powerlessness” which can be defined as lacking control of a situation or significant aspects of one’s life.
nams<-names(dat)
head(nams, n=10)
## [1] "_CNTYNAM" "_STATE" "FMONTH" "IDATE" "IMONTH" "IDAY"
## [7] "IYEAR" "INTVID" "DISPCODE" "SEQNO"
myvariables<-tolower(names(dat))
names(dat)<-myvariables
Recode for poor mental health
dat$poormenhlth<-recode (dat$menthlth,recodes= "88=0; 77=NA; 99=NA")
Recode for intimate partner threat
dat$threat<-recode (dat$ipvthrat, recodes = "7:9=NA; 1=1; 2=0")
Recode for marital status
dat$marst<-recode(dat$marital, recodes="1='married'; 2='divorced'; 3='widowed'; 4='separated'; 5='nm';6='cohab'; else=NA", as.factor.result=T)
dat$separated<-ifelse(dat$marst=='separated', 1, 0)
dat$divorced<-ifelse(dat$marst=='divorced', 1, 0)
Recode for education level
dat$educ<-recode(dat$educa,
recodes="1:3='LessHS'; 4:6='HSGradColl'; 9=NA",
as.factor.result=T)
dat$LessHS<-ifelse(dat$educ=='LessHS', 1, 0)
dat$HSGradColl<-ifelse(dat$educ=='HSGradColl', 1, 0)
Recode for employment
dat$myemploy<-recode(dat$employ, recodes="1:2='Employed'; 3:4='Unemployed'; 5:8= 'NoLaborForce'; else=NA", as.factor.result=T)
dat$unemployed<-ifelse(dat$myemploy=='Unemployed', 1, 0)
Recode for strata and weight
dat$strata<-dat$`_ststr`
dat$weight<-dat$`_cntywt`
Age cut into invervals
dat$agec<-cut(dat$age, breaks=c(0,24,39,59,79,99))
Analysis, screeplot and summary statistics, histogram
pcdat.pc<-prcomp(~poormenhlth+threat+separated+LessHS+unemployed, data=dat, center=T, scale=T, retx=T)
screeplot(pcdat.pc, type = "l", main = "Scree Plot")
abline(h=1)

summary(pcdat.pc)
## Importance of components:
## PC1 PC2 PC3 PC4 PC5
## Standard deviation 1.131 1.0058 0.9848 0.9563 0.9077
## Proportion of Variance 0.256 0.2023 0.1940 0.1829 0.1648
## Cumulative Proportion 0.256 0.4584 0.6523 0.8352 1.0000
pcdat.pc$rotation
## PC1 PC2 PC3 PC4 PC5
## poormenhlth 0.5934206 -0.11049397 0.11620104 0.3513471 -0.70618388
## threat 0.5086709 -0.52896877 0.07546508 0.2272522 0.63569443
## separated 0.3737526 -0.02229655 -0.79118926 -0.4807757 -0.05182793
## LessHS 0.2908118 0.79472000 -0.15518504 0.4123003 0.29962421
## unemployed 0.4060092 0.27551064 0.57509956 -0.6509799 0.06881950
The loadings for PC1 demonstrate positive associations with everything, thus illustrating this component as an index for my latent variable “powerlessness.”
For PC2, the two loading variables with large coefficients are threat and LessHS.
Plot the first 2 components
hist(pcdat.pc$x[,1])

hist(pcdat.pc$x[,2])

Calculate the correlation
cor(pcdat.pc$x[,1:2])
## PC1 PC2
## PC1 1.000000e+00 1.044117e-13
## PC2 1.044117e-13 1.000000e+00
scores<-data.frame(pcdat.pc$x)
scores$name<-rownames(pcdat.pc$x)
dat$name<-rownames(dat)
dat<-merge(dat, scores, by.x="name", by.y="name", all.x=F)
tail(names(dat), 20)
## [1] "adjcnty" "_cntywt" "poormenhlth" "threat" "marst"
## [6] "separated" "divorced" "educ" "LessHS" "HSGradColl"
## [11] "myemploy" "unemployed" "strata" "weight" "agec"
## [16] "PC1" "PC2" "PC3" "PC4" "PC5"
Examine correlation among variables
round(cor(dat[,c("poormenhlth", "threat", "separated", "LessHS", "unemployed")], method= "spearman"), 3)
## poormenhlth threat separated LessHS unemployed
## poormenhlth 1.000 0.187 0.058 0.035 0.075
## threat 0.187 1.000 0.070 -0.005 0.060
## separated 0.058 0.070 1.000 0.046 0.030
## LessHS 0.035 -0.005 0.046 1.000 0.058
## unemployed 0.075 0.060 0.030 0.058 1.000
Examine correlations among original variables and components
round(cor(dat[,c("poormenhlth", "threat", "separated", "LessHS", "unemployed", "PC1", "PC2")], method= "spearman"), 3)
## poormenhlth threat separated LessHS unemployed PC1 PC2
## poormenhlth 1.000 0.187 0.058 0.035 0.075 0.701 -0.567
## threat 0.187 1.000 0.070 -0.005 0.060 0.578 -0.557
## separated 0.058 0.070 1.000 0.046 0.030 0.249 -0.114
## LessHS 0.035 -0.005 0.046 1.000 0.058 0.333 0.508
## unemployed 0.075 0.060 0.030 0.058 1.000 0.318 0.234
## PC1 0.701 0.578 0.249 0.333 0.318 1.000 -0.480
## PC2 -0.567 -0.557 -0.114 0.508 0.234 -0.480 1.000