-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanálise TCC.R
414 lines (273 loc) · 9.94 KB
/
análise TCC.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
setwd("D:/")
load("dataset completo.Rda")
pos=which(dataset$Endereco.do.PED=="AVE PADRE JOSE MAURICIO 46 ")
dataset$Latitude[pos]=-19.94964
dataset$Longitude[pos]=-43.99045
pos=which(dataset$Endereco.do.PED=="RUA FORMIGA 294 ")
dataset$Latitude[pos]=-19.90432
dataset$Longitude[pos]=-43.94531
pos=which(dataset$Endereco.do.PED=="AVE BERNARDO MONTEIRO 390 ")
dataset$Latitude[pos]=-19.92130
dataset$Longitude[pos]=-43.92722
dias=union(as.character(dataset$Dia.Semana),as.character(dataset$Dia.Semana))
rotas=intersect(dataset$Rota,dataset$Rota)
df=subset(dataset,Dia.Semana==dias[1]) ##para considerar apenas um dos dias
DF=df
df=subset(dataset,Rota==rotas[1])
linha=intersect(as.character(df$Linha.SubLinha),as.character(df$Linha.SubLinha))
d=data.frame(de="a",para="b")
peso=c()
for(onibus in linha){
df1=subset(df,Linha.SubLinha==onibus)
periodo=nrow(df1)/max(df1$Sequencia.do.PED)
v=df1$Endereco.do.PED[1:max(df1$Sequencia.do.PED)]
d=rbind(d,data.frame(de=v[1:(length(v)-1)],para=v[2:length(v)]))
}
d=d[2:nrow(d),]
d=na.omit(d)
g=graph.data.frame(d,directed=TRUE)
g=simplify(g)
elos=get.edgelist(g)
peso=c()
for(rua in elos[,1]){
peso=c(peso,sum(df$Saldo[which(df$Endereco.do.PED==rua)]))
}
E(g)$weight=peso
g1=g
df=subset(dataset,Rota==rotas[2])
linha=intersect(as.character(df$Linha.SubLinha),as.character(df$Linha.SubLinha))
d=data.frame(de="a",para="b")
peso=c()
for(onibus in linha){
df1=subset(df,Linha.SubLinha==onibus)
periodo=nrow(df1)/max(df1$Sequencia.do.PED)
v=df1$Endereco.do.PED[1:max(df1$Sequencia.do.PED)]
d=rbind(d,data.frame(de=v[1:(length(v)-1)],para=v[2:length(v)]))
}
d=d[2:nrow(d),]
d=na.omit(d)
g=graph.data.frame(d,directed=TRUE)
g=simplify(g)
elos=get.edgelist(g)
peso=c()
for(rua in elos[,1]){
peso=c(peso,sum(df$Saldo[which(df$Endereco.do.PED==rua)]))
}
E(g)$weight=peso
g2=g
df=subset(dataset,Rota==rotas[3])
linha=intersect(as.character(df$Linha.SubLinha),as.character(df$Linha.SubLinha))
d=data.frame(de="a",para="b")
peso=c()
for(onibus in linha){
df1=subset(df,Linha.SubLinha==onibus)
periodo=nrow(df1)/max(df1$Sequencia.do.PED)
v=df1$Endereco.do.PED[1:max(df1$Sequencia.do.PED)]
d=rbind(d,data.frame(de=v[1:(length(v)-1)],para=v[2:length(v)]))
}
d=d[2:nrow(d),]
d=na.omit(d)
g=graph.data.frame(d,directed=TRUE)
g=simplify(g)
elos=get.edgelist(g)
peso=c()
for(rua in elos[,1]){
peso=c(peso,sum(df$Saldo[which(df$Endereco.do.PED==rua)]))
}
E(g)$weight=peso
g3=g
e1=as.data.frame(get.edgelist(g1))
e1=rbind(e1,as.data.frame(get.edgelist(g2)))
e1=rbind(e1,as.data.frame(get.edgelist(g3)))
g=graph.data.frame(e1)
E(g)$weight=c(E(g1)$weight,E(g2)$weight,E(g3)$weight)
g=simplify(g)
G=g
#plot(E(g)$weight)
#Agora vamos fazer a figura da rede
df=DF
v=union(names(V(G)),names(V(G)))
lat=c()
long=c()
for(j in v){
lat=c(lat,dataset$Latitude[which(dataset$Endereco.do.PED==j)[1]])
long=c(long,dataset$Longitude[which(dataset$Endereco.do.PED==j)[1]])
print(length(lat)-length(V(G)))
}
require(leaflet)
require(sp)
meta=data.frame(node=v,lat=lat,lon=long)
#meta=data.frame(node=1:length(D1[,1]),lat=D1[["lat"]],lon=D1[["lon"]])
str(meta)
g=graph.data.frame(as.data.frame(get.edgelist(G)),directed=TRUE,vertices=meta)
gg <- get.data.frame(g, "both")
vert <- gg$vertices
coordinates(vert) <- ~lon+lat
edges <- gg$edges
edges <- lapply(1:nrow(edges), function(i) {
as(rbind(vert[vert$name == edges[i, "from"], ],
vert[vert$name == edges[i, "to"], ]),
"SpatialLines")
})
for (i in seq_along(edges)) {
edges[[i]] <- spChFIDs(edges[[i]], as.character(i))
}
edges <- do.call(rbind, edges)
leaflet(vert) %>% addTiles() %>%
addPolylines(data = edges, weight = 1.5, opacity = 0.15, color = "black", fillOpacity = 0.9) %>%
addCircleMarkers(color = rgb(1,0,0,0.3),radius=0.5)
# Informação da rede
c(length(V(G)),length(E(G)))
## 9572 14865
##faz-se o mesmo para todos os dias, acrescentando esse parâmetro no início
#grau dos pontos da rede completa
summary(degree(g))
# the diameter of each component in a random graph
components <- decompose(g, min.vertices=2)
sapply(components, diameter)
library(hrbrthemes)
library(ggplot2)
# dataset:
datatest= as.data.frame(E(g)$weight)
# basic histogram
p <- ggplot(datatest, aes(x=E(g)$weight)) +
geom_histogram(fill="#69b3a2", color="#e9ecef", alpha=0.9)+
theme_ipsum() +
theme(
plot.title = element_text(size=15)
)
p
summary(peso)
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.0 105.2 260.5 367.0 548.0 1738.0
###dia 1 - sábado
sabado=subset(dataset,Dia.Semana==dias[1])
saldo = sabado$Saldo
saldos2 <- as.data.frame(saldo)
# basic histogram
p <- ggplot(saldos2, aes(x=saldo)) +
geom_histogram(fill="#69b3a2", color="#e9ecef", alpha=0.9)+
ggtitle( "Sábado")+
theme_ipsum() +
theme(
plot.title = element_text(size=15)
)
p
segunda=subset(dataset,Dia.Semana==dias[7])
saldo = segunda$Saldo
saldos3 <- as.data.frame(saldo)
# basic histogram
p <- ggplot(saldos3, aes(x=saldo)) +
geom_histogram(fill="#69b3a2", color="#e9ecef", alpha=0.9)+
ggtitle( "Segunda")+
theme_ipsum() +
theme(
plot.title = element_text(size=15)
)
p
##comparação domingo com e sem campanha de popularizaão do teatro
dd <- unique(domingo$Data.Pesquisa)
dplyr::arrange(dd, desc(dd))
##tratando os dados
cdd<- filter(domingo, Data.Pesquisa == c("2011-02-20", "2011-02-27") )
ccdd <- filter(domingo, Data.Pesquisa == c("2011-03-13", "2011-10-16") )
compdomingo = data.frame(
x=cdd$Saldo[1:825],
y= ccdd$Saldo
)
# Create 2 vectors
a <- cdd$Saldo[1:825]
b <- ccdd$Saldo
# Make a list of these 2 vectors
C <- list(a,b)
# Change the names of the elements of the list :
names(C) <- c(paste("Campanha"), paste("Controle"))
# Change the mgp argument: avoid text overlaps axis
par(mgp=c(1,1,0))
# Final Boxplot
boxplot(C , col="#69b3a2", main = "Saldo de pessoas - 2011")
##tratando os dados
cdd<- filter(domingo, Data.Pesquisa == c( "2016-02-21", "2016-02-28") )
ccdd <- filter(domingo, Data.Pesquisa == c("2016-03-13", "2016-03-20") )
compdomingo = data.frame(
x=cdd$Saldo[1:4337],
y= ccdd$Saldo
)
# Create 2 vectors
a <- cdd$Saldo[1:4337]
b <- ccdd$Saldo
# Make a list of these 2 vectors
C <- list(a,b)
# Change the names of the elements of the list :
names(C) <- c(paste("Campanha"), paste("Controle"))
# Change the mgp argument: avoid text overlaps axis
par(mgp=c(1,1,0))
# Final Boxplot
boxplot(C , col="#69b3a2", main = "Saldo de pessoas - 2016")
##tratando os dados
cdd<- filter(domingo, Data.Pesquisa == c( "2018-02-25", "2018-03-04") )
ccdd <- filter(domingo, Data.Pesquisa == c( "2018-03-11", "2018-03-18") )
compdomingo = data.frame(
x=cdd$Saldo,
y= ccdd$Saldo[1:524]
)
# Create 2 vectors
a <- cdd$Saldo
b <- ccdd$Saldo[1:524]
# Make a list of these 2 vectors
C <- list(a,b)
# Change the names of the elements of the list :
names(C) <- c(paste("Campanha"), paste("Controle"))
# Change the mgp argument: avoid text overlaps axis
par(mgp=c(1,1,0))
# Final Boxplot
boxplot(C , col="#69b3a2", main = "Saldo de pessoas - 2018")
##mapa com valores do dia
difund1 <- domingo
cordomingod <- difund1 %>% group_by(Endereco.do.PED) %>% summarise(avg = mean(Desembarque)) %>%
arrange(desc(avg))
cordomingod2 <- difund1 %>% group_by(Endereco.do.PED) %>% summarise(avg2 = mean(Embarque)) %>%
arrange(desc(avg2))
cordomingod2 <- as.data.frame(cordomingod2)
cordomingod <- as.data.frame(cordomingod)
colnames(cordomingod)[1] <- "name"
colnames(cordomingod2)[1] <- "name"
cordomingods <- merge(cordomingod, cordomingod2)
##mapa com valores
difund <- dataset
difund %>% group_by(Endereco.do.PED) %>% summarise(avg = mean(Desembarque)) %>% arrange(desc(avg))
#retirando dados
corbus1 <- difund %>% group_by(Endereco.do.PED) %>% summarise(avg1 = mean(Desembarque)) %>%
arrange(desc(avg1))
corbus2 <- difund %>% group_by(Endereco.do.PED) %>% summarise(avg2 = mean(Embarque)) %>%
arrange(desc(avg2))
corbus3 <- difund %>% group_by(Endereco.do.PED) %>% summarise(avg = mean(Saldo)) %>%
arrange(desc(avg))
corbus1 <- as.data.frame(corbus1)
corbus2 <- as.data.frame(corbus2)
corbus3 <- as.data.frame(corbus3)
colnames(corbus1)[1] <- "name"
colnames(corbus2)[1] <- "name"
colnames(corbus3)[1] <- "name"
corbus <- full_join(corbus1, corbus2, by= "name")
corbus <- full_join(corbus, corbus3, by= "name")
##colocando no mapa
vertbus <- merge(vert,corbus, by="name")
##criando paleta
#spalette <- colorBin(palette = "YlGnBu", domain=vertbus$avg2, na.color="transparent", bins=mybins)
mybins <- quantile(vertbus$avg)
pal <- colorBin(palette = "Purples", domain = vertbus$avg, na.color="transparent", alpha = 0.9, bins = mybins)
mybins1 <- quantile(vertbus$avg1)
pal1 <- colorBin(palette = "YlGnBu", domain = vertbus$avg1, na.color="transparent", alpha = 0.9, bins = mybins1)
mybins2 <- quantile(vertbus$avg2)
pal2 <- colorBin(palette = "YlOrBr", domain = vertbus$avg2, na.color="transparent", alpha = 0.9, bins = mybins2)
leaflet(vertbus) %>% addTiles() %>%
addPolylines(data = edges, weight = 1.5, opacity = 0.15, color = "black", fillOpacity = 0.9) %>%
addCircleMarkers(fillColor = ~pal(avg), fillOpacity = 0.7, color="white", radius=3, stroke=FALSE, group="Saldo") %>%
addCircleMarkers(fillColor = ~pal1(avg1), fillOpacity = 0.7, color="white", radius=3, stroke=FALSE, group="Desembarque")%>%
addCircleMarkers(fillColor = ~pal2(avg2), fillOpacity = 0.7, color="white", radius=3, stroke=FALSE, group="Embarque")%>%
#legenda
addLegend( pal=pal, values=~avg, opacity=0.9, title = "Saldo", position = "bottomright", group="Saldo") %>%
addLegend( pal=pal1, values=~avg1, opacity=0.9, title = "Desembarque", position = "bottomright", group="Desembarque" ) %>%
addLegend( pal=pal2, values=~avg2, opacity=0.9, title = "Embarque", position = "bottomright", group="Embarque" ) %>%
addLayersControl(overlayGroups = c("Saldo","Desembarque", "Embarque"), options = layersControlOptions(collapsed = FALSE))