-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
201 lines (157 loc) · 5.15 KB
/
app.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(ggplot2)
library(MFAg)
library(shiny)
library(mfaR)
# Define UI for application that draws a histogram
ui <- shinyUI(fluidPage(
# Application title
titlePanel("MFA for Wine Tasting"),
# Sidebars for input
sidebarLayout(
sidebarPanel(
selectInput("select", label = h3("Plot Options"),
choices = list("Eigenvalue Barplot" = 1, "Common factor scores" = 2,
"Partial factors scores." = 3, "Loadings" = 4),
selected = 1),
sliderInput(inputId = "table",
label = "Which Wine Critic?",
min = 1,
max = 10,
value = 1)
),
# Show plots
mainPanel(
plotOutput("plot")
)
)
))
# Define server
server <- shinyServer(function(input, output) {
wines <- read.csv("wines.csv")
# Add row names
row.names(wines) <- wines[,1]
# Separate the grand table into individual tables (create the set list)
col_ind = grep("V2", colnames(wines))
sets = list()
for (i in 1:(length(col_ind))){
# First 9 tables
if (i < 10){
sets[[i]]= (col_ind[i]-1):(col_ind[i+1]-2)
}
# Last table
else{
sets[[i]]= (col_ind[i]-1):(col_ind[i]+2)
}
}
col_ind = grep("V15", colnames(wines))
colnames(wines)[col_ind] <- "Peach"
col_ind = grep("V14", colnames(wines))
colnames(wines)[col_ind] <- "Grass"
col_ind = grep("V13", colnames(wines))
colnames(wines)[col_ind] <- "Melon"
col_ind = grep("V12", colnames(wines))
colnames(wines)[col_ind] <- "Hay"
col_ind = grep("V11", colnames(wines))
colnames(wines)[col_ind] <- "Vegetal"
col_ind = grep("V10", colnames(wines))
colnames(wines)[col_ind] <- "Flinty"
col_ind = grep("V9", colnames(wines))
colnames(wines)[col_ind] <- "Grassy"
col_ind = grep("V8", colnames(wines))
colnames(wines)[col_ind] <- "Leafy"
col_ind = grep("V7", colnames(wines))
colnames(wines)[col_ind] <- "Tropical"
col_ind = grep("V6", colnames(wines))
colnames(wines)[col_ind] <- "Citrus"
col_ind = grep("V5", colnames(wines))
colnames(wines)[col_ind] <- "Smoky"
col_ind = grep("V4", colnames(wines))
colnames(wines)[col_ind] <- "Mineral"
col_ind = grep("V3", colnames(wines))
colnames(wines)[col_ind] <- "Green Pepper"
col_ind = grep("V2", colnames(wines))
colnames(wines)[col_ind] <- "Passion Fruit"
col_ind = grep("V1", colnames(wines))
colnames(wines)[col_ind] <- "Cat Pee"
# Construct the object first
wine <- mfa_const(data = wines, sets = sets, ncomps = 11)
# Extract features from the object
eigenv = wine@eigenvalues
cfs = wine@cfs
pfs = wine@pfs
Q = wine@mol
plot_pfs_shiny <- function(dimension1,
dimension2,
rownames_vec = as.character(1:length(dimension1))){
dat <- data.frame(x = dimension1, y = dimension2, label = rownames_vec)
ggplot(dat) +
geom_point(aes(x,y)) +
labs(title="Partial Factor Scores", x ="1", y = "2") +
geom_text(data = dat, aes(x,y, label = rownames_vec), vjust = -0.5) +
xlim(-2,2) +
ylim(-2,2)
}
plot_compromise_shiny <- function(dimension1,
dimension2,
rownames_vec = as.character(1:length(dimension1))){
dat <- data.frame(x = dimension1, y = dimension2, label = rownames_vec)
colnames(dat) <- c("x","y")
ggplot(dat) +
geom_point(aes(x,y)) +
labs(title="Compromise of the tables", x ="1", y = "2") +
ylim(-1, 1) +
geom_text(data = dat, aes(x,y, label = rownames_vec), vjust = -2)
}
plot_vload <- function(dimension1,
dimension2,
rownames_vec = as.character(1:length(dimension1))){
dat <- data.frame(x = dimension1, y = dimension2, label = rownames_vec)
ggplot(dat) +
geom_point(aes(x,y)) +
labs(title="Variable loadings", x ="1", y = "2") +
geom_text(data = dat, aes(x,y, label = rownames_vec), vjust = -0.5) +
xlim(-0.5,0.5) +
ylim(-0.6,0.6)
}
# Make the plots
pt <- reactive({
input$select
input$table
eigenv_name = c(1,2,3,4,5,6,7,8,9,10,11)
# Eigenvalue?
if (input$select == 1){
return(barplot(eigenv,
names.arg= eigenv_name,
ylab="Magnitude of Eigenvalues",
xlab="Number of component"))
}
# Compromise?
else if(input$select == 2){
cfs_dim1 = cfs[,1]
cfs_dim2 = cfs[,2]
return(plot_compromise_shiny(cfs_dim1,cfs_dim2,row.names(wines)))
}
# pfs?
else if(input$select == 3){
return(plot_pfs_shiny(pfs[[input$table]][,1], pfs[[input$table]][,2], rownames(wines)))
}
# Loading?
else if(input$select == 4){
return(plot_vload( Q[[input$table]][,1], Q[[input$table]][,2], colnames(wines)[sets[[input$table]]]))
}
else{
return(NULL)
}
})
output$plot = renderPlot({pt()})
})
# Run the application
shinyApp(ui = ui, server = server)