Análise Exploratória
#-----------------------------------------------------------------------
# Carrega os pacotes necessários.
library(EACS)
library(quantreg)
library(tidyverse)
Prepara a tabela de dados
# glimpse(teca_arv)
# glimpse(teca_qui)
# glimpse(teca_crapar)
# glimpse(teca_gran)
# Junções.
tb <- full_join(teca_qui, teca_crapar)
## Joining, by = c("loc", "cam")
tb <- inner_join(tb, teca_arv[, c("loc", "prod")])
## Joining, by = "loc"
tb <- as_tibble(tb)
glimpse(tb)
## Observations: 150
## Variables: 24
## $ loc <int> 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6…
## $ cam <fct> "[0, 5)", "[5, 40)", "[40, 80)", "[0, 5)", "[5, 40)"…
## $ ph <dbl> 6.8, 6.7, 6.7, 4.7, 4.7, 4.9, 7.6, 6.8, 6.9, 6.6, 6.…
## $ p <dbl> 22.51, 0.83, 0.01, 3.89, 0.69, 0.10, 11.52, 1.66, 0.…
## $ k <dbl> 72.24, 13.42, 7.23, 48.13, 12.34, 8.64, 114.55, 25.8…
## $ ca <dbl> 8.27, 2.91, 2.33, 0.97, 0.76, 0.21, 7.63, 3.22, 2.22…
## $ mg <dbl> 1.70, 1.77, 0.51, 0.16, 0.14, 0.00, 1.53, 1.31, 1.09…
## $ al <dbl> 0.0, 0.0, 0.0, 0.3, 0.6, 0.6, 0.0, 0.0, 0.0, 0.0, 0.…
## $ ctc <dbl> 12.47, 6.57, 4.52, 5.30, 4.17, 3.47, 12.41, 6.26, 5.…
## $ sat <dbl> 81.41, 71.74, 63.23, 23.66, 22.35, 6.69, 76.14, 73.4…
## $ mo <dbl> 72.2, 25.6, 9.7, 34.4, 8.7, 9.7, 50.4, 15.6, 9.5, 50…
## $ arg <dbl> 183.7, 215.0, 285.5, 231.9, 212.6, 237.0, 192.6, 234…
## $ are <dbl> 769.5, 749.5, 674.5, 741.0, 775.0, 752.0, 715.5, 698…
## $ cas <dbl> 1.8, 2.2, 3.7, 0.4, 1.1, 1.7, 8.4, 19.0, 14.3, 5.5, …
## $ acc <dbl> 769.93, 750.04, 675.69, 741.11, 775.25, 752.42, 717.…
## $ Ur <dbl> 0.22288275, 0.17845472, 0.18791987, 0.13107692, 0.16…
## $ Us <dbl> 0.6477212, 0.5799842, 0.6472682, 0.6967997, 0.572831…
## $ alp <dbl> -0.7242483, -0.8327602, -0.7221707, -0.5484938, -0.5…
## $ n <dbl> 3.589612, 3.112403, 3.250733, 4.006821, 2.917137, 2.…
## $ cad <dbl> 0.2268530, 0.2171637, 0.2473961, 0.2996195, 0.222109…
## $ Ui <dbl> 0.4497358, 0.3956184, 0.4353160, 0.4306964, 0.387045…
## $ I <dbl> 0.8152153, 0.9572842, 0.8352604, 0.6201503, 0.690926…
## $ S <dbl> -0.34126593, -0.27326860, -0.32901803, -0.51467615, …
## $ prod <dbl> 68.22631, 68.22631, 68.22631, 24.27449, 24.27449, 24…
# Trata valor alto de P.
tb$p[tb$p > 80] <- NA
# Empilha os valores das variáveis de solo.
tb_long <- tb %>%
gather(key = "variable", value = "valor", ph:cad)
# Protótipo.
gg <- ggplot(data = tb_long,
mapping = aes(x = valor, y = prod, color = cam)) +
facet_wrap(facets = ~variable, scales = "free_x", ncol = 3) +
geom_point(shape = 1) +
labs(color = "Camada") +
xlab("Valor da variável de solo") +
ylab("Produção de teca")
# Regressão linear simples.
gg +
geom_smooth(method = "lm",
formula = y ~ poly(x, degree = 2),
se = FALSE)

# Regressão quantílica.
gg +
geom_quantile(quantiles = 0.9,
formula = y ~ poly(x, degree = 2))

Regressão quantílica
tb_fit <- tb_long %>%
group_by(variable, cam) %>%
drop_na() %>%
nest()
# ATTENTION: o modelo 0 é reajustado em função dos missings que mudam de
# quantidade como função do split.
tb_fit <- tb_fit %>%
mutate(qtl_0 = map(data,
rq,
formula = prod ~ 1,
tau = 0.9),
qtl_1 = map(data,
rq,
formula = prod ~ valor,
tau = 0.9),
qtl_2 = map(data,
rq,
formula = prod ~ valor + I(valor^2),
tau = 0.9))
# # Quadro de anova para modelos encaixados.
# tb_fit <- tb_fit %>%
# mutate(anova_01 = map(qtl_1, anova, qtl_0),
# anova_12 = map2(qtl_2, qtl_1, anova))
extract_coef <- function(fit_work, fit_ref) {
if (missing(fit_ref)) {
p <- NULL
} else {
p <- anova(fit_work, fit_ref)$table$pvalue
}
b <- coef(fit_work)
c(b, pvalue = p) %>%
as.list() %>%
data.frame(check.names = FALSE)
}
# # Teste.
# extract_coef(tb_fit$qtl_2[[1]], tb_fit$qtl_1[[1]])
# extract_coef(tb_fit$qtl_1[[1]], qtl_0)
# Extrai as estimativas e valor p para criar tabela.
tb_result <- tb_fit %>%
transmute(variable = variable,
cam = cam,
null = map(qtl_0, extract_coef),
first = map2(qtl_1, qtl_0, extract_coef),
secon = map2(qtl_2, qtl_1, extract_coef),
) %>%
gather("model", "coef", -variable, -cam) %>%
unnest(coef)
tb_result
## # A tibble: 162 x 7
## variable cam model `(Intercept)` valor pvalue `I(valor^2)`
## <chr> <fct> <chr> <dbl> <dbl> <dbl> <dbl>
## 1 ph [0, 5) null 131. NA NA NA
## 2 ph [5, 40) null 131. NA NA NA
## 3 ph [40, 80) null 125. NA NA NA
## 4 p [0, 5) null 131. NA NA NA
## 5 p [5, 40) null 131. NA NA NA
## 6 p [40, 80) null 125. NA NA NA
## 7 k [0, 5) null 131. NA NA NA
## 8 k [5, 40) null 131. NA NA NA
## 9 k [40, 80) null 125. NA NA NA
## 10 ca [0, 5) null 131. NA NA NA
## # … with 152 more rows
# Organiza para melhor disposição da informação.
tb_result <- tb_result %>%
rename(beta_0 = "(Intercept)",
beta_1 = "valor",
beta_2 = "I(valor^2)") %>%
select(-pvalue, everything()) %>%
mutate(model = factor(model, levels = unique(model))) %>%
arrange(variable, cam, model)
tb_result
## # A tibble: 162 x 7
## variable cam model beta_0 beta_1 beta_2 pvalue
## <chr> <fct> <fct> <dbl> <dbl> <dbl> <dbl>
## 1 acc [0, 5) null 131. NA NA NA
## 2 acc [0, 5) first 243. -0.169 NA 0.0862
## 3 acc [0, 5) secon 173. 0.0925 -0.000229 0.748
## 4 acc [5, 40) null 131. NA NA NA
## 5 acc [5, 40) first 199. -0.105 NA 0.307
## 6 acc [5, 40) secon 389. -0.696 0.000456 0.700
## 7 acc [40, 80) null 125. NA NA NA
## 8 acc [40, 80) first 88.7 0.0715 NA 0.206
## 9 acc [40, 80) secon 157. -0.211 0.000254 0.365
## 10 al [0, 5) null 131. NA NA NA
## # … with 152 more rows
# Ajustes significativos a 5%. Pega o maior modelo.
tb_signif <- tb_result %>%
filter(pvalue < 0.05) %>%
group_by(variable, cam) %>%
slice(n()) %>%
ungroup()
knitr::kable(tb_result)
| acc |
[0, 5) |
null |
130.728188 |
NA |
NA |
NA |
| acc |
[0, 5) |
first |
242.781491 |
-0.1692722 |
NA |
0.0862196 |
| acc |
[0, 5) |
secon |
172.864052 |
0.0925460 |
-0.0002291 |
0.7480308 |
| acc |
[5, 40) |
null |
130.728188 |
NA |
NA |
NA |
| acc |
[5, 40) |
first |
199.214327 |
-0.1049009 |
NA |
0.3071615 |
| acc |
[5, 40) |
secon |
389.403598 |
-0.6962770 |
0.0004563 |
0.7002075 |
| acc |
[40, 80) |
null |
125.339960 |
NA |
NA |
NA |
| acc |
[40, 80) |
first |
88.695421 |
0.0715023 |
NA |
0.2059398 |
| acc |
[40, 80) |
secon |
156.694228 |
-0.2107799 |
0.0002542 |
0.3646995 |
| al |
[0, 5) |
null |
130.728188 |
NA |
NA |
NA |
| al |
[0, 5) |
first |
130.728188 |
-170.7276572 |
NA |
0.7960320 |
| al |
[0, 5) |
secon |
130.728188 |
-1555.7598254 |
6925.1608409 |
0.0000000 |
| al |
[5, 40) |
null |
130.728188 |
NA |
NA |
NA |
| al |
[5, 40) |
first |
137.121381 |
-105.5689730 |
NA |
0.0018612 |
| al |
[5, 40) |
secon |
137.121381 |
-34.6994165 |
-141.7391130 |
0.4943634 |
| al |
[40, 80) |
null |
125.339960 |
NA |
NA |
NA |
| al |
[40, 80) |
first |
130.728188 |
-61.6602672 |
NA |
0.0000215 |
| al |
[40, 80) |
secon |
130.728188 |
-73.5421455 |
8.4870559 |
0.9628971 |
| alp |
[0, 5) |
null |
130.728188 |
NA |
NA |
NA |
| alp |
[0, 5) |
first |
147.257008 |
30.9784959 |
NA |
0.0601243 |
| alp |
[0, 5) |
secon |
146.556419 |
12.1585501 |
-37.3017227 |
0.8362290 |
| alp |
[5, 40) |
null |
130.728188 |
NA |
NA |
NA |
| alp |
[5, 40) |
first |
120.380978 |
-28.6306365 |
NA |
0.0012059 |
| alp |
[5, 40) |
secon |
125.219739 |
-21.1110934 |
-12.5222173 |
0.5432304 |
| alp |
[40, 80) |
null |
125.339960 |
NA |
NA |
NA |
| alp |
[40, 80) |
first |
131.221962 |
5.7890335 |
NA |
0.6615589 |
| alp |
[40, 80) |
secon |
122.228387 |
18.9924755 |
21.7062767 |
0.6467618 |
| are |
[0, 5) |
null |
130.728188 |
NA |
NA |
NA |
| are |
[0, 5) |
first |
239.969660 |
-0.1734461 |
NA |
0.0495940 |
| are |
[0, 5) |
secon |
199.600414 |
-0.0208108 |
-0.0001290 |
0.7398378 |
| are |
[5, 40) |
null |
130.728188 |
NA |
NA |
NA |
| are |
[5, 40) |
first |
206.049372 |
-0.1312763 |
NA |
0.2014347 |
| are |
[5, 40) |
secon |
242.857395 |
-0.2544612 |
0.0000971 |
0.9148647 |
| are |
[40, 80) |
null |
125.339960 |
NA |
NA |
NA |
| are |
[40, 80) |
first |
113.870825 |
0.0170039 |
NA |
0.7341244 |
| are |
[40, 80) |
secon |
79.339917 |
0.2071561 |
-0.0002060 |
0.2244371 |
| arg |
[0, 5) |
null |
130.728188 |
NA |
NA |
NA |
| arg |
[0, 5) |
first |
84.294377 |
0.1912636 |
NA |
0.1850338 |
| arg |
[0, 5) |
secon |
108.516515 |
0.0499888 |
0.0001940 |
0.7766805 |
| arg |
[5, 40) |
null |
130.728188 |
NA |
NA |
NA |
| arg |
[5, 40) |
first |
79.519910 |
0.1542883 |
NA |
0.2708579 |
| arg |
[5, 40) |
secon |
322.236612 |
-1.3774678 |
0.0022913 |
0.1490499 |
| arg |
[40, 80) |
null |
125.339960 |
NA |
NA |
NA |
| arg |
[40, 80) |
first |
129.806721 |
-0.0197644 |
NA |
0.7935989 |
| arg |
[40, 80) |
secon |
79.934206 |
0.2873313 |
-0.0003824 |
0.0327669 |
| ca |
[0, 5) |
null |
130.728188 |
NA |
NA |
NA |
| ca |
[0, 5) |
first |
58.699588 |
8.7339937 |
NA |
0.0000426 |
| ca |
[0, 5) |
secon |
45.171458 |
15.7922484 |
-0.5923057 |
0.2422225 |
| ca |
[5, 40) |
null |
130.728188 |
NA |
NA |
NA |
| ca |
[5, 40) |
first |
80.881755 |
13.8068958 |
NA |
0.0000000 |
| ca |
[5, 40) |
secon |
36.847035 |
43.1311267 |
-3.4654478 |
0.0216533 |
| ca |
[40, 80) |
null |
125.339960 |
NA |
NA |
NA |
| ca |
[40, 80) |
first |
99.302556 |
11.0265372 |
NA |
0.0785754 |
| ca |
[40, 80) |
secon |
87.567507 |
21.7280416 |
-2.1231636 |
0.6569827 |
| cad |
[0, 5) |
null |
130.728188 |
NA |
NA |
NA |
| cad |
[0, 5) |
first |
16.106129 |
493.4425225 |
NA |
0.1633881 |
| cad |
[0, 5) |
secon |
-64.698171 |
1454.5346898 |
-2657.1934029 |
0.2255103 |
| cad |
[5, 40) |
null |
130.728188 |
NA |
NA |
NA |
| cad |
[5, 40) |
first |
192.875023 |
-243.3021733 |
NA |
0.3640972 |
| cad |
[5, 40) |
secon |
63.187790 |
571.4228015 |
-1198.2065064 |
0.6989956 |
| cad |
[40, 80) |
null |
125.339960 |
NA |
NA |
NA |
| cad |
[40, 80) |
first |
162.221952 |
-169.7106073 |
NA |
0.3920597 |
| cad |
[40, 80) |
secon |
176.636698 |
-272.5796695 |
135.7535957 |
0.9927015 |
| cas |
[0, 5) |
null |
130.728188 |
NA |
NA |
NA |
| cas |
[0, 5) |
first |
106.725229 |
0.4131318 |
NA |
0.3737966 |
| cas |
[0, 5) |
secon |
102.601836 |
0.6523066 |
-0.0020711 |
0.0609441 |
| cas |
[5, 40) |
null |
130.728188 |
NA |
NA |
NA |
| cas |
[5, 40) |
first |
123.123311 |
0.1166657 |
NA |
0.5845268 |
| cas |
[5, 40) |
secon |
105.095667 |
0.4527831 |
-0.0010058 |
0.6573749 |
| cas |
[40, 80) |
null |
125.339960 |
NA |
NA |
NA |
| cas |
[40, 80) |
first |
110.683788 |
0.1686210 |
NA |
0.2749414 |
| cas |
[40, 80) |
secon |
101.392228 |
0.5292657 |
-0.0010270 |
0.0000000 |
| ctc |
[0, 5) |
null |
130.728188 |
NA |
NA |
NA |
| ctc |
[0, 5) |
first |
18.484601 |
9.3194642 |
NA |
0.0000000 |
| ctc |
[0, 5) |
secon |
7.356695 |
11.6546367 |
-0.1012332 |
0.7889734 |
| ctc |
[5, 40) |
null |
130.728188 |
NA |
NA |
NA |
| ctc |
[5, 40) |
first |
77.397176 |
6.9469844 |
NA |
0.0933460 |
| ctc |
[5, 40) |
secon |
19.326865 |
21.8761331 |
-0.7893185 |
0.1650525 |
| ctc |
[40, 80) |
null |
125.339960 |
NA |
NA |
NA |
| ctc |
[40, 80) |
first |
59.817425 |
11.7929533 |
NA |
0.0246995 |
| ctc |
[40, 80) |
secon |
8.710036 |
33.2096973 |
-2.0109619 |
0.9512799 |
| k |
[0, 5) |
null |
130.728188 |
NA |
NA |
NA |
| k |
[0, 5) |
first |
94.840386 |
0.2905706 |
NA |
0.3479489 |
| k |
[0, 5) |
secon |
101.911988 |
0.1454464 |
0.0005700 |
0.8877390 |
| k |
[5, 40) |
null |
130.728188 |
NA |
NA |
NA |
| k |
[5, 40) |
first |
98.632993 |
1.0351537 |
NA |
0.1952030 |
| k |
[5, 40) |
secon |
78.327564 |
2.4080313 |
-0.0193771 |
0.2162798 |
| k |
[40, 80) |
null |
125.339960 |
NA |
NA |
NA |
| k |
[40, 80) |
first |
112.072516 |
0.2632894 |
NA |
0.4437051 |
| k |
[40, 80) |
secon |
117.748947 |
-0.3262582 |
0.0038429 |
0.5738796 |
| mg |
[0, 5) |
null |
130.728188 |
NA |
NA |
NA |
| mg |
[0, 5) |
first |
88.371996 |
24.1620676 |
NA |
0.0074906 |
| mg |
[0, 5) |
secon |
76.469264 |
35.4321684 |
-2.2813969 |
0.6510028 |
| mg |
[5, 40) |
null |
130.728188 |
NA |
NA |
NA |
| mg |
[5, 40) |
first |
98.341215 |
20.6097284 |
NA |
0.0611025 |
| mg |
[5, 40) |
secon |
90.796574 |
30.5876948 |
-2.6183831 |
0.9656940 |
| mg |
[40, 80) |
null |
125.339960 |
NA |
NA |
NA |
| mg |
[40, 80) |
first |
104.206295 |
20.4014558 |
NA |
0.0014016 |
| mg |
[40, 80) |
secon |
96.027430 |
43.2873381 |
-11.0994357 |
0.8440816 |
| mo |
[0, 5) |
null |
130.728188 |
NA |
NA |
NA |
| mo |
[0, 5) |
first |
88.203002 |
0.8820689 |
NA |
0.0001942 |
| mo |
[0, 5) |
secon |
92.421358 |
0.4386504 |
0.0042559 |
0.4877171 |
| mo |
[5, 40) |
null |
130.728188 |
NA |
NA |
NA |
| mo |
[5, 40) |
first |
90.494090 |
2.1278751 |
NA |
0.0031680 |
| mo |
[5, 40) |
secon |
90.494090 |
2.3889639 |
-0.0099522 |
0.7829282 |
| mo |
[40, 80) |
null |
125.339960 |
NA |
NA |
NA |
| mo |
[40, 80) |
first |
66.594986 |
6.1836815 |
NA |
0.1859671 |
| mo |
[40, 80) |
secon |
-10.734153 |
21.9431202 |
-0.7348095 |
0.7395119 |
| n |
[0, 5) |
null |
130.728188 |
NA |
NA |
NA |
| n |
[0, 5) |
first |
106.127584 |
7.5803934 |
NA |
0.6059800 |
| n |
[0, 5) |
secon |
343.586124 |
-141.0437397 |
21.6700783 |
0.0279743 |
| n |
[5, 40) |
null |
130.728188 |
NA |
NA |
NA |
| n |
[5, 40) |
first |
56.645541 |
30.3521323 |
NA |
0.0906437 |
| n |
[5, 40) |
secon |
-3.155471 |
93.4554648 |
-14.8056498 |
0.0619327 |
| n |
[40, 80) |
null |
125.339960 |
NA |
NA |
NA |
| n |
[40, 80) |
first |
150.132267 |
-11.2830135 |
NA |
0.4396747 |
| n |
[40, 80) |
secon |
147.279806 |
-8.6197431 |
-0.5841696 |
0.8753187 |
| p |
[0, 5) |
null |
130.728188 |
NA |
NA |
NA |
| p |
[0, 5) |
first |
110.858520 |
1.7298968 |
NA |
0.0509950 |
| p |
[0, 5) |
secon |
104.577135 |
5.5184715 |
-0.1561011 |
0.7985717 |
| p |
[5, 40) |
null |
130.728188 |
NA |
NA |
NA |
| p |
[5, 40) |
first |
120.612419 |
2.8479160 |
NA |
0.5360190 |
| p |
[5, 40) |
secon |
132.718250 |
-10.1819150 |
1.1580122 |
0.4542632 |
| p |
[40, 80) |
null |
125.339960 |
NA |
NA |
NA |
| p |
[40, 80) |
first |
118.043369 |
5.2926930 |
NA |
0.1854796 |
| p |
[40, 80) |
secon |
131.029421 |
-30.1789505 |
5.5598812 |
0.1258969 |
| ph |
[0, 5) |
null |
130.728188 |
NA |
NA |
NA |
| ph |
[0, 5) |
first |
-185.281836 |
45.4089038 |
NA |
0.0050642 |
| ph |
[0, 5) |
secon |
-228.333106 |
58.1992239 |
-0.9474311 |
0.9059899 |
| ph |
[5, 40) |
null |
130.728188 |
NA |
NA |
NA |
| ph |
[5, 40) |
first |
-78.171224 |
32.5016237 |
NA |
0.0111125 |
| ph |
[5, 40) |
secon |
-354.429475 |
128.3794871 |
-8.1252427 |
0.4379442 |
| ph |
[40, 80) |
null |
125.339960 |
NA |
NA |
NA |
| ph |
[40, 80) |
first |
-12.217865 |
22.4721278 |
NA |
0.0015246 |
| ph |
[40, 80) |
secon |
367.744856 |
-106.1368807 |
10.6243523 |
0.6842495 |
| sat |
[0, 5) |
null |
130.728188 |
NA |
NA |
NA |
| sat |
[0, 5) |
first |
-19.784806 |
1.8621851 |
NA |
0.0000011 |
| sat |
[0, 5) |
secon |
-19.271722 |
1.8346242 |
0.0002483 |
0.9865070 |
| sat |
[5, 40) |
null |
130.728188 |
NA |
NA |
NA |
| sat |
[5, 40) |
first |
30.713736 |
1.4601676 |
NA |
0.0022785 |
| sat |
[5, 40) |
secon |
43.262662 |
0.7606329 |
0.0070839 |
0.7861011 |
| sat |
[40, 80) |
null |
125.339960 |
NA |
NA |
NA |
| sat |
[40, 80) |
first |
85.808372 |
0.7463872 |
NA |
0.0523710 |
| sat |
[40, 80) |
secon |
124.265271 |
-1.7909532 |
0.0270597 |
0.1288512 |
| Ur |
[0, 5) |
null |
130.728188 |
NA |
NA |
NA |
| Ur |
[0, 5) |
first |
146.753627 |
-95.2672771 |
NA |
0.5377988 |
| Ur |
[0, 5) |
secon |
70.566721 |
660.7814220 |
-1775.0689420 |
0.1810386 |
| Ur |
[5, 40) |
null |
130.728188 |
NA |
NA |
NA |
| Ur |
[5, 40) |
first |
96.497956 |
165.7179692 |
NA |
0.2358282 |
| Ur |
[5, 40) |
secon |
91.903460 |
311.3371599 |
-597.2970319 |
0.8581565 |
| Ur |
[40, 80) |
null |
125.339960 |
NA |
NA |
NA |
| Ur |
[40, 80) |
first |
100.507132 |
86.3463986 |
NA |
0.4829927 |
| Ur |
[40, 80) |
secon |
43.527268 |
615.9799131 |
-1048.1004065 |
0.4165006 |
| Us |
[0, 5) |
null |
130.728188 |
NA |
NA |
NA |
| Us |
[0, 5) |
first |
36.503671 |
147.4547824 |
NA |
0.6494943 |
| Us |
[0, 5) |
secon |
-366.718007 |
1545.6595871 |
-1200.3176207 |
0.7464097 |
| Us |
[5, 40) |
null |
130.728188 |
NA |
NA |
NA |
| Us |
[5, 40) |
first |
261.805185 |
-200.3486456 |
NA |
0.1888603 |
| Us |
[5, 40) |
secon |
-598.570964 |
2242.0511630 |
-1719.6932484 |
0.8035137 |
| Us |
[40, 80) |
null |
125.339960 |
NA |
NA |
NA |
| Us |
[40, 80) |
first |
222.999653 |
-139.9538365 |
NA |
0.4329792 |
| Us |
[40, 80) |
secon |
-191.624247 |
883.2021409 |
-614.2777354 |
0.0000146 |
knitr::kable(tb_signif)
| al |
[0, 5) |
secon |
130.72819 |
-1555.7598254 |
6925.1608409 |
0.0000000 |
| al |
[5, 40) |
first |
137.12138 |
-105.5689730 |
NA |
0.0018612 |
| al |
[40, 80) |
first |
130.72819 |
-61.6602672 |
NA |
0.0000215 |
| alp |
[5, 40) |
first |
120.38098 |
-28.6306365 |
NA |
0.0012059 |
| are |
[0, 5) |
first |
239.96966 |
-0.1734461 |
NA |
0.0495940 |
| arg |
[40, 80) |
secon |
79.93421 |
0.2873313 |
-0.0003824 |
0.0327669 |
| ca |
[0, 5) |
first |
58.69959 |
8.7339937 |
NA |
0.0000426 |
| ca |
[5, 40) |
secon |
36.84704 |
43.1311267 |
-3.4654478 |
0.0216533 |
| cas |
[40, 80) |
secon |
101.39223 |
0.5292657 |
-0.0010270 |
0.0000000 |
| ctc |
[0, 5) |
first |
18.48460 |
9.3194642 |
NA |
0.0000000 |
| ctc |
[40, 80) |
first |
59.81743 |
11.7929533 |
NA |
0.0246995 |
| mg |
[0, 5) |
first |
88.37200 |
24.1620676 |
NA |
0.0074906 |
| mg |
[40, 80) |
first |
104.20630 |
20.4014558 |
NA |
0.0014016 |
| mo |
[0, 5) |
first |
88.20300 |
0.8820689 |
NA |
0.0001942 |
| mo |
[5, 40) |
first |
90.49409 |
2.1278751 |
NA |
0.0031680 |
| n |
[0, 5) |
secon |
343.58612 |
-141.0437397 |
21.6700783 |
0.0279743 |
| ph |
[0, 5) |
first |
-185.28184 |
45.4089038 |
NA |
0.0050642 |
| ph |
[5, 40) |
first |
-78.17122 |
32.5016237 |
NA |
0.0111125 |
| ph |
[40, 80) |
first |
-12.21786 |
22.4721278 |
NA |
0.0015246 |
| sat |
[0, 5) |
first |
-19.78481 |
1.8621851 |
NA |
0.0000011 |
| sat |
[5, 40) |
first |
30.71374 |
1.4601676 |
NA |
0.0022785 |
| Us |
[40, 80) |
secon |
-191.62425 |
883.2021409 |
-614.2777354 |
0.0000146 |
# Limites de recomendação.
# Torância: 10% abaixo da maior produção observada.
tol <- max(teca_arv$prod) - max(teca_arv$prod) * 0.9
quantile_limits <- function(beta_0, beta_1, beta_2, tol, x_range) {
coefs <- c(c = beta_0, b = beta_1, a = beta_2)
# ex <- extendrange(x_range)
ex <- x_range
x_seq <- seq(ex[1], ex[2], length.out = 51)
if (is.finite(beta_2)) {
# Modelo de segundo grau.
x_opt <- -beta_1/(2 * beta_2)
y_opt <- c(x_opt^(0:2) %*% coefs)
roots <- with(as.list(coefs), {
delta <- sqrt(b^2 - 4 * a * (c - y_opt + tol))
suppressWarnings({
(-b + c(-1, 1) * delta)/(2 * a)
})
})
limits <- data.frame(y_opt = y_opt,
x_opt = x_opt,
y_tol = y_opt - tol,
x_tol_inf = min(roots),
x_tol_sup = max(roots))
pred <- data.frame(x = x_seq,
y = beta_0 + beta_1 * x_seq + beta_2 * x_seq^2)
return(list(limits, pred))
} else if (is.finite(beta_1)) {
# Modelo de primeiro grau.
y_range <- beta_0 + beta_1 * x_range
x_opt <- x_range[which.max(y_range)]
y_opt <- max(y_range)
x_tol <- x_opt - tol/beta_1
limits <- data.frame(y_opt = y_opt,
x_opt = x_opt,
y_tol = y_opt - tol,
x_tol_inf = ifelse(x_tol < x_opt, x_tol, NA),
x_tol_sup = ifelse(x_tol > x_opt, x_tol, NA))
pred <- data.frame(x = x_seq,
y = beta_0 + beta_1 * x_seq)
return(list(limits, pred))
} else {
# Modelo nulo.
pred <- data.frame(x = x_seq,
y = beta_0)
limits <- data.frame(y_opt = beta_0,
x_opt = NA,
y_tol = beta_0 - tol,
x_tol_inf = NA,
x_tol_sup = NA)
return(list(limits, pred))
}
}
# Amplitude das variáveis preditoras.
tb_range <- tb_long %>%
drop_na() %>%
group_by(variable, cam) %>%
summarise(x_range = list(range(valor)))
# O modelo para cada camada com prioridade secon > first > null.
tb_model <- tb_result %>%
filter(pvalue < 0.05 | is.na(pvalue)) %>%
group_by(variable, cam) %>%
slice(n()) %>%
ungroup()
# xtabs(~variable + cam, data = tb_model)
# Junta com a informação de amplitude das variáveis.
tb_limits <- inner_join(tb_model, tb_range)
## Joining, by = c("variable", "cam")
# Obtém os limites e a tabela de valores preditos.
tb_limits <- tb_limits %>%
mutate(limits = pmap(list(beta_0,
beta_1,
beta_2,
tol = tol,
x_range = x_range),
quantile_limits))
tb_limits
## # A tibble: 54 x 9
## variable cam model beta_0 beta_1 beta_2 pvalue x_range
## <chr> <fct> <fct> <dbl> <dbl> <dbl> <dbl> <list>
## 1 acc [0, … null 131. NA NA NA <dbl […
## 2 acc [5, … null 131. NA NA NA <dbl […
## 3 acc [40,… null 125. NA NA NA <dbl […
## 4 al [0, … secon 131. -1.56e+3 6925. 7.16e-13 <dbl […
## 5 al [5, … first 137. -1.06e+2 NA 1.86e- 3 <dbl […
## 6 al [40,… first 131. -6.17e+1 NA 2.15e- 5 <dbl […
## 7 alp [0, … null 131. NA NA NA <dbl […
## 8 alp [5, … first 120. -2.86e+1 NA 1.21e- 3 <dbl […
## 9 alp [40,… null 125. NA NA NA <dbl […
## 10 are [0, … first 240. -1.73e-1 NA 4.96e- 2 <dbl […
## # … with 44 more rows, and 1 more variable: limits <list>
# Variávies para traçar as linhas de orientação.
tb_lines <- tb_limits %>%
select(variable, cam, limits) %>%
mutate(limits = map(limits, `[[`, 1)) %>%
unnest(limits)
# xtabs(~variable + cam, data = tb_lines)
# Valores preditos de acordo com os modelos escolhidos.
tb_pred <- tb_limits %>%
select(variable, cam, limits) %>%
mutate(limits = map(limits, `[[`, 2)) %>%
unnest(limits)
# xtabs(~variable + cam, data = tb_pred)
# Gráfico.
ggplot(data = tb_long,
mapping = aes(x = valor, y = prod, color = cam)) +
facet_wrap(facets = ~variable, scales = "free_x", ncol = 3) +
geom_point(shape = 1) +
# ylim(extendrange(teca_arv$prod)) +
labs(color = "Camada") +
xlab("Valor da variável de solo") +
ylab("Produção de teca") +
# geom_quantile(quantiles = 0.9,
# formula = y ~ poly(x, degree = 2),
# linetype = 2) +
geom_line(data = tb_pred,
mapping = aes(x = x, y = y, color = cam),
size = 1.25) +
# geom_hline(data = tb_lines,
# mapping = aes(yintercept = y_opt, color = cam),
# linetype = 2) +
# geom_hline(data = tb_lines,
# mapping = aes(yintercept = y_tol, color = cam),
# linetype = 3) +
geom_vline(data = tb_lines,
mapping = aes(xintercept = x_opt, color = cam)) +
geom_vline(data = tb_lines,
mapping = aes(xintercept = x_tol_inf, color = cam),
linetype = 4) +
geom_vline(data = tb_lines,
mapping = aes(xintercept = x_tol_sup, color = cam),
linetype = 4) +
theme()
