library(ggplot2)
library(ggthemes)
## Warning: package 'ggthemes' was built under R version 4.1.3
library(showtext)
## Warning: package 'showtext' was built under R version 4.1.3
## Loading required package: sysfonts
## Warning: package 'sysfonts' was built under R version 4.1.3
## Loading required package: showtextdb
## Warning: package 'showtextdb' was built under R version 4.1.3
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## v purrr   0.3.4
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(lme4)
## Warning: package 'lme4' was built under R version 4.1.3
## Loading required package: Matrix
## 
## Attaching package: 'Matrix'
## The following objects are masked from 'package:tidyr':
## 
##     expand, pack, unpack
library(car)
## Warning: package 'car' was built under R version 4.1.3
## Loading required package: carData
## Warning: package 'carData' was built under R version 4.1.3
## 
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
## 
##     recode
## The following object is masked from 'package:purrr':
## 
##     some
#Ejercicio 1
#Cargar datos
Datos1<-read.csv("pacientes.csv", header=TRUE)

#Modelo lineal generalizado mixto
pacientes_cov <- glmer(Recuperacion_Postcovid~BhLinfocitosAbsolutos+edad+peso+(1|sexo),data=Datos1, family="binomial")
## boundary (singular) fit: see help('isSingular')
summary(pacientes_cov)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: binomial  ( logit )
## Formula: Recuperacion_Postcovid ~ BhLinfocitosAbsolutos + edad + peso +  
##     (1 | sexo)
##    Data: Datos1
## 
##      AIC      BIC   logLik deviance df.resid 
##     87.0    100.1    -38.5     77.0       97 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -0.4773 -0.4104 -0.3758 -0.3257  3.5053 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  sexo   (Intercept) 0        0       
## Number of obs: 102, groups:  sexo, 2
## 
## Fixed effects:
##                         Estimate Std. Error z value Pr(>|z|)
## (Intercept)           -1.1090463  2.2572238  -0.491    0.623
## BhLinfocitosAbsolutos -0.3600108  0.4414253  -0.816    0.415
## edad                   0.0009554  0.0210235   0.045    0.964
## peso                  -0.0048660  0.0189265  -0.257    0.797
## 
## Correlation of Fixed Effects:
##             (Intr) BhLnfA edad  
## BhLnfctsAbs -0.602              
## edad        -0.637  0.300       
## peso        -0.809  0.327  0.142
## optimizer (Nelder_Mead) convergence code: 0 (OK)
## boundary (singular) fit: see help('isSingular')
AIC(pacientes_cov)
## [1] 86.99109
#Modelo nulo
modelo.null <- glmer(Recuperacion_Postcovid ~ 1+(1|sexo), data=Datos1, family="binomial")
## boundary (singular) fit: see help('isSingular')
summary(modelo.null)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: binomial  ( logit )
## Formula: Recuperacion_Postcovid ~ 1 + (1 | sexo)
##    Data: Datos1
## 
##      AIC      BIC   logLik deviance df.resid 
##     81.8     87.1    -38.9     77.8      100 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -0.3822 -0.3822 -0.3822 -0.3822  2.6165 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  sexo   (Intercept) 0        0       
## Number of obs: 102, groups:  sexo, 2
## 
## Fixed effects:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  -1.9237     0.2969  -6.479 9.24e-11 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## optimizer (Nelder_Mead) convergence code: 0 (OK)
## boundary (singular) fit: see help('isSingular')
AIC(modelo.null)
## [1] 81.8285
#AIC del modelo nulo es menor, no se encuentran diferencias significativas, no hay efecto
#Comparación de modelos con prueba ANOVA
anova(modelo.null, pacientes_cov, test = "Chi")
## Data: Datos1
## Models:
## modelo.null: Recuperacion_Postcovid ~ 1 + (1 | sexo)
## pacientes_cov: Recuperacion_Postcovid ~ BhLinfocitosAbsolutos + edad + peso + (1 | sexo)
##               npar    AIC     BIC  logLik deviance  Chisq Df Pr(>Chisq)
## modelo.null      2 81.828  87.078 -38.914   77.828                     
## pacientes_cov    5 86.991 100.116 -38.496   76.991 0.8374  3     0.8405
#ANOVA de modelo mixto para obtener valores de P de variables
Anova(pacientes_cov)
## Analysis of Deviance Table (Type II Wald chisquare tests)
## 
## Response: Recuperacion_Postcovid
##                        Chisq Df Pr(>Chisq)
## BhLinfocitosAbsolutos 0.6651  1     0.4147
## edad                  0.0021  1     0.9638
## peso                  0.0661  1     0.7971
#Gráfico de barras No. pacientes recuperados y no recuperados
recuperacion<-table(Datos1$Recuperacion_Postcovid)
recuperacion
## 
##  0  1 
## 89 13
windows()
barplot(recuperacion, main = "Pacientes con Síndrome \nPostcovid", sub = "", xlab = "Estado de pacientes", ylab = "No. de pacientes", xaxt = "n", 
        col = c("#009688", "#9E9E9E"))
legend(x = "topright", legend = c("Recuperado", "No recuperado"), fill = c("#009688", "#9E9E9E"), 
       title = "")

#Gráfico de barras No. pacientes recuperados y no recuperados y sexo de pacientes
sexo_recuperacion<-table(Datos1$Recuperacion_Postcovid, Datos1$sexo)
sexo_recuperacion
##    
##      1  2
##   0 47 42
##   1  9  4
windows()
barplot(sexo_recuperacion, main = "Sexo de Pacientes con Síndrome Postcovid", xlab = "Sexo", ylab = "No. de Pacientes", 
        col = c("#009688", "#9E9E9E"))
legend(x = "topright", legend = c("Recuperado", "No recuperado"), fill = c("#009688", "#9E9E9E"), 
       title = "")

#Boxplot de Cantidad de Linfocitos en pacientes
windows()
ggplot(data = Datos1, aes(x = factor(Recuperacion_Postcovid), y = BhLinfocitosAbsolutos, fill=factor(Recuperacion_Postcovid))) +
  geom_boxplot(outlier.color="#222424")  +
  scale_fill_manual(values=c("#009688", "#9E9E9E"), labels = c("Recuperado", "No recuperado")) +
  geom_jitter(color="#222424", size=1, alpha=1) +
  labs(title="Cantidad de linfocitos en pacientes \ncon Síndrome Post-covid", 
       x="Recuperación Post-covid", y="Linfocitos B") + 
  theme_few()+
  theme(plot.title=element_text(face="bold", hjust = 0.5),
        axis.text.x = element_text(size=rel(1.35)),
        axis.title.x = element_text(face="bold", hjust=0.5, size=15),                                               
        axis.title.y = element_text(face="bold", hjust=0.5, size=15),
        legend.title=element_text(face="bold", color="black", hjust=0.5))+
  labs(fill="Pacientes con Síndrome \nPost-covid")+
  scale_x_discrete(
    "",
    labels = c(
      "0" = "Recuperado",
      "1" = "No Recuperado"))

#Edad
windows()
ggplot(data = Datos1, aes(x = factor(Recuperacion_Postcovid), y = edad, fill=factor(Recuperacion_Postcovid))) +
  geom_boxplot(outlier.color="#222424", shape=17)   +
  scale_fill_manual(values=c("#009688", "#9E9E9E"), labels = c("Recuperado", "No recuperado")) +
  geom_jitter(color="#222424", size=1, alpha=1) +
  labs(title="Edad de pacientes con \nSíndrome Post-covid", 
       x="Recuperación Post-covid", y="Edad en años") + 
  theme_few()+
  theme(plot.title=element_text(face="bold", hjust = 0.5),
        axis.text.x = element_text(size=rel(1.35)),
        axis.title.x = element_text(face="bold", hjust=0.5, size=15),                                               
        axis.title.y = element_text(face="bold", hjust=0.5, size=15),
        legend.title=element_text(face="bold", color="black", hjust=0.5))+
  labs(fill="Pacientes con Síndrome \nPost-covid")+
  scale_x_discrete(
    "",
    labels = c(
      "0" = "Recuperado",
      "1" = "No Recuperado"))

#Peso
windows()
ggplot(data = Datos1, aes(x = factor(Recuperacion_Postcovid), y = peso, fill=factor(Recuperacion_Postcovid))) +
  geom_boxplot(outlier.color="gray") +
  scale_fill_manual(values=c("#009688", "#9E9E9E"), labels = c("Recuperado", "No recuperado")) +
  geom_jitter(color="#222424", size=1, alpha=1) +
  labs(title="Peso de pacientes con \nSíndrome Post-covid", 
       x="Recuperación Post-covid", y="Peso en kg") + 
  theme_few()+
  theme(plot.title=element_text(face="bold", hjust = 0.5),
        axis.text.x = element_text(size=rel(1.35)),
        axis.title.x = element_text(face="bold", hjust=0.5, size=15),                                               
        axis.title.y = element_text(face="bold", hjust=0.5, size=15),
        legend.title=element_text(face="bold", color="black", hjust=0.5))+
  labs(fill="Pacientes con Síndrome \nPost-covid")+
  scale_x_discrete(
    "",
    labels = c(
      "0" = "Recuperado",
      "1" = "No Recuperado"))