##
Vamos a cargar la base de datos
library(readxl)
dbdraastrid <- read_excel("E:/OneDrive - CINVESTAV/PROYECTO MDatos/TRABAJOS/Dra Astrid Hernandez Hosp PEMEX MDatos/dbdraastrid.xlsx")
dbastrid<-dbdraastrid
head(dbastrid)
## # A tibble: 6 × 7
## `Nombre del paciente` Ficha Edad Sexo `PCR SARS CoV-2` `CFS (Rockwood)`
## <chr> <chr> <dbl> <chr> <chr> <dbl>
## 1 GRACIELA GARCIA GUZMAN 5633… 60 F si 3
## 2 FRANCISCO JAVIER VEGA ROD… 6107… 64 M si 1
## 3 CELIA COBOS SIFUENTES 1890… 79 F si 6
## 4 MARIA ISABEL FERNANDEZ GO… 0840… 67 F si 5
## 5 SERGIO VIZCAINO MARTINEZ … 1808… 60 M si 2
## 6 FABIAN BALDERAS LOPEZ 2079… 60 M si 2
## # ℹ 1 more variable: Fallecieron <chr>
str(dbastrid)
## tibble [568 × 7] (S3: tbl_df/tbl/data.frame)
## $ Nombre del paciente: chr [1:568] "GRACIELA GARCIA GUZMAN" "FRANCISCO JAVIER VEGA RODRIGUEZ" "CELIA COBOS SIFUENTES" "MARIA ISABEL FERNANDEZ GONZALEZ**" ...
## $ Ficha : chr [1:568] "563313-00" "610734-00" "189038-00" "084001-00" ...
## $ Edad : num [1:568] 60 64 79 67 60 60 89 87 76 64 ...
## $ Sexo : chr [1:568] "F" "M" "F" "F" ...
## $ PCR SARS CoV-2 : chr [1:568] "si" "si" "si" "si" ...
## $ CFS (Rockwood) : num [1:568] 3 1 6 5 2 2 7 5 6 5 ...
## $ Fallecieron : chr [1:568] "Si" "Si" "Si" "Si" ...
length(dbastrid$Sexo)
## [1] 568
unique(dbastrid$Sexo)
## [1] "F" "M" "m" NA
unique(dbastrid$Fallecieron)
## [1] "Si" "No"
##ANALISIS EXPLORATORIO CON EDAD Y GENERO
#cargamos paquetes requeridos:
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.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)
gghistogram(dbastrid, x="Edad", fill="#0073C2FF", bins = 30, color = "#0073C2FF",
add = "median", rug = TRUE)
## Warning: `geom_vline()`: Ignoring `mapping` because `xintercept` was provided.
## Warning: `geom_vline()`: Ignoring `data` because `xintercept` was provided.
## Warning: Removed 4 rows containing non-finite values (`stat_bin()`).
#para resolver este eror usamos la siguiente función
# Utilizamos la función subset() para filtrar el DataFrame y quedarnos solo con los valores "F" y "M"
dbastrid <- subset(dbastrid, Sexo %in% c("F", "M"))
gghistogram(dbastrid, x = "Edad", y = "count",ylab = "conteo",xlab = "Edad", title="Distribución de la Edad por Sexo población total", bins = 30, panel.labs = list(SEXO = c("F", "M")),
add = "median", rug = TRUE,add_density = TRUE,
color = "Sexo", fill = "Sexo")
#vamos a hacerlo ahora con un boxplot
ggboxplot(dbastrid, x = "Sexo", y = "Edad",ylab = "Edad años",xlab = "Sexo", title="Distribución de la Edad por Sexo población total",add_density = TRUE,
color = "black", fill = "Sexo",
palette = c("#0073C2FF", "#FC4E07"))
library(ggsignif)
dbastrid$Fallecieron <- factor(dbastrid$Fallecieron, levels = c("Si", "No"), labels = c("No sobrevivieron", "Sobrevivieron"))
# Crea el gráfico con la prueba de U de Mann-Whitney o Wilcoxon
ggplot(dbastrid, aes(x = Sexo, y = Edad, color = Sexo)) +
stat_summary(fun = "median", geom = "errorbar", width = 0.2, size = 1.5, color = "black") +
geom_jitter(position = position_jitter(0.2), size = 3) +
stat_summary(fun.y = median, geom = "point") +
stat_summary(fun.y = median, fun.ymin = median, fun.ymax = median, geom = "crossbar", width = 0.5, color = "black") +
stat_compare_means(comparisons = list(c("F", "M")), method = "wilcox.test", label = "p.signif") +
facet_wrap(~ Fallecieron, nrow = 1, ncol = 2) +
theme_minimal() +
theme(
strip.text = element_text(face = "bold", size = 12),
axis.text.x = element_text(size = 12,color="black"),
axis.text.y = element_text(size = 12, color="black"),
axis.title.y = element_text(face="bold", size= 12),
axis.title.x = element_text(face="bold", size= 12),
axis.title = element_text(face="bold", size= 50),
) +
labs(
x = element_blank(), # Para eliminar el título del eje x
y = "Edad (años)", # Etiqueta del eje y
color = "",
title = "Distribución de edad entre pacientes que sobrevivieron y no sobrevivieron",
subtitle = "",
caption = "La línea horizontal (Negra) representa a la mediana"
)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: The `fun.y` argument of `stat_summary()` is deprecated as of ggplot2 3.3.0.
## ℹ Please use the `fun` argument instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: The `fun.ymin` argument of `stat_summary()` is deprecated as of ggplot2 3.3.0.
## ℹ Please use the `fun.min` argument instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: The `fun.ymax` argument of `stat_summary()` is deprecated as of ggplot2 3.3.0.
## ℹ Please use the `fun.max` argument instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
# Carga la biblioteca necesaria
library(dplyr)
# Filtrar los datos para los pacientes que "Sobrevivieron"
sobrevivieron_data <- dbastrid %>% filter(Fallecieron == "Sobrevivieron")
# Filtrar los datos para los pacientes que "No Sobrevivieron"
no_sobrevivieron_data <- dbastrid %>% filter(Fallecieron != "Sobrevivieron")
# Realizar la prueba de Wilcoxon-Mann-Whitney para los pacientes que "Sobrevivieron"
wilcox_sobrevivieron <- wilcox.test(Edad ~ Sexo, data = sobrevivieron_data)
# Realizar la prueba de Wilcoxon-Mann-Whitney para los pacientes que "No Sobrevivieron"
wilcox_no_sobrevivieron <- wilcox.test(Edad ~ Sexo, data = no_sobrevivieron_data)
# Calcular la diferencia entre medianas para cada grupo
diferencia_medianas_sobrevivieron <- abs(median(sobrevivieron_data$Edad[sobrevivieron_data$Sexo == "F"]) -
median(sobrevivieron_data$Edad[sobrevivieron_data$Sexo == "M"]))
diferencia_medianas_no_sobrevivieron <- abs(median(no_sobrevivieron_data$Edad[no_sobrevivieron_data$Sexo == "F"]) -
median(no_sobrevivieron_data$Edad[no_sobrevivieron_data$Sexo == "M"]))
# Crear una tabla con los resultados
resultados_tabla <- data.frame(comparación = c("F vs. M", "F vs. M"),
Grupo = c("Sobrevivieron", "No Sobrevivieron"),
Valor_de_p = c(wilcox_sobrevivieron$p.value, wilcox_no_sobrevivieron$p.value),
Diferencia_estadistica = c(wilcox_sobrevivieron$statistic, wilcox_no_sobrevivieron$statistic),
Diferencia_medianas = c(diferencia_medianas_sobrevivieron, diferencia_medianas_no_sobrevivieron))
# Imprimir la tabla
print(resultados_tabla)
## comparación Grupo Valor_de_p Diferencia_estadistica
## 1 F vs. M Sobrevivieron 0.39504276 14247
## 2 F vs. M No Sobrevivieron 0.07500113 7045
## Diferencia_medianas
## 1 2
## 2 4
# Crea un dataframe con el conteo y porcentaje de CFS (Rockwood)
df <- dbastrid %>%
group_by(`CFS (Rockwood)`) %>%
summarise(Count = n(), .groups = 'drop') %>%
mutate(Percentage = Count / sum(Count))
# Crea el gráfico de barras
ggplot(df, aes(x=`CFS (Rockwood)`, y=Count)) +
geom_bar(stat="identity", fill="#69b3a2") +
geom_text(aes(label=Count), vjust=-0.2, color="black") +
geom_text(aes(label=paste0("(", scales::percent(Percentage), ")")), vjust=1.2, color="black") +
theme_minimal() +
labs(x="CFS (Rockwood)", y="Conteo", title="Distribución de CFS (Rockwood)") +
scale_x_continuous(breaks = 1:10)+theme_minimal() +
theme(
strip.text = element_text(face = "bold", size = 12),
axis.text.x = element_text(size = 12,color="black"),
axis.text.y = element_text(size = 12, color="black"),
axis.title.y = element_text(face="bold", size= 12),
axis.title.x = element_text(face="bold", size= 12),
axis.title = element_text(face="bold", size= 50),
)
# Crea un dataframe con el conteo y porcentaje de CFS (Rockwood) agrupado por Fallecieron
df <- dbastrid %>%
group_by(`CFS (Rockwood)`, Fallecieron) %>%
summarise(Count = n(), .groups = 'drop') %>%
mutate(Percentage = Count / sum(Count))
# Crea un dataframe con el conteo de CFS (Rockwood) agrupado por Fallecieron
df <- dbastrid %>%
group_by(`CFS (Rockwood)`, Fallecieron) %>%
summarise(Count = n(), .groups = 'drop')
# Realiza la prueba de chi-cuadrada
chisq_result <- chisq.test(df$Count)
# Crea el gráfico de barras
ggplot(df, aes(x=`CFS (Rockwood)`, y=Count, fill=Fallecieron)) +
geom_bar(stat="identity", position="dodge") +
theme_minimal() +
labs(x="CFS (Rockwood)", y="Count", title="Distribución de CFS (Rockwood) Agrupado por Fallecieron") +
scale_x_continuous(breaks = 1:11) +
annotate("text", x=5, y=max(df$Count), label=paste("p-value =", round(chisq_result$p.value, digits=3)), size=5, color="black")
# Crea una tabla de contingencia
contingency_table <- table(dbastrid$`CFS (Rockwood)`, dbastrid$Fallecieron)
# Realiza la prueba de chi-cuadrado
chi_test <- chisq.test(contingency_table)
# Crea un dataframe con el conteo de CFS (Rockwood) agrupado por Fallecieron
df <- dbastrid %>%
group_by(`CFS (Rockwood)`, Fallecieron) %>%
summarise(Count = n(), .groups = 'drop')
# Crea el gráfico de barras
p <- ggplot(df, aes(x=`CFS (Rockwood)`, y=Count, fill=Fallecieron)) +
geom_bar(stat="identity", position="dodge") +
theme_minimal() +
labs(x="CFS (Rockwood)", y="Count", title="Distribución de CFS (Rockwood) Agrupado por Fallecieron") +
scale_x_continuous(breaks = 1:11)
# Agrega el valor p al gráfico
p + annotate("text", x=5, y=max(df$Count), label=paste("p-value =", round(chi_test$p.value, digits=3)), size=5, color="black")
library(dplyr)
library(ggplot2)
library(scales)
##
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
##
## discard
## The following object is masked from 'package:readr':
##
## col_factor
library(purrr)
library(rstatix)
##
## Attaching package: 'rstatix'
## The following object is masked from 'package:stats':
##
## filter
library(RColorBrewer)
# Crea una tabla de contingencia
contingency_table <- table(dbastrid$`CFS (Rockwood)`, dbastrid$Fallecieron)
# Realiza la prueba de chi-cuadrado
chi_test <- chisq.test(contingency_table)
# Crea un dataframe con el conteo de CFS (Rockwood) agrupado por Fallecieron
df <- dbastrid %>%
group_by(`CFS (Rockwood)`, Fallecieron) %>%
summarise(Count = n(), .groups = 'drop')
# Crea el gráfico de barras
p <- ggplot(df, aes(x=`CFS (Rockwood)`, y=Count, fill=Fallecieron)) +
geom_bar(stat="identity", position="dodge") +
scale_fill_brewer(palette="Set1") +
theme_minimal() +
labs(x="CFS (Rockwood)", y="Conteo", title="Distribución de CFS (Rockwood) Agrupado por Fallecieron", fill=" ") +
scale_x_continuous(breaks = 1:9)
# Agrega el valor p al gráfico
p_value_text <- ifelse(chi_test$p.value < 0.0001, "<0.0001", round(chi_test$p.value, digits=3))
p <- p + annotate("text", x=8, y=max(df$Count), label=paste("p =", p_value_text), size=5, color="black")
# Realiza pruebas de chi-cuadrado entre cada uno de los elementos de "CFS (Rockwood)" agrupados por "Fallecieron"
p_values <- map_dbl(1:9, function(i) {
contingency_table_i <- table(dbastrid$`CFS (Rockwood)` == i, dbastrid$Fallecieron)
chisq.test(contingency_table_i)$p.value
})
# Ajusta los valores p para las comparaciones múltiples
p_values_adjusted <- p.adjust(p_values, method = "bonferroni")
# Agrega asteriscos al gráfico según la significancia
for (i in 1:9) {
significance <- case_when(
p_values_adjusted[i] < 0.001 ~ "***",
p_values_adjusted[i] < 0.01 ~ "**",
p_values_adjusted[i] < 0.05 ~ "*",
TRUE ~ ""
)
p <- p + annotate("text", x=i, y=max(df$Count[df$`CFS (Rockwood)` == i]), label=significance, size=8, color="black")
}
# Agrega una leyenda al gráfico
p <- p + labs(caption = "Prueba de chi-cuadrado. Significancias: '***' p<0.001, '**' p<0.01, '*' p<0.05")
# Muestra el gráfico
print(p)
library(gtsummary)
dbastrid %>% select(`CFS (Rockwood)`,Fallecieron) %>% tbl_summary(by=Fallecieron)%>% add_p() %>% add_overall()
| Characteristic | Overall, N = 5631 | No sobrevivieron, N = 2241 | Sobrevivieron, N = 3391 | p-value2 |
|---|---|---|---|---|
| CFS (Rockwood) | <0.001 | |||
| 1 | 57 (10%) | 7 (3.1%) | 50 (15%) | |
| 2 | 98 (17%) | 33 (15%) | 65 (19%) | |
| 3 | 64 (11%) | 24 (11%) | 40 (12%) | |
| 4 | 65 (12%) | 26 (12%) | 39 (12%) | |
| 5 | 89 (16%) | 41 (18%) | 48 (14%) | |
| 6 | 57 (10%) | 27 (12%) | 30 (8.8%) | |
| 7 | 73 (13%) | 42 (19%) | 31 (9.1%) | |
| 8 | 38 (6.7%) | 16 (7.1%) | 22 (6.5%) | |
| 9 | 22 (3.9%) | 8 (3.6%) | 14 (4.1%) | |
| 1 n (%) | ||||
| 2 Pearson’s Chi-squared test | ||||