Homework-9
Madan Neupane
2022-10-27
Answer no. example(3.25)
H 0 :W =0 v s H 0 : W ≠ 0
#install.packages("epitools")
library(epitools)
gender <- matrix(c(20,80,50,400),
nrow = 2, ncol = 2, byrow = TRUE,
dimnames = list(c("Male", "female"),
c("Success", "Failure")))
gender
## Success Failure
## Male 20 80
## female 50 400
oddsratio(gender)
## $data
## Success Failure Total
## Male 20 80 100
## female 50 400 450
## Total 70 480 550
##
## $measure
## NA
## odds ratio with 95% C.I. estimate lower upper
## Male 1.000000 NA NA
## female 2.004587 1.109171 3.514536
##
## $p.value
## NA
## two-sided midp.exact fisher.exact chi.square
## Male NA NA NA
## female 0.02207671 0.0202162 0.01584399
##
## $correction
## [1] FALSE
##
## attr(,"method")
## [1] "median-unbiased estimate & mid-p exact CI"
At α =0.05
Since pvalue=0.0158 < α =0.05 . Then reject null hypothesis.
Therefore there is enough evidence to conclude that the odds ratio test of male and female
is statistically significant.
Answer no. (3.67)
H 0 : P11k =P1 .k∗P.1 k v s H 0 : P11k <¿ P1 . k∗P.1k where K =1,2,3,4
H 0=O d d s f o r r R e gu l a r f u e l i s s a m e a s t h e o d d s f o r a n y o f t y p e s o f f u e l vs
H a =Od d s f o r r R e g ul a r f u e l i s n o t s a me a s t h e o d d s f o r a n y o f t y p e s o f f u e l
#odds ratio test using Mantel and Haenszel
gas<-
array(c(209,384,363,157,116,321,389,229,121,329,284,173,336,141,388,184),
dim=c(2,2,4), dimnames = list(Timesperweek =c(">1","<=1"),
Vehicles=c("Car","Truck"), Fuel=c("Regular","Unleashed","Super
Unleashed","Diesel")))
gas
## , , Fuel = Regular
##
## Vehicles
## Timesperweek Car Truck
## >1 209 363
## <=1 384 157
##
## , , Fuel = Unleashed
##
## Vehicles
## Timesperweek Car Truck
## >1 116 389
## <=1 321 229
##
## , , Fuel = Super Unleashed
##
## Vehicles
## Timesperweek Car Truck
## >1 121 284
## <=1 329 173
##
## , , Fuel = Diesel
##
## Vehicles
## Timesperweek Car Truck
## >1 336 388
## <=1 141 184
mantelhaen.test(gas)
##
## Mantel-Haenszel chi-squared test with continuity correction
##
## data: gas
## Mantel-Haenszel X-squared = 279.33, df = 1, p-value < 2.2e-16
## alternative hypothesis: true common odds ratio is not equal to 1
## 95 percent confidence interval:
## 0.3013742 0.3897974
## sample estimates:
## common odds ratio
## 0.3427461
At α =0.05
Since X-squared(TS)=279.33 < X-squared(table)=3.841459. Then reject null hypothesis.
Therefore there is enough evidence to conclude that the common oddsratio for rRegular
fuel is higher.
Answer no .(3.75)
H 0 : Pm a l e =Pf em a l e v s H 0 : Pm a l e < P f e ma l e
Attendence <- matrix(c(3,2,10,15),
nrow = 2, ncol = 2, byrow = TRUE,
dimnames = list(c("Male", "female"),
c("Less than 5 Absenses", "More than 5 Absenses")))
Attendence
## Less than 5 Absenses More than 5 Absenses
## Male 3 2
## female 10 15
chisq.test(Attendence, simulate.p.value = FALSE,correct = FALSE)
## Warning in chisq.test(Attendence, simulate.p.value = FALSE, correct =
FALSE):
## Chi-squared approximation may be incorrect
##
## Pearson's Chi-squared test
##
## data: Attendence
## X-squared = 0.67873, df = 1, p-value = 0.41
qchisq(1-0.05,1)
## [1] 3.841459
At α =0.05
Since X-squared(TS)=0.67873 < X-squared(table)=3.841459. Then fails to reject null
hypothesis.
Therefore there is not enough evidence to conclude that the proportion of females failing
their course due to at least 5 absences is higher than males.
Answer no. (3.82)
H 0 : Pi j=Pi .∗P. j v s H 0 : Pi j ≠ P i.∗P. j ( F o r a t l e a s t o n e ) where i,j =1,2, i≠ j
shop <- matrix(c(90,40,40,50),
nrow = 2, ncol = 2, byrow = TRUE,
dimnames = list(c("Men", "Women"),
c("Work Out", "Don't Work Out")))
shop
## Work Out Don't Work Out
## Men 90 40
## Women 40 50
chisq.test(shop)
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: shop
## X-squared = 12.51, df = 1, p-value = 0.0004048
qchisq(1-0.05,1)
## [1] 3.841459
At α =0.05
Since X-squared(TS)=12.51 > X-squared(table)=3.841. Then reject null hypothesis.
Therefore there is enough evidence to conclude that the gender is unrelated to working out
against the alternative that gender and working out are dependent.
Answer no. example(4.4)
Test goodness of fit for Poisson distribution
H 0=d at a a r e f r o ma r a n d o m v a r ia b l e s P o i s s o n d i s t r ib u t i o n
H a =d at a a r e n o t f r o m a r a n d o m v a r i a b l e s P oi s s o n d i s t r ib u t i o n
data<- data.frame(c(0:12),c(24,16,16,18,15,9,6,5,3,4,3,0,1))
names(data) <- c('borers','frequency')
n <- sum(data$frequency) #120
mean_pois <- sum(data$borers*data$frequency)/n #3.166667
prob_pois <- dpois(0:12,lambda = mean_pois) ##Poisson Expected proportion
expected_frequency <- round(prob_pois*n,3) ##e_i
data$e_i <- c(round(prob_pois*n,3))
data
## borers frequency e_i
## 1 0 24 5.057
## 2 1 16 16.015
## 3 2 16 25.357
## 4 3 18 26.765
## 5 4 15 21.189
## 6 5 9 13.420
## 7 6 6 7.083
## 8 7 5 3.204
## 9 8 3 1.268
## 10 9 4 0.446
## 11 10 3 0.141
## 12 11 0 0.041
## 13 12 1 0.011
##Test Statistics
Chisquare <- sum((data$frequency-data$e_i)^2/data$e_i)
df <- nrow(data)
qchisq(0.95,df-1)
## [1] 21.02607
OR,
data1<- data.frame(c(0:8),c(24,16,16,18,15,9,6,5,11))
names(data1) <- c('borers','frequency')
n1 <- sum(data$frequency) #120
mean_pois1 <- sum(data1$borers*data1$frequency)/n1 #3.166667
prob_pois1 <- dpois(0:8,lambda = mean_pois1) ##Poisson Expected proportion
expected_frequency1 <- round(prob_pois1*n1,3) ##e_i
data1$e_i1 <- c(round(prob_pois1*n1,3))
data1
## borers frequency e_i1
## 1 0 24 5.683
## 2 1 16 17.333
## 3 2 16 26.433
## 4 3 18 26.874
## 5 4 15 20.491
## 6 5 9 12.500
## 7 6 6 6.354
## 8 7 5 2.769
## 9 8 11 1.056
##Test Statistics
Chisquare1 <- sum((data1$frequency-data1$e_i1)^2/data1$e_i)
Chisquare1
## [1] 164.0966
df <- nrow(data1)
qchisq(0.95,df-1)
## [1] 15.50731
At α =0.05
Since X-squared(TS)=259.33 or 164.0966 > X-squared(table)=3.841. Then reject null
hypothesis.
Therefore there is not enough evidence to conclude that the data are from a random
variable Poisson distribution.