#Base de datos -Bajo Peso al Nacer(Hosmer & Lemeshow)
library(readxl)
library(dplyr)
datos <- read_excel("D:/DISCOE/CLASES/JAVERIANA_SALUD/MSC_EPI_CLINICA/BIOESTADISTICA/Low_bw.xlsx")
##Comparación del peso promedio entre madres que Fuman y no Fuman (varianzas iguales)
library(EnvStats)

#Estadisticas descriptivas
summary(datos$bwt)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     709    2414    2977    2944    3475    4990
summaryFull(datos$bwt)
##                               datos$bwt
## N                             189.00000
## Mean                         2944.00000
## Median                       2977.00000
## 10% Trimmed Mean             2961.00000
## Geometric Mean               2841.00000
## Skew                           -0.20860
## Kurtosis                       -0.08175
## Min                           709.00000
## Max                          4990.00000
## Range                        4281.00000
## 1st Quartile                 2414.00000
## 3rd Quartile                 3475.00000
## Standard Deviation            729.00000
## Geometric Standard Deviation    1.32600
## Interquartile Range          1061.00000
## Median Absolute Deviation     819.90000
## Coefficient of Variation        0.24760
## attr(,"class")
## [1] "summaryStats"
## attr(,"stats.in.rows")
## [1] TRUE
## attr(,"drop0trailing")
## [1] TRUE
grupos=group_by(datos,smoke)
summarise(grupos, mean=mean(bwt), var=var(bwt), n=n())  
## # A tibble: 2 x 4
##   smoke  mean     var     n
##   <chr> <dbl>   <dbl> <int>
## 1 No    3055. 566119.   115
## 2 Yes   2772. 435346.    74
#Prueba F -Prueba de comparación de varianzas (Ho:varianzas iguales vs Ha:varianza diferentes)
var.test(datos$bwt~datos$smoke, alternative="t", conf.level=0.95)
## 
##  F test to compare two variances
## 
## data:  datos$bwt by datos$smoke
## F = 1.3004, num df = 114, denom df = 73, p-value = 0.2275
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
##  0.8476388 1.9566446
## sample estimates:
## ratio of variances 
##            1.30039
#Prueba t
t.test(datos$bwt~datos$smoke, mu=0, alternative="t", conf.level=0.95, var.equal=T )
## 
##  Two Sample t-test
## 
## data:  datos$bwt by datos$smoke
## t = 2.6428, df = 187, p-value = 0.00892
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##   71.66693 493.65152
## sample estimates:
##  mean in group No mean in group Yes 
##          3054.957          2772.297
##Comparación de la edad promedio entre madres con recien nacidos con bajo peso y sin bajo peso (varianzas diferentes)

#grupos por bajo peso al nacer
grupos=group_by(datos,low)
summarise(grupos, mean=mean(bwt), var=var(bwt), n=n())  
## `summarise()` ungrouping output (override with `.groups` argument)
## # A tibble: 2 x 4
##   low    mean     var     n
##   <chr> <dbl>   <dbl> <int>
## 1 No    3329. 228585.   130
## 2 Yes   2097. 152785.    59
#Prueba F (varianzas)
var.test(datos$age~datos$low, alternative="t", conf.level=0.95)
## 
##  F test to compare two variances
## 
## data:  datos$age by datos$low
## F = 1.5323, num df = 129, denom df = 58, p-value = 0.06885
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
##  0.9669261 2.3389614
## sample estimates:
## ratio of variances 
##           1.532254
#Prueba t
t.test(datos$age~datos$low, mu=0, alternative="t", conf.level=0.95, var.equal=F)
## 
##  Welch Two Sample t-test
## 
## data:  datos$age by datos$low
## t = 1.7737, df = 136.94, p-value = 0.07834
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -0.1558349  2.8687423
## sample estimates:
##  mean in group No mean in group Yes 
##          23.66154          22.30508
##Observacines dependientes (datos pareados)
library(readxl)

#Base de datos -Peso antes y después
datos2 <- read_excel("D:/DISCOE/CLASES/JAVERIANA_SALUD/MSC_EPI_CLINICA/BIOESTADISTICA/DatosPareados.xlsx")

#Prueba t (Promedio de las diferencias)
t.test(datos2$Peso_1,datos2$Peso_3, mu=0, alternative="t", conf.level=0.95, paired=T)
## 
##  Paired t-test
## 
## data:  datos2$Peso_1 and datos2$Peso_3
## t = 4.5503, df = 65, p-value = 2.403e-05
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  1.215718 3.117615
## sample estimates:
## mean of the differences 
##                2.166667
##Comparación de Proporciones

#tabla cruzada bajo peso y presencia irritabilidad uterina
prop.table(table(datos$low, datos$ui), 1)
##      
##              No       Yes
##   No  0.8923077 0.1076923
##   Yes 0.7627119 0.2372881
prop.test(table(datos$low, datos$ui), alternative="t", correct=TRUE)
## 
##  2-sample test for equality of proportions with continuity correction
## 
## data:  table(datos$low, datos$ui)
## X-squared = 4.4227, df = 1, p-value = 0.03546
## alternative hypothesis: two.sided
## 95 percent confidence interval:
##  -0.003651494  0.262843150
## sample estimates:
##    prop 1    prop 2 
## 0.8923077 0.7627119