library(snpar)
library(DescTools)
library(boot)

#3.1

penguins.before<-rnorm(200, 50, 23)
penguins.after<-rnorm(250, 50, 23) -rpois(200,5)
## Warning in rnorm(250, 50, 23) - rpois(200, 5): longitud de objeto mayor no es
## múltiplo de la longitud de uno menor
runs.test(penguins.before)
## 
##  Approximate runs rest
## 
## data:  penguins.before
## Runs = 99, p-value = 0.7768
## alternative hypothesis: two.sided

Here we cannot reject the null hypothesis that the sample of penguins before is random because of the large p-value.

runs.test(penguins.after)
## 
##  Approximate runs rest
## 
## data:  penguins.after
## Runs = 135, p-value = 0.254
## alternative hypothesis: two.sided

Here we cannot reject the null hypothesis that the sample of penguins before is random because of the large p-value.

We continue with the Shaphiro-Wilk test, which will determine if this samples comes from a normal distribution, comparing the variance of the sample with a normalized variance.

shapiro.test(penguins.before)
## 
##  Shapiro-Wilk normality test
## 
## data:  penguins.before
## W = 0.9898, p-value = 0.167
shapiro.test(penguins.after)
## 
##  Shapiro-Wilk normality test
## 
## data:  penguins.after
## W = 0.99625, p-value = 0.8148

Both p-values are big so we know that the samples follow a normal distribution.

#3.2

Test if the administratio of the pill worked

t.test(penguins.after)
## 
##  One Sample t-test
## 
## data:  penguins.after
## t = 30.891, df = 249, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  40.57240 46.09824
## sample estimates:
## mean of x 
##  43.33532
penguins.after2 <- boot(data = penguins.after, statistic = function(penguins.after,i) median(penguins.after[i]),R = 1000)
boot.ci(penguins.after2)
## Warning in boot.ci(penguins.after2): bootstrap variances needed for studentized
## intervals
## BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
## Based on 1000 bootstrap replicates
## 
## CALL : 
## boot.ci(boot.out = penguins.after2)
## 
## Intervals : 
## Level      Normal              Basic         
## 95%   (41.49, 46.71 )   (41.47, 46.93 )  
## 
## Level     Percentile            BCa          
## 95%   (40.39, 45.86 )   (40.31, 45.72 )  
## Calculations and Intervals on Original Scale

We are applying a Poisson distribution with the rpois function, and we can see in this test that the administration of the pill did work.