Para estos ejercicios se usaron funciones para permitir hacer pruebas de ajuste estadístico que permitieran determinar si uno datos determinados se ajustaban a un tipo específico de distribución estadística.
Primero se generaron los vectores que contendrían los datos de diferentes distribuciones estadísticas:
set.seed(1073173761)
expo<-round(rexp(100,0.0625),2) # Distribucion exponencial
normal<-round(rnorm(100,179.1,28.2),2) # Distribucion normal
poisson<-round(rpois(100,43.2),2) # Distribucion Poisson
binomial<-round(rbinom(100,20,0.5),2) # Distribucion binomial
chi<-round(rchisq(100,2),2) # Distribucion chi cuadrado
lognormal<-round(rlnorm(100,2.32,0.2),2) # Distribucion lognormal
beta<-round(rbeta(100,2,2),2) # Distribucion beta
gamma<-round(rgamma(100,6,2),2) # Distribucion gamma
Para correr algunas de las funciones usadas para las pruebas de ajuste, se cargo la librería fitdistrplus.
library(fitdistrplus)
## Warning: package 'fitdistrplus' was built under R version 3.6.3
## Loading required package: MASS
## Warning: package 'MASS' was built under R version 3.6.3
## Loading required package: survival
## Warning: package 'survival' was built under R version 3.6.3
1) Prueba de ajuste para la distribución exponecial:
fit1<-fitdist(expo, "exp")
ks.test(expo,"pexp",fit1$estimate)
## Warning in ks.test(expo, "pexp", fit1$estimate): ties should not be present
## for the Kolmogorov-Smirnov test
##
## One-sample Kolmogorov-Smirnov test
##
## data: expo
## D = 0.078546, p-value = 0.568
## alternative hypothesis: two-sided
El p-valor muestra que no se rechaza la hipótesis nula, que en este caso es que los datos tienen una distribución exponencial.
2) Prueba de ajuste para la distribución normal:
shapiro.test(normal)
##
## Shapiro-Wilk normality test
##
## data: normal
## W = 0.9906, p-value = 0.7132
El p-valor muestra que no se rechaza la hipótesis nula, que en este caso es que los datos tienen una distribución normal.
3) Prueba de ajuste para la distribución Poisson:
fit1<-fitdist(poisson,"pois",method = "mle")
summary(fit1)
## Fitting of the distribution ' pois ' by maximum likelihood
## Parameters :
## estimate Std. Error
## lambda 44.21 0.664906
## Loglikelihood: -326.3517 AIC: 654.7034 BIC: 657.3086
estfit1<-gofstat(fit1)
estfit1$chisqpvalue
## [1] 0.2499646
El p-valor muestra que no se rechaza la hipótesis nula, que en este caso es que los datos tienen una distribución Poisson.
4) Prueba de ajuste para la distribución binomial:
fitBinom<-fitdist(data=binomial,
dist="binom", fix.arg=list(size=20), start=list(prob=0.5))
summary(fitBinom)
## Fitting of the distribution ' binom ' by maximum likelihood
## Parameters :
## estimate Std. Error
## prob 0.5005 0.01118029
## Fixed parameters:
## value
## size 20
## Loglikelihood: -226.4045 AIC: 454.8091 BIC: 457.4143
estfit2<-gofstat(fitBinom)
estfit2$chisqpvalue
## [1] 0.3051297
El p-valor muestra que no se rechaza la hipótesis nula, que en este caso es que los datos tienen una distribución binomial.
5) Prueba de ajuste para la distribución chi cuadrado:
ks.test(chi,pchisq,df=2)
## Warning in ks.test(chi, pchisq, df = 2): ties should not be present for the
## Kolmogorov-Smirnov test
##
## One-sample Kolmogorov-Smirnov test
##
## data: chi
## D = 0.053951, p-value = 0.933
## alternative hypothesis: two-sided
El p-valor muestra que no se rechaza la hipótesis nula, que en este caso es que los datos tienen una distribución chi cuadrado.
6) Prueba de ajuste para la distribución beta:
fit4<-fitdist(beta, "beta");fit4
## Fitting of the distribution ' beta ' by maximum likelihood
## Parameters:
## estimate Std. Error
## shape1 2.046996 0.2749395
## shape2 2.206214 0.2988415
ks.test(beta,"pbeta",2.28,2.24)
## Warning in ks.test(beta, "pbeta", 2.28, 2.24): ties should not be present
## for the Kolmogorov-Smirnov test
##
## One-sample Kolmogorov-Smirnov test
##
## data: beta
## D = 0.084885, p-value = 0.4671
## alternative hypothesis: two-sided
El p-valor muestra que no se rechaza la hipótesis nula, que en este caso es que los datos tienen una distribución beta.
7) Prueba de ajuste para la distribución gamma:
fit5<-fitdist(gamma, "gamma");fit5
## Fitting of the distribution ' gamma ' by maximum likelihood
## Parameters:
## estimate Std. Error
## shape 8.672165 1.2035745
## rate 2.929333 0.4185426
ks.test(gamma,"pgamma",7.09,2.36)
## Warning in ks.test(gamma, "pgamma", 7.09, 2.36): ties should not be present
## for the Kolmogorov-Smirnov test
##
## One-sample Kolmogorov-Smirnov test
##
## data: gamma
## D = 0.050508, p-value = 0.9606
## alternative hypothesis: two-sided
El p-valor muestra que no se rechaza la hipótesis nula, que en este caso es que los datos tienen una distribución gamma.