library(gt)
library(ggplot2)
library(dplyr)
library(knitr)
library(scales)
library(e1071)
datos <- read.csv(
"floricultura_cayambe_100.csv",
header = TRUE,
sep = ",",
na.strings = c("", "-", "NA"),
check.names = FALSE
)
# ==============================================================================
# Selección y conversión a numérico de altitud_msnm
altitud <- suppressWarnings(as.numeric(datos[["altitud_msnm"]]))
altitud <- altitud[is.finite(altitud)]
n <- length(altitud)
min_alt <- min(altitud)
max_alt <- max(altitud)
# ==============================================================================
# Calculo de frecuencia (Regla de Sturges)
R <- max_alt - min_alt
k_detallado <- ceiling(1 + 3.322 * log10(n))
A <- R / k_detallado
cat("Número de intervalos (k):", k_detallado, "\n")
## Número de intervalos (k): 8
Li <- seq(from = min_alt, to = max_alt - A, by = A)
Ls <- c(seq(from = min_alt + A, to = max_alt - A, by = A), max_alt)
altitud <- round(altitud, 3)
Li <- round(Li, 3)
Ls <- round(Ls, 3)
MC <- (Li + Ls) / 2
ni <- numeric(length(Li))
for (i in 1:length(Li)) {
if (i < length(Li)) {
ni[i] <- sum(altitud >= Li[i] & altitud < Ls[i])
} else {
ni[i] <- sum(altitud >= Li[i] & altitud <= Ls[i])
}
}
hi <- (ni / n) * 100
Ni_asc <- cumsum(ni)
Ni_desc <- rev(cumsum(rev(ni)))
Hi_asc <- cumsum(hi)
Hi_desc <- rev(cumsum(rev(hi)))
Intervalo <- paste0("[", round(Li, 2), " - ", round(Ls, 2), ")")
Intervalo[length(Intervalo)] <- paste0(
"[",
round(Li[length(Li)], 2),
" - ",
round(Ls[length(Ls)], 2),
"]"
)
TDF_altitud <- data.frame(
Intervalo = Intervalo,
MC = round(MC, 2),
ni = ni,
hi = round(hi, 2),
Ni_ascendente = Ni_asc,
Ni_descendente = Ni_desc,
Hi_ascendente = round(Hi_asc, 2),
Hi_descendente = round(Hi_desc, 2)
)
totales <- data.frame(
Intervalo = "Totales",
MC = "-",
ni = sum(ni),
hi = round(sum(hi), 2),
Ni_ascendente = "-",
Ni_descendente = "-",
Hi_ascendente = "-",
Hi_descendente = "-"
)
TDF_altitud_completa <- rbind(TDF_altitud, totales)
TDF_altitud_completa %>%
gt() %>%
tab_header(
title = "Tabla Nro. 1",
subtitle = "Distribución de frecuencia de la altitud (msnm) en fincas florícolas de Cayambe"
) %>%
tab_source_note(
source_note = md("Elaborado por: Nahomi Chimbo, Cristina Fuel, Blanca Guatemal, Dayanara Lopez, Melany Tuza <br> Fuente: floricultura_cayambe_100.csv (variable altitud_msnm)")
) %>%
tab_style(
style = cell_borders(sides = "left", color = "black", weight = px(2)),
locations = cells_body()
) %>%
tab_style(
style = cell_borders(sides = "right", color = "black", weight = px(2)),
locations = cells_body()
) %>%
tab_style(
style = cell_borders(sides = "left", color = "black", weight = px(2)),
locations = cells_column_labels()
) %>%
tab_style(
style = cell_borders(sides = "right", color = "black", weight = px(2)),
locations = cells_column_labels()
) %>%
tab_options(
table.border.top.color = "black",
table.border.bottom.color = "black",
table.border.top.style = "solid",
table.border.bottom.style = "solid",
column_labels.border.top.color = "black",
column_labels.border.bottom.color = "black",
column_labels.border.bottom.width = px(2),
row.striping.include_table_body = TRUE,
heading.border.bottom.color = "black",
heading.border.bottom.width = px(2),
table_body.hlines.color = "gray",
table_body.border.bottom.color = "black"
)
| Tabla Nro. 1 | |||||||
| Distribución de frecuencia de la altitud (msnm) en fincas florícolas de Cayambe | |||||||
| Intervalo | MC | ni | hi | Ni_ascendente | Ni_descendente | Hi_ascendente | Hi_descendente |
|---|---|---|---|---|---|---|---|
| [2685 - 2743.12) | 2714.06 | 6 | 6 | 6 | 100 | 6 | 100 |
| [2743.12 - 2801.25) | 2772.19 | 12 | 12 | 18 | 94 | 18 | 94 |
| [2801.25 - 2859.38) | 2830.31 | 31 | 31 | 49 | 82 | 49 | 82 |
| [2859.38 - 2917.5) | 2888.44 | 37 | 37 | 86 | 51 | 86 | 51 |
| [2917.5 - 2975.62) | 2946.56 | 0 | 0 | 86 | 14 | 86 | 14 |
| [2975.62 - 3033.75) | 3004.69 | 0 | 0 | 86 | 14 | 86 | 14 |
| [3033.75 - 3091.88) | 3062.81 | 5 | 5 | 91 | 14 | 91 | 14 |
| [3091.88 - 3150] | 3120.94 | 9 | 9 | 100 | 9 | 100 | 9 |
| Totales | - | 100 | 100 | - | - | - | - |
| Elaborado por: Nahomi Chimbo, Cristina Fuel, Blanca Guatemal, Dayanara Lopez, Melany Tuza Fuente: floricultura_cayambe_100.csv (variable altitud_msnm) |
|||||||
lim_inf <- floor(min_alt / 50) * 50
lim_sup <- ceiling(max_alt / 50) * 50
A2 <- 50
Li2 <- seq(from = lim_inf, to = lim_sup - A2, by = A2)
Ls2 <- Li2 + A2
MC2 <- (Li2 + Ls2) / 2
ni2 <- numeric(length(Li2))
for (i in 1:length(Li2)) {
if (i < length(Li2)) {
ni2[i] <- sum(altitud >= Li2[i] & altitud < Ls2[i])
} else {
ni2[i] <- sum(altitud >= Li2[i] & altitud <= Ls2[i])
}
}
hi2 <- (ni2 / n) * 100
Ni2_asc <- cumsum(ni2)
Ni2_desc <- rev(cumsum(rev(ni2)))
Hi2_asc <- cumsum(hi2)
Hi2_desc <- rev(cumsum(rev(hi2)))
Intervalo2 <- paste0("[", Li2, " - ", Ls2, ")")
Intervalo2[length(Intervalo2)] <- paste0(
"[",
Li2[length(Li2)],
" - ",
Ls2[length(Ls2)],
"]"
)
TDF_alt_10 <- data.frame(
Intervalo = Intervalo2,
MC = MC2,
ni = ni2,
hi = round(hi2, 2),
Ni_ascendente = Ni2_asc,
Ni_descendente = Ni2_desc,
Hi_ascendente = round(Hi2_asc, 2),
Hi_descendente = round(Hi2_desc, 2)
)
totales2 <- data.frame(
Intervalo = "Totales",
MC = "-",
ni = sum(ni2),
hi = round(sum(hi2), 2),
Ni_ascendente = "-",
Ni_descendente = "-",
Hi_ascendente = "-",
Hi_descendente = "-"
)
TDF_alt_10_completa <- rbind(TDF_alt_10, totales2)
TDF_alt_10_completa %>%
gt() %>%
tab_header(
title = "Tabla Nro. 2",
subtitle = "Distribución de frecuencia reducida de la altitud (intervalos de 50 msnm)"
) %>%
tab_source_note(
source_note = md("Elaborado por: Nahomi Chimbo, Cristina Fuel, Blanca Guatemal, Dayanara Lopez, Melany Tuza <br> Fuente: floricultura_cayambe_100.csv (variable altitud_msnm)")
) %>%
tab_style(
style = cell_borders(sides = "left", color = "black", weight = px(2)),
locations = cells_body()
) %>%
tab_style(
style = cell_borders(sides = "right", color = "black", weight = px(2)),
locations = cells_body()
) %>%
tab_style(
style = cell_borders(sides = "left", color = "black", weight = px(2)),
locations = cells_column_labels()
) %>%
tab_style(
style = cell_borders(sides = "right", color = "black", weight = px(2)),
locations = cells_column_labels()
) %>%
tab_options(
table.border.top.color = "black",
table.border.bottom.color = "black",
table.border.top.style = "solid",
table.border.bottom.style = "solid",
column_labels.border.top.color = "black",
column_labels.border.bottom.color = "black",
column_labels.border.bottom.width = px(2),
row.striping.include_table_body = TRUE,
heading.border.bottom.color = "black",
heading.border.bottom.width = px(2),
table_body.hlines.color = "gray",
table_body.border.bottom.color = "black"
)
| Tabla Nro. 2 | |||||||
| Distribución de frecuencia reducida de la altitud (intervalos de 50 msnm) | |||||||
| Intervalo | MC | ni | hi | Ni_ascendente | Ni_descendente | Hi_ascendente | Hi_descendente |
|---|---|---|---|---|---|---|---|
| [2650 - 2700) | 2675 | 4 | 4 | 4 | 100 | 4 | 100 |
| [2700 - 2750) | 2725 | 2 | 2 | 6 | 96 | 6 | 96 |
| [2750 - 2800) | 2775 | 12 | 12 | 18 | 94 | 18 | 94 |
| [2800 - 2850) | 2825 | 27 | 27 | 45 | 82 | 45 | 82 |
| [2850 - 2900) | 2875 | 33 | 33 | 78 | 55 | 78 | 55 |
| [2900 - 2950) | 2925 | 8 | 8 | 86 | 22 | 86 | 22 |
| [2950 - 3000) | 2975 | 0 | 0 | 86 | 14 | 86 | 14 |
| [3000 - 3050) | 3025 | 0 | 0 | 86 | 14 | 86 | 14 |
| [3050 - 3100) | 3075 | 5 | 5 | 91 | 14 | 91 | 14 |
| [3100 - 3150] | 3125 | 9 | 9 | 100 | 9 | 100 | 9 |
| Totales | - | 100 | 100 | - | - | - | - |
| Elaborado por: Nahomi Chimbo, Cristina Fuel, Blanca Guatemal, Dayanara Lopez, Melany Tuza Fuente: floricultura_cayambe_100.csv (variable altitud_msnm) |
|||||||
Histograma_alt <- hist(altitud, breaks = k_detallado, plot = FALSE)
hist(altitud, breaks = k_detallado,
main = "Grafica Nro.1 de distribución de frecuencias de la altitud en fincas de Cayambe",
xlab = "Altitud (msnm)",
ylab = "Cantidad",
ylim = c(0, max(Histograma_alt$counts)),
col = "darkseagreen3",
cex.main = 0.9,
cex.lab = 1,
cex.axis = 0.9,
xaxt = "n")
axis(1, at = Histograma_alt$breaks,
labels = round(Histograma_alt$breaks, 0), las = 1,
cex.axis = 0.85)
grid()
Histograma_alt <- hist(altitud, breaks = k_detallado, plot = FALSE)
par(mgp = c(3.2, 1, 0))
hist(altitud, breaks = k_detallado,
main = "Grafica Nro.2 de distribución de frecuencias de la altitud en fincas de Cayambe",
xlab = "Altitud (msnm)",
ylab = "Cantidad",
ylim = c(0, max(Histograma_alt$counts) * 1.1),
col = "darkseagreen3",
cex.main = 0.9,
cex.lab = 1,
cex.axis = 0.9,
xaxt = "n")
axis(1,
at = Histograma_alt$breaks,
labels = round(Histograma_alt$breaks, 0),
las = 1,
cex.axis = 0.85)
grid()
bp <- barplot(hi2,
space = 0,
names.arg = FALSE,
xaxt = "n",
main = "Grafica Nro.3 de distribución porcentual de la altitud en fincas de Cayambe",
xlab = "Altitud (msnm)",
ylab = "Porcentaje (%)",
col = "darkseagreen3",
border = "black",
ylim = c(0, max(hi2) + 5),
cex.main = 0.9,
cex.lab = 1,
cex.axis = 0.9)
bordes <- c(bp[1] - 0.5, bp + 0.5)
axis(1,
at = bordes,
labels = c(Li2, tail(Ls2, 1)),
las = 2,
cex.axis = 0.8,
tck = -0.03)
grid()
bp <- barplot(hi2,
space = 0,
names.arg = FALSE,
xaxt = "n",
yaxt = "n",
main = "Grafica Nro.4 de distribución porcentual de la altitud en fincas de Cayambe",
xlab = "Altitud (msnm)",
ylab = "Porcentaje (%)",
col = "darkseagreen3",
border = "black",
ylim = c(0, 100),
cex.main = 0.9,
cex.lab = 1,
cex.axis = 0.9)
bordes <- c(bp[1] - 0.5, bp + 0.5)
axis(1,
at = bordes,
labels = c(Li2, tail(Ls2, 1)),
las = 2,
cex.axis = 0.8,
tck = -0.03)
axis(2,
at = seq(0, 100, by = 20),
labels = paste0(seq(0, 100, by = 20)),
las = 1)
grid()
# Diagrama de caja y bigotes general
op <- par(no.readonly = TRUE)
on.exit(par(op))
h_dens <- hist(altitud, breaks = k_detallado, plot = FALSE)
y_top <- max(h_dens$density) * 1.15
par(mgp = c(3.5, 1, 0))
hist(altitud,
breaks = k_detallado,
freq = FALSE,
main = "Grafica Nro.5: Histograma de densidad y diagrama de caja de la altitud en Cayambe",
xlab = "Altitud (msnm)",
ylab = "Densidad",
ylim = c(0, y_top),
xlim = c(lim_inf, lim_sup),
col = "white",
border = "black",
cex.main = 0.9,
cex.lab = 1,
cex.axis = 0.9,
xaxt = "n")
axis(1,
at = seq(lim_inf, lim_sup, by = 100),
labels = seq(lim_inf, lim_sup, by = 100),
las = 1,
cex.axis = 0.85)
q <- quantile(altitud, probs = c(0.25, 0.50, 0.75), na.rm = TRUE)
ric <- q[3] - q[1]
big_inf <- max(min(altitud, na.rm = TRUE), q[1] - 1.5 * ric)
big_sup <- min(max(altitud, na.rm = TRUE), q[3] + 1.5 * ric)
y_mid <- y_top * 0.48
alto_c <- y_top * 0.14
alto_b <- y_top * 0.06
segments(x0 = c(big_inf, q[3]), y0 = y_mid,
x1 = c(q[1], big_sup), y1 = y_mid,
lty = 2, lwd = 1.2, col = "black")
segments(x0 = c(big_inf, big_sup), y0 = y_mid - alto_b,
x1 = c(big_inf, big_sup), y1 = y_mid + alto_b,
lwd = 1.2, col = "black")
rect(xleft = q[1], ybottom = y_mid - alto_c,
xright = q[3], ytop = y_mid + alto_c,
col = "grey", border = "black", lwd = 1.3)
segments(x0 = q[2], y0 = y_mid - alto_c,
x1 = q[2], y1 = y_mid + alto_c,
lwd = 2.8, col = "black")
Por tener datos atípicos se debió realizar otra caja y bigotes
# Diagramas de caja de valores atípicos (izquierda y derecha)
op <- par(no.readonly = TRUE)
on.exit(par(op))
h_dens <- hist(altitud, breaks = k_detallado, plot = FALSE)
y_top <- max(h_dens$density) * 1.15
par(mgp = c(3.5, 1, 0))
hist(altitud,
breaks = k_detallado,
freq = FALSE,
main = "Grafica Nro.6: Histograma de densidad y diagramas de caja de valores atípicos\nde la altitud en Cayambe",
xlab = "Altitud (msnm)",
ylab = "Densidad",
ylim = c(0, y_top),
xlim = c(lim_inf, lim_sup),
col = "white",
border = "black",
cex.main = 0.9,
cex.lab = 1,
cex.axis = 0.9,
xaxt = "n")
axis(1,
at = seq(lim_inf, lim_sup, by = 100),
labels = seq(lim_inf, lim_sup, by = 100),
las = 1,
cex.axis = 0.85)
atipicos <- boxplot.stats(altitud)$out
med_gral <- median(altitud, na.rm = TRUE)
atip_izq <- atipicos[atipicos < med_gral]
atip_der <- atipicos[atipicos >= med_gral]
dibujar_caja_grupo <- function(vec, y_centro, alto_caja, alto_bigote) {
if (length(vec) == 0) return(invisible(NULL))
if (length(unique(vec)) == 1) {
val <- unique(vec)[1]
segments(x0 = val, y0 = y_centro - alto_caja,
x1 = val, y1 = y_centro + alto_caja,
lwd = 2.5, col = "black")
return(invisible(NULL))
}
q_g <- quantile(vec, probs = c(0.25, 0.50, 0.75), na.rm = TRUE)
ric_g <- q_g[3] - q_g[1]
b_inf <- max(min(vec, na.rm = TRUE), q_g[1] - 1.5 * ric_g)
b_sup <- min(max(vec, na.rm = TRUE), q_g[3] + 1.5 * ric_g)
segments(x0 = c(b_inf, q_g[3]), y0 = y_centro,
x1 = c(q_g[1], b_sup), y1 = y_centro,
lty = 2, lwd = 1.2, col = "black")
segments(x0 = c(b_inf, b_sup), y0 = y_centro - alto_bigote,
x1 = c(b_inf, b_sup), y1 = y_centro + alto_bigote,
lwd = 1.2, col = "black")
rect(xleft = q_g[1], ybottom = y_centro - alto_caja,
xright = q_g[3], ytop = y_centro + alto_caja,
col = "grey", border = "black", lwd = 1.3)
segments(x0 = q_g[2], y0 = y_centro - alto_caja,
x1 = q_g[2], y1 = y_centro + alto_caja,
lwd = 2.8, col = "black")
}
y_mid <- y_top * 0.48
alto_c <- y_top * 0.14
alto_b <- y_top * 0.06
dibujar_caja_grupo(atip_izq, y_mid, alto_c, alto_b)
dibujar_caja_grupo(atip_der, y_mid, alto_c, alto_b)
## 5.4.- Diagrama de Ojivas
plot(Ls2, Ni2_asc,
type = "b",
pch = 16,
col = "green",
lwd = 1,
ylim = c(0, n),
xlim = c(min(Ls2), max(Ls2)),
xaxt = "n",
xlab = "Altitud (msnm)",
ylab = "Cantidad",
main = "Gráfica N°7: Ojiva ascendente y descendente de la altitud en Cayambe",
cex.main = 1)
axis(1, at = Ls2, las = 2, cex.axis = 0.8)
lines(Ls2, Ni2_desc,
type = "b",
pch = 16,
col = "black",
lwd = 1)
grid()
box()
plot(Ls2, Hi2_asc,
type = "b",
pch = 16,
col = "green",
lwd = 1,
ylim = c(0, 100),
xlim = c(min(Ls2), max(Ls2)),
xaxt = "n",
xlab = "Altitud (msnm)",
ylab = "Porcentaje (%)",
main = "Gráfica N°8: Ojiva porcentual ascendente y descendente de la altitud en Cayambe",
cex.main = 1)
axis(1, at = Ls2, las = 2, cex.axis = 0.8)
lines(Ls2, Hi2_desc,
type = "b",
pch = 16,
col = "black",
lwd = 1)
grid()
box()
X <- mean(altitud, na.rm = TRUE)
Me <- median(altitud, na.rm = TRUE)
moda_index <- which.max(TDF_alt_10_completa$ni[1:(nrow(TDF_alt_10_completa) - 1)])
Mo <- TDF_alt_10_completa$Intervalo[moda_index]
desv <- sd(altitud, na.rm = TRUE)
CV <- (desv / X) * 100
m_alt <- mean(altitud, na.rm = TRUE)
s_alt <- sd(altitud, na.rm = TRUE)
n_alt <- length(altitud)
As <- (sum((altitud - m_alt)^3) / n_alt) / (s_alt^3)
K <- (sum((altitud - m_alt)^4) / n_alt) / (s_alt^4) - 3
Tabla_indicadores <- data.frame(
Variable = "altitud_msnm",
Rango = paste0("[", round(min(altitud), 2), " - ", round(max(altitud), 2), "]"),
Media = X,
Mediana = Me,
Moda = Mo,
DesvEst = desv,
CV = CV,
Asimetria = As,
Curtosis = K
)
Tabla_indicadores %>%
gt() %>%
cols_label(
Variable = "Variable",
Rango = "Rango",
Media = "Media (X)",
Mediana = "Mediana (Me)",
Moda = "Moda (Mo)",
DesvEst = "Desv. Est. (sd)",
CV = "CV (%)",
Asimetria = "Asimetria (As)",
Curtosis = "Curtosis (K)"
) %>%
tab_header(
title = "Tabla Nro. 3",
subtitle = "Indicadores Estadisticos de la altitud en fincas florícolas de Cayambe"
) %>%
tab_source_note(
source_note = "Elaborado por: Nahomi Chimbo, Cristina Fuel, Blanca Guatemal, Dayanara Lopez, Melany Tuza | Fuente: floricultura_cayambe_100.csv (variable altitud_msnm)"
) %>%
tab_spanner(
label = "Tendencia Central",
columns = c(Media, Mediana, Moda)
) %>%
tab_spanner(
label = "Dispersion",
columns = c(DesvEst, CV)
) %>%
tab_spanner(
label = "Forma",
columns = c(Asimetria, Curtosis)
) %>%
fmt_number(
columns = c(Media, Mediana, DesvEst, CV, Asimetria, Curtosis),
decimals = 2
) %>%
tab_style(
style = cell_borders(
sides = c("left", "right", "top", "bottom"),
color = "black",
weight = px(1)
),
locations = list(
cells_body(columns = everything(), rows = everything()),
cells_column_labels(columns = everything()),
cells_column_spanners(spanners = everything())
)
) %>%
tab_options(
table.border.top.color = "black",
table.border.bottom.color = "black",
table.border.left.color = "black",
table.border.right.color = "black",
table_body.hlines.color = "black",
table_body.vlines.color = "black",
column_labels.border.bottom.width = px(2)
)
| Tabla Nro. 3 | ||||||||
| Indicadores Estadisticos de la altitud en fincas florícolas de Cayambe | ||||||||
| Variable | Rango |
Tendencia Central
|
Dispersion
|
Forma
|
||||
|---|---|---|---|---|---|---|---|---|
| Media (X) | Mediana (Me) | Moda (Mo) | Desv. Est. (sd) | CV (%) | Asimetria (As) | Curtosis (K) | ||
| altitud_msnm | [2685 - 3150] | 2,874.95 | 2,860.00 | [2850 - 2900) | 107.92 | 3.75 | 1.04 | 0.85 |
| Elaborado por: Nahomi Chimbo, Cristina Fuel, Blanca Guatemal, Dayanara Lopez, Melany Tuza | Fuente: floricultura_cayambe_100.csv (variable altitud_msnm) | ||||||||
La variable altitud (altitud_msnm) fluctúa entre 2685 m y 3150 m y su valor representativo gira en torno a 2860.00 m (Mediana, ante asimetría positiva de \(1.04\) y presencia de valores atípicos en el diagrama de caja), siendo un conjunto de valores homogéneos (\(CV = 3.75\%\)), donde la concentración de datos es fuerte en la parte media-baja de la variable (33.00% en el intervalo principal [2850 - 2900) m) y con la incidencia de 20 valores atípicos que van desde 2685 m hasta 3150 m. Bajo nuestro criterio, este comportamiento es favorable para la floricultura de Cayambe porque la mayoría de las fincas se ubican en un rango altitudinal intermedio-alto y estable que favorece la luminosidad, el desarrollo del tallo y la calidad de la flor, mientras que los extremos más bajos o más altos reflejan variaciones locales del terreno.