-
Notifications
You must be signed in to change notification settings - Fork 0
/
RinA CH14 Code.txt
executable file
·104 lines (75 loc) · 2.98 KB
/
RinA CH14 Code.txt
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
#------------------------------------------- #
# R in Action: Chapter 14 #
# requires that the psych has been installed #
# install.packages(c('psych')) #
#------------------------------------------- #
# pause on each graph
par(ask = TRUE)
set.seed(1234)
# determine number of components in USJudgeRatings
library(psych)
fa.parallel(USJudgeRatings[, -1], fa = "pc", ntrials = 100,
show.legend = FALSE, main = "Scree plot with parallel analysis")
# Listing 14.1 - Principal components anaalysis of US Judge Ratings
pc <- principal(USJudgeRatings[, -1], nfactors = 1, score = TRUE)
pc
# determine number of components in Harman data
library(psych)
fa.parallel(Harman23.cor$cov, n.obs=302, fa="pc", ntrials=100,
show.legend=FALSE, main="Scree plot with parallel analysis")
# Listing 14.2 Pricnicapl components analysis of body measurements
library(psych)
fa.parallel(Harman23.cor$cov, n.obs = 302, fa = "pc",
ntrials = 100, show.legend = FALSE,
main = "Scree plot with parallel analysis")
pc <- principal(Harman23.cor$cov, nfactors = 2, rotate = "none")
pc
# Listing 14.3 Principal components analysis with varimax rotation
rc <- principal(Harman23.cor$cov, nfactors = 2, rotate = "varimax")
rc
# Listing 14.4 - Obtaining component scores from raw data
library(psych)
pc <- principal(USJudgeRatings[,-1], nfactors=1, score=TRUE)
head(pc$scores)
# correlation between PC score and number of lawyer contacts
cor(USJudgeRatings$CONT, pc$score)
# Listing 14.5 - Obtaining principal component scroing coefficients
library(psych)
rc <- principal(Harman23.cor$cov, nfactors=2, rotate="varimax")
round(unclass(rc$weights), 2)
# Exploratory factor analysis of ability.cov data
options(digits = 2)
library(psych)
covariances <- ability.cov$cov
# convert covariances to correlations
correlations <- cov2cor(covariances)
correlations
# determine number of factors to extract
fa.parallel(correlations, n.obs = 112, fa = "both",
ntrials = 100, main = "Scree plots with parallel analysis")
# Listing 14.6 - Principal axis factoring without rotation
fa <- fa(correlations, nfactors = 2, rotate = "none", fm = "pa")
fa
# Listing 14.7 - Factor extraction with orthogonal rotation
fa.varimax <- fa(correlations, nfactors = 2, rotate = "varimax",
fm = "pa")
fa.varimax
# Listing 14.8 - Factor extraction with oblique rotation
fa.promax <- fa(correlations, nfactors = 2, rotate = "promax",
fm = "pa")
fa.promax
# Calculate factor loading matrix
fsm <- function(oblique) {
if (class(oblique)[2]=="fa" & is.null(oblique$Phi)) {
warning("Object doesn't look like oblique EFA")
} else {
P <- unclass(oblique$loading)
F <- P %*% oblique$Phi
colnames(F) <- c("PA1", "PA2")
return(F)
}
}
fsm(fa.promax)
# plot factor solution
factor.plot(fa.promax, labels = rownames(fa.promax$loadings))
fa.diagram(fa.promax, simple = FALSE)