# Executar com `Rscript -e 'rmarkdown::render("2_regressao_simples.R")'`
#///////////////////////////////////////////////////////////////////////
# Pacotes --------------------------------------------------------------

rm(list = ls())

suppressPackageStartupMessages({
    library(car)
    library(DataExplorer)
    library(corrplot)
    library(tidyverse)
})

# 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 = "_"),
           Profundidade = paste0(Profundidade, " m") |>
               str_replace_all(",", ".")
           )

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
tb |>
    count(Profundidade)
## # A tibble: 2 × 2
##   Profundidade     n
##   <chr>        <int>
## 1 0-0.05 m       159
## 2 0.15-0.20 m    159
# tb |>
#     count(Local, Profundidade) |>
#     print(n = Inf)

# 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-0.05 m        53
## 2 EstPres_CarPres 0.15-0.20 m     53
## 3 EstAus_CarPres  0-0.05 m        53
## 4 EstAus_CarPres  0.15-0.20 m     53
## 5 EstAus_CarOxi   0-0.05 m        53
## 6 EstAus_CarOxi   0.15-0.20 m     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)

# DANGER: O comando acima exclui locais que tiveram CAD = 0.
tb_sel |>
    count(Local) |>
    arrange(n) |>
    print(n = Inf)
## # A tibble: 50 × 2
##    Local       n
##    <chr>   <int>
##  1 IF CP01     1
##  2 P06         1
##  3 P07         1
##  4 P10         1
##  5 P14         1
##  6 P30         1
##  7 P36         1
##  8 P41         1
##  9 P42         1
## 10 P45         1
## 11 P47         1
## 12 P48         1
## 13 P09         2
## 14 P12         2
## 15 P13         2
## 16 P18         2
## 17 P19         2
## 18 P23         2
## 19 P25         2
## 20 P28         2
## 21 P35         2
## 22 P37         2
## 23 P38         2
## 24 P39         2
## 25 P40         2
## 26 P46         2
## 27 P51         2
## 28 P03         3
## 29 P08         3
## 30 P15         3
## 31 P16         3
## 32 P21         3
## 33 P24         3
## 34 P27         3
## 35 P31         3
## 36 P32         3
## 37 P34         3
## 38 P43         3
## 39 P44.1       3
## 40 P44.2       3
## 41 P49         3
## 42 P50         3
## 43 P52         3
## 44 P02         4
## 45 P04         4
## 46 P05         4
## 47 P17         4
## 48 P20         4
## 49 P26         4
## 50 P33         4
tb_sel_wide <-
    tb_sel |>
    # arrange(Local, Trat) |>
    pivot_wider(names_from = Trat, values_from = CAD) |>
    drop_na()

tb_sel |>
    count(Profundidade)
## # A tibble: 2 × 2
##   Profundidade     n
##   <chr>        <int>
## 1 0-0.05 m        49
## 2 0.15-0.20 m     69
tb_sel <-
    tb_sel_wide |>
    pivot_longer(cols = c(4, 3),
                 names_to = "Trat",
                 values_to = "CAD") |>
    mutate(Trat = factor(Trat, levels = levels(tb$Trat)))

# ATTENTION: Os NA foram removidos para manter apenas observações com
# pares completos.

legend <-
    list(trat_levels = c(
        "EstAus_CarPres" = expression(S^{"-"}~C^{"+"}),
        "EstAus_CarOxi" = expression(S^{"-"}~C^{"-"}),
        "EstPres_CarPres" = expression(S^{"+"}~C^{"+"})
    ),
    areia_levels = c(
        "Grossa" = "Coarse sand",
        "Media" = "Medium sand",
        "Fina" = "Fine sand"
    ),
    argila_axis = expression("Clay content"~(g~kg^{-1})),
    trat_axis = "Treatments",
    profundidade_axis = "Depth (m)",
    concentracao_axis = "Total elemental content (%)",
    awc_label = expression("Available water capacity"~(cm^3~cm^{-3})),
    rwc = expression("Residual water content"~(theta[r] * ", " ~ cm^3~cm^{-3})),
    wci = expression("Water content at the inflection point" ~ (theta[I] * ", " ~ cm^3~cm^{-3})),
    wcs = expression("Water content at saturation" ~ (theta[s] * ", " ~ cm^3~cm^{-3})),
    alpha = expression("Shape parameter" ~ (alpha * ", " ~ kPa^{-1})),
    n = expression("Shape parameter" ~ (italic(n))),
    m = expression("Shape parameter" ~ (italic(m))),
    I = expression("Tension at the inflection point" ~ (kPa)),
    # S = expression("Slope at the inflection point" ~ ("S - index," ~ cm^3~cm^{-3}~kPa^{-1}))
    S = "Slope at the inflection point (S - index)"
    )

gg <-
    tb_sel |>
    ggplot(data = _,
           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) +
    scale_x_discrete(labels = legend$trat_levels) +
    labs(x = legend$trat_axis,
         y = legend$awc_label)
## 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.
gg

dir.create("figs", showWarnings = FALSE)

# pdf("figs/awc_carbono_estrutura.pdf",
#     width = 8, height = 6)
# print(gg)
# dev.off()

postscript("figs/awc_carbono_estrutura.eps",
           width = 8, height = 6)
print(gg)
dev.off()
## png 
##   2
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             30 0.309631 0.010321  1.4621 0.1264931    
## Profundidade       1 0.000327 0.000327  0.0464 0.8305310    
## Trat               1 0.116371 0.116371 16.4849 0.0002092 ***
## Profundidade:Trat  1 0.001838 0.001838  0.2604 0.6125169    
## Residuals         42 0.296486 0.007059                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
par(mfrow = c(2, 2))
plot(m0)

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.198 0.0156 42   0.1663    0.229
##  EstAus_CarOxi   0.123 0.0156 42   0.0913    0.154
## 
## Results are averaged over the levels of: Local, Profundidade 
## Confidence level used: 0.95
# im <- influence.measures(m0)
# tb_sel[im$infmat[, "hat"] > 0.9, ]

#///////////////////////////////////////////////////////////////////////
# Estrutura interfere na á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-0.05 m        53
## 2 EstPres_CarPres 0.15-0.20 m     53
## 3 EstAus_CarPres  0-0.05 m        53
## 4 EstAus_CarPres  0.15-0.20 m     53
## 5 EstAus_CarOxi   0-0.05 m        53
## 6 EstAus_CarOxi   0.15-0.20 m     53
tb_sel <-
    tb |>
    filter(
        Trat %in% c("EstPres_CarPres", "EstAus_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)

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

tb_sel |>
    count(Profundidade)
## # A tibble: 2 × 2
##   Profundidade     n
##   <chr>        <int>
## 1 0-0.05 m        73
## 2 0.15-0.20 m     85
# ATTENTION: Os NA foram removidos para manter apenas observações com
# pares completos.

tb_sel <-
    tb_sel_wide |>
    pivot_longer(cols = c(4, 3),
                 names_to = "Trat",
                 values_to = "CAD") |>
    mutate(Trat = factor(Trat, levels = levels(tb$Trat)))

gg <-
    tb_sel |>
    ggplot(data = _,
       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 = EstPres_CarPres,
                     yend = EstAus_CarPres),
                 color = "gray", size = 0.5) +
    scale_x_discrete(labels = legend$trat_levels) +
    labs(x = legend$trat_axis,
         y = legend$awc_label)
gg

postscript("figs/awc_estrutura_carbono_preservado.eps",
           width = 8, height = 6)
print(gg)
dev.off()
## png 
##   2
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             41 0.68364 0.016674  3.4060 2.070e-06 ***
## Profundidade       1 0.00270 0.002699  0.5513    0.4601    
## Trat               1 0.11300 0.113000 23.0821 7.772e-06 ***
## Profundidade:Trat  1 0.00639 0.006386  1.3045    0.2570    
## Residuals         75 0.36717 0.004896                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
par(mfrow = c(2, 2))
plot(m0)

layout(1)

emmeans::emmeans(m0, "Trat")
## NOTE: Results may be misleading due to involvement in interactions
##  Trat            emmean      SE df lower.CL upper.CL
##  EstPres_CarPres  0.147 0.00948 75    0.128    0.165
##  EstAus_CarPres   0.206 0.00948 75    0.187    0.225
## 
## 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-0.05 m        53
## 2 EstPres_CarPres 0.15-0.20 m     53
## 3 EstAus_CarPres  0-0.05 m        53
## 4 EstAus_CarPres  0.15-0.20 m     53
## 5 EstAus_CarOxi   0-0.05 m        53
## 6 EstAus_CarOxi   0.15-0.20 m     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, thr, U)

gg <-
    ggplot(tb_sel, aes(x = Argila, y = CAD)) +
    facet_wrap(~ Profundidade) +
    geom_point() +
    geom_smooth(method = "lm", se = FALSE) +
    labs(x = legend$argila_axis,
         y = legend$awc_label)
gg
## `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()`).

postscript("figs/awc_argila_preservado.eps",
           width = 8, height = 6)
print(gg)
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 2 rows containing non-finite outside the scale range (`stat_smooth()`).
## Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).
dev.off()
## png 
##   2
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 m           0.0918009506 1.588907e-02 5.777615 1.282448e-07
## Profundidade0.15-0.20 m        0.0718919956 1.583686e-02 4.539536 1.888807e-05
## Profundidade0-0.05 m:Argila    0.0003169155 5.296993e-05 5.982931 5.345369e-08
## Profundidade0.15-0.20 m:Argila 0.0001494467 3.853511e-05 3.878196 2.098373e-04
gg <-
    ggplot(tb_sel, aes(x = Argila, y = thr)) +
    facet_wrap(~ Profundidade) +
    geom_point() +
    geom_smooth(method = "lm", se = FALSE) +
    labs(x = legend$argila_axis,
         y = legend$rwc)
gg
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 2 rows containing non-finite outside the scale range (`stat_smooth()`).
## Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).

postscript("figs/rwc_argila_preservado.eps",
           width = 8, height = 6)
print(gg)
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 2 rows containing non-finite outside the scale range (`stat_smooth()`).
## Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).
dev.off()
## png 
##   2
m0 <- lm(thr ~ Profundidade * Argila, data = tb_sel)
anova(m0)
## Analysis of Variance Table
## 
## Response: thr
##                     Df   Sum Sq  Mean Sq F value    Pr(>F)    
## Profundidade         1 0.007520 0.007520  2.1015    0.1509    
## Argila               1 0.247312 0.247312 69.1112 1.544e-12 ***
## Profundidade:Argila  1 0.001157 0.001157  0.3232    0.5712    
## Residuals           83 0.297013 0.003578                      
## ---
## 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 m           0.0474492491 1.602344e-02 2.9612405 3.994479e-03
## Profundidade0.15-0.20 m        0.0151328474 1.597078e-02 0.9475334 3.461177e-01
## Profundidade0-0.05 m:Argila    0.0002858043 5.341786e-05 5.3503505 7.625783e-07
## Profundidade0.15-0.20 m:Argila 0.0002482488 3.886097e-05 6.3881275 9.223422e-09
gg <-
    ggplot(tb_sel, aes(x = Argila, y = U)) +
    facet_wrap(~ Profundidade) +
    geom_point() +
    geom_smooth(method = "lm", se = FALSE) +
    labs(x = legend$argila_axis,
         y = legend$wci)
gg
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 2 rows containing non-finite outside the scale range (`stat_smooth()`).
## Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).

postscript("figs/wci_argila_preservado.eps",
           width = 8, height = 6)
print(gg)
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 2 rows containing non-finite outside the scale range (`stat_smooth()`).
## Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).
dev.off()
## png 
##   2
m0 <- lm(U ~ Profundidade * Argila, data = tb_sel)
anova(m0)
## Analysis of Variance Table
## 
## Response: U
##                     Df  Sum Sq Mean Sq F value    Pr(>F)    
## Profundidade         1 0.17410 0.17410 21.4001 1.358e-05 ***
## Argila               1 0.64387 0.64387 79.1410 1.054e-13 ***
## Profundidade:Argila  1 0.03290 0.03290  4.0441   0.04757 *  
## Residuals           83 0.67526 0.00814                      
## ---
## 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 m           0.1961506759 2.416040e-02 8.118683 3.774783e-12
## Profundidade0.15-0.20 m        0.1234639528 2.408101e-02 5.127026 1.891807e-06
## Profundidade0-0.05 m:Argila    0.0005525066 8.054434e-05 6.859657 1.144825e-09
## Profundidade0.15-0.20 m:Argila 0.0003522061 5.859522e-05 6.010833 4.742038e-08
#///////////////////////////////////////////////////////////////////////
# 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-0.05 m        53
## 2 EstPres_CarPres 0.15-0.20 m     53
## 3 EstAus_CarPres  0-0.05 m        53
## 4 EstAus_CarPres  0.15-0.20 m     53
## 5 EstAus_CarOxi   0-0.05 m        53
## 6 EstAus_CarOxi   0.15-0.20 m     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")
gg <-
    tb_sel |>
    pivot_longer(cols = frac_areia,
                 names_to = "Fracao",
                 values_to = "Valor") |>
    mutate(Fracao = factor(Fracao, levels = frac_areia)) |>
    mutate(Fracao = fct_collapse(Fracao,
                                 "Grossa" = c("Muito_grossa", "Grossa"),
                                 "Fina" = c("Fina", "Muito_fina"))) |>
    ggplot(aes(x = Valor, y = CAD, color = Profundidade)) +
    facet_wrap(~Fracao, scales = "free_x",
               labeller = labeller(Fracao = legend$areia_levels)) +
    geom_point() +
    geom_smooth(method = "lm", se = FALSE) +
    labs(x = legend$argila_axis,
         y = legend$awc_label,
         color = legend$profundidade_axis)
## 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.
postscript("figs/awc_fracoes_areia_preservado.eps",
           width = 8, height = 6)
print(gg)
## `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()`).
dev.off()
## png 
##   2
tb_sel[["Grossa"]] <- tb_sel[["Muito_grossa"]] + tb_sel[["Grossa"]]
tb_sel[["Fina"]] <- tb_sel[["Fina"]] + tb_sel[["Muito_fina"]]
tb_sel[["Muito_grossa"]] <- NULL
tb_sel[["Muito_fina"]] <- NULL
frac_areia <- c("Grossa", "Media", "Fina")

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

map_dbl(fits, ~summary(.)$r.squared)
##    Grossa     Media      Fina 
## 0.4852980 0.5581455 0.4996979
map(fits, anova)
## $Grossa
## Analysis of Variance Table
## 
## Response: CAD
##                     Df   Sum Sq  Mean Sq F value    Pr(>F)    
## Profundidade         1 0.048803 0.048803 15.1423 0.0002005 ***
## Grossa               1 0.194208 0.194208 60.2574 1.929e-11 ***
## Profundidade:Grossa  1 0.009214 0.009214  2.8587 0.0946312 .  
## Residuals           83 0.267507 0.003223                      
## ---
## 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  15.578 0.0001651 ***
## Fina               1 0.176245 0.176245  56.258 6.366e-11 ***
## Profundidade:Fina  1 0.034661 0.034661  11.064 0.0013129 ** 
## Residuals         83 0.260023 0.003133                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
map(fits, ~summary(.)$coefficients)
## $Grossa
##                                    Estimate   Std. Error   t value     Pr(>|t|)
## (Intercept)                     0.258110066 0.0156222294 16.521974 5.688544e-28
## Profundidade0.15-0.20 m        -0.096200171 0.0201853412 -4.765843 7.913281e-06
## Grossa                         -0.003996542 0.0005895279 -6.779226 1.638682e-09
## Profundidade0.15-0.20 m:Grossa  0.001442379 0.0008530856  1.690779 9.463117e-02
## 
## $Media
##                                    Estimate   Std. Error   t value     Pr(>|t|)
## (Intercept)                    0.2748301109 1.505140e-02 18.259439 8.314554e-31
## Profundidade0.15-0.20 m       -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 m:Media  0.0002027491 7.514347e-05  2.698160 8.444471e-03
## 
## $Fina
##                                   Estimate   Std. Error   t value     Pr(>|t|)
## (Intercept)                   0.3001459951 1.927534e-02 15.571506 2.375329e-26
## Profundidade0.15-0.20 m      -0.1258312119 2.637956e-02 -4.770027 7.785450e-06
## Fina                         -0.0004298257 5.705745e-05 -7.533210 5.487078e-11
## Profundidade0.15-0.20 m:Fina  0.0002584012 7.768582e-05  3.326234 1.312890e-03
#///////////////////////////////////////////////////////////////////////
# Os nutrientes conseguem prever a CAD? --------------------------------

# str(tb)
# names(tb) |>
#     dput()

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))

# Retirar valor extremo de Carbono.
tb_sel$C <- ifelse(tb_sel$C > 100, NA, tb_sel$C)

gg <-
    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(x = legend$concentracao_axis,
         y = legend$awc_label,
         color = legend$profundidade_axis)
## 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.
gg
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 76 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 76 rows containing missing values or values outside the scale range
## (`geom_point()`).

postscript("figs/awc_nutrientes_preservado.eps",
           width = 8, height = 9)
print(gg)
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 76 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Removed 76 rows containing missing values or values outside the scale range
## (`geom_point()`).
dev.off()
## png 
##   2
# DANGER: Nem todos os nutrientes possuem valores em %.

fits <-
    map(qui, function(x) {
        formula <- sprintf("CAD ~ Profundidade * %s", x)
        tb_sel |>
            select(Profundidade, all_of(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.5596402 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.050675 0.050675 18.1823 5.338e-05 ***
## C               1 0.233479 0.233479 83.7722 3.576e-14 ***
## Profundidade:C  1 0.006290 0.006290  2.2569    0.1369    
## Residuals      82 0.228540 0.002787                      
## ---
## 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 m    0.04141896 0.02882025  1.4371476 1.544353e-01
## N                          1.45344684 0.19342321  7.5143354 5.978894e-11
## Profundidade0.15-0.20 m:N -0.16896426 0.36176973 -0.4670492 6.416885e-01
## 
## $C
##                              Estimate Std. Error   t value     Pr(>|t|)
## (Intercept)                0.02619601 0.01905926  1.374451 1.730466e-01
## Profundidade0.15-0.20 m    0.03742788 0.02504040  1.494700 1.388313e-01
## C                          0.08599089 0.01033355  8.321527 1.608940e-12
## Profundidade0.15-0.20 m:C -0.02671301 0.01778129 -1.502310 1.368585e-01
## 
## $Mg
##                              Estimate Std. Error   t value     Pr(>|t|)
## (Intercept)                 0.3132459 0.05508927  5.686151 4.896859e-07
## Profundidade0.15-0.20 m    -0.1682615 0.08087765 -2.080444 4.207199e-02
## Mg                         -0.2613972 0.08726710 -2.995369 4.077405e-03
## Profundidade0.15-0.20 m: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 m     0.12737423 0.049942447  2.550420 1.259612e-02
## Al                          0.03439764 0.004283605  8.030068 5.668474e-12
## Profundidade0.15-0.20 m: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 m    -0.27146391 0.065625436 -4.136565 8.402035e-05
## Si                         -0.02115072 0.002384266 -8.870957 1.183565e-13
## Profundidade0.15-0.20 m: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 m   -0.1075318 0.05334467 -2.0157926 4.705685e-02
## P                         -1.7586149 0.90679609 -1.9393719 5.585338e-02
## Profundidade0.15-0.20 m: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 m    -0.11711425 0.04536183 -2.581780 1.158567e-02
## Ca                         -0.06384028 0.03234875 -1.973501 5.176592e-02
## Profundidade0.15-0.20 m: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 m     0.044843085 0.02810127  1.595767 1.143416e-01
## Ti                          0.187534051 0.02032896  9.224972 2.315958e-14
## Profundidade0.15-0.20 m: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 m    0.02372997 0.02228528  1.064827 2.900421e-01
## V                          3.19495745 0.32930337  9.702170 2.578570e-15
## Profundidade0.15-0.20 m: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 m     0.04167767  0.0268645  1.5514031 1.246100e-01
## Cr                         10.35193906  1.2243872  8.4547916 8.050400e-13
## Profundidade0.15-0.20 m: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 m    -0.02647168 0.03430232 -0.7717168 4.427732e-01
## Mn                          3.51987176 1.35058843  2.6061764 1.109211e-02
## Profundidade0.15-0.20 m: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 m     0.02049070 0.022451019  0.9126847 3.640518e-01
## Fe                          0.02362469 0.002511831  9.4053682 1.009281e-14
## Profundidade0.15-0.20 m: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 m      0.01048837  0.02970898  0.3530369 7.249558e-01
## Cu                          43.12071625  6.57505690  6.5582271 4.365432e-09
## Profundidade0.15-0.20 m: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 m    -0.01411057 0.03248779 -0.4343346 6.651726e-01
## Zn                         17.06862181 3.38000782  5.0498764 2.578831e-06
## Profundidade0.15-0.20 m: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 m      0.02123262  0.02256904  0.9407854 3.497570e-01
## As                          89.80551572 10.46357064  8.5826836 7.652269e-13
## Profundidade0.15-0.20 m: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 m     0.03253912 0.02769165  1.1750520 2.433339e-01
## Zr                          4.94960507 0.57249693  8.6456447 3.342782e-13
## Profundidade0.15-0.20 m:Zr -2.72096417 0.77885980 -3.4935224 7.671559e-04
#///////////////////////////////////////////////////////////////////////
# Quais os erros em estimar a CRAS usando a amostra sem estrutura? -----

# cras_params <- c("thr", "ths", "alp", "nscal", "mscal", "I", "U", "S", "CAD")

# Umidade residual (thr) ¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬

y <- "thr"
tb_sel <-
    tb |>
    select(Local, Profundidade, Trat, y = any_of(y)) |>
    filter(Trat %in% c("EstPres_CarPres", "EstAus_CarPres"),
           y > 0)

# tb_sel[["y"]] <- tb_sel[["y"]] |>
#     log10()

tb_sel_wide <-
    tb_sel |>
    pivot_wider(names_from = Trat, values_from = y) |>
    drop_na()

tb_sel <-
    tb_sel_wide |>
    pivot_longer(cols = -c("Local", "Profundidade"),
                 names_to = "Trat",
                 values_to = "y") |>
    mutate(Trat = factor(Trat, levels = levels(tb$Trat)))

gg <-
    tb_sel |>
    ggplot(data = _,
           aes(x = Trat, y = y)) +
    facet_wrap(~ Profundidade) +
    geom_boxplot() +
    geom_jitter(width = 0.05, color = "orange") +
    geom_segment(data = tb_sel_wide,
                 aes(x = 1,
                     xend = 2,
                     y = EstPres_CarPres,
                     yend = EstAus_CarPres),
                 color = "gray", size = 0.5) +
    scale_x_discrete(labels = legend$trat_levels) +
    labs(x = legend$trat_axis,
         y = legend$rwc)
gg

postscript("figs/rwc_estrutura_carbono_preservado.eps",
           width = 8, height = 6)
print(gg)
dev.off()
## png 
##   2
m0 <- lm(y ~ Local + Profundidade * Trat, data = tb_sel)
anova(m0)
## Analysis of Variance Table
## 
## Response: y
##                   Df  Sum Sq   Mean Sq F value    Pr(>F)    
## Local             45 0.77631 0.0172513  6.7621 3.248e-15 ***
## Profundidade       1 0.01179 0.0117897  4.6213   0.03412 *  
## Trat               1 0.00045 0.0004497  0.1763   0.67554    
## Profundidade:Trat  1 0.00594 0.0059350  2.3264   0.13052    
## Residuals         95 0.24236 0.0025512                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# par(mfrow = c(2, 2))
# plot(m0)
# layout(1)
# MASS::boxcox(m0)

emmeans::emmeans(m0, ~Trat | Profundidade) |>
    multcomp::cld(Letters = letters)
## Profundidade = 0-0.05 m:
##  Trat            emmean      SE df lower.CL upper.CL .group
##  EstPres_CarPres  0.118 0.00911 95   0.0998    0.136  a    
##  EstAus_CarPres   0.135 0.00911 95   0.1166    0.153  a    
## 
## Profundidade = 0.15-0.20 m:
##  Trat            emmean      SE df lower.CL upper.CL .group
##  EstAus_CarPres   0.101 0.00876 95   0.0831    0.118  a    
##  EstPres_CarPres  0.109 0.00876 95   0.0921    0.127  a    
## 
## Results are averaged over the levels of: Local 
## Confidence level used: 0.95 
## significance level used: alpha = 0.05 
## NOTE: If two or more means share the same grouping symbol,
##       then we cannot show them to be different.
##       But we also did not show them to be the same.
# Umidade de saturação (ths) ¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬

y <- "ths"
tb_sel <-
    tb |>
    select(Local, Profundidade, Trat, y = any_of(y)) |>
    filter(Trat %in% c("EstPres_CarPres", "EstAus_CarPres"),
           y > 0, y < 1)

# tb_sel[["y"]] <- tb_sel[["y"]] |>
#     log10()

tb_sel_wide <-
    tb_sel |>
    pivot_wider(names_from = Trat, values_from = y) |>
    drop_na()

tb_sel <-
    tb_sel_wide |>
    pivot_longer(cols = -c("Local", "Profundidade"),
                 names_to = "Trat",
                 values_to = "y") |>
    mutate(Trat = factor(Trat, levels = levels(tb$Trat)))

gg <-
    tb_sel |>
    ggplot(data = _,
           aes(x = Trat, y = y)) +
    facet_wrap(~ Profundidade) +
    geom_boxplot() +
    geom_jitter(width = 0.05, color = "orange") +
    geom_segment(data = tb_sel_wide,
                 aes(x = 1,
                     xend = 2,
                     y = EstPres_CarPres,
                     yend = EstAus_CarPres),
                 color = "gray", size = 0.5) +
    scale_x_discrete(labels = legend$trat_levels) +
    labs(x = legend$trat_axis,
         y = legend$wcs)
gg

postscript("figs/rws_estrutura_carbono_preservado.eps",
           width = 8, height = 6)
print(gg)
dev.off()
## png 
##   2
m0 <- lm(y ~ Local + Profundidade * Trat, data = tb_sel)
anova(m0)
## Analysis of Variance Table
## 
## Response: y
##                    Df  Sum Sq Mean Sq F value    Pr(>F)    
## Local              49 2.13133 0.04350  6.3269 1.006e-15 ***
## Profundidade        1 0.14664 0.14664 21.3296 1.082e-05 ***
## Trat                1 0.35748 0.35748 51.9987 8.290e-11 ***
## Profundidade:Trat   1 0.08321 0.08321 12.1032 0.0007292 ***
## Residuals         107 0.73561 0.00687                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# par(mfrow = c(2, 2))
# plot(m0)
# layout(1)
# MASS::boxcox(m0)

emmeans::emmeans(m0, ~Trat | Profundidade) |>
    multcomp::cld(Letters = letters)
## Profundidade = 0-0.05 m:
##  Trat            emmean     SE  df lower.CL upper.CL .group
##  EstPres_CarPres  0.412 0.0151 107    0.382    0.442  a    
##  EstAus_CarPres   0.455 0.0151 107    0.425    0.485   b   
## 
## Profundidade = 0.15-0.20 m:
##  Trat            emmean     SE  df lower.CL upper.CL .group
##  EstPres_CarPres  0.297 0.0127 107    0.271    0.322  a    
##  EstAus_CarPres   0.431 0.0127 107    0.406    0.456   b   
## 
## Results are averaged over the levels of: Local 
## Confidence level used: 0.95 
## significance level used: alpha = 0.05 
## NOTE: If two or more means share the same grouping symbol,
##       then we cannot show them to be different.
##       But we also did not show them to be the same.
# Umidade no ponto de inflexão (I) ¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬

y <- "U"
tb_sel <-
    tb |>
    select(Local, Profundidade, Trat, y = any_of(y)) |>
    filter(Trat %in% c("EstPres_CarPres", "EstAus_CarPres"),
           y > 0, y < 1)

# tb_sel[["y"]] <- tb_sel[["y"]] |>
#     log10()

tb_sel_wide <-
    tb_sel |>
    pivot_wider(names_from = Trat, values_from = y) |>
    drop_na()

tb_sel <-
    tb_sel_wide |>
    pivot_longer(cols = -c("Local", "Profundidade"),
                 names_to = "Trat",
                 values_to = "y") |>
    mutate(Trat = factor(Trat, levels = levels(tb$Trat)))

gg <-
    tb_sel |>
    ggplot(data = _,
           aes(x = Trat, y = y)) +
    facet_wrap(~ Profundidade) +
    geom_boxplot() +
    geom_jitter(width = 0.05, color = "orange") +
    geom_segment(data = tb_sel_wide,
                 aes(x = 1,
                     xend = 2,
                     y = EstPres_CarPres,
                     yend = EstAus_CarPres),
                 color = "gray", size = 0.5) +
    scale_x_discrete(labels = legend$trat_levels) +
    labs(x = legend$trat_axis,
         y = legend$wci)
gg

postscript("figs/awi_estrutura_carbono_preservado.eps",
           width = 8, height = 6)
print(gg)
dev.off()
## png 
##   2
m0 <- lm(y ~ Local + Profundidade * Trat, data = tb_sel)
anova(m0)
## Analysis of Variance Table
## 
## Response: y
##                    Df  Sum Sq  Mean Sq F value    Pr(>F)    
## Local              50 2.44817 0.048963  6.2955 < 2.2e-16 ***
## Profundidade        1 0.23701 0.237008 30.4735 1.728e-07 ***
## Trat                1 0.20508 0.205079 26.3683 9.885e-07 ***
## Profundidade:Trat   1 0.02281 0.022811  2.9329   0.08914 .  
## Residuals         132 1.02663 0.007778                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# par(mfrow = c(2, 2))
# plot(m0)
# layout(1)
# MASS::boxcox(m0)

emmeans::emmeans(m0, ~Trat | Profundidade) |>
    multcomp::cld(Letters = letters)
## Profundidade = 0-0.05 m:
##  Trat            emmean     SE  df lower.CL upper.CL .group
##  EstPres_CarPres  0.331 0.0135 132    0.304    0.358  a    
##  EstAus_CarPres   0.375 0.0135 132    0.348    0.402   b   
## 
## Profundidade = 0.15-0.20 m:
##  Trat            emmean     SE  df lower.CL upper.CL .group
##  EstPres_CarPres  0.234 0.0129 132    0.208    0.260  a    
##  EstAus_CarPres   0.322 0.0129 132    0.296    0.347   b   
## 
## Results are averaged over the levels of: Local 
## Confidence level used: 0.95 
## significance level used: alpha = 0.05 
## NOTE: If two or more means share the same grouping symbol,
##       then we cannot show them to be different.
##       But we also did not show them to be the same.
# Parâmetro de forma alpha ¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬

y <- "alp"
tb_sel <-
    tb |>
    select(Local, Profundidade, Trat, y = any_of(y)) |>
    filter(Trat %in% c("EstPres_CarPres", "EstAus_CarPres"))

# tb_sel[["y"]] <- tb_sel[["y"]] |>
#     log10()

tb_sel_wide <-
    tb_sel |>
    pivot_wider(names_from = Trat, values_from = y) |>
    drop_na()

tb_sel <-
    tb_sel_wide |>
    pivot_longer(cols = -c("Local", "Profundidade"),
                 names_to = "Trat",
                 values_to = "y") |>
    mutate(Trat = factor(Trat, levels = levels(tb$Trat)))

gg <-
    tb_sel |>
    ggplot(data = _,
       aes(x = Trat, y = y)) +
    facet_wrap(~ Profundidade) +
    geom_boxplot() +
    geom_jitter(width = 0.05, color = "orange") +
    geom_segment(data = tb_sel_wide,
                 aes(x = 1,
                     xend = 2,
                     y = EstPres_CarPres,
                     yend = EstAus_CarPres),
                 color = "gray", size = 0.5) +
    scale_x_discrete(labels = legend$trat_levels) +
    labs(x = legend$trat_axis,
         y = legend$alpha)
gg

postscript("figs/alpha_estrutura_carbono_preservado.eps",
           width = 8, height = 6)
print(gg)
dev.off()
## png 
##   2
m0 <- lm(y ~ Local + Profundidade * Trat, data = tb_sel)
anova(m0)
## Analysis of Variance Table
## 
## Response: y
##                    Df  Sum Sq Mean Sq F value Pr(>F)
## Local              50  52.889 1.05778  0.9896 0.5037
## Profundidade        1   0.049 0.04911  0.0459 0.8306
## Trat                1   0.047 0.04715  0.0441 0.8340
## Profundidade:Trat   1   0.002 0.00212  0.0020 0.9645
## Residuals         132 141.099 1.06893
# par(mfrow = c(2, 2))
# plot(m0)
# layout(1)
# MASS::boxcox(m0)

emmeans::emmeans(m0, ~Trat | Profundidade) |>
    multcomp::cld(Letters = letters)
## Profundidade = 0-0.05 m:
##  Trat            emmean    SE  df lower.CL upper.CL .group
##  EstPres_CarPres  0.825 0.159 132    0.511     1.14  a    
##  EstAus_CarPres   0.850 0.159 132    0.536     1.16  a    
## 
## Profundidade = 0.15-0.20 m:
##  Trat            emmean    SE  df lower.CL upper.CL .group
##  EstPres_CarPres  0.852 0.151 132    0.553     1.15  a    
##  EstAus_CarPres   0.891 0.151 132    0.591     1.19  a    
## 
## Results are averaged over the levels of: Local 
## Confidence level used: 0.95 
## significance level used: alpha = 0.05 
## NOTE: If two or more means share the same grouping symbol,
##       then we cannot show them to be different.
##       But we also did not show them to be the same.
# Parâmetro de forma n ¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬

y <- "nscal"
tb_sel <-
    tb |>
    select(Local, Profundidade, Trat, y = any_of(y)) |>
    filter(Trat %in% c("EstPres_CarPres", "EstAus_CarPres"))

tb_sel_wide <-
    tb_sel |>
    pivot_wider(names_from = Trat, values_from = y) |>
    drop_na()

tb_sel <-
    tb_sel_wide |>
    pivot_longer(cols = -c("Local", "Profundidade"),
                 names_to = "Trat",
                 values_to = "y") |>
    mutate(Trat = factor(Trat, levels = levels(tb$Trat)))

gg <-
    tb_sel |>
    ggplot(data = _,
           aes(x = Trat, y = y)) +
    facet_wrap(~ Profundidade) +
    geom_boxplot() +
    geom_jitter(width = 0.05, color = "orange") +
    geom_segment(data = tb_sel_wide,
                 aes(x = 1,
                     xend = 2,
                     y = EstPres_CarPres,
                     yend = EstAus_CarPres),
                 color = "gray", size = 0.5) +
    scale_y_log10(
        breaks = scales::trans_breaks("log10", \(x) 10^x),
        labels = scales::trans_format("log10", scales::math_format(10^.x))
    ) +
    annotation_logticks(
        sides = "l",
        short = unit(0.05, "cm"),
        mid = unit(0.1, "cm"),
        long = unit(0.15, "cm"),
        colour = "black",
        linewidth = 0.25,
        outside = TRUE) +
    coord_cartesian(clip = "off") +
    scale_x_discrete(labels = legend$trat_levels) +
    labs(x = legend$trat_axis,
         y = legend$n)
gg

postscript("figs/n_estrutura_carbono_preservado.eps",
           width = 8, height = 6)
print(gg)
dev.off()
## png 
##   2
tb_sel[["y"]] <- tb_sel[["y"]] |>
    log10()

m0 <- lm(y ~ Local + Profundidade * Trat, data = tb_sel)
anova(m0)
## Analysis of Variance Table
## 
## Response: y
##                    Df Sum Sq Mean Sq F value   Pr(>F)    
## Local              50 25.519  0.5104  1.6198  0.01575 *  
## Profundidade        1  0.450  0.4498  1.4274  0.23433    
## Trat                1 18.781 18.7810 59.6050 2.55e-12 ***
## Profundidade:Trat   1  0.814  0.8136  2.5821  0.11047    
## Residuals         132 41.592  0.3151                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# par(mfrow = c(2, 2))
# plot(m0)
# layout(1)
# MASS::boxcox(m0)

emmeans::emmeans(m0, ~Trat | Profundidade) |>
    multcomp::cld(Letters = letters)
## Profundidade = 0-0.05 m:
##  Trat             emmean     SE  df lower.CL upper.CL .group
##  EstAus_CarPres  -0.2592 0.0861 132   -0.430  -0.0888  a    
##  EstPres_CarPres  0.5129 0.0861 132    0.343   0.6833   b   
## 
## Profundidade = 0.15-0.20 m:
##  Trat             emmean     SE  df lower.CL upper.CL .group
##  EstAus_CarPres  -0.0234 0.0822 132   -0.186   0.1393  a    
##  EstPres_CarPres  0.4841 0.0822 132    0.321   0.6467   b   
## 
## Results are averaged over the levels of: Local 
## Confidence level used: 0.95 
## significance level used: alpha = 0.05 
## NOTE: If two or more means share the same grouping symbol,
##       then we cannot show them to be different.
##       But we also did not show them to be the same.
# Parâmetro de forma m ¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬

y <- "mscal"
tb_sel <-
    tb |>
    select(Local, Profundidade, Trat, y = any_of(y)) |>
    filter(Trat %in% c("EstPres_CarPres", "EstAus_CarPres"))

tb_sel_wide <-
    tb_sel |>
    pivot_wider(names_from = Trat, values_from = y) |>
    drop_na()

tb_sel <-
    tb_sel_wide |>
    pivot_longer(cols = -c("Local", "Profundidade"),
                 names_to = "Trat",
                 values_to = "y") |>
    mutate(Trat = factor(Trat, levels = levels(tb$Trat)))

gg <-
    tb_sel |>
    ggplot(data = _,
       aes(x = Trat, y = y)) +
    facet_wrap(~ Profundidade) +
    geom_boxplot() +
    geom_jitter(width = 0.05, color = "orange") +
    geom_segment(data = tb_sel_wide,
                 aes(x = 1,
                     xend = 2,
                     y = EstPres_CarPres,
                     yend = EstAus_CarPres),
                 color = "gray", size = 0.5) +
    scale_y_log10(
        breaks = scales::trans_breaks("log10", \(x) 10^x),
        labels = scales::trans_format("log10", scales::math_format(10^.x))
    ) +
    annotation_logticks(
        sides = "l",
        short = unit(0.05, "cm"),
        mid = unit(0.1, "cm"),
        long = unit(0.15, "cm"),
        colour = "black",
        linewidth = 0.25,
        outside = TRUE) +
    coord_cartesian(clip = "off") +
    scale_x_discrete(labels = legend$trat_levels) +
    labs(x = legend$trat_axis,
         y = legend$m)
gg

postscript("figs/m_estrutura_carbono_preservado.eps",
           width = 8, height = 6)
print(gg)
dev.off()
## png 
##   2
tb_sel[["y"]] <- tb_sel[["y"]] |>
    log10()

m0 <- lm(y ~ Local + Profundidade * Trat, data = tb_sel)
anova(m0)
## Analysis of Variance Table
## 
## Response: y
##                    Df Sum Sq Mean Sq F value    Pr(>F)    
## Local              50 30.152  0.6030  1.5165 0.0318492 *  
## Profundidade        1  0.599  0.5993  1.5071 0.2217661    
## Trat                1  5.176  5.1765 13.0173 0.0004367 ***
## Profundidade:Trat   1  0.033  0.0328  0.0824 0.7745321    
## Residuals         132 52.491  0.3977                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# par(mfrow = c(2, 2))
# plot(m0)
# layout(1)
# MASS::boxcox(m0)

emmeans::emmeans(m0, ~Trat | Profundidade) |>
    multcomp::cld(Letters = letters)
## Profundidade = 0-0.05 m:
##  Trat            emmean     SE  df lower.CL upper.CL .group
##  EstPres_CarPres -0.653 0.0968 132   -0.845   -0.462  a    
##  EstAus_CarPres  -0.292 0.0968 132   -0.484   -0.101   b   
## 
## Profundidade = 0.15-0.20 m:
##  Trat            emmean     SE  df lower.CL upper.CL .group
##  EstPres_CarPres -0.746 0.0924 132   -0.929   -0.564  a    
##  EstAus_CarPres  -0.438 0.0924 132   -0.621   -0.256   b   
## 
## Results are averaged over the levels of: Local 
## Confidence level used: 0.95 
## significance level used: alpha = 0.05 
## NOTE: If two or more means share the same grouping symbol,
##       then we cannot show them to be different.
##       But we also did not show them to be the same.
# Tensão do ponto de inflexão ¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬

y <- "I"
tb_sel <-
    tb |>
    select(Local, Profundidade, Trat, y = any_of(y)) |>
    filter(Trat %in% c("EstPres_CarPres", "EstAus_CarPres"))

# tb_sel[["y"]] <- tb_sel[["y"]] |>
#     log10()

tb_sel_wide <-
    tb_sel |>
    pivot_wider(names_from = Trat, values_from = y) |>
    drop_na()

tb_sel <-
    tb_sel_wide |>
    pivot_longer(cols = -c("Local", "Profundidade"),
                 names_to = "Trat",
                 values_to = "y") |>
    mutate(Trat = factor(Trat, levels = levels(tb$Trat)))

gg <-
    tb_sel |>
    ggplot(data = _,
       aes(x = Trat, y = y)) +
    facet_wrap(~ Profundidade) +
    geom_boxplot() +
    geom_jitter(width = 0.05, color = "orange") +
    geom_segment(data = tb_sel_wide,
                 aes(x = 1,
                     xend = 2,
                     y = EstPres_CarPres,
                     yend = EstAus_CarPres),
                 color = "gray", size = 0.5) +
    scale_x_discrete(labels = legend$trat_levels) +
    labs(x = legend$trat_axis,
         y = legend$I)
gg

postscript("figs/i_estrutura_carbono_preservado.eps",
           width = 8, height = 6)
print(gg)
dev.off()
## png 
##   2
m0 <- lm(y ~ Local + Profundidade * Trat, data = tb_sel)
anova(m0)
## Analysis of Variance Table
## 
## Response: y
##                    Df Sum Sq Mean Sq F value Pr(>F)
## Local              50 212.67  4.2533  1.0538 0.3980
## Profundidade        1   4.14  4.1363  1.0248 0.3132
## Trat                1   4.75  4.7531  1.1776 0.2798
## Profundidade:Trat   1   1.45  1.4501  0.3593 0.5499
## Residuals         132 532.80  4.0364
# par(mfrow = c(2, 2))
# plot(m0)
# layout(1)
# MASS::boxcox(m0)

emmeans::emmeans(m0, ~Trat | Profundidade) |>
    multcomp::cld(Letters = letters)
## Profundidade = 0-0.05 m:
##  Trat            emmean    SE  df lower.CL upper.CL .group
##  EstPres_CarPres  0.721 0.308 132    0.111     1.33  a    
##  EstAus_CarPres   0.859 0.308 132    0.249     1.47  a    
## 
## Profundidade = 0.15-0.20 m:
##  Trat            emmean    SE  df lower.CL upper.CL .group
##  EstPres_CarPres  0.859 0.294 132    0.276     1.44  a    
##  EstAus_CarPres   1.349 0.294 132    0.767     1.93  a    
## 
## Results are averaged over the levels of: Local 
## Confidence level used: 0.95 
## significance level used: alpha = 0.05 
## NOTE: If two or more means share the same grouping symbol,
##       then we cannot show them to be different.
##       But we also did not show them to be the same.
# Inclinação no ponto de inflexão (S) ¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬

y <- "S"
tb_sel <-
    tb |>
    select(Local, Profundidade, Trat, y = any_of(y)) |>
    filter(Trat %in% c("EstPres_CarPres", "EstAus_CarPres"))

# tb_sel[["y"]] <- -tb_sel[["y"]] |>
#     log10()

tb_sel_wide <-
    tb_sel |>
    pivot_wider(names_from = Trat, values_from = y) |>
    drop_na()

tb_sel <-
    tb_sel_wide |>
    pivot_longer(cols = -c("Local", "Profundidade"),
                 names_to = "Trat",
                 values_to = "y") |>
    mutate(Trat = factor(Trat, levels = levels(tb$Trat)))

gg <-
    tb_sel |>
    ggplot(data = _,
       aes(x = Trat, y = y)) +
    facet_wrap(~ Profundidade) +
    geom_boxplot() +
    geom_jitter(width = 0.05, color = "orange") +
    geom_segment(data = tb_sel_wide,
                 aes(x = 1,
                     xend = 2,
                     y = EstPres_CarPres,
                     yend = EstAus_CarPres),
                 color = "gray", size = 0.5) +
    scale_x_discrete(labels = legend$trat_levels) +
    labs(x = legend$trat_axis,
         y = legend$S)
gg

postscript("figs/s_index_estrutura_carbono_preservado.eps",
           width = 8, height = 6)
print(gg)
dev.off()
## png 
##   2
m0 <- lm(y ~ Local + Profundidade * Trat, data = tb_sel)
anova(m0)
## Analysis of Variance Table
## 
## Response: y
##                    Df   Sum Sq   Mean Sq F value    Pr(>F)    
## Local              50 0.118988 0.0023798  2.5896 8.219e-06 ***
## Profundidade        1 0.007257 0.0072569  7.8968  0.005706 ** 
## Trat                1 0.026571 0.0265708 28.9138 3.331e-07 ***
## Profundidade:Trat   1 0.024232 0.0242321 26.3688 9.882e-07 ***
## Residuals         132 0.121304 0.0009190                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# par(mfrow = c(2, 2))
# plot(m0)
# layout(1)
# MASS::boxcox(m0)

emmeans::emmeans(m0, ~Trat | Profundidade) |>
    multcomp::cld(Letters = letters)
## Profundidade = 0-0.05 m:
##  Trat             emmean      SE  df lower.CL upper.CL .group
##  EstPres_CarPres -0.0956 0.00465 132  -0.1048  -0.0863  a    
##  EstAus_CarPres  -0.0481 0.00465 132  -0.0573  -0.0389   b   
## 
## Profundidade = 0.15-0.20 m:
##  Trat             emmean      SE  df lower.CL upper.CL .group
##  EstPres_CarPres -0.0596 0.00444 132  -0.0684  -0.0508  a    
##  EstAus_CarPres  -0.0578 0.00444 132  -0.0666  -0.0490  a    
## 
## Results are averaged over the levels of: Local 
## Confidence level used: 0.95 
## significance level used: alpha = 0.05 
## NOTE: If two or more means share the same grouping symbol,
##       then we cannot show them to be different.
##       But we also did not show them to be the same.
#///////////////////////////////////////////////////////////////////////