-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPCA_06_08_20.R
More file actions
311 lines (216 loc) · 9.81 KB
/
PCA_06_08_20.R
File metadata and controls
311 lines (216 loc) · 9.81 KB
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
#' -------
#' title: "Evaluacion de especies maderables alternativas, para disminuir la sobreexplotacion
# a especies de alto valor comercial en las provincias de Loja y Zamora Chinchipe."
#' subtitle: "Analisis de clasificacion de datos (PCA)"
#' author: "Keyla Cartuche"
# date: "21.ago.2020"
#' -------
# Codificado: Darwin Pucha C.
#################### ANALISIS DE COMPONENTES PRINCIPALES ##########################
rm(list=ls()) # Limpiar espacio de trabajo
graphics.off() # Limpiar area de graficos
# Ctrl + l # Limpiar consola
## 1. IMPORTACION, VISUALIZACION, ESTADISTICOS BASICOS Y EXPORTACION DE DATOS
# Fijar la carpeta de trabajo
setwd("M:/Documents/ACADEMICA/TESIS - MADERAS/r/PCA_KC")
# importar archivo CSV
db <- read.csv("KC_Base_datos_maderas_PCA.csv", sep = ";")
# Subtabla
db2.1 <- db[, c("fam", "Name", "T.bosque", "P_alt") ] # Categorias
db2.2 <- db[, sapply(db, class) == "numeric"] # solo numericos
#db2.3 <- db[, sapply(db, class) == "integer"] # solo enteros
# nueva tabla
db2 <- cbind(db2.1, db2.2)
# Eliminar manualmente variables
#db2$DAP_cm <- NULL
#db2$HT_m <- NULL
#db2$C_long <- NULL
#db2$R_tan_rad <- NULL
db2[, c("Temp", "C_x", "C_y","fam",
"DAP","HT")] <- NULL
# Filtro para dejar solo celdas completas
db3 <- db2[complete.cases(db2), ]
rownames(db3) <- db3$Name # poner nombres a las filas
db3$Name <- NULL
## ANALISIS DE DATOS
# 2. DENDROGRAMAS
###############################
# Heat Maps
db4 <- data.matrix(db3) # to matrix
##db4 <- t(db4) # Transponer datos
db4 <- scale(db4, center=T, scale=T) # data standarization
###############################################
# 3. PCA (Analisis de componentes principales)
#
###############################################
## PCA
# Crear un objeto PCA
#TODAS
db4.pca <- prcomp(db4[ , 3:71] , scale = T, center = T) # menos primeras 4 columnas
db4.pca.fis <- prcomp(db4[ , 16:21 ], scale = T, center = T) # solo propiedades fisicas
db4.pca.org <- prcomp(db4[ , 3:15 ], scale = T, center = T) # solo propiedades Organolepticas
db4.pca.anat <- prcomp(db4[ , 22:71 ], scale = T, center = T) # solo Caracteristicas Anatomicas
# 3.1. VISUALIZACION DE DATOS
# 3.1.1. Datos INDIVIDUALES
library(factoextra) # Abrir libreria para realizar los graficos
jpeg(filename="pca.ind_all_P_alt.jpg", width = 350, height = 200, units = "mm", res=500)
fviz_pca_ind(db4.pca,
title = expression("PCA - Ind: MADERAS DEL SUR DEL ECUADOR - (Piso altitudinal)" * italic(" TODAS ")),
legend.title = "Variables:",
habillage = db3$P_alt,
addEllipses = T,
ellipse.level = 0.99,
label = "n", # "all" es para ver todas las etiquetas, para ninguno "n"
repel = TRUE,
labelsize = 3.5,
pointsize = "contrib" )
dev.off()
# 3.1.2. Datos por VARIABLES
####fviz_pca_var(db4.pca,
# title = expression("PCA (Variables) MADERAS DEL SUR DEL ECUADOR - TODAS"),
# col.var = "contrib",
# repel = T,
# label = "all",
# legend.title = "Contribucion:")
############## 3.1.3. PCA IND ######
### Por tipo de bosque y piso altitudinal
#### A. TODOS
jpeg(filename="pca.ind_all_P_alt.jpg", width = 350, height = 200, units = "mm", res=500)
fviz_pca_ind(db4.pca,
title = expression("PCA - Ind: MADERAS DEL SUR DEL ECUADOR - (Piso altitudinal)" * italic(" TODAS ")),
legend.title = "Variables:",
habillage = db3$P_alt,
addEllipses = T,
ellipse.level = 0.99,
label = "n", # "all" es para ver todas las etiquetas, para ninguno "n"
repel = TRUE,
labelsize = 3.5,
pointsize = "contrib" )
dev.off()
#### B. CAR. ORGANOLEPTICAS
jpeg(filename="pca.ind_org_T_bosque(etiquetas).jpg", width = 300, height = 200, units = "mm", res=500)
fviz_pca_ind(db4.pca.org,
title = expression("PCA - Ind: MADERAS DEL SUR DEL ECUADOR - (Tipo de bosque)" * italic(" Características organolépticas ")),
legend.title = "Variables:",
habillage = db3$T.bosque,
addEllipses = T,
ellipse.level = 0.99,
label = "all", # "all" es para ver todas las etiquetas, para ninguno "n"
repel = TRUE,
labelsize = 3.5,
pointsize = "contrib" )
dev.off()
#### C. cAR. FÍSICAS
jpeg(filename="pca.ind_fisica_P_alt(etiquetas).jpg", width = 300, height = 200, units = "mm", res=500)
fviz_pca_ind(db4.pca.fis,
title = expression("PCA - Ind: MADERAS DEL SUR DEL ECUADOR - (Piso altitudinal)" * italic(" Características físicas")),
legend.title = "Variables:",
habillage = db3$P_alt,
addEllipses = T,
ellipse.level = 0.99,
label = "all", # "all" es para ver todas las etiquetas, para ninguno "n"
repel = TRUE,
labelsize = 3.5,
pointsize = "contrib" )
dev.off()
##### D. CAR. ANATOMICAS
jpeg(filename="pca.ind_anat_T_bosque (etiquetas).jpg", width = 300, height = 200, units = "mm", res=500)
fviz_pca_ind(db4.pca.anat,
title = expression("PCA - Ind: MADERAS DEL SUR DEL ECUADOR - (Tipo de bosque)" * italic(" Características anatómicas")),
legend.title = "Variables:",
habillage = db3$T.bosque,
addEllipses = T,
ellipse.level = 0.99,
label = "all", # "all" es para ver todas las etiquetas, para ninguno "n"
repel = TRUE,
labelsize = 3.5,
pointsize = "contrib" )
dev.off()
####### 3.1.4. PCA BIPLOT #########
# A. TODOS
jpeg(filename="pca.biplot_all_P_alt(etiquetas).jpg", width = 350, height = 200, units = "mm", res=500)
fviz_pca_biplot(db4.pca,
title = expression("PCA - Biplot: MADERAS DEL SUR DEL ECUADOR - (Piso altitudinal)" * italic(" TODAS")),
col.var = "dodgerblue",
#col.ind = "forestgreen", # "grey70", # Individuals color
addEllipses = T,
habillage = db3$P_alt,
legend.title = "Variabilidad",
label = "all",
repel = T)
dev.off()
####### B. Propiedades fisicas
# Pisos altitudinales
jpeg(filename="pca.biplot_fisicas_P_alt(etiquetadas).jpg", width = 300, height = 200, units = "mm", res=500)
fviz_pca_biplot(db4.pca.fis,
title = expression("PCA - Biplot: MADERAS DEL SUR DEL ECUADOR - (Piso altitudinal)" * italic(" Características físicas")),
col.var = "dodgerblue",
#col.ind = "forestgreen", # "grey70", # Individuals color
addEllipses = T,
habillage = db3$P_alt,
legend.title = "Variabilidad",
label = "all",
repel = T)
dev.off()
# Tipo de bosque
jpeg(filename="pca.biplot_fisicas_T_bosque(etiquetas).jpg", width = 300, height = 200, units = "mm", res=500)
fviz_pca_biplot(db4.pca.fis,
title = expression("PCA - Biplot: MADERAS DEL SUR DEL ECUADOR - (Tipo de Bosque)" * italic(" Características físicas")),
col.var = "dodgerblue",
#col.ind = "forestgreen", # "grey70", # Individuals color
addEllipses = T,
habillage = db3$T.bosque,
legend.title = "Variabilidad",
label = "all",
repel = T)
dev.off()
####### C. Propiedades organolepticas
# Pisos altitudinales
jpeg(filename="pca.biplot_org_P_alt.jpg", width = 300, height = 200, units = "mm", res=500)
fviz_pca_biplot(db4.pca.org,
title = expression("PCA - Biplot: MADERAS DEL SUR DEL ECUADOR - (Piso altitudinal)" * italic(" Características organolépticas")),
col.var = "dodgerblue",
#col.ind = "forestgreen", # "grey70", # Individuals color
addEllipses = T,
habillage = db3$P_alt,
legend.title = "Variabilidad",
label = "n",
repel = T)
dev.off()
# Tipo de bosque
jpeg(filename="pca.biplot_org_T_bosque(etiquetas).jpg", width = 300, height = 200, units = "mm", res=500)
fviz_pca_biplot(db4.pca.org,
title = expression("PCA - Biplot: MADERAS DEL SUR DEL ECUADOR - (Tipo de bosque)" * italic(" Características organolépticas")),
col.var = "dodgerblue",
#col.ind = "forestgreen", # "grey70", # Individuals color
addEllipses = T,
habillage = db3$T.bosque,
legend.title = "Variabilidad",
label = "all",
repel = T)
dev.off()
####### D. Caracteristicas anatomicas
# Pisos altitudinales
jpeg(filename="pca.biplot_anatomicas_P_alt.jpg", width = 300, height = 200, units = "mm", res=500)
fviz_pca_biplot(db4.pca.anat,
title = expression("PCA - Biplot: MADERAS DEL SUR DEL ECUADOR - (Piso altitudinal)" * italic(" Características anatómicas")),
col.var = "dodgerblue",
#col.ind = "forestgreen", # "grey70", # Individuals color
addEllipses = T,
habillage = db3$P_alt,
legend.title = "Variabilidad",
label = "n",
repel = T)
dev.off()
# Tipo de bosque
jpeg(filename="pca.biplot_anatomicas_T_bosque.jpg", width = 300, height = 200, units = "mm", res=500)
fviz_pca_biplot(db4.pca.anat,
title = expression("PCA - Biplot: MADERAS DEL SUR DEL ECUADOR - (Tipo de bosque)" * italic(" Características anatómicas")),
col.var = "dodgerblue",
#col.ind = "forestgreen", # "grey70", # Individuals color
addEllipses = T,
habillage = db3$T.bosque,
legend.title = "Variabilidad",
label = "n",
repel = T)
dev.off()