#///////////////////////////////////////////////////////////////////////
# Pacotes --------------------------------------------------------------

library(car)
## Loading required package: carData
library(DataExplorer)
library(corrplot)
## corrplot 0.92 loaded
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ purrr::%||%()   masks base::%||%()
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ✖ dplyr::recode() masks car::recode()
## ✖ purrr::some()   masks car::some()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
# options(scipen = 4, width = 120)
options(width = 120)

#///////////////////////////////////////////////////////////////////////
# Importação dos dados -------------------------------------------------

tb <- read_csv("Parecis_wrc_params.csv")
## Rows: 318 Columns: 42
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (2): Local, Profundidade
## dbl (39): ID, Estrutura, Carbono, Tensao, Ds, Umid, Argila, Areia, Silte, Muito_grossa, Grossa, Media, Fina, Muito_f...
## lgl  (1): SOC
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
attr(tb, "spec") <- NULL
str(tb)
## spc_tbl_ [318 × 42] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ ID          : num [1:318] 1 2 3 4 5 6 7 8 9 10 ...
##  $ Local       : chr [1:318] "IF CP01" "IF CP01" "IF CP01" "IF CP01" ...
##  $ Profundidade: chr [1:318] "0-0,05" "0-0,05" "0-0,05" "0,15-0,20" ...
##  $ Estrutura   : num [1:318] 1 2 2 1 2 2 1 2 2 1 ...
##  $ Carbono     : num [1:318] 1 1 2 1 1 2 1 1 2 1 ...
##  $ Tensao      : num [1:318] 0 0 0 0 0 0 0 0 0 0 ...
##  $ Ds          : num [1:318] 0.969 NA NA 1.357 NA ...
##  $ Umid        : num [1:318] 0.653 0.513 0.553 0.386 0.515 ...
##  $ Argila      : num [1:318] 508 508 508 568 568 ...
##  $ Areia       : num [1:318] 335 335 335 331 331 ...
##  $ Silte       : num [1:318] 157 157 157 101 101 ...
##  $ Muito_grossa: num [1:318] 0 0 0 0 0 0 0 0 0 0 ...
##  $ Grossa      : num [1:318] 5.5 5.5 5.5 6.5 6.5 6.5 35.5 35.5 35.5 31 ...
##  $ Media       : num [1:318] 122 122 122 118 118 ...
##  $ Fina        : num [1:318] 158 158 158 166 166 ...
##  $ Muito_fina  : num [1:318] 40.5 40.5 40.5 35 35 35 68 68 68 69 ...
##  $ SOC         : logi [1:318] NA NA NA NA NA NA ...
##  $ N           : num [1:318] 0.1436 0.1436 0.0748 0.0897 0.0897 ...
##  $ C           : num [1:318] 2.224 2.224 0.341 1.578 1.578 ...
##  $ Mg          : num [1:318] 0.478 0.478 NA NA NA ...
##  $ Al          : num [1:318] 10.2 10.2 10.8 11 11 ...
##  $ Si          : num [1:318] 17.1 17.1 17.2 17.6 17.6 ...
##  $ P           : num [1:318] 0.0302 0.0302 0.0459 0.0347 0.0347 0.0393 0.032 0.032 0.069 0.021 ...
##  $ Ca          : num [1:318] 0.736 0.736 0.611 0.552 0.552 ...
##  $ Ti          : num [1:318] 1.26 1.26 1.31 1.29 1.29 ...
##  $ V           : num [1:318] 0.0627 0.0627 0.0644 0.0616 0.0616 0.0686 0.0118 0.0118 0.0114 0.0143 ...
##  $ Cr          : num [1:318] 0.0162 0.0162 0.0238 0.0188 0.0188 0.02 0.0085 0.0085 0.007 0.0044 ...
##  $ Mn          : num [1:318] 0.0113 0.0113 0.014 0.006 0.006 0.0088 0.0018 0.0018 0.0015 NA ...
##  $ Fe          : num [1:318] 7.48 7.48 8.05 7.54 7.54 ...
##  $ Cu          : num [1:318] 0.0032 0.0032 0.0034 0.0031 0.0031 0.0043 0.0014 0.0014 0.0014 0.001 ...
##  $ Zn          : num [1:318] 0.0059 0.0059 0.0074 0.0058 0.0058 0.0063 0.003 0.003 0.0032 0.0017 ...
##  $ As          : num [1:318] 0.0017 0.0017 0.0021 0.0016 0.0016 0.0019 NA NA 0.0007 0.0003 ...
##  $ Zr          : num [1:318] 0.0407 0.0407 0.0502 0.0503 0.0503 0.0488 0.0152 0.0152 0.0199 0.0198 ...
##  $ thr         : num [1:318] 0.229 0.219 0.214 0.141 NA ...
##  $ ths         : num [1:318] 0.653 0.527 0.565 0.396 NA ...
##  $ alp         : num [1:318] 1.891 0.1 0.675 0.582 NA ...
##  $ nscal       : num [1:318] 9.202 0.733 1.041 0.718 NA ...
##  $ mscal       : num [1:318] 0.041 1.439 0.56 0.325 NA ...
##  $ I           : num [1:318] -0.29 1.802 0.949 2.105 NA ...
##  $ U           : num [1:318] 0.6 0.363 0.412 0.302 NA ...
##  $ S           : num [1:318] -0.1345 -0.0623 -0.0737 -0.0284 NA ...
##  $ CAD         : num [1:318] 2.89e-01 1.12e-07 2.05e-01 2.07e-01 NA ...
##  - attr(*, "problems")=<externalptr>
tb <-
    tb |>
    mutate(Estrutura = factor(Estrutura,
                              levels = c(1, 2),
                              labels = c("EstPres", "EstAus")),
           Carbono = factor(Carbono,
                            levels = c(1, 2),
                            labels = c("CarPres", "CarOxi")),
           Trat = interaction(Estrutura, Carbono,
                              drop = TRUE, sep = "_"))

tb |>
    count(Trat, Estrutura, Carbono)
## # A tibble: 3 × 4
##   Trat            Estrutura Carbono     n
##   <fct>           <fct>     <fct>   <int>
## 1 EstPres_CarPres EstPres   CarPres   106
## 2 EstAus_CarPres  EstAus    CarPres   106
## 3 EstAus_CarOxi   EstAus    CarOxi    106
unique(tb$Profundidade)
## [1] "0-0,05"    "0,15-0,20"
# tb <-
#     tb |>
#     # filter(Profundidade != "0-0,05")
#     filter(Profundidade == "0-0,05")

#///////////////////////////////////////////////////////////////////////
# Carbono incrementa a água disponível? --------------------------------

# Comparar `EstAus_CarPres` vs `EstAus_CarOxi`.

tb |>
    count(Trat, Profundidade)
## # A tibble: 6 × 3
##   Trat            Profundidade     n
##   <fct>           <chr>        <int>
## 1 EstPres_CarPres 0,15-0,20       53
## 2 EstPres_CarPres 0-0,05          53
## 3 EstAus_CarPres  0,15-0,20       53
## 4 EstAus_CarPres  0-0,05          53
## 5 EstAus_CarOxi   0,15-0,20       53
## 6 EstAus_CarOxi   0-0,05          53
tb_sel <-
    tb |>
    filter(
        Trat %in% c("EstAus_CarPres", "EstAus_CarOxi"),
        # Profundidade == "0-0,05",
        # Profundidade == "0,15-0,20",
        between(CAD, 0.000001, 1)) |>
    # mutate(CAD = CAD^(1/3)) |>
    select(Local, Profundidade, Trat, CAD)

tb_sel_wide <-
    tb_sel |>
    # arrange(Local, Trat) |>
    pivot_wider(names_from = Trat, values_from = CAD)

ggplot(tb_sel, aes(x = Trat, y = CAD)) +
    facet_wrap(~ Profundidade) +
    geom_boxplot() +
    geom_jitter(width = 0.05, color = "orange") +
    # Segmentos que conectam pontos do mesmo local.
    geom_segment(data = tb_sel_wide,
                 aes(x = 1,
                     xend = 2,
                     y = EstAus_CarPres,
                     yend = EstAus_CarOxi),
                 color = "gray", size = 0.5) +
    labs(title = "Carbono incrementa a água disponível?",
         x = "Tratamento",
         y = "CAD")
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: Removed 11 rows containing missing values or values outside the scale range
## (`geom_segment()`).
## Warning: Removed 16 rows containing missing values or values outside the scale range
## (`geom_segment()`).
## Warning: Removed 15 rows containing missing values or values outside the scale range
## (`geom_segment()`).

m0 <- lm(CAD ~ Local + Profundidade * Trat, data = tb_sel)
anova(m0)
## Analysis of Variance Table
## 
## Response: CAD
##                   Df  Sum Sq  Mean Sq F value    Pr(>F)    
## Local             49 0.76034 0.015517  2.6746 0.0001144 ***
## Profundidade       1 0.00023 0.000234  0.0403 0.8414708    
## Trat               1 0.12382 0.123820 21.3422  1.87e-05 ***
## Profundidade:Trat  1 0.00813 0.008133  1.4019 0.2407129    
## Residuals         65 0.37711 0.005802                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
par(mfrow = c(2, 2))
plot(m0)
## Warning: not plotting observations with leverage one:
##   1, 17, 18, 29, 67, 83, 92, 103, 106, 107
## Warning in sqrt(crit * p * (1 - hh)/hh): NaNs produced
## Warning in sqrt(crit * p * (1 - hh)/hh): NaNs produced

layout(1)

emmeans::emmeans(m0, "Trat")
## NOTE: Results may be misleading due to involvement in interactions
##  Trat           emmean     SE df lower.CL upper.CL
##  EstAus_CarPres  0.211 0.0100 65    0.191    0.231
##  EstAus_CarOxi   0.143 0.0128 65    0.118    0.169
## 
## Results are averaged over the levels of: Local, Profundidade 
## Confidence level used: 0.95
#///////////////////////////////////////////////////////////////////////
# Qual a importância da Argila para a água disponível? -----------------

tb |>
    count(Trat, Profundidade)
## # A tibble: 6 × 3
##   Trat            Profundidade     n
##   <fct>           <chr>        <int>
## 1 EstPres_CarPres 0,15-0,20       53
## 2 EstPres_CarPres 0-0,05          53
## 3 EstAus_CarPres  0,15-0,20       53
## 4 EstAus_CarPres  0-0,05          53
## 5 EstAus_CarOxi   0,15-0,20       53
## 6 EstAus_CarOxi   0-0,05          53
tb_sel <-
    tb |>
    filter(
        Trat %in% c("EstPres_CarPres"),
        # Profundidade == "0-0,05",
        # Profundidade == "0,15-0,20",
        between(CAD, 0.000001, 1)) |>
    # mutate(CAD = CAD^(1/3)) |>
    select(Local, Profundidade, Trat, Argila, CAD)

ggplot(tb_sel, aes(x = Argila, y = CAD)) +
    facet_wrap(~ Profundidade) +
    geom_point() +
    geom_smooth(method = "lm", se = FALSE) +
    labs(title = "Argila e água disponível",
         x = "Argila",
         y = "CAD")
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 2 rows containing non-finite outside the scale range (`stat_smooth()`).
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).

m0 <- lm(CAD ~ Profundidade * Argila, data = tb_sel)
anova(m0)
## Analysis of Variance Table
## 
## Response: CAD
##                     Df   Sum Sq  Mean Sq F value    Pr(>F)    
## Profundidade         1 0.048803 0.048803 13.8697 0.0003561 ***
## Argila               1 0.155877 0.155877 44.2996 2.835e-09 ***
## Profundidade:Argila  1 0.022999 0.022999  6.5363 0.0123902 *  
## Residuals           83 0.292052 0.003519                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
par(mfrow = c(2, 2))
plot(m0)

layout(1)

m1 <- update(m0, . ~ 0 + Profundidade/Argila)
summary(m1)$coefficients
##                                  Estimate   Std. Error  t value     Pr(>|t|)
## Profundidade0-0,05           0.0918009506 1.588907e-02 5.777615 1.282448e-07
## Profundidade0,15-0,20        0.0718919956 1.583686e-02 4.539536 1.888807e-05
## Profundidade0-0,05:Argila    0.0003169155 5.296993e-05 5.982931 5.345369e-08
## Profundidade0,15-0,20:Argila 0.0001494467 3.853511e-05 3.878196 2.098373e-04
#///////////////////////////////////////////////////////////////////////
# Qual a importância das frações de areia? -----------------------------

tb |>
    count(Trat, Profundidade)
## # A tibble: 6 × 3
##   Trat            Profundidade     n
##   <fct>           <chr>        <int>
## 1 EstPres_CarPres 0,15-0,20       53
## 2 EstPres_CarPres 0-0,05          53
## 3 EstAus_CarPres  0,15-0,20       53
## 4 EstAus_CarPres  0-0,05          53
## 5 EstAus_CarOxi   0,15-0,20       53
## 6 EstAus_CarOxi   0-0,05          53
tb_sel <-
    tb |>
    filter(
        Trat %in% c("EstPres_CarPres"),
        # Profundidade == "0-0,05",
        # Profundidade == "0,15-0,20",
        between(CAD, 0.000001, 1)) |>
    # mutate(CAD = CAD^(1/3)) |>
    select(Local, Profundidade, Trat,
           Muito_grossa, Grossa, Media, Fina, Muito_fina, CAD)

frac_areia <- c("Muito_grossa", "Grossa", "Media", "Fina", "Muito_fina")
tb_sel |>
    pivot_longer(cols = frac_areia,
                 names_to = "Fracao",
                 values_to = "Valor") |>
    mutate(Fracao = factor(Fracao, levels = frac_areia)) |>
    ggplot(aes(x = Valor, y = CAD, color = Profundidade)) +
    facet_wrap(~Fracao, scales = "free_x") +
    geom_point() +
    geom_smooth(method = "lm", se = FALSE) +
    labs(title = "Frações de areia e água disponível",
         x = "Argila",
         y = "CAD")
## Warning: Using an external vector in selections was deprecated in tidyselect 1.1.0.
## ℹ Please use `all_of()` or `any_of()` instead.
##   # Was:
##   data %>% select(frac_areia)
## 
##   # Now:
##   data %>% select(all_of(frac_areia))
## 
## See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 10 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 10 rows containing missing values or values outside the scale range
## (`geom_point()`).

fits <-
    map(frac_areia, function(x) {
        formula <- sprintf("CAD ~ Profundidade * %s", x)
        tb_sel |>
            select(Profundidade, x, CAD) |>
            lm(formula, data = _)
    })
## Warning: Using an external vector in selections was deprecated in tidyselect 1.1.0.
## ℹ Please use `all_of()` or `any_of()` instead.
##   # Was:
##   data %>% select(x)
## 
##   # Now:
##   data %>% select(all_of(x))
## 
## See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
names(fits) <- frac_areia

map_dbl(fits, ~summary(.)$r.squared)
## Muito_grossa       Grossa        Media         Fina   Muito_fina 
##    0.1292112    0.5091890    0.5581455    0.5130215    0.3902592
map(fits, anova)
## $Muito_grossa
## Analysis of Variance Table
## 
## Response: CAD
##                           Df  Sum Sq  Mean Sq F value   Pr(>F)   
## Profundidade               1 0.04880 0.048803  8.9502 0.003652 **
## Muito_grossa               1 0.01834 0.018343  3.3640 0.070220 . 
## Profundidade:Muito_grossa  1 0.00001 0.000009  0.0017 0.967595   
## Residuals                 83 0.45258 0.005453                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Grossa
## Analysis of Variance Table
## 
## Response: CAD
##                     Df   Sum Sq  Mean Sq F value    Pr(>F)    
## Profundidade         1 0.048803 0.048803 15.8794 0.0001444 ***
## Grossa               1 0.205193 0.205193 66.7647 2.969e-12 ***
## Profundidade:Grossa  1 0.010646 0.010646  3.4638 0.0662654 .  
## Residuals           83 0.255090 0.003073                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Media
## Analysis of Variance Table
## 
## Response: CAD
##                    Df   Sum Sq  Mean Sq F value    Pr(>F)    
## Profundidade        1 0.048803 0.048803 17.6388 6.680e-05 ***
## Media               1 0.221140 0.221140 79.9258 8.605e-14 ***
## Profundidade:Media  1 0.020143 0.020143  7.2801  0.008444 ** 
## Residuals          83 0.229646 0.002767                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Fina
## Analysis of Variance Table
## 
## Response: CAD
##                   Df   Sum Sq  Mean Sq F value    Pr(>F)    
## Profundidade       1 0.048803 0.048803  16.004 0.0001366 ***
## Fina               1 0.182368 0.182368  59.805 2.204e-11 ***
## Profundidade:Fina  1 0.035463 0.035463  11.629 0.0010047 ** 
## Residuals         83 0.253098 0.003049                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Muito_fina
## Analysis of Variance Table
## 
## Response: CAD
##                         Df  Sum Sq  Mean Sq F value    Pr(>F)    
## Profundidade             1 0.04880 0.048803 12.7821 0.0005866 ***
## Muito_fina               1 0.13035 0.130346 34.1391 9.722e-08 ***
## Profundidade:Muito_fina  1 0.02368 0.023681  6.2022 0.0147534 *  
## Residuals               83 0.31690 0.003818                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
map(fits, ~summary(.)$coefficients)
## $Muito_grossa
##                                        Estimate  Std. Error     t value     Pr(>|t|)
## (Intercept)                         0.160963209 0.012321408 13.06370214 7.761805e-22
## Profundidade0,15-0,20              -0.039766672 0.017032249 -2.33478685 2.197013e-02
## Muito_grossa                        0.008313858 0.004624521  1.79777724 7.584875e-02
## Profundidade0,15-0,20:Muito_grossa  0.001061895 0.026060555  0.04074721 9.675953e-01
## 
## $Grossa
##                                  Estimate   Std. Error   t value     Pr(>|t|)
## (Intercept)                   0.255788965 0.0145844205 17.538507 1.192576e-29
## Profundidade0,15-0,20        -0.094249659 0.0191464462 -4.922567 4.279103e-06
## Grossa                       -0.004091921 0.0005665362 -7.222701 2.241787e-10
## Profundidade0,15-0,20:Grossa  0.001537317 0.0008260100  1.861136 6.626535e-02
## 
## $Media
##                                  Estimate   Std. Error   t value     Pr(>|t|)
## (Intercept)                  0.2748301109 1.505140e-02 18.259439 8.314554e-31
## Profundidade0,15-0,20       -0.0987559442 2.090379e-02 -4.724308 9.298217e-06
## Media                       -0.0004370271 5.307088e-05 -8.234782 2.214651e-12
## Profundidade0,15-0,20:Media  0.0002027491 7.514347e-05  2.698160 8.444471e-03
## 
## $Fina
##                                 Estimate   Std. Error   t value     Pr(>|t|)
## (Intercept)                 0.2987979418 1.860829e-02 16.057247 3.477059e-27
## Profundidade0,15-0,20      -0.1244659636 2.560987e-02 -4.860077 5.474186e-06
## Fina                       -0.0005168189 6.656078e-05 -7.764617 1.910632e-11
## Profundidade0,15-0,20:Fina  0.0003100524 9.091922e-05  3.410196 1.004651e-03
## 
## $Muito_fina
##                                      Estimate   Std. Error   t value     Pr(>|t|)
## (Intercept)                       0.288041737 0.0224978270 12.803091 2.389035e-21
## Profundidade0,15-0,20            -0.118222388 0.0302480815 -3.908426 1.888637e-04
## Muito_fina                       -0.002203420 0.0003811947 -5.780302 1.267934e-07
## Profundidade0,15-0,20:Muito_fina  0.001286182 0.0005164523  2.490418 1.475339e-02
#///////////////////////////////////////////////////////////////////////
# Os nutrientes conseguem prever a CAD? --------------------------------

str(tb)
## tibble [318 × 43] (S3: tbl_df/tbl/data.frame)
##  $ ID          : num [1:318] 1 2 3 4 5 6 7 8 9 10 ...
##  $ Local       : chr [1:318] "IF CP01" "IF CP01" "IF CP01" "IF CP01" ...
##  $ Profundidade: chr [1:318] "0-0,05" "0-0,05" "0-0,05" "0,15-0,20" ...
##  $ Estrutura   : Factor w/ 2 levels "EstPres","EstAus": 1 2 2 1 2 2 1 2 2 1 ...
##  $ Carbono     : Factor w/ 2 levels "CarPres","CarOxi": 1 1 2 1 1 2 1 1 2 1 ...
##  $ Tensao      : num [1:318] 0 0 0 0 0 0 0 0 0 0 ...
##  $ Ds          : num [1:318] 0.969 NA NA 1.357 NA ...
##  $ Umid        : num [1:318] 0.653 0.513 0.553 0.386 0.515 ...
##  $ Argila      : num [1:318] 508 508 508 568 568 ...
##  $ Areia       : num [1:318] 335 335 335 331 331 ...
##  $ Silte       : num [1:318] 157 157 157 101 101 ...
##  $ Muito_grossa: num [1:318] 0 0 0 0 0 0 0 0 0 0 ...
##  $ Grossa      : num [1:318] 5.5 5.5 5.5 6.5 6.5 6.5 35.5 35.5 35.5 31 ...
##  $ Media       : num [1:318] 122 122 122 118 118 ...
##  $ Fina        : num [1:318] 158 158 158 166 166 ...
##  $ Muito_fina  : num [1:318] 40.5 40.5 40.5 35 35 35 68 68 68 69 ...
##  $ SOC         : logi [1:318] NA NA NA NA NA NA ...
##  $ N           : num [1:318] 0.1436 0.1436 0.0748 0.0897 0.0897 ...
##  $ C           : num [1:318] 2.224 2.224 0.341 1.578 1.578 ...
##  $ Mg          : num [1:318] 0.478 0.478 NA NA NA ...
##  $ Al          : num [1:318] 10.2 10.2 10.8 11 11 ...
##  $ Si          : num [1:318] 17.1 17.1 17.2 17.6 17.6 ...
##  $ P           : num [1:318] 0.0302 0.0302 0.0459 0.0347 0.0347 0.0393 0.032 0.032 0.069 0.021 ...
##  $ Ca          : num [1:318] 0.736 0.736 0.611 0.552 0.552 ...
##  $ Ti          : num [1:318] 1.26 1.26 1.31 1.29 1.29 ...
##  $ V           : num [1:318] 0.0627 0.0627 0.0644 0.0616 0.0616 0.0686 0.0118 0.0118 0.0114 0.0143 ...
##  $ Cr          : num [1:318] 0.0162 0.0162 0.0238 0.0188 0.0188 0.02 0.0085 0.0085 0.007 0.0044 ...
##  $ Mn          : num [1:318] 0.0113 0.0113 0.014 0.006 0.006 0.0088 0.0018 0.0018 0.0015 NA ...
##  $ Fe          : num [1:318] 7.48 7.48 8.05 7.54 7.54 ...
##  $ Cu          : num [1:318] 0.0032 0.0032 0.0034 0.0031 0.0031 0.0043 0.0014 0.0014 0.0014 0.001 ...
##  $ Zn          : num [1:318] 0.0059 0.0059 0.0074 0.0058 0.0058 0.0063 0.003 0.003 0.0032 0.0017 ...
##  $ As          : num [1:318] 0.0017 0.0017 0.0021 0.0016 0.0016 0.0019 NA NA 0.0007 0.0003 ...
##  $ Zr          : num [1:318] 0.0407 0.0407 0.0502 0.0503 0.0503 0.0488 0.0152 0.0152 0.0199 0.0198 ...
##  $ thr         : num [1:318] 0.229 0.219 0.214 0.141 NA ...
##  $ ths         : num [1:318] 0.653 0.527 0.565 0.396 NA ...
##  $ alp         : num [1:318] 1.891 0.1 0.675 0.582 NA ...
##  $ nscal       : num [1:318] 9.202 0.733 1.041 0.718 NA ...
##  $ mscal       : num [1:318] 0.041 1.439 0.56 0.325 NA ...
##  $ I           : num [1:318] -0.29 1.802 0.949 2.105 NA ...
##  $ U           : num [1:318] 0.6 0.363 0.412 0.302 NA ...
##  $ S           : num [1:318] -0.1345 -0.0623 -0.0737 -0.0284 NA ...
##  $ CAD         : num [1:318] 2.89e-01 1.12e-07 2.05e-01 2.07e-01 NA ...
##  $ Trat        : Factor w/ 3 levels "EstPres_CarPres",..: 1 2 3 1 2 3 1 2 3 1 ...
names(tb) |>
    dput()
## c("ID", "Local", "Profundidade", "Estrutura", "Carbono", "Tensao", 
## "Ds", "Umid", "Argila", "Areia", "Silte", "Muito_grossa", "Grossa", 
## "Media", "Fina", "Muito_fina", "SOC", "N", "C", "Mg", "Al", "Si", 
## "P", "Ca", "Ti", "V", "Cr", "Mn", "Fe", "Cu", "Zn", "As", "Zr", 
## "thr", "ths", "alp", "nscal", "mscal", "I", "U", "S", "CAD", 
## "Trat")
qui <- c(
    "N", "C", "Mg", "Al", "Si", "P", "Ca", "Ti", "V", "Cr", "Mn",
    "Fe", "Cu", "Zn", "As", "Zr"
)

tb_sel <-
    tb |>
    filter(
        Trat %in% c("EstPres_CarPres"),
        # Profundidade == "0-0,05",
        # Profundidade == "0,15-0,20",
        between(CAD, 0.000001, 1)) |>
    # mutate(CAD = CAD^(1/3)) |>
    select(Local, Profundidade, Trat, CAD, any_of(qui))


tb_sel |>
    pivot_longer(cols = qui,
                 names_to = "Nutriente",
                 values_to = "Valor") |>
    mutate(Fracao = factor(Nutriente, levels = qui)) |>
    ggplot(aes(x = Valor, y = CAD, color = Profundidade)) +
    facet_wrap(~Nutriente, scales = "free_x") +
    geom_point() +
    geom_smooth(method = "lm", se = FALSE) +
    labs(title = "Frações de areia e água disponível",
         x = "Argila",
         y = "CAD")
## Warning: Using an external vector in selections was deprecated in tidyselect 1.1.0.
## ℹ Please use `all_of()` or `any_of()` instead.
##   # Was:
##   data %>% select(qui)
## 
##   # Now:
##   data %>% select(all_of(qui))
## 
## See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 75 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 75 rows containing missing values or values outside the scale range
## (`geom_point()`).

fits <-
    map(qui, function(x) {
        formula <- sprintf("CAD ~ Profundidade * %s", x)
        tb_sel |>
            select(Profundidade, x, CAD) |>
            lm(formula, data = _)
    })
names(fits) <- qui

map_dbl(fits, ~summary(.)$r.squared)
##         N         C        Mg        Al        Si         P        Ca        Ti         V        Cr        Mn        Fe 
## 0.5213386 0.4702840 0.2127714 0.5364417 0.5889933 0.1394291 0.1371322 0.6022130 0.6256020 0.5817285 0.1606472 0.6112882 
##        Cu        Zn        As        Zr 
## 0.4553027 0.3750770 0.5900188 0.5728256
map(fits, anova)
## $N
## Analysis of Variance Table
## 
## Response: CAD
##                Df   Sum Sq  Mean Sq F value    Pr(>F)    
## Profundidade    1 0.048803 0.048803 16.2824 0.0001208 ***
## N               1 0.221499 0.221499 73.8997 4.192e-13 ***
## Profundidade:N  1 0.000654 0.000654  0.2181 0.6416885    
## Residuals      83 0.248776 0.002997                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $C
## Analysis of Variance Table
## 
## Response: CAD
##                Df   Sum Sq  Mean Sq F value    Pr(>F)    
## Profundidade    1 0.048803 0.048803  14.713 0.0002431 ***
## C               1 0.002620 0.002620   0.790 0.3766750    
## Profundidade:C  1 0.192998 0.192998  58.185 3.566e-11 ***
## Residuals      83 0.275310 0.003317                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Mg
## Analysis of Variance Table
## 
## Response: CAD
##                 Df   Sum Sq  Mean Sq F value  Pr(>F)  
## Profundidade     1 0.027833 0.027833  5.7886 0.01946 *
## Mg               1 0.033661 0.033661  7.0008 0.01056 *
## Profundidade:Mg  1 0.011281 0.011281  2.3462 0.13122  
## Residuals       56 0.269260 0.004808                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Al
## Analysis of Variance Table
## 
## Response: CAD
##                 Df   Sum Sq  Mean Sq F value    Pr(>F)    
## Profundidade     1 0.050197 0.050197  17.271 7.836e-05 ***
## Al               1 0.189860 0.189860  65.324 4.460e-12 ***
## Profundidade:Al  1 0.039103 0.039103  13.454 0.0004305 ***
## Residuals       83 0.241232 0.002906                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Si
## Analysis of Variance Table
## 
## Response: CAD
##                 Df   Sum Sq  Mean Sq F value    Pr(>F)    
## Profundidade     1 0.050197 0.050197  19.480 3.036e-05 ***
## Si               1 0.225730 0.225730  87.596 1.248e-14 ***
## Profundidade:Si  1 0.030581 0.030581  11.867 0.0008984 ***
## Residuals       83 0.213885 0.002577                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $P
## Analysis of Variance Table
## 
## Response: CAD
##                Df  Sum Sq  Mean Sq F value  Pr(>F)   
## Profundidade    1 0.05020 0.050197  9.3034 0.00307 **
## P               1 0.02038 0.020375  3.7763 0.05537 . 
## Profundidade:P  1 0.00199 0.001985  0.3679 0.54578   
## Residuals      83 0.44783 0.005396                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Ca
## Analysis of Variance Table
## 
## Response: CAD
##                 Df  Sum Sq  Mean Sq F value   Pr(>F)   
## Profundidade     1 0.05020 0.050197  9.2786 0.003107 **
## Ca               1 0.01015 0.010152  1.8766 0.174421   
## Profundidade:Ca  1 0.01101 0.011013  2.0357 0.157394   
## Residuals       83 0.44903 0.005410                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Ti
## Analysis of Variance Table
## 
## Response: CAD
##                 Df   Sum Sq  Mean Sq F value    Pr(>F)    
## Profundidade     1 0.050197 0.050197  20.127 2.310e-05 ***
## Ti               1 0.229632 0.229632  92.072 4.211e-15 ***
## Profundidade:Ti  1 0.033558 0.033558  13.455 0.0004303 ***
## Residuals       83 0.207005 0.002494                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $V
## Analysis of Variance Table
## 
## Response: CAD
##                Df   Sum Sq  Mean Sq F value    Pr(>F)    
## Profundidade    1 0.050197 0.050197  21.384 1.367e-05 ***
## V               1 0.242335 0.242335 103.236 3.157e-16 ***
## Profundidade:V  1 0.033026 0.033026  14.069 0.0003252 ***
## Residuals      83 0.194834 0.002347                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Cr
## Analysis of Variance Table
## 
## Response: CAD
##                 Df   Sum Sq  Mean Sq F value    Pr(>F)    
## Profundidade     1 0.050197 0.050197  19.141 3.505e-05 ***
## Cr               1 0.223507 0.223507  85.227 2.244e-14 ***
## Profundidade:Cr  1 0.029023 0.029023  11.067  0.001311 ** 
## Residuals       83 0.217665 0.002622                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Mn
## Analysis of Variance Table
## 
## Response: CAD
##                 Df  Sum Sq  Mean Sq F value   Pr(>F)   
## Profundidade     1 0.02479 0.024791  4.9002 0.029978 * 
## Mn               1 0.04503 0.045031  8.9008 0.003874 **
## Profundidade:Mn  1 0.00086 0.000864  0.1708 0.680620   
## Residuals       73 0.36932 0.005059                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Fe
## Analysis of Variance Table
## 
## Response: CAD
##                 Df   Sum Sq  Mean Sq F value    Pr(>F)    
## Profundidade     1 0.050197 0.050197  20.597 1.897e-05 ***
## Fe               1 0.234691 0.234691  96.298 1.549e-15 ***
## Profundidade:Fe  1 0.033221 0.033221  13.631  0.000397 ***
## Residuals       83 0.202283 0.002437                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Cu
## Analysis of Variance Table
## 
## Response: CAD
##                 Df   Sum Sq  Mean Sq F value    Pr(>F)    
## Profundidade     1 0.050197 0.050197 14.6985 0.0002447 ***
## Cu               1 0.180020 0.180020 52.7123 1.891e-10 ***
## Profundidade:Cu  1 0.006719 0.006719  1.9674 0.1644509    
## Residuals       83 0.283456 0.003415                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Zn
## Analysis of Variance Table
## 
## Response: CAD
##                 Df  Sum Sq  Mean Sq F value    Pr(>F)    
## Profundidade     1 0.05020 0.050197 12.8115 0.0005787 ***
## Zn               1 0.14340 0.143404 36.5999 4.011e-08 ***
## Profundidade:Zn  1 0.00159 0.001586  0.4049 0.5263184    
## Residuals       83 0.32521 0.003918                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $As
## Analysis of Variance Table
## 
## Response: CAD
##                 Df   Sum Sq  Mean Sq F value    Pr(>F)    
## Profundidade     1 0.060502 0.060502  23.215 7.116e-06 ***
## As               1 0.188897 0.188897  72.481 1.040e-12 ***
## Profundidade:As  1 0.039400 0.039400  15.118 0.0002127 ***
## Residuals       77 0.200675 0.002606                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Zr
## Analysis of Variance Table
## 
## Response: CAD
##                 Df   Sum Sq  Mean Sq F value    Pr(>F)    
## Profundidade     1 0.050197 0.050197  18.742 4.155e-05 ***
## Zr               1 0.215209 0.215209  80.353 7.709e-14 ***
## Profundidade:Zr  1 0.032688 0.032688  12.205 0.0007672 ***
## Residuals       83 0.222298 0.002678                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
map(fits, ~summary(.)$coefficients)
## $N
##                            Estimate Std. Error    t value     Pr(>|t|)
## (Intercept)              0.01350543 0.02243159  0.6020720 5.487674e-01
## Profundidade0,15-0,20    0.04141896 0.02882025  1.4371476 1.544353e-01
## N                        1.45344684 0.19342321  7.5143354 5.978894e-11
## Profundidade0,15-0,20:N -0.16896426 0.36176973 -0.4670492 6.416885e-01
## 
## $C
##                            Estimate Std. Error   t value     Pr(>|t|)
## (Intercept)              0.02619601 0.02079237  1.259885 2.112417e-01
## Profundidade0,15-0,20    0.09520874 0.02257128  4.218137 6.249717e-05
## C                        0.08599089 0.01127321  7.627899 3.565376e-11
## Profundidade0,15-0,20:C -0.08599075 0.01127321 -7.627887 3.565577e-11
## 
## $Mg
##                            Estimate Std. Error   t value     Pr(>|t|)
## (Intercept)               0.3132459 0.05508927  5.686151 4.896859e-07
## Profundidade0,15-0,20    -0.1682615 0.08087765 -2.080444 4.207199e-02
## Mg                       -0.2613972 0.08726710 -2.995369 4.077405e-03
## Profundidade0,15-0,20:Mg  0.2012633 0.13139645  1.531726 1.312203e-01
## 
## $Al
##                             Estimate  Std. Error   t value     Pr(>|t|)
## (Intercept)              -0.12546195 0.037696143 -3.328244 1.304572e-03
## Profundidade0,15-0,20     0.12737423 0.049942447  2.550420 1.259612e-02
## Al                        0.03439764 0.004283605  8.030068 5.668474e-12
## Profundidade0,15-0,20:Al -0.02062487 0.005622940 -3.667986 4.305115e-04
## 
## $Si
##                             Estimate  Std. Error   t value     Pr(>|t|)
## (Intercept)               0.59284273 0.048295648 12.275283 2.384253e-20
## Profundidade0,15-0,20    -0.27146391 0.065625436 -4.136565 8.402035e-05
## Si                       -0.02115072 0.002384266 -8.870957 1.183565e-13
## Profundidade0,15-0,20:Si  0.01115844 0.003239133  3.444884 8.984006e-04
## 
## $P
##                           Estimate Std. Error    t value     Pr(>|t|)
## (Intercept)              0.2485639 0.04205314  5.9107084 7.281404e-08
## Profundidade0,15-0,20   -0.1075318 0.05334467 -2.0157926 4.705685e-02
## P                       -1.7586149 0.90679609 -1.9393719 5.585338e-02
## Profundidade0,15-0,20:P  0.9581386 1.57955322  0.6065884 5.457797e-01
## 
## $Ca
##                             Estimate Std. Error   t value     Pr(>|t|)
## (Intercept)               0.23550498 0.03505847  6.717491 2.156421e-09
## Profundidade0,15-0,20    -0.11711425 0.04536183 -2.581780 1.158567e-02
## Ca                       -0.06384028 0.03234875 -1.973501 5.176592e-02
## Profundidade0,15-0,20:Ca  0.06854568 0.04804220  1.426781 1.573941e-01
## 
## $Ti
##                              Estimate Std. Error   t value     Pr(>|t|)
## (Intercept)              -0.003808733 0.02031747 -0.187461 8.517567e-01
## Profundidade0,15-0,20     0.044843085 0.02810127  1.595767 1.143416e-01
## Ti                        0.187534051 0.02032896  9.224972 2.315958e-14
## Profundidade0,15-0,20:Ti -0.101922551 0.02778606 -3.668118 4.303214e-04
## 
## $V
##                            Estimate Std. Error   t value     Pr(>|t|)
## (Intercept)              0.03228826 0.01599758  2.018322 4.678724e-02
## Profundidade0,15-0,20    0.02372997 0.02228528  1.064827 2.900421e-01
## V                        3.19495745 0.32930337  9.702170 2.578570e-15
## Profundidade0,15-0,20:V -1.69858156 0.45284485 -3.750913 3.251726e-04
## 
## $Cr
##                             Estimate Std. Error    t value     Pr(>|t|)
## (Intercept)               0.00571906  0.0209361  0.2731674 7.854031e-01
## Profundidade0,15-0,20     0.04167767  0.0268645  1.5514031 1.246100e-01
## Cr                       10.35193906  1.2243872  8.4547916 8.050400e-13
## Profundidade0,15-0,20:Cr -5.29241049  1.5908729 -3.3267338 1.310816e-03
## 
## $Mn
##                             Estimate Std. Error    t value     Pr(>|t|)
## (Intercept)               0.12462622 0.02091067  5.9599329 8.225652e-08
## Profundidade0,15-0,20    -0.02647168 0.03430232 -0.7717168 4.427732e-01
## Mn                        3.51987176 1.35058843  2.6061764 1.109211e-02
## Profundidade0,15-0,20:Mn  1.47738685 3.57486732  0.4132704 6.806200e-01
## 
## $Fe
##                             Estimate  Std. Error    t value     Pr(>|t|)
## (Intercept)               0.03712734 0.016003752  2.3199148 2.280168e-02
## Profundidade0,15-0,20     0.02049070 0.022451019  0.9126847 3.640518e-01
## Fe                        0.02362469 0.002511831  9.4053682 1.009281e-14
## Profundidade0,15-0,20:Fe -0.01271258 0.003443236 -3.6920430 3.970096e-04
## 
## $Cu
##                              Estimate  Std. Error    t value     Pr(>|t|)
## (Intercept)                0.04590651  0.02090908  2.1955292 3.091710e-02
## Profundidade0,15-0,20      0.01048837  0.02970898  0.3530369 7.249558e-01
## Cu                        43.12071625  6.57505690  6.5582271 4.365432e-09
## Profundidade0,15-0,20:Cu -14.83164723 10.57402482 -1.4026492 1.644509e-01
## 
## $Zn
##                             Estimate Std. Error    t value     Pr(>|t|)
## (Intercept)               0.06476343 0.02291332  2.8264540 5.895615e-03
## Profundidade0,15-0,20    -0.01411057 0.03248779 -0.4343346 6.651726e-01
## Zn                       17.06862181 3.38000782  5.0498764 2.578831e-06
## Profundidade0,15-0,20:Zn  4.59899767 7.22748437  0.6363207 5.263184e-01
## 
## $As
##                              Estimate  Std. Error    t value     Pr(>|t|)
## (Intercept)                0.05371852  0.01665850  3.2246908 1.851272e-03
## Profundidade0,15-0,20      0.02123262  0.02256904  0.9407854 3.497570e-01
## As                        89.80551572 10.46357064  8.5826836 7.652269e-13
## Profundidade0,15-0,20:As -54.70010286 14.06828380 -3.8881859 2.127042e-04
## 
## $Zr
##                             Estimate Std. Error    t value     Pr(>|t|)
## (Intercept)               0.01405273 0.01968376  0.7139249 4.772765e-01
## Profundidade0,15-0,20     0.03253912 0.02769165  1.1750520 2.433339e-01
## Zr                        4.94960507 0.57249693  8.6456447 3.342782e-13
## Profundidade0,15-0,20:Zr -2.72096417 0.77885980 -3.4935224 7.671559e-04
#///////////////////////////////////////////////////////////////////////