Below are the solutions to these exercises on statistical tests.

#################### # # # Exercise 1 # # # #################### sample.norm<-function(size) { data<-rnorm(size,mean=5+8/12,sd=2.94/12) return(sum(data>=5+8/12&data<=5+9/12)/size) } set.seed(42) sample.norm(200)

## [1] 0.135

#################### # # # Exercise 2 # # # #################### prob<-pnorm(5+9/12,mean=5+8/12,sd=2.94/12)-pnorm(5+8/12,mean=5+8/12,sd=2.94/12) print(prob)

## [1] 0.133123

sample.diff<-function(size1,size2) { results<-NULL for(i in size1:size2) { results<-c( results,((sample.norm(i)-prob)/prob)*100) } return(results) } set.seed(42) diff.result<-sample.diff(1,10000) str(diff.result)

## num [1:10000] -100 -100 -100 -100 -100 ...

plot(diff.result,type='l',lwd=3)

#################### # # # Exercise 3 # # # #################### #1 pnorm(0,mean=0,sd=1)

## [1] 0.5

x.norm <- seq(-4, 4, 0.01) plot(x.norm,dnorm(x.norm, mean=0, sd=1)) abline(v=0,col="red") #2 1-pnorm(0,mean=0,sd=1)

## [1] 0.5

#or pnorm(0,mean=0,sd=1,lower.tail = FALSE)

## [1] 0.5

#3 pnorm(-0.25,mean=0,sd=1)

## [1] 0.4012937

abline(v=-0.25,col="green")

#4 pnorm(0,mean=0,sd=1)-pnorm(-0.25,mean=0,sd=1)

## [1] 0.09870633

#################### # # # Exercise 4 # # # #################### qnorm(0.99,mean=0,sd=1)

## [1] 2.326348

qnorm(0.975,mean=0,sd=1)

## [1] 1.959964

qnorm(0.95,mean=0,sd=1)

## [1] 1.644854

qnorm(0.01,mean=0,sd=1)

## [1] -2.326348

qnorm(0.025,mean=0,sd=1)

## [1] -1.959964

qnorm(0.05,mean=0,sd=1)

## [1] -1.644854

#################### # # # Exercise 5 # # # #################### #1 data.ex.5<-read.csv( "https://www.r-exercises.com/wp-content/uploads/2017/08/data.ex_.5.csv") str(data.ex.5)

## 'data.frame': 150 obs. of 1 variable: ## $ x: num 13.889 25.002 2.682 20.755 0.123 ...

library(boot) prob.boot<-function(data,indices) { return(sum(data[indices,1]>5&data[indices,1]<10)/length(data[indices,1])) } set.seed(42) prob.5.10<-boot(data.ex.5,prob.boot,R=10000) print(prob.5.10)

## ## ORDINARY NONPARAMETRIC BOOTSTRAP ## ## ## Call: ## boot(data = data.ex.5, statistic = prob.boot, R = 10000) ## ## ## Bootstrap Statistics : ## original bias std. error ## t1* 0.1733333 -0.0001986667 0.03074905

quantile(prob.5.10$t,c(0.025,0.975))

## 2.5% 97.5% ## 0.1133333 0.2333333

#2 ((quantile(prob.5.10$t,0.975)-quantile(prob.5.10$t,0.025,0.975))/2)*100

## 97.5% ## 6

#################### # # # Exercise 6 # # # #################### #1 data.ex.6<-read.csv("https://www.r-exercises.com/wp-content/uploads/2017/08/data.ex_.6.csv") str(data.ex.6)

## 'data.frame': 200 obs. of 1 variable: ## $ x: num 373 357 367 363 346 ...

mean(data.ex.6$x)

## [1] 361.4657

#2 mean.boot<-function(data,indices) { return(mean(data[indices,])) } set.seed(42) mean.ex.6<-boot(data.ex.6,mean.boot,R=10000) print(mean.ex.6)

## ## ORDINARY NONPARAMETRIC BOOTSTRAP ## ## ## Call: ## boot(data = data.ex.6, statistic = mean.boot, R = 10000) ## ## ## Bootstrap Statistics : ## original bias std. error ## t1* 361.4657 0.003623713 0.8038724

(sum(mean.ex.6$t<=360)/length(mean.ex.6$t))*100

## [1] 3.39

#Since this probability is smaller than 5%, we reject the null hypothesis and conclude that the mean is bigger than 360. At a level of 1% we cannot reject the null hypothesis. #################### # # # Exercise 7 # # # #################### hist(mean.ex.6$t) abline(v=360,col="red") abline(v=quantile(mean.ex.6$t,0.05),col="green")

#################### # # # Exercise 8 # # # #################### #1 data.ex.8<-data.ex.6-mean(data.ex.6$x)+363 #2 set.seed(42) mean.ex.8<-boot(data.ex.8,mean.boot,R=10000) #3 endpoint.1<-mean(data.ex.6$x) endpoint.2<-363-endpoint.1+363 #4 (sum((mean.ex.8$t<=endpoint.1)|(mean.ex.8$t>=endpoint.2))/length(mean.ex.8$t))*100

## [1] 5.56

#5 #Since 5.56% of the results are more far from 363 than our average, we cannot say that our average is statistically different from 363. We cannot reject the null hypothesis and conclude that the mean is equal to 363 at level 5%. #################### # # # Exercise 9 # # # #################### (sum(mean.ex.8$t<=endpoint.1)/length(mean.ex.8$t))*100

## [1] 2.81

#Since only 2.81% of the results are as extreme as our average, we can claim that such a result would be rare if the mean was of 363. As a consequence, we reject the null hypothesis and conclude that the mean is significantly smaller than 363. #################### # # # Exercise 10 # # # #################### data.ex.9<-data.ex.5-mean(data.ex.5$x)+13 set.seed(42) mean.ex.9<-boot(data.ex.9,mean.boot,R=10000) endpoint.1<-mean(data.ex.5$x) endpoint.2<-13-endpoint.1+13 (sum((mean.ex.9$t<=endpoint.1)|(mean.ex.9$t>=endpoint.2))/length(mean.ex.9$t))*100

## [1] 0.03

ricardo says

The exercise one stated the mean as 5 foot 9 inches. The solution sets the mean of the data 5+8/12