forked from DaliODalmas/MachineLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMCA.Rmd
83 lines (61 loc) · 1.69 KB
/
MCA.Rmd
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
---
title: "MCA"
author: "Dalmas Otieno"
date: "February 12, 2018"
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(FactoMineR)
library(ggplot2)
data(tea)
View(tea)
```
```{r}
newtea=tea[,c("Tea","How","how","sugar","where","always")]
head(newtea)
```
```{r}
g<-function(x){
nlevels(as.factor(x))
}
cuts<-apply(newtea,2,g)
#cuts function(x) nlevels(as.factor(x))
cuts
```
##mca
```{r}
mca1<-MCA(newtea,graph = FALSE)
mca1
```
```{r}
# data frame with variable coordinates
mca1_vars_df = data.frame(mca1$var$coord, Variable = rep(names(cuts), cuts))
#rep(names(cuts), cuts)
#View(mca1_vars_df)
# data frame with observation coordinates
mca1_obs_df = data.frame(mca1$ind$coord)
#View(mca1_obs_df)
# plot of variable categories
ggplot(data=mca1_vars_df,
aes(x = Dim.1, y = Dim.2, label = rownames(mca1_vars_df))) +
geom_hline(yintercept = 0, colour = "gray70") +
geom_vline(xintercept = 0, colour = "gray70") +
geom_text(aes(colour=Variable)) +
ggtitle("MCA plot of variables using R package FactoMineR")
```
```{r}
# MCA plot of observations and categories
ggplot(data = mca1_obs_df, aes(x = Dim.1, y = Dim.2)) +
geom_hline(yintercept = 0, colour = "gray70") +
geom_vline(xintercept = 0, colour = "gray70") +
geom_point(colour = "gray50", alpha = 0.7) +
geom_density2d(colour = "gray80") +
geom_text(data = mca1_vars_df,
aes(x = Dim.1, y = Dim.2,
label = rownames(mca1_vars_df), colour = Variable)) +
ggtitle("MCA plot of variables using R package FactoMineR") +
scale_colour_discrete(name = "Variable")
```