Integrantes del grupo:
Esta tarea tiene como propósito aplicar regresiones lineales simples
y múltiples al dataset Soils, usando R. Se analizarán
variables fisicoquímicas del suelo, identificando patrones lineales y no
lineales.
data(Soils)
glimpse(Soils)
## Rows: 48
## Columns: 14
## $ Group <fct> 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6,…
## $ Contour <fct> Top, Top, Top, Top, Top, Top, Top, Top, Top, Top, Top, Top, To…
## $ Depth <fct> 0-10, 0-10, 0-10, 0-10, 10-30, 10-30, 10-30, 10-30, 30-60, 30-…
## $ Gp <fct> T0, T0, T0, T0, T1, T1, T1, T1, T3, T3, T3, T3, T6, T6, T6, T6…
## $ Block <fct> 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1,…
## $ pH <dbl> 5.40, 5.65, 5.14, 5.14, 5.14, 5.10, 4.70, 4.46, 4.37, 4.39, 4.…
## $ N <dbl> 0.188, 0.165, 0.260, 0.169, 0.164, 0.094, 0.100, 0.112, 0.112,…
## $ Dens <dbl> 0.92, 1.04, 0.95, 1.10, 1.12, 1.22, 1.52, 1.47, 1.07, 1.54, 1.…
## $ P <int> 215, 208, 300, 248, 174, 129, 117, 170, 121, 115, 112, 117, 12…
## $ Ca <dbl> 16.35, 12.25, 13.02, 11.92, 14.17, 8.55, 8.74, 9.49, 8.85, 4.7…
## $ Mg <dbl> 7.65, 5.15, 5.68, 7.88, 8.12, 6.92, 8.16, 9.16, 10.35, 6.91, 7…
## $ K <dbl> 0.72, 0.71, 0.68, 1.09, 0.70, 0.81, 0.39, 0.70, 0.74, 0.77, 0.…
## $ Na <dbl> 1.14, 0.94, 0.60, 1.01, 2.17, 2.67, 3.32, 3.76, 5.74, 5.85, 5.…
## $ Conduc <dbl> 1.09, 1.35, 1.41, 1.64, 1.85, 3.18, 4.16, 5.14, 5.73, 6.45, 8.…
pH: Nivel de acidez del suelo. Importante en la
absorción de nutrientes.N: Nitrógeno. Esencial para el crecimiento de las
plantas.Dens: Densidad aparente. Afecta la compactación y
aireación del suelo.P: Fósforo. Vital para el desarrollo de raíces.Ca: Calcio. Mejora la estructura del suelo.Mg: Magnesio. Componente de la clorofila.K: Potasio. Regula la apertura estomática.Na: Sodio. Puede indicar salinidad del suelo.Conduc: Conductividad eléctrica. Relacionada con la
salinidad.summary(Soils)
## Group Contour Depth Gp Block pH
## 1 : 4 Depression:16 0-10 :12 D0 : 4 1:12 Min. :3.740
## 2 : 4 Slope :16 10-30:12 D1 : 4 2:12 1st Qu.:4.058
## 3 : 4 Top :16 30-60:12 D3 : 4 3:12 Median :4.545
## 4 : 4 60-90:12 D6 : 4 4:12 Mean :4.669
## 5 : 4 S0 : 4 3rd Qu.:5.140
## 6 : 4 S1 : 4 Max. :6.670
## (Other):24 (Other):24
## N Dens P Ca
## Min. :0.03000 Min. :0.780 Min. : 79.0 Min. : 3.820
## 1st Qu.:0.05075 1st Qu.:1.127 1st Qu.:108.8 1st Qu.: 5.040
## Median :0.08450 Median :1.400 Median :131.0 Median : 7.305
## Mean :0.10194 Mean :1.316 Mean :166.2 Mean : 8.029
## 3rd Qu.:0.12925 3rd Qu.:1.502 3rd Qu.:214.2 3rd Qu.: 9.735
## Max. :0.29800 Max. :1.600 Max. :445.0 Max. :16.350
##
## Mg K Na Conduc
## Min. : 5.150 Min. :0.1400 Min. : 0.600 Min. : 0.670
## 1st Qu.: 7.537 1st Qu.:0.2750 1st Qu.: 2.545 1st Qu.: 2.790
## Median : 8.515 Median :0.4250 Median : 5.520 Median : 6.635
## Mean : 8.465 Mean :0.4662 Mean : 5.600 Mean : 6.589
## 3rd Qu.: 9.648 3rd Qu.:0.6425 3rd Qu.: 8.355 3rd Qu.: 9.852
## Max. :10.960 Max. :1.0900 Max. :11.040 Max. :13.320
##
vars <- dplyr::select(Soils, pH, N, P, Ca, Mg, K, Na, Conduc)
# Histograma
vars %>%
pivot_longer(cols = everything(), names_to = "variable", values_to = "valor") %>%
ggplot(aes(x = valor)) +
facet_wrap(~variable, scales = "free") +
geom_histogram(bins = 30, fill = "skyblue", color = "black") +
theme_minimal()
# Densidad
vars %>%
pivot_longer(cols = everything(), names_to = "variable", values_to = "valor") %>%
ggplot(aes(x = valor)) +
facet_wrap(~variable, scales = "free") +
geom_density(fill = "orange", alpha = 0.5) +
theme_minimal()
chart.Correlation(dplyr::select(as.data.frame(Soils), pH, N, Dens, P, Ca, Mg, K, Na, Conduc), histogram = TRUE)
Interpretación: Observamos una alta correlación
entre Na y Conduc, lo que sugiere que el sodio
influye directamente en la conductividad del suelo.
modelo_simple <- lm(Conduc ~ Na, data = Soils)
summary(modelo_simple)
##
## Call:
## lm(formula = Conduc ~ Na, data = Soils)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.29260 -0.70832 0.04578 0.43814 2.13464
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.01291 0.27008 -0.048 0.962
## Na 1.17892 0.04170 28.271 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.9403 on 46 degrees of freedom
## Multiple R-squared: 0.9456, Adjusted R-squared: 0.9444
## F-statistic: 799.3 on 1 and 46 DF, p-value: < 2.2e-16
Interpretación: La variable Na tiene un
efecto positivo significativo sobre Conduc. El R² indica
qué tan bien se ajusta el modelo a los datos.
modelo_inicial <- lm(Conduc ~ pH + N + Dens + P + Ca + Mg + K + Na, data = Soils)
modelo_step <- stepAIC(modelo_inicial, direction = "both")
## Start: AIC=-23.77
## Conduc ~ pH + N + Dens + P + Ca + Mg + K + Na
##
## Df Sum of Sq RSS AIC
## - P 1 0.040 20.145 -25.675
## - Ca 1 0.132 20.237 -25.458
## <none> 20.105 -23.771
## - Dens 1 0.900 21.005 -23.668
## - N 1 0.988 21.093 -23.469
## - Mg 1 1.364 21.469 -22.619
## - K 1 3.787 23.892 -17.487
## - pH 1 4.164 24.269 -16.736
## - Na 1 112.490 132.595 64.773
##
## Step: AIC=-25.68
## Conduc ~ pH + N + Dens + Ca + Mg + K + Na
##
## Df Sum of Sq RSS AIC
## - Ca 1 0.100 20.245 -27.437
## <none> 20.145 -25.675
## - N 1 1.037 21.183 -25.265
## - Dens 1 1.122 21.267 -25.073
## - Mg 1 1.393 21.539 -24.465
## + P 1 0.040 20.105 -23.771
## - K 1 3.749 23.895 -19.483
## - pH 1 4.590 24.736 -17.822
## - Na 1 124.693 144.838 67.012
##
## Step: AIC=-27.44
## Conduc ~ pH + N + Dens + Mg + K + Na
##
## Df Sum of Sq RSS AIC
## <none> 20.245 -27.437
## - N 1 0.953 21.199 -27.229
## - Dens 1 1.266 21.511 -26.526
## - Mg 1 1.551 21.797 -25.894
## + Ca 1 0.100 20.145 -25.675
## + P 1 0.009 20.237 -25.458
## - K 1 4.249 24.495 -20.292
## - pH 1 8.638 28.883 -12.382
## - Na 1 125.443 145.688 65.293
summary(modelo_step)
##
## Call:
## lm(formula = Conduc ~ pH + N + Dens + Mg + K + Na, data = Soils)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.20385 -0.52459 -0.00915 0.40134 1.50754
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.04020 2.05533 2.452 0.018542 *
## pH -0.91000 0.21758 -4.182 0.000148 ***
## N 4.93184 3.54947 1.389 0.172194
## Dens 1.53105 0.95619 1.601 0.117012
## Mg -0.16353 0.09226 -1.772 0.083748 .
## K -2.00285 0.68274 -2.934 0.005465 **
## Na 0.99974 0.06272 15.939 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.7027 on 41 degrees of freedom
## Multiple R-squared: 0.9729, Adjusted R-squared: 0.9689
## F-statistic: 245.4 on 6 and 41 DF, p-value: < 2.2e-16
Interpretación: El modelo final selecciona las
variables más relevantes para explicar Conduc, optimizando
el equilibrio entre precisión y complejidad (criterio AIC).
Na y Conduc.