Buku Azen and Walker "Categorical Data Analysis : No 4.1 - 4.6

4.1 Suppose that the fourth-grade students form a national random sample were classified as either proficient or not proficient in mathematics as well as whether each was a native English Speaker (NES). Possible results (loosely based on data from the National Center for Education Statistics) are shown in table_grade.

Input data

grade <- matrix(c(35, 5, 93, 67), nrow=2, byrow=TRUE)
colnames(grade) <- c("no", "yes")
rownames(grade) <- c("NES_NO", "NES_YES")
tabel_grade <- as.table(grade)
tabel_grade
##         no yes
## NES_NO  35   5
## NES_YES 93  67

a. Compute the odds of proficiency based in these data and interpret this value.

data_grade <- as.data.frame(grade)
data_grade
##         no yes
## NES_NO  35   5
## NES_YES 93  67
addmargins(tabel_grade)
##          no yes Sum
## NES_NO   35   5  40
## NES_YES  93  67 160
## Sum     128  72 200

menghitung peluang (odds of proficiency)

a.1 Peluang Proficient No

a <- 128/200
a
## [1] 0.64

a.2 Peluang Proficient Yes

b <- 72/200
b
## [1] 0.36

a.3 Peluang Proficient No Jika NES No

c <- 35/40
c
## [1] 0.875

a.4 Peluang Proficient No Jika NES Yes

d <- 93/160 d

a.5 Peluang Proficient Yes Jika NES No

e <- 5/40
e
## [1] 0.125

a.6 Peluang Proficient Yes Jika NES Yes

f <- 67/160
f
## [1] 0.41875

b. Compute the odds ratio for this table and interpret this value

prop.out <- prop.table(tabel_grade, margin=1)
prop.out
##              no     yes
## NES_NO  0.87500 0.12500
## NES_YES 0.58125 0.41875

b.1 relative risk of Nes-no vs Nes-yes

P(No|Nes_No)

prop.out[1,1]
## [1] 0.875

P(Yes|Nes_No)

prop.out[2,1]
## [1] 0.58125

Nilai Relative Risk (RR) : yaitu apabila diketahui NES adalah No, maka kecenderungan untuk dikatakan proficient No adalah 1,505376 kali dari kecenderungan NES-nya Yes.

prop.out[1,1]/prop.out[2,1]
## [1] 1.505376

b.2 mencari nilai odds ratio, terlebih dahulu dihitung odds masing-masing NES-nya

odds of nes-no : nilai odds ini berarti bahwa peluang NES No yang profienct-nya No adalah 7 kali dari peluang NES-nya Yes yang proficient-nya adalah Yes.

nes_no <- prop.out[1,1]/prop.out[1,2]
nes_no
## [1] 7
nes_yes <- prop.out[2,1]/prop.out[2,2]
nes_yes
## [1] 1.38806

odds of nes-yes : nilai odds ini berarti bahwa peluang NES Yes yang proficient-nya No adalah 1,38 kali dari peluang NES Yes yang proficient-nya Yes

nilai Odds Ratio (OR) juga dapat dihitung secara manual, berikut :

OR <- nes_no/nes_yes
OR 
## [1] 5.043011

Diperoleh nilai OR sebesar 5.043 artinya adalah odds NES_no 5.043 kali dari odds NES_Yes

4.2 Find the 95% confidence interval for the odds ratio in Problem 4.1 and interpret the result

Secara manual dapat dihitung dengan cara berikut : ## mencari simpangan baku bagi statistik logaritma nisbah odds

s = sqrt(1/35+1/5+1/93+1/67)
s
## [1] 0.5042316

dihitung SK 95% dari OR Proficientnya : ln(teta)+-Z(0.05/2)*s

teta = OR
teta
## [1] 5.043011
z = 1.96
low <- log(teta)-z*s
low
## [1] 0.6297094
exp(low)
## [1] 1.877065
up <- log(teta)+z*s
up 
## [1] 2.606297
exp(up)
## [1] 13.54879

Sehingga diperoleh selang kepercayaan 95% bagi odds ratio-nya adalah : 1.877065 < teta < 13.54879

Kemudian akan dihitung Selang Kepercayaan odds rationya, untuk mencari oddsratio install packages epitools

library(epitools)
## Warning: package 'epitools' was built under R version 3.6.3
or.out <- oddsratio(tabel_grade, method = "wald", conf = 0.95, correct = FALSE)
or.out
## $data
##          no yes Total
## NES_NO   35   5    40
## NES_YES  93  67   160
## Total   128  72   200
## 
## $measure
##                         NA
## odds ratio with 95% C.I. estimate    lower    upper
##                  NES_NO  1.000000       NA       NA
##                  NES_YES 5.043011 1.877099 13.54854
## 
## $p.value
##          NA
## two-sided   midp.exact fisher.exact   chi.square
##   NES_NO            NA           NA           NA
##   NES_YES 0.0003171897 0.0003963768 0.0005364223
## 
## $correction
## [1] FALSE
## 
## attr(,"method")
## [1] "Unconditional MLE & normal approximation (Wald) CI"

4.3 Is there an asociation between proficiency and NES status based on the data in Problem 4.1? State the null hypothesis and interpret the results of the hypothesis test

H0 : proficient dan NES saling bebas vs H1 : proficient dan NES tidak saling bebas

Langkah awal adalah menghitung nilai harapan sebagai berikut :

n_11=128*40/200
n_11
## [1] 25.6
n_12=72*40/200
n_12
## [1] 14.4
n_21=128*160/200
n_21
## [1] 102.4
n_22=72*160/200 
n_22
## [1] 57.6

Langkah berikutnya adalah statistik uji (dengan manual) :

a=(35-25.6)^2/25.6
a
## [1] 3.451562
b=(5-14.4)^2/14.4
b
## [1] 6.136111
c=(93-102.4)^2/102.4
c
## [1] 0.8628906
d=(67-57.6)^2/57.6
d
## [1] 1.534028
stat_uji <- a+b+c+d
stat_uji
## [1] 11.98459

Dimana Tolak H0 jika khi-kuadrat hitung > khi-kuadrat tabel 0.05;1 = 3.841. Keputusan : Tolak H0 artinya bahwa proficient dan NES tidak saling bebas (terdapat asosiasi/hubungan) pada taraf nyata 5%.

Uji Khi-Kuadrat dengan cara lain :

chisq.test(tabel_grade, correct = FALSE)
## 
##  Pearson's Chi-squared test
## 
## data:  tabel_grade
## X-squared = 11.985, df = 1, p-value = 0.0005364

4.6 Use the computer software and the data in table_colds to test whether there is an association between Vitamin C and the incidence of colds.

Input data

colds <- matrix(c(32, 16, 13, 27, 5, 7), nrow=3,byrow=TRUE)
rownames(colds) <- c("few", "some", "many")
colnames(colds) <- c("VitC_Yes", "VitC_No")
tabel_colds <- as.table(colds)
tabel_colds
##      VitC_Yes VitC_No
## few        32      16
## some       13      27
## many        5       7
addmargins(tabel_colds)
##      VitC_Yes VitC_No Sum
## few        32      16  48
## some       13      27  40
## many        5       7  12
## Sum        50      50 100

a. Report the results of the Pearson chi-squared statistics and its pvalue

H0 : vitamin C dan incident of colds saling bebas vs H1 : vitamin C dan incident of colds tidak saling bebas

chisq.test(tabel_colds, correct = FALSE)
## 
##  Pearson's Chi-squared test
## 
## data:  tabel_colds
## X-squared = 10.567, df = 2, p-value = 0.005075

Dari hasil Uji Khi Kuadrat Pearson diperoleh bahwa nilai Khi Kuadrat = 10.567 dan p-value = 0.005075. Tolak H0 jika nilai p-value < alpha 0.05. Dengan nilai p-value 0.005075 < alfa, sehingga keputusan adalah tidak tolak H0. Hal ini menunjukkan bahwa vitamin C dan incident of colds tidak saling bebas (terdapat asosiasi/hubungan) pada taraf nyata 5%.

b. Report the results of the likelihood ratio test statistic and its pvalue

library(DescTools)
## Warning: package 'DescTools' was built under R version 3.6.3
GTest(tabel_colds)
## 
##  Log likelihood ratio (G-test) test of independence without correction
## 
## data:  tabel_colds
## G = 10.777, X-squared df = 2, p-value = 0.004569

Dari hasil Statistik Uji dengan Likelihood diperoleh bahwa nilai G = 10.777 dan p-value = 0.004569. Tolak H0 jika nilai p-value < alpha 0.05. Dengan nilai p-value 0.004569 < alfa, sehingga keputusan adalah tidak tolak H0. Artinya adalah vitamin C dan incident of colds tidak saling bebas (terdapat asosiasi/hubungan) pada taraf nyata 5%.

c. Report the conclusions

Hasil uji Khi Kuadrat Pearson dan Likelihood diperoleh kesimpulan yang sama yaitu tidak tolak H0, yang berarti bahwa vitamin C dan incident of colds tidak saling bebas (terdapat asosiasi/hubungan) pada taraf nyata 5%.