library(kableExtra)
## Warning: package 'kableExtra' was built under R version 4.5.1
# Leer datos
datos <- read.csv2("IC.csv", sep = ";", header = TRUE)
#####Regresion logaritmica
#y=a+bln(x)
#y=B0+B1X1
#a=B0
#b=B1
##titulo
plot(1, type = "n", axes = FALSE, xlab = "", ylab = "")

text(x = 1, y = 1, 
     labels = "RELACIÓN ENTRE VARIABLES",
     cex = 2,  
     col = "blue",  
     font = 6) 

# Asegurar que las columnas sean numéricas
datos$total_veg <- as.numeric(datos$total_veg)
datos$sup_t_a <- as.numeric(datos$sup_t_a)
xo <- datos$total_veg 
yo <- datos$sup_t_a
plot(xo,yo,main= "Gráfica N°10: Diagrama de dispersion de puntos entre total de vegetacion y superficie total 
          de los indencios en Chile", ylab = "sup_t_a (ha)",
     xlab = "total_veg (ha)")

# Filtrar valores razonables
datos_filtrados <- subset(datos, total_veg <=2 & sup_t_a <= 2 & total_veg > 0)

# Crear variables
x <- datos_filtrados$total_veg
y <- datos_filtrados$sup_t_a
summary(x)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  0.0001  0.0400  0.2000  0.3887  0.5000  2.0000
plot(x,y,main= "Gráfica N°10.1: Diagrama de dispersion de puntos(datos filtrados) entre total de vegetacion
y superficie total de los indencios en Chile", ylab = "sup_t_a (ha)",
     xlab = "total_veg (ha)")

# Ajustar modelo logarítmico
regreslog <- lm(y ~ log(x)) 

# Graficar
plot(x, y,xlim= c(0,0.8), ylim = c(0, 1),
     main ="Gráfica N°10.2: Diagrama regresion logaritmica entre pastizal y total de vegetacion 
     de los indencios en Chile",
     xlab = "total_veg (ha)", ylab = "sup_t_a (ha)",
     pch = 19, col = "red")

a <- regreslog$coefficients[1]
b <- regreslog$coefficients[2]
curve(a+b*log(x),add = T,col="black",lwd=2)

####test de bondad 
##coeficiente de determinacion
r <- cor(x,y)
r
## [1] 0.9505295
r2 <- (r^2)*100
r2
## [1] 90.35064
##restricciones x>0
xi=exp(-a/b)
xi
## (Intercept) 
##  0.02004894
####Estimacion
plot(1, type = "n", axes = FALSE, xlab = "", ylab = "") 
text(x = 1, y = 1, 
     labels = paste("Cálculo de estimación\n",
                    "¿Qué superficie total se espera\ncuando el total de vegetacion es 0.6 ha?\n",
                    "R: 0.77(ha)"),
     cex = 1.4, col = "blue", font = 6)

##Aplicando la fórmula del modelo
suptaestimada <- 0.894+0.229*log(0.6)
suptaestimada
## [1] 0.7770209
# CONCLUSIÓN: TABLA RESUMEN
######################

tabla_resumen <- data.frame(
  Variables = c("total de vegetación (x), Sup. total (y)", ""),
  Modelo = c("Regresión logaritmica", ""),
  Ecuación = c("y = a+b*ln(x)", ""),
  Parámetros = c("a = 0.894","b =0.229"),
  Dominio = c("x ∈ (0 ; 0.8]", "")
)

kable(tabla_resumen, align = 'c', caption = "Conclusiones del Modelo de Regresión Exponencial")
Conclusiones del Modelo de Regresión Exponencial
Variables Modelo Ecuación Parámetros Dominio
total de vegetación (x), Sup. total (y) Regresión logaritmica y = a+b*ln(x) a = 0.894 x ∈ (0 ; 0.8]
b =0.229