getwd()
## [1] "C:/Users/Caleb/Documents/Civil Engineering degree coursework/Applied Statistical Methods/Labs/Lab 11"
The confidence interval for \(\mu\) is given by
\[\mu= \bar y \pm t_{\frac{\alpha}{2}}\frac{s}{\sqrt{n}}\]
d=c(5.0581, 4.9707, 5.0893, 4.9334, 4.9777, 5.0285, 4.8555, 4.9565, 4.9769, 4.9722, 4.999, 4.9925, 4.9686, 5.0662, 4.9239,4.9781, 5.0485, 5.0014, 4.9957, 5.0195, 5.0118, 4.9928, 5.0361, 5.0185, 4.9879)
n=length(d)
df=n-1
s=sd(d)
ybar=mean(d)
# 95% ci
alpha=0.05
t_quant=qt(1-alpha/2,df)
L=ybar-(t_quant*s)/(sqrt(n))
L
## [1] 4.974137
U=ybar+(t_quant*s)/(sqrt(n))
U
## [1] 5.014607
# 90% ci
alpha=0.1
t_quant=qt(1-alpha/2,df)
L=ybar-(t_quant*s)/(sqrt(n))
L
## [1] 4.977598
U=ybar+(t_quant*s)/(sqrt(n))
U
## [1] 5.011146
# 80% ci
alpha=0.2
t_quant=qt(1-alpha/2,df)
L=ybar-(t_quant*s)/(sqrt(n))
L
## [1] 4.981452
U=ybar+(t_quant*s)/(sqrt(n))
U
## [1] 5.007292
# 50% ci
alpha=0.5
t_quant=qt(1-alpha/2,df)
L=ybar-(t_quant*s)/(sqrt(n))
L
## [1] 4.987658
U=ybar+(t_quant*s)/(sqrt(n))
U
## [1] 5.001086
test=t.test(d,conf.level=0.8)
test$conf.int
## [1] 4.981452 5.007292
## attr(,"conf.level")
## [1] 0.8
Confidence interval for \(\sigma^2\) is
\[\sigma^2=[\frac{(n-1)s^2}{\chi_{\frac{\alpha}{2}}^2},\frac{(n-1)s^2}{\chi_{1-\frac{\alpha}{2}}^2}]\]
# 95% ci
alpha=0.05
upperchi_quant=qchisq(1-alpha/2,n-1)
lowerchi_quant=qchisq(alpha/2,n-1)
L=((n-1)*s^2)/(upperchi_quant)
L
## [1] 0.001465121
U=((n-1)*s^2)/(lowerchi_quant)
U
## [1] 0.004650629
# 90% ci
alpha=0.1
upperchi_quant=qchisq(1-alpha/2,n-1)
lowerchi_quant=qchisq(alpha/2,n-1)
L=((n-1)*s^2)/(upperchi_quant)
L
## [1] 0.001583773
U=((n-1)*s^2)/(lowerchi_quant)
U
## [1] 0.0041646
# 80% ci
alpha=0.2
upperchi_quant=qchisq(1-alpha/2,n-1)
lowerchi_quant=qchisq(alpha/2,n-1)
L=((n-1)*s^2)/(upperchi_quant)
L
## [1] 0.00173734
U=((n-1)*s^2)/(lowerchi_quant)
U
## [1] 0.003683142
# 50% ci
alpha=0.5
upperchi_quant=qchisq(1-alpha/2,n-1)
lowerchi_quant=qchisq(alpha/2,n-1)
L=((n-1)*s^2)/(upperchi_quant)
L
## [1] 0.002042167
U=((n-1)*s^2)/(lowerchi_quant)
U
## [1] 0.003029489
For small samples \(n_1<30\) or \(n_2<30\) and \(\sigma_1=\sigma_2\) the confidence interval for \(\mu_1-\mu_2\) is
\[\mu_1-\mu_2\approx \bar y_1- \bar y_2 \pm t_{\frac{\alpha}{2}}\sqrt{S_p^2(\frac{1}{n_1}+\frac{1}{n_2})}\]
Where,
\[S_p^2=\frac{(n_1-1)s_1^2+(n_2-1)s_2^2}{n_1+n_2-2}\]
and the degrees of freedom for \(t_{\frac{\alpha}{2}}\) are given by \(n_1+n_2-2\)
blue=c(21.65, 17.48, 20.1, 21.57, 14.82, 19.17, 21.08, 18.23, 22.93, 15.66, 20.89, 21.66, 18.5, 20.59, 18.63, 18.91, 19.53, 17.7, 16.5, 19.03)
snapper=c(31.65, 27.48, 30.1, 31.57, 24.82, 29.17, 31.08, 28.23, 32.93, 25.66, 30.89, 31.66, 28.5, 30.59, 28.63)
n1=length(blue)
n2=length(snapper)
df=n1+n2-2
ybar1=mean(blue)
ybar2=mean(snapper)
s1=sd(blue)
s2=sd(snapper)
Spsq=((n1-1)*s1^2+(n2-1)*s2^2)/df
# 95% ci \mu_blue-\mu_snapper
alpha=0.05
t_quant=qt(1-alpha/2,df)
L=ybar1-ybar2-t_quant*sqrt(Spsq*((1/n1)+(1/n2)))
L
## [1] -11.84075
U=ybar1-ybar2+t_quant*sqrt(Spsq*((1/n1)+(1/n2)))
U
## [1] -8.757585
# 95% ci \mu_snapper-\mu_blue
alpha=0.05
t_quant=qt(1-alpha/2,df)
L=ybar2-ybar1-t_quant*sqrt(Spsq*((1/n1)+(1/n2)))
L
## [1] 8.757585
U=ybar2-ybar1+t_quant*sqrt(Spsq*((1/n1)+(1/n2)))
U
## [1] 11.84075
The above interval means that with 95% confidence the Snappers had a mean length that is between 8.76 inches and 11.84 inches longer than the mean length of the Blue cod fish.
# 95% ci \mu_snapper-\mu_blue
t.test(snapper,blue,var.equal=TRUE,conf.level=0.95)$conf.int
## [1] 8.757585 11.840749
## attr(,"conf.level")
## [1] 0.95
# 85% ci \mu_snapper-\mu_blue
t.test(snapper,blue,var.equal=TRUE,conf.level=0.85)$conf.int
## [1] 9.182414 11.415919
## attr(,"conf.level")
## [1] 0.85
# 75% ci \mu_snapper-\mu_blue
t.test(snapper,blue,var.equal=TRUE,conf.level=0.75)$conf.int
## [1] 9.411911 11.186422
## attr(,"conf.level")
## [1] 0.75
# 25% ci \mu_snapper-\mu_blue
t.test(snapper,blue,var.equal=TRUE,conf.level=0.25)$conf.int
## [1] 10.05570 10.54263
## attr(,"conf.level")
## [1] 0.25
As the confidence level decreases the interval gets smaller.
Exam1=c(40.98, 59.36, 46.69, 41.8, 61.63, 65.31, 62.96, 60.21, 56.89, 78.41, 53.44, 75.2, 60.54, 52.43, 41.41, 70.79, 73.55, 55.65, 61.43, 63.84, 58.07, 53.79, 54.45, 67.18, 44.46)
Exam2=c(50.22, 66.19, 58.75, 51.88, 66.61, 70.86, 74.25, 70.23, 69.55, 87.18, 63.62, 81.7, 70.5, 66.02, 51.35, 80.92, 85.65, 65.44, 74.37, 75.28, 67.86, 59.92, 64.42, 73.57, 57.15)
exdif=Exam1-Exam2
t.test(exdif)$conf.int
## [1] -10.731329 -8.710271
## attr(,"conf.level")
## [1] 0.95
With 95% confidence the mean grade of Exam 1 is between 8.71 and 10.73 points below the mean grade of Exam 2.
# 90% ci
t.test(exdif,conf.level=0.9)$conf.int
## [1] -10.558485 -8.883115
## attr(,"conf.level")
## [1] 0.9
# 80% ci
t.test(exdif,conf.level=0.8)$conf.int
## [1] -10.366041 -9.075559
## attr(,"conf.level")
## [1] 0.8
# 70% ci
t.test(exdif,conf.level=0.7)$conf.int
## [1] -10.239465 -9.202135
## attr(,"conf.level")
## [1] 0.7
# 60% ci
t.test(exdif,conf.level=0.6)$conf.int
## [1] -10.140335 -9.301265
## attr(,"conf.level")
## [1] 0.6
# 10% ci
t.test(exdif,conf.level=0.1)$conf.int
## [1] -9.782981 -9.658619
## attr(,"conf.level")
## [1] 0.1
For \(n_1 \geq 30\) and \(n_2 \geq 30\) a \((1-\alpha)*100%\) confidence interval for \(p_1-p_2\) is given by
\[p_1-p_2 \approx \hat p_1 -\hat p_2 \pm z_{\frac{\alpha}{2}}\sqrt{\frac{\hat p_1 \hat q_1}{n_1}+\frac{\hat p_2 \hat q_2}{n_2}}\]
birds <- read.csv("NZBIRDS.csv")
with(birds, addmargins(table(Extinct,Flight)))
## Flight
## Extinct No Yes Sum
## No 7 71 78
## Yes 21 17 38
## Sum 28 88 116
# population 1 is extinct birds
# population 2 is non-extinct birds
# finding difference in their proportions of flightless birds
n1=38
n2=78
phat1=21/n1
qhat1=1-phat1
phat2=7/n2
qhat2=1-phat1
alpha=0.05
z_quant=qnorm(1-alpha/2,0,1)
L=phat1-phat2-z_quant*sqrt((phat1*qhat1)/(n1) + (phat2*qhat2)/(n2))
L
## [1] 0.2986625
U=phat1-phat2+z_quant*sqrt((phat1*qhat1)/(n1) + (phat2*qhat2)/(n2))
U
## [1] 0.6271135
A \((1-\alpha)*100%\) confidence interval for \(\frac{\sigma_1^2}{\sigma_2^2}\) is given by
\[\frac{s_1^2}{s_2^2} \frac{1}{F_{\frac{\alpha}{2}(v_1,v_2)}} \leq \frac{\sigma_1^2}{\sigma_2^2} \leq \frac{s_1^2}{s_2^2} F_{\frac{\alpha}{2}(v_2,v_1)}\]
Where \(v_1=n_1-1\) and \(v_2=n_2-1\).
set.seed(35);sam1=rnorm(25,mean=10,sd=5); set.seed(45);sam2=rnorm(34,mean=40,sd=8)
n1=length(sam1)
n2=length(sam2)
v1=n1-1
v2=n2-1
s1=sd(sam1)
s2=sd(sam2)
upperf_quant=qf(1-alpha/2,v2,v1)
lowerf_quant=qf(alpha/2,v2,v1)
L=(lowerf_quant*s1^2)/s2^2
L
## [1] 0.2257732
U=(upperf_quant*s1^2)/s2^2
U
## [1] 1.030944
var.test(sam1,sam2,conf.level=0.8)$conf.int
## [1] 0.2920457 0.7831464
## attr(,"conf.level")
## [1] 0.8
var.test(sam1,sam2,conf.level=0.7)$conf.int
## [1] 0.3204384 0.7105597
## attr(,"conf.level")
## [1] 0.7
var.test(sam1,sam2,conf.level=0.6)$conf.int
## [1] 0.3449688 0.6581343
## attr(,"conf.level")
## [1] 0.6
var.test(sam1,sam2,conf.level=0.5)$conf.int
## [1] 0.3675269 0.6165110
## attr(,"conf.level")
## [1] 0.5
set.seed(23);x = rnorm(30,mean=10,sd=12)
MATH4753GRAY::myci(x,alpha=0.05)
## [1] 8.124741
## [1] 15.64894