# load packages
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.1     ✔ readr     2.2.0
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.3     ✔ tibble    3.3.1
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ✔ purrr     1.2.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggpubr)
library(table1)
## 
## Anexando pacote: 'table1'
## 
## Os seguintes objetos são mascarados por 'package:base':
## 
##     units, units<-
library(nortest)
library(patchwork)
library(readxl)
# load data
df <- read_excel("df.xlsx")

df$cidade[df$cidade == 0] <- "Porto Alegre"
df$cidade[df$cidade == 1] <- "Canoas"
df$cidade[df$cidade == 2] <- "Guaiba"
df$cidade[df$cidade == 3] <- "Gravatai"

df$cidade <- as.factor(df$cidade)

# tabagismo
df$tabagismo[df$tabagismo == 0] <- "Não Fumante"
df$tabagismo[df$tabagismo == 1] <- "Ex-Fumante"
df$tabagismo[df$tabagismo == 2] <- "Fumante"
df$tabagismo <- as.factor(df$tabagismo)
#chunk#2

library(table1)
table1(~ idade + altura + peso + imc + tabagismo | cidade, data = df)
Canoas
(N=75)
Gravatai
(N=75)
Guaiba
(N=75)
Porto Alegre
(N=75)
Overall
(N=300)
idade
Mean (SD) 35.6 (11.2) 29.8 (10.3) 36.1 (11.6) 34.0 (11.0) 33.9 (11.3)
Median [Min, Max] 35.7 [18.6, 62.9] 26.1 [18.1, 54.2] 34.8 [18.3, 55.9] 35.5 [18.0, 53.8] 33.0 [18.0, 62.9]
altura
Mean (SD) 175 (6.78) 175 (8.13) 172 (7.86) 173 (7.78) 174 (7.69)
Median [Min, Max] 175 [162, 196] 174 [150, 197] 172 [157, 197] 174 [157, 192] 174 [150, 197]
peso
Mean (SD) 82.8 (14.2) 81.1 (13.5) 80.8 (15.6) 82.0 (15.4) 81.7 (14.6)
Median [Min, Max] 82.0 [50.0, 132] 80.0 [54.0, 122] 80.0 [58.0, 140] 82.0 [54.0, 125] 80.0 [50.0, 140]
imc
Mean (SD) 27.0 (3.96) 26.6 (4.32) 27.2 (4.37) 26.9 (5.70) 26.9 (4.62)
Median [Min, Max] 26.6 [17.7, 39.0] 25.8 [18.6, 41.7] 26.1 [19.6, 40.9] 27.0 [0.00257, 38.7] 26.4 [0.00257, 41.7]
tabagismo
Ex-Fumante 13 (17.3%) 5 (6.7%) 17 (22.7%) 14 (18.7%) 49 (16.3%)
Fumante 18 (24.0%) 17 (22.7%) 6 (8.0%) 15 (20.0%) 56 (18.7%)
Não Fumante 44 (58.7%) 53 (70.7%) 52 (69.3%) 46 (61.3%) 195 (65.0%)
lillie.test(df$fvcz)
## 
##  Lilliefors (Kolmogorov-Smirnov) normality test
## 
## data:  df$fvcz
## D = 0.058471, p-value = 0.01504
lillie.test(df$fev1z)
## 
##  Lilliefors (Kolmogorov-Smirnov) normality test
## 
## data:  df$fev1z
## D = 0.050336, p-value = 0.06404
lillie.test(df$fev1fvcz)
## 
##  Lilliefors (Kolmogorov-Smirnov) normality test
## 
## data:  df$fev1fvcz
## D = 0.050541, p-value = 0.06195
lillie.test(df$fef2575z)
## 
##  Lilliefors (Kolmogorov-Smirnov) normality test
## 
## data:  df$fef2575z
## D = 0.056845, p-value = 0.02054
kruskal.test(fvcz ~ cidade, data = df)
## 
##  Kruskal-Wallis rank sum test
## 
## data:  fvcz by cidade
## Kruskal-Wallis chi-squared = 15.091, df = 3, p-value = 0.00174
kruskal.test(fev1z ~ cidade, data = df)
## 
##  Kruskal-Wallis rank sum test
## 
## data:  fev1z by cidade
## Kruskal-Wallis chi-squared = 17.793, df = 3, p-value = 0.0004852
kruskal.test(fev1fvcz ~ cidade, data = df)
## 
##  Kruskal-Wallis rank sum test
## 
## data:  fev1fvcz by cidade
## Kruskal-Wallis chi-squared = 4.733, df = 3, p-value = 0.1924
kruskal.test(fef2575z ~ cidade, data = df)
## 
##  Kruskal-Wallis rank sum test
## 
## data:  fef2575z by cidade
## Kruskal-Wallis chi-squared = 7.9169, df = 3, p-value = 0.04776
##6”: faça um teste post-hoc comparando a função pulmonar entre as cidades (fvcz fev1z fev1fvcz e fef2575z) (dica: pairwise.wilcox.test)
pairwise.wilcox.test(df$fvcz, df$cidade, p.adjust.method = "bonferroni")
## 
##  Pairwise comparisons using Wilcoxon rank sum test with continuity correction 
## 
## data:  df$fvcz and df$cidade 
## 
##              Canoas Gravatai Guaiba
## Gravatai     0.0013 -        -     
## Guaiba       0.2041 0.4600   -     
## Porto Alegre 0.0540 1.0000   1.0000
## 
## P value adjustment method: bonferroni
pairwise.wilcox.test(df$fev1z, df$cidade, p.adjust.method = "bonferroni")
## 
##  Pairwise comparisons using Wilcoxon rank sum test with continuity correction 
## 
## data:  df$fev1z and df$cidade 
## 
##              Canoas  Gravatai Guaiba 
## Gravatai     0.00026 -        -      
## Guaiba       0.97223 0.01835  -      
## Porto Alegre 0.45626 0.21576  1.00000
## 
## P value adjustment method: bonferroni
pairwise.wilcox.test(df$fev1fvcz, df$cidade, p.adjust.method = "bonferroni")
## 
##  Pairwise comparisons using Wilcoxon rank sum test with continuity correction 
## 
## data:  df$fev1fvcz and df$cidade 
## 
##              Canoas Gravatai Guaiba
## Gravatai     1.00   -        -     
## Guaiba       0.84   0.41     -     
## Porto Alegre 1.00   0.85     1.00  
## 
## P value adjustment method: bonferroni
#```{r chunk7}
library(ggplot2)
library(ggpubr)

g1 <- ggplot(df, aes(x = cidade, y = fvcz)) +
  geom_boxplot() +
  labs(title = "FVCz", x = "Cidade", y = "FVCz")

g2 <- ggplot(df, aes(x = cidade, y = fev1z)) +
  geom_boxplot() +
  labs(title = "FEV1z", x = "Cidade", y = "FEV1z")

g3 <- ggplot(df, aes(x = cidade, y = fev1fvcz)) +
  geom_boxplot() +
  labs(title = "FEV1/FVCz", x = "Cidade", y = "FEV1/FVCz")

g4 <- ggplot(df, aes(x = cidade, y = fef2575z)) +
  geom_boxplot() +
  labs(title = "FEF25-75z", x = "Cidade", y = "FEF25-75z")

ggarrange(g1, g2, g3, g4, ncol = 2, nrow = 2)

g1 <- ggplot(df, aes(x = cidade, y = fvcz)) +
  geom_boxplot(fill = "gray") +
  stat_compare_means() +
  xlab("Municipio") + ylab("FVCz") +
  ggtitle("FVCz por cidade")

g2 <- ggplot(df, aes(x = cidade, y = fev1z)) +
  geom_boxplot(fill = "gray") +
  stat_compare_means() +
  xlab("Municipio") + ylab("FEV1z") +
  ggtitle("FEV1z por cidade")

g3 <- ggplot(df, aes(x = cidade, y = fev1fvcz)) +
  geom_boxplot(fill = "gray") +
  stat_compare_means() +
  xlab("Municipio") + ylab("FEV1/FVCz") +
  ggtitle("FEV1/FVCz por cidade")

g4 <- ggplot(df, aes(x = cidade, y = fef2575z)) +
  geom_boxplot(fill = "gray") +
  stat_compare_means() +
  xlab("Municipio") + ylab("FEF25-75") +
  ggtitle("FEF25-75 por cidade")

ggarrange(g1, g2, g3, g4, ncol = 2, nrow = 2)

g4 <- ggplot(df, aes(x = cidade, y = fef2575z, fill=cidade)) +
  geom_boxplot() +
  stat_compare_means(method = "kruskal.test") +
  xlab("Municipio") + ylab("FEF25-75z") +
  scale_fill_manual(values = c(
  "Porto Alegre" = "lightblue",
  "Canoas" = "lightgreen",
  "Guaiba" = "lightpink",
  "Gravatai" = "lightyellow")) +
  ggtitle("FEF25-75z por cidade")
g4