forked from slancast/fiber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactor_and_lda_tutorials.R
62 lines (44 loc) · 1.67 KB
/
factor_and_lda_tutorials.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
#/usr/bin/R
#This will be to test the factor analysis and the linear descriminant analysis
#First up the factor analysis
#From https://www.promptcloud.com/blog/exploratory-factor-analysis-in-r/
data <- read.csv("/Users/SLancaster/Downloads/EFA.csv",header=TRUE)
head(data)
library('psych')
#We’ll be using `Psych` package’s `fa.parallel` function to execute parallel analysis. Here we specify the data frame and factor method (`minres` in our case). Run the following to find acceptable number of factors and generate the `scree plot`:
parallel <- fa.parallel(data, fm = 'minres', fa = 'fa')
#Now that we’ve arrived at probable number number of factors, let’s start off with 3 as the number of factors. In order to perform factor analysis, we’ll use `psych` package’s `fa()function.
threefactor <- fa(data,nfactors = 3,rotate = "oblimin",fm="minres")
print(threefactor)
fa.diagram(threefactor)
#Now for LDA
#From
#Already problems in the frst line, the website calls the data wdbc.csv but it's rather called
#wdbc.data. Makes it very difficult to figure out what is going on
library(MASS)
data(iris)
r <- lda(formula = Species ~ .,
data = iris,
prior = c(1,1,1)/3)
r$prior
r$counts
r$means
r$scaling
r$svd
prop = r$svd^2/sum(r$svd^2)
r2 <- lda(formula = Species ~ .,
data = iris,
prior = c(1,1,1)/3,
CV = TRUE)
head(r2$class)
head(r2$posterior, 3)
train <- sample(1:150, 75)
r3 <- lda(Species ~ ., # training model
iris,
prior = c(1,1,1)/3,
subset = train)
plda = predict(object = r, # predictions
newdata = iris[-train, ])
head(plda$class)
head(plda$posterior, 3)
head(plda$x, 3)