-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAQ-Valencia-8.R
395 lines (251 loc) · 11.6 KB
/
AQ-Valencia-8.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
# Sol Represa
# AQ Valencia
# 25/01/2019
# Modificado 8/8/19 La Plata, Argentina
# Objetivo: Correlacion y PCA
library(psych)
library(dplyr)
library(ggplot2)
library(reshape2)
library(pvclust)
medias_dia <- readRDS("medias_dia_est_utiles.Rds")
pollutant_names <- readRDS("pollutant_names.Rds")
pollutant <- readRDS("pollutant_ut.Rds")
sitios <- read.csv("estaciones_utiles.csv")
sitios <- data.frame( Estacion = levels(sitios$Estacion),
Abrev = c("Pla", "Raba", "Beni", "Elx", "Elda", "Alcora", "Alma", "Ben", "Bur", "Peny", "Grau",
"Pobla", "SJ", "VCid", "Viver", "Zorita", "Alba", "Buniol", "Caude", "Quart", "Sagunt",
"Pista", "Poli", "Moli", "Villar"))
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
## 1 - Analisis de correlacion ####
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
source("/home/usuario/Sol/AQ-Valencia/fuction/cor_mtest.R")
#x = medias_dia
x = pollutant
g <- list()
## Algoritmo para generar plot de correlación:
col <- colorRampPalette(c("#BB4444", "#EE9988", "#FFFFFF", "#77AADD", "#4477AA")) # Rampa de colores
method = "spearman" # "spearman" "kendall" o "pearson"
for(i in 1:length(x)){
tabla <- x[[i]]
tabla <- tabla[-c(1,2)] # cuando uso POLLUTANT, si uso medias_dia bloquear! <<<<<<<<<<<<<<<<<<<
# tabla <- tabla[-1] # paa cuando uso "medias_dia"
names(tabla) <- sitios[which(sitios$Estacion %in% names(tabla)), 2]
tabla_cor <- cor( tabla[, 1:length(tabla)] ,
use= "pairwise.complete.obs")
p.mat <- cor_mtest(tabla[, 1:length(tabla)], method = method)
# Grafica correlacion
png(paste("corrplot_", pollutant_names[i],"_", method , ".png", sep="") , width = 480, height = 450)
corrplot(tabla_cor,
method = "color",
title = paste(pollutant_names[i], method, sep=" ") ,
col = col(200),
diag=FALSE, # tl.pos="d",
type="upper",
order="original",
addCoef.col = "black", # Add coefficient of correlation
p.mat = p.mat,
cl.cex = 0.6, #cambia tamaño letra leyenda
tl.cex = 0.6, #cambia tamaño letra estaciones
number.cex = 0.5, #cambia tamaño letra correlacion
number.font = 1,
sig.level = 0.05,
insig = "pch",
mar=c(0,0,1,0) )
dev.off()
}
### ATENCION nombres gráficos:
# 1- datos horarios
# 2- medias diarias
# Grafica correlacion 2 # # # # # # # # # # # # # # # #
for(i in 1:length(x)){
tabla <- x[[i]]
tabla <- tabla[-c(1,2)] # cuando uso POLLUTANT, si uso medias_dia bloquear! <<<<<<<<<<<<<<<<<<<
# tabla <- tabla[-1] # paa cuando uso "medias_dia"
names(tabla) <- sitios[which(sitios$Estacion %in% names(tabla)), 2] # Cambiar nombres
tabla_cor <- cor( tabla[, 1:length(tabla)] ,
use= "pairwise.complete.obs")
tabla_cor[tabla_cor == 1] <- NA
# Grafica histograma de correlacion
#png(paste("hist_corrplot_", pollutant_names[i],"_", method , ".png", sep="") , width = 480, height = 450)
#hist(tabla_cor)
#dev.off()
print(c(pollutant_names[i], round(mean(tabla_cor, na.rm = TRUE), 3),
"(", round(min(tabla_cor, na.rm = TRUE ), 3), ",", round(max(tabla_cor, na.rm = TRUE), 3), ")"))
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
############################################################
## 2) Hirarquical Cluster en matriz ####
#https://www.statmethods.net/advstats/cluster.html
# Hierarchical Clustering with p-value
# based on multiscale bootstrap resampling.
# Clusters that are highly supported by the data will have large p values.
###########################################################
## 2.1 Hirarquical Cluster en matriz de corelación ####
x = pollutant
l <- list()
for(i in 1:length(x)){ #problemas con i=7 por falta de datos
tabla <- x[[i]]
tabla <- tabla[-c(1,2)] # cuando uso POLLUTANT, si uso medias_dia bloquear! <<<<<<<<<<<<<<<<<<<
names(tabla) <- sitios[which(sitios$Estacion %in% names(tabla)), 2] # Cambiar nombres
mydata <- cor( tabla[,1:length(tabla)] ,
use= "pairwise.complete.obs")
set.seed(1)
# Dendrogramas con linkage complete y average
fit <- pvclust(mydata, method.hclust="average",
method.dist="euclidean")
# Generar gráfica
png(paste("cluster_r_", pollutant_names[i] , ".png", sep="") , width = 600, height = 500)
plot(fit) # dendogram with p values
pvrect(fit, alpha=.95) # add rectangles
dev.off()
# para ver cuantos datos se usaron
#valores[i,1] <- pollutant_names[i]
#valores[i,2] <- dim(tabla)[1]
# guardar cluster formados
l[[i]] <- pvpick(fit)$clusters
}
#saveRDS(l, file="cluster_estaciones.Rds")
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# CLUSTERs generados sobre matriz de correlacion
nombres <- c( "X03014006","X03014009", "X03031002", "X03065006", "X03066003", "X12005005",
"X12009007", "X12028001", "X12032001", "X12040008", "X12040010", "X12093004",
"X12099001", "X12129001", "X12140002", "X12141002", "X46010001", "X46077006",
"X46095001", "X46102002", "X46220010", "X46250030", "X46250046", "X46250048",
"X46258001")
NO = c("A", "-", "C","A", "A", "A", "A", "A", "A", "D", "A", "E", "B", "B", "F",
"-", "G", "H" , "I", "A", "A", "A", "-", "-","J")
NO2 = c("A", "-", "A", "A", "A", "A" ,"A", "A", "A", "A", "A", "B", "C", "D", "A",
"-", "A", "A", "A", "A", "A", "A", "-", "-", "A")
O3 = c("A", "A", "A", "A", "A", "A", "-", "-", "A", "A", "A", "B", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A")
SO2 = c("A", "A", "-", "A", "A", "A", "A", "A", "A", "-", "A", "B", "-", "A", "A", "C",
"A", "A", "A", "A", "A", "-", "A", "A", "A")
PM10 = c("-", "C", "-", "-", "A", "-", "B", "B", "-", "B", "-", "-", "-", "-", "A", "A",
"-", "A", "A", "-", "B", "D", "B", "B", "A")
PM25 = c("-", "B", "-", "-", "B", "C", "B", "B", "-", "B", "-", "-", "-", "-", "A", "A",
"B", "A", "A", "-", "B", "-", "B", "B", "A")
cluster <- data.frame(nombres, NO, NO2, O3, PM10, PM25)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
## 2.1 Hirarquical Cluster sobre datos####
x = pollutant
l <- list()
for(i in 1:length(x)){ #problemas con i=7 por falta de datos
tabla <- x[[i]]
tabla <- tabla[-c(1,2)] # cuando uso POLLUTANT, si uso medias_dia bloquear! <<<<<<<<<<<<<<<<<<<
names(tabla) <- sitios[which(sitios$Estacion %in% names(tabla)), 2] # Cambiar nombres
#remove columnas vacías
mydata <- tabla[!sapply(tabla, function(x) all(x == ""))]
#mydata <- complete.cases( tabla[,2:length(tabla)])
#mydata <- apply(mydata, 2, scale) # no siempre escalara es deseable.
set.seed(1)
# Dendrogramas con linkage complete y average
fit <- pvclust(mydata, method.hclust="average",
method.dist="euclidean")
# Generar gráfica
png(paste("cluster_datos_", pollutant_names[i] , ".png", sep="") , width = 600, height = 500)
plot(fit) # dendogram with p values
pvrect(fit, alpha=.95) # add rectangles
dev.off()
# para ver cuantos datos se usaron
#valores[i,1] <- pollutant_names[i]
#valores[i,2] <- dim(tabla)[1]
# guardar cluster formados
l[[i]] <- pvpick(fit)$clusters
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
## 3 - Analisis de PCA ####
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# 3.1) PCA - Analisis de componentes
x = pollutant
pollutant_pca <- list()
for(i in 1:length(x)){
tabla <- x[[i]]
tabla <- tabla[-c(1,2)]
tabla <- tabla[!sapply(tabla, function(x) all(x == ""))] #por si tengo columnas vacias
### Pruebas estadisticas
# Test de Esfericidad de Barlett
# cortest.bartlett(tabla_cor, n=nrow(tabla)) # el p <0,05 solo si Mardia dio NORMAL
# KMO
# KMO(tabla_cor) # KMO > 0,9 Adecuado
# KMO high proportion of variance in your variables that might be caused by underlying factors
### PCA
pca <- princomp(na.omit(tabla[1:length(tabla)]), cor=TRUE) #PCA en la matriz de covarianza
plot (pca, type="l", main="Gráfica de sedimentación")
png(paste("PCA_", pollutant_names[i] , ".png", sep="") , width = 320, height = 220)
qplot(c(1:length(pca$sdev)), round(pca$sdev^2/sum(pca$sdev^2),2)) +
geom_line() +
xlab("PC") +
ylab("Varianza explicada") +
ggtitle(pollutant_names[i]) +
ylim(0,1) +
theme_bw()
dev.off()
png(paste("PCA_cum_", pollutant_names[i] , ".png", sep="") , width = 320, height = 220)
qplot(c(1:length(pca$sdev)), cumsum(round(pca$sdev^2/sum(pca$sdev^2),2))) +
geom_hline( yintercept = 0.7, col="red", linetype="dashed" ) +
geom_line() +
xlab("PC") +
ylab("Varianza acumulada") +
ggtitle(pollutant_names[i]) +
ylim(0, 1.1) +
theme_bw()
dev.off()
pollutant_pca[[i]] <- pca$loadings
}
## 3.2 ) Grafica de pesos ####
# Analisis de representación de las estaciones por componentes
i=1
PC <- c(10, 8, 8, 1, 4, 5, 11) #los obtuve de analizar las graficas creadas en punto anterior
x = pollutant
orden <- c("RC1", "RC2", "RC3", "RC4", "RC5", "RC6", "RC7", "RC8", "RC9", "RC10" )
for( i in 1:length(pollutant)){
tabla <- x[[i]]
tabla <- tabla[-c(1,2)]
names(tabla) <- sitios[which(sitios$Estacion %in% names(tabla)), 2] # Cambiar nombres
tabla <- tabla[!sapply(tabla, function(x) all(x == ""))] #por si tengo columnas vacias
set.seed(123)
vari <- principal(na.omit(tabla[1:length(tabla)]), nfactors= PC[i],
rotate="varimax", covar = FALSE)
pesos <- with(vari, unclass(loadings))
pesos <- as.data.frame(pesos) #convierto para trabajar con ggplot
pesos <- add_rownames(pesos, "indicadores") #recupera la row.name como columna
pesos_m <- reshape2::melt(pesos, value.name= "Carga")
pesos_m$Carga <- round(pesos_m$Carga,2)
pesos_m$variable <- factor(pesos_m$variable, levels = orden[1:(length(pesos)-1)])
pesos_m[which(pesos_m$Carga < 0.4 ), 3] = NA
png(paste("PCA_CP_", pollutant_names[i] , ".png", sep="") , width = 420, height = 370)
ggplot(data = pesos_m, aes(x = variable, y = indicadores)) +
geom_tile(aes(fill = Carga)) +
scale_fill_continuous(low="thistle2", high ="darkred", na.value = "transparent") +
xlab("") +
ylab("") +
#scale_x_discrete( labels = orden[1:(length(pesos)-1)]) +
geom_text(aes(label= Carga*100)) +
theme_bw() +
theme(legend.position = "none") +
labs(title= pollutant_names[i])
dev.off()
}
###########################################################################
# A continuacion scrip que compara 2 funciones en 2 paquetes de R # # # # #
# ATENTI: ver relacion entre funciones de R # # # # #
i=1
# "princomp()" es de paquete stat meintras "principal()" es de psych
# estas 2 expresiones son iguales!
pca <- princomp(na.omit(tabla[1:length(tabla)]), cor = FALSE) #PCA en la matriz de covarianza
pca15 <- principal(na.omit(tabla[1:length(tabla)]), nfactors=21,
rotate="none", use="none", covar = TRUE )
# para verlo podemos hacer:
a <- pca$loadings[1:3,1:5]
b <- pca15$loadings[1:3,1:5]
a/b #hay un factor escalar
# y también:
pca$sdev
pca15$loadings
# estas 2 expresiones también son iguales =
pca <- princomp(na.omit(tabla[1:length(tabla)]), cor=TRUE) #PCA en la matriz de covarianza
pca15 <- principal(na.omit(tabla[1:length(tabla)]), nfactors=21,
rotate="none", use="none", covar = FALSE)
###########################################################################
tabla[35065:43848]