#UNIVERSIDAD CENTRAL DEL ECUADOR
#FACULTAD: FIGEMPA
#CARRERA: INGENIERIA AMBIENTAL
#AUTOR: KEVIN CHICAIZA
#TEMA CONTINUA
#VARIABLE: ARBOLADO (AREAS AFECTADAS POR ESPECIES ARBOREAS NATIVAS)

#============================ MULTIVARIABLE ================================
plot(1, type = "n", axes = FALSE, xlab = "", ylab = "")
text(x = 1, y = 1, 
     labels = "MULTIVARIABLE",
     cex = 2,  
     col = "blue",  
     font =6)

# EXTRAER DATOS
library(readxl)
datos <- read_excel("C:/Estadistica/iNCENDIOS FORESTALES CHILE DATOS.xlsx")

#EXTRACCION DE DATOS#
datos$arbolado <- as.numeric(gsub(",", ".", datos$arbolado))
datos$matorral <- as.numeric(gsub(",", ".", datos$matorral))

# RELACION ENTRE VARIABLES
plot(datos$matorral, datos$arbolado,
     main = "Grafico n°7: Relacion entre arbolado y matorral",
     xlab = "Matorral", ylab = "Arbolado", cex.main = 1,
     pch = 19, col = "blue")

# Conjetura de modelo matematico
plot(1, type = "n", axes = FALSE, xlab = "", ylab = "")
text(x = 1, y = 1, 
     labels = "Modelo Potencial",
     cex = 2,  
     col = "blue",  
     font =6)

datos_pot <- subset(datos, arbolado > 0 & matorral > 0)
datos_pot$log_arbolado <- log(datos_pot$arbolado)
datos_pot$log_matorral <- log(datos_pot$matorral)

x <- log(datos_pot$arbolado)
y <- log(datos_pot$matorral)

modelo_potencial <- lm(log_arbolado ~ log_matorral, data = datos_pot)
summary(modelo_potencial)
## 
## Call:
## lm(formula = log_arbolado ~ log_matorral, data = datos_pot)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.7652 -0.5829  0.0277  0.6771  5.6419 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  -0.30779    0.06026  -5.108  4.6e-07 ***
## log_matorral  0.87837    0.01871  46.944  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.211 on 512 degrees of freedom
## Multiple R-squared:  0.8115, Adjusted R-squared:  0.8111 
## F-statistic:  2204 on 1 and 512 DF,  p-value: < 2.2e-16
#Parametros#
log_a <- coef(modelo_potencial)[1]
b <- coef(modelo_potencial)[2]
a <- exp(log_a)

cat("Modelo potencial: arbolado =", round(a, 4), "* matorral^", round(b, 4), "\n")
## Modelo potencial: arbolado = 0.7351 * matorral^ 0.8784
# Formamos Ecuacion
plot(1, type = "n", axes = FALSE, xlab = "", ylab = "")
text(x = 1, y = 1, 
     labels = "Ecuacion Potencial \n y = aX^b \n y = 0.735X^(0.878)",
     cex = 2,  
     col = "blue",  
     font =6)

# RELACION ENTRE VARIABLES VS MODELO POTENCIAL
plot(datos_pot$matorral, datos_pot$arbolado,
     xlab = "Matorral (ha)", ylab = "Arbolado (ha)",
     main = "Grafico n°7.1: Modelo de dispersion con
  linea de regresion",
     pch = 19, col = "darkgreen")

curve(a * x^b, add = TRUE, col = "red", lwd = 2)

# Test de pearson
cor.test(datos$matorral, datos$arbolado, method = "pearson")
## 
##  Pearson's product-moment correlation
## 
## data:  datos$matorral and datos$arbolado
## t = 98.292, df = 5232, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.7956905 0.8147347
## sample estimates:
##       cor 
## 0.8054204
r <- cor(x,y)
r
## [1] 0.9008161
# restricciones
# X y Y no pueden ser negativas
# y = 0
# 0 = 0.0.735x^0.878
# x = 0 (restriccion cuando matorral es igual a 0 ha)

# Que valor de arbolado se espera cuando matorral es 500

y_est <- 0.735*(500^0.878)
y_est
## [1] 172.1801
plot(1, type = "n", axes = FALSE, xlab = "", ylab = "")
text(x = 1, y = 1, 
     labels = "¿Que cantidad de superficie arbolada afectada\nse espera cuando se tengan 500 ha de matorral?\n\nR: 172.18 ha",
     cex = 1.5,  
     col = "blue",  
     font =6)

#La variacion entre arbolado y matorral se relacionan con un modelo potencial, siendo Y arbolado y X matorral, cuando matorral es 2000 (ha), se espera que el arbolado sea 581.5562 (ha)#

# Conclusiones
plot(1, type = "n", axes = FALSE, xlab = "", ylab = "")
text(x = 1, y = 1, 
     labels = "Conclusiones",
     cex = 2,  
     col = "blue",  
     font =6)

library(knitr)
Tabla_resumen <- data.frame(Variables = c("x", "y"),
                            Modelo = c("matorral (ha)", "arbolado (ha)"),
                            Restricciones = c("x > 0 (ha)","y > 0 (ha)"),
                            Coeficiente_pearson = c("0.90 ", ""),
                            Estimacion = c("500 (ha)", "172.18 (ha)"),
                            Ecuacion = c("y = 0.735X^(0.878)", ""))
colnames(Tabla_resumen) <- c("Variables", "Nombres", "Restricciones",
                             "Coef. Pearson", "Estimacion", "Ecuacion")
kable(Tabla_resumen, align = 'c', caption = "Resumen del Modelo Potencial")
Resumen del Modelo Potencial
Variables Nombres Restricciones Coef. Pearson Estimacion Ecuacion
x matorral (ha) x > 0 (ha) 0.90 500 (ha) y = 0.735X^(0.878)
y arbolado (ha) y > 0 (ha) 172.18 (ha)