Nikol Benavides (Código:2032111009), Juan Rodríguez (Código:2032111013), Daniel Montero (Código:2032111022), Miguel Acosta (Código:2032111018), Paulina Montaño (Código:2032111001), Valentina Jiménez (Código:2032111064), Luis Sandoval (Código:2032111005)
Asignatura: Estadística
Programa Ingeniería Civil
library(reticulate)import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from pydataset import datadata()## dataset_id title
## 0 AirPassengers Monthly Airline Passenger Numbers 1949-1960
## 1 BJsales Sales Data with Leading Indicator
## 2 BOD Biochemical Oxygen Demand
## 3 Formaldehyde Determination of Formaldehyde
## 4 HairEyeColor Hair and Eye Color of Statistics Students
## .. ... ...
## 752 VerbAgg Verbal Aggression item responses
## 753 cake Breakage Angle of Chocolate Cakes
## 754 cbpp Contagious bovine pleuropneumonia
## 755 grouseticks Data on red grouse ticks from Elston et al. 2001
## 756 sleepstudy Reaction times in a sleep deprivation study
##
## [757 rows x 2 columns]
women = data("women")trees = data('trees')Veremos a continuación si las variables “weight” y “height” del dataset “women”(dataset que cuenta con los datos de promedios de alturas y pesos para mujeres americanas) están relacionadas, usando la función crosstab de Pandas.
import pandas as pd
# Tabla de contingencia Weight / Height
pd.crosstab(index=women['height'],
columns=women['weight'], margins=True)
# Esta es la parte descriptiva no se puede afirmar nada## weight 115 117 120 123 126 129 132 ... 142 146 150 154 159 164 All
## height ...
## 58 1 0 0 0 0 0 0 ... 0 0 0 0 0 0 1
## 59 0 1 0 0 0 0 0 ... 0 0 0 0 0 0 1
## 60 0 0 1 0 0 0 0 ... 0 0 0 0 0 0 1
## 61 0 0 0 1 0 0 0 ... 0 0 0 0 0 0 1
## 62 0 0 0 0 1 0 0 ... 0 0 0 0 0 0 1
## 63 0 0 0 0 0 1 0 ... 0 0 0 0 0 0 1
## 64 0 0 0 0 0 0 1 ... 0 0 0 0 0 0 1
## 65 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 1
## 66 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 1
## 67 0 0 0 0 0 0 0 ... 1 0 0 0 0 0 1
## 68 0 0 0 0 0 0 0 ... 0 1 0 0 0 0 1
## 69 0 0 0 0 0 0 0 ... 0 0 1 0 0 0 1
## 70 0 0 0 0 0 0 0 ... 0 0 0 1 0 0 1
## 71 0 0 0 0 0 0 0 ... 0 0 0 0 1 0 1
## 72 0 0 0 0 0 0 0 ... 0 0 0 0 0 1 1
## All 1 1 1 1 1 1 1 ... 1 1 1 1 1 1 15
##
## [16 rows x 16 columns]
# Tabla de contingencia en porcentajes relativos total
pd.crosstab(index=women['height'], columns=women['weight'],
margins=True).apply(lambda r: r/len(women) *100,
axis=1)## weight 115 117 120 ... 159 164 All
## height ...
## 58 6.666667 0.000000 0.000000 ... 0.000000 0.000000 6.666667
## 59 0.000000 6.666667 0.000000 ... 0.000000 0.000000 6.666667
## 60 0.000000 0.000000 6.666667 ... 0.000000 0.000000 6.666667
## 61 0.000000 0.000000 0.000000 ... 0.000000 0.000000 6.666667
## 62 0.000000 0.000000 0.000000 ... 0.000000 0.000000 6.666667
## 63 0.000000 0.000000 0.000000 ... 0.000000 0.000000 6.666667
## 64 0.000000 0.000000 0.000000 ... 0.000000 0.000000 6.666667
## 65 0.000000 0.000000 0.000000 ... 0.000000 0.000000 6.666667
## 66 0.000000 0.000000 0.000000 ... 0.000000 0.000000 6.666667
## 67 0.000000 0.000000 0.000000 ... 0.000000 0.000000 6.666667
## 68 0.000000 0.000000 0.000000 ... 0.000000 0.000000 6.666667
## 69 0.000000 0.000000 0.000000 ... 0.000000 0.000000 6.666667
## 70 0.000000 0.000000 0.000000 ... 0.000000 0.000000 6.666667
## 71 0.000000 0.000000 0.000000 ... 6.666667 0.000000 6.666667
## 72 0.000000 0.000000 0.000000 ... 0.000000 6.666667 6.666667
## All 6.666667 6.666667 6.666667 ... 6.666667 6.666667 100.000000
##
## [16 rows x 16 columns]
Plantemos dos hipótesis de la Chi cuadrado Ho: El peso de las mujeres SÍ está asociado con la altura Ha: El peso de las mujeres NO está asociado con la altura
Criterio: Si el valor de P del test de Chi cuadrado es menor que 0.05, entonces se rechaza la hipótesis nula.
from scipy.stats import chi2_contingency
# Tabla de contingencia Weight / Height
data = pd.crosstab(index=women['height'],
columns=women['weight'], margins=True)
chi2_contingency(data)## Chi2ContingencyResult(statistic=210.0, pvalue=0.755484036173565, dof=225, expected_freq=array([[ 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 1. ],
## [ 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 1. ],
## [ 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 1. ],
## [ 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 1. ],
## [ 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 1. ],
## [ 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 1. ],
## [ 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 1. ],
## [ 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 1. ],
## [ 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 1. ],
## [ 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 1. ],
## [ 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 1. ],
## [ 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 1. ],
## [ 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 1. ],
## [ 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 1. ],
## [ 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667,
## 1. ],
## [ 1. , 1. , 1. , 1. , 1. ,
## 1. , 1. , 1. , 1. , 1. ,
## 1. , 1. , 1. , 1. , 1. ,
## 15. ]]))
Decisión: como el valor de p = 0.75548403617356 es mayor que 0.05, entonces NO se rechaza la Ho: El peso de las mujeres SÍ está asociado con la altura, conclusión: “La altura de las mujeres sí está relacionada con el peso”
Se reliza un análisis de correlación de 2 variables numéricas del dataset “trees”: “girth” y “height”. Este dataset incluye datos de la circunferencia, altura y volumen de árboles de bayas negras
girth <- c(8.3, 8.6, 8.8, 10.5, 10.7, 10.8, 11.0, 11.0, 11.1, 11.2, 11.3, 11.4, 11.4, 11.7, 12.0, 12.9, 12.9, 13.3, 13.7, 13.8, 14.0, 14.2, 14.5, 16.0, 16.3, 17.3, 17.5, 17.9, 18.0, 18.0, 20.6)
height <- c(70, 65, 63, 72, 81, 83, 66, 75, 80, 75, 79, 76, 76, 69, 75, 74, 85, 86, 71, 64, 78, 80, 74, 72, 77, 81, 82, 80, 80, 80, 87)
# Ho: girth y height SON NORMALES
# Ha: girth y height NO SON NORMALES
shapiro.test(girth)##
## Shapiro-Wilk normality test
##
## data: girth
## W = 0.94117, p-value = 0.08893
shapiro.test(height)##
## Shapiro-Wilk normality test
##
## data: height
## W = 0.96545, p-value = 0.4034
Como el valor de p de ambas variables fueron 0.08893 y 0.4034, respectivamente, mayores que 0.05, entonces NO se rechaza la hipótesis nula, conclusión: las variables SON NORMALES.
Entonces, procedemos a realizar el test de correlación de Pearson
# Como son normales realizamos el test de Pearson
# Ho: NO están correlacionadas linealmente
# Ha: SÍ están correlacionadas linealmente
cor.test(girth, height)##
## Pearson's product-moment correlation
##
## data: girth and height
## t = 3.2722, df = 29, p-value = 0.002758
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.2021327 0.7378538
## sample estimates:
## cor
## 0.5192801
Como se obtuvo un valor de p de 0.002758, el cual es menor que 0.05, entonces se rechaza la hipótesis nula Ho, lo que significa que las variables sí están correlacionadas linealmente