-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqualmod_app.R
289 lines (243 loc) · 8.28 KB
/
qualmod_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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#
# 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(shiny)
#library(data.table); library(Rpath)
library(tidyverse)
library(LoopAnalyst)
library(permute)
library(igraph)
#library(ggrepel)
#-------------------------------------------------------------------------------
#User created functions
#-------------------------------------------------------------------------------
# Using matrix inverse
adjoint3 <- function(A) det(A)*solve(A)
#library(googlesheets)
url <- "https://docs.google.com/spreadsheets/d/1-ukFR9B6idnCR_o2G4BXd25AD1ucAl-OPkjBcuHW3UA/edit#gid=1853171097"
lookup <- data.frame(response = c("Positive","Negative","None"),
value = c(1,-1,0))
matmatch <- data.frame(qnum = 1:11,
i = c(3,4,6,2,1,1,2,6,3,4,5),
j = c(6,5,5,6,6,7,7,7,7,7,7))
#gs_ls()
#be <- gs_title("GCRL_model")
#lab <- gs_ws_ls(be)
#mydat <- as_tibble(gs_read(ss=be, ws = lab[1], skip=0))
mydat <- googlesheets4::read_sheet(url)
nobs <- nrow(mydat)
nudat <- mydat %>%
mutate(id = 1:nrow(mydat)) %>%
select(id,everything()) %>%
gather(key = "question", value = "response", 3:(ncol(mydat)+1)) %>%
mutate(qnum = rep(1:11, each=nobs)) %>%
left_join(lookup) %>%
left_join(matmatch)
B <- matrix(c(-1,-1, 0, 0, 0, 0, 0,
1,-1,-1, 0, 0, 0, 0,
0, 1,-1,-1,-1, 0, 0,
0, 0, 1,-1, 0, 0, 0,
0, 0, 1, 0,-1, 0, 0,
0, 0, 0, 0, 0,-1, 0,
0, 0, 0, 0, 0, 0,-1), byrow=TRUE, nrow = 7)
Wstore <- matrix(NA,nrow=7,ncol=nobs)
Astore <- matrix(NA,nrow=7,ncol=nobs)
evaldat <- nudat
for (k in unique(nudat$id)) {
A <- B
temp <- filter(nudat,id==k)
for (irow in 1:nrow(temp))
A[temp$i[irow],temp$j[irow]] <- temp$value[irow]
#}
adj_A <- adjoint3(A)
adj_A
Tmat <- LoopAnalyst::make.T(A,status=TRUE)
#system.time(Tmat <- LoopAnalyst::make.T(A,status=TRUE))
Wmat <- abs(adj_A)/Tmat
Wmat
adj_AA <- adj_A
#image showing sign of adjoint and weights
colfunc <- colorRampPalette(c("white", "steelblue"))
image(1:7,1:7,t(Wmat[7:1,]),col = colfunc(7))
text(1,0,"+",col="white",cex=2)
pick <- which(adj_AA>0)
x <- ceiling(pick/7)
y <- 8-pick%%7
y[y==8] <- 1
text(x,y,"+",col="white",cex=1.5)
pick <- which(adj_AA<0)
x <- ceiling(pick/7)
y <- 8-pick%%7
y[y==8] <- 1
text(x,y,"-",col="white",cex=1.5)
Wstore[,k] <- rev(Wmat[,7])
Astore[,k] <- rev(adj_AA[,7])
}
#group_pic
#image showing sign of adjoint and weights
colfunc <- colorRampPalette(c("white", "steelblue"))
image(1:nobs,1:7,t(Wstore),col = colfunc(5))
#text(1,0,"+",col="white",cex=2)
pick <- which(Astore>0)
x <- ceiling(pick/7)
y <- 8-pick%%7
y[y==8] <- 1
text(x,y,"+",col="white",cex=1.5)
pick <- which(Astore<0)
x <- ceiling(pick/7)
y <- 8-pick%%7
y[y==8] <- 1
text(x,y,"-",col="white",cex=1.5)
g1 <- graph_from_adjacency_matrix( abs(t(A)) , diag = FALSE)
V(g1)$size = 20
plot(g1,edge.arrow.size=.4)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("GCRL: Qualitative Network Modeling"),
mainPanel(
# Output: Tabset w/ plot, summary, and table ----
tabsetPanel(type = "tabs",
tabPanel("Networks", plotOutput("networkplot")),
#tabPanel("Perturbations", plotOutput("perplot")),
tabPanel("Evaluation", plotOutput("evalplot"))#,
# tabPanel("Summary", verbatimTextOutput("summary")),
#tabPanel("Summary Table", tableOutput("table"))
)
#plotOutput("EwEplot")
)
# )
)
# Define server logic
server <- function(input, output) {
output$networkplot <- renderPlot({
if (length(unique(nudat$id))>2)
nudat <- filter(nudat,id>2)
pick <- sample(unique(nudat$id),size = min(4,length(unique(nudat$id))), replace=FALSE)
nudat <- filter(nudat, id %in% pick)
nobs <- length(unique(nudat$id))
#Wstore <- matrix(NA,nrow=7,ncol=nobs)
#Astore <- matrix(NA,nrow=7,ncol=nobs)
k <- unique(nudat$id)[1]
A <- B
temp <- filter(nudat,id==k)
for (irow in 1:nrow(temp))
A[temp$i[irow],temp$j[irow]] <- temp$value[irow]
g1 <- graph_from_adjacency_matrix( abs(t(A)) , diag = FALSE)
V(g1)$size = 20
#plot(g1,edge.arrow.size=.4)
k <- unique(nudat$id)[2]
A <- B
temp <- filter(nudat,id==k)
for (irow in 1:nrow(temp))
A[temp$i[irow],temp$j[irow]] <- temp$value[irow]
g2 <- graph_from_adjacency_matrix( abs(t(A)) , diag = FALSE)
V(g2)$size = 20
#plot(g1,edge.arrow.size=.4)
if (nobs>2) {
k <- unique(nudat$id)[3]
A <- B
temp <- filter(nudat,id==k)
for (irow in 1:nrow(temp))
A[temp$i[irow],temp$j[irow]] <- temp$value[irow]
g3 <- graph_from_adjacency_matrix( abs(t(A)) , diag = FALSE)
V(g3)$size = 20
}
#plot(g1,edge.arrow.size=.4)
if (nobs>3) {
k <- unique(nudat$id)[4]
A <- B
temp <- filter(nudat,id==k)
for (irow in 1:nrow(temp))
A[temp$i[irow],temp$j[irow]] <- temp$value[irow]
g4 <- graph_from_adjacency_matrix( abs(t(A)) , diag = FALSE)
V(g4)$size = 20
}
#plot(g1,edge.arrow.size=.4)
par(mfrow=c(2,2), mar=c(0,0,0,0))
plot(g1,edge.arrow.size=.4)
plot(g2,edge.arrow.size=.4)
if (nobs >2) plot(g3,edge.arrow.size=.4)
if (nobs >3) plot(g4,edge.arrow.size=.4)
})
output$evalplot <- renderPlot({
Wstore <- matrix(NA,nrow=7,ncol=nobs)
Astore <- matrix(NA,nrow=7,ncol=nobs)
if (length(unique(evaldat$id))>2)
nudat <- filter(evaldat,id>2)
pick <- sample(unique(evaldat$id),size = max(5,length(unique(nudat$id))), replace=FALSE)
nudat <- filter(evaldat, id %in% pick)
nobs <- length(unique(nudat$id))
Wstore <- matrix(NA,nrow=7,ncol=nobs)
Astore <- matrix(NA,nrow=7,ncol=nobs)
for (k in unique(nudat$id)) {
A <- B
temp <- filter(nudat,id==k)
for (irow in 1:nrow(temp))
A[temp$i[irow],temp$j[irow]] <- temp$value[irow]
#}
adj_A <- adjoint3(A)
adj_A
Tmat <- LoopAnalyst::make.T(A,status=TRUE)
#system.time(Tmat <- LoopAnalyst::make.T(A,status=TRUE))
Wmat <- abs(adj_A)/Tmat
Wmat
adj_AA <- adj_A
#image showing sign of adjoint and weights
#colfunc <- colorRampPalette(c("white", "steelblue"))
#image(1:7,1:7,t(Wmat[7:1,]),col = colfunc(7))
#text(1,0,"+",col="white",cex=2)
#pick <- which(adj_AA>0)
#x <- ceiling(pick/7)
#y <- 8-pick%%7
#y[y==8] <- 1
#text(x,y,"+",col="white",cex=1.5)
#pick <- which(adj_AA<0)
#x <- ceiling(pick/7)
#y <- 8-pick%%7
#y[y==8] <- 1
#text(x,y,"-",col="white",cex=1.5)
Wstore[,which(unique(nudat$id)==k)] <- rev(Wmat[,7])
Astore[,which(unique(nudat$id)==k)] <- rev(adj_AA[,7])
}
labs <- c("Manager",
"Habitat",
"Fish",
"Seabirds",
"Fish",
"ZP",
"PP")
#group_pic
#image showing sign of adjoint and weights
colfunc <- colorRampPalette(c("white", "steelblue"))
par(mar=c(1,5,4,3),las=0)
image(1:nobs,1:7,t(Wstore),col = colfunc(5),
xlab = "",
ylab = "", axes=F)
box()
#axis(1,labels=rep("",nobs),at=seq(0.5,nobs+0.5,1),tcl=-0.2)
par(las=2)
axis(2,labels=labs,at=seq(1,7,1),tcl=-0.2,cex=0.8)
par(las=0)
axis(3,labels=1:nobs,at=seq(1,nobs),tcl=-0.2,cex=0.8)
mtext(side=3,"Model",line = 3)
#text(1,0,"+",col="white",cex=2)
pick <- which(Astore>0)
x <- ceiling(pick/7)
y <- 8-pick%%7
y[y==8] <- 1
text(x,y,"+",col="white",cex=1.5)
pick <- which(Astore<0)
x <- ceiling(pick/7)
y <- 8-pick%%7
y[y==8] <- 1
text(x,y,"-",col="white",cex=1.5)
})
}
# Run the application
shinyApp(ui = ui, server = server)