You are on page 1of 11

Name: Raunak Malkani Date: 19/01/2022

UID: 20BIT032
PRACTICAL 8: CHI-SQUARE TEST
A) GOODNESS OF FIT TEST

1. Consider the built-in data survey and check the percentage of people
doing frequent exercise is 23, not doing is 5 and sometimes is 72. (single
categorical variable)
CODE:
library(MASS)
survey
#Consider the built-in data survey and check the percentage of people doing
frequent exercise is 23, not doing is 5 and sometimes is 72. (single
categorical variable)
H0="people doing frequent exercise is 23, not doing is 5 and sometimes is
72."
H1="not according to the distribution"

levels(survey$Exer)
ob_freq=table(survey$Exer)
print(ob_freq)

ex_dist=c(0.23,0.05,0.72)
#freq=0.23*237
#none=0.05*237
#some=0.72*237
chq=chisq.test(ob_freq,p=ex_dist)
pv=chq$p.value
print(pv)

if(pv>los)
{
print("We accept null hypothesis")
print(H0)
}else{
print("We reject null hypothesis")
print(H1)
}

OUTPUT:
2. CODE:
H0 = "Ratio is correct."
H1 = "Ratio is incorrect."

df=read.csv("C:/Users/singh/OneDrive/Documents/COSNT/ct2.csv")
df
im=df$improvement
los=0.05

ob_freq = table(im)
print(ob_freq)

ex_freq = c(0.4, 0.6)

chq = chisq.test(ob_freq, p = ex_freq)


pv = chq$p.value
print(pv)

if(los < pv){


print("We accept null hypothesis")
print(H0)
}else{
print("We reject null hypothesis")
print(H1)
}

OUTPUT:
 Create .csv file in text document and save as .csv and give the path
B) TEST FOR INDEPENDENCE

3. CODE:
library(MASS)
survey

H0="Smoke & exercise are independent categorical variable"

H1="Smoke & exercise are not independent categorical variable"

ex=survey$Exer
smk=survey$Smoke

los=0.05
ct1=table(ex,smk) #to create contingency table
print(ct1)

en_ct1 = cbind(ct1["Freq",],ct1["None",]+ct1["Some",])
print(en_ct1)

chqTestValue1 = chisq.test(en_ct1)
print(chqTestValue)

pv = chqTestValue1$p.value
print(pv)

if(los < pv){


print("We accept null hypothesis")
print(H0)
}else{
print("We reject null hypothesis")
print(H1)
}

OUTPUT:
4. CODE:

H0="Sex & exercise are independent categorical variable"

H1="Sex & exercise are not independent categorical variable"

ex=survey$Exer
sex=survey$Sex
los=0.05
ct1=table(ex,sex) #to create contingency table
print(ct1)

chqTestValue=chisq.test(ex,sex,correct=FALSE)
print(chqTestValue)

pv=chqTestValue$p.value
print(pv)

if(los < pv){


print("We accept null hypothesis")
print(H0)
}else{
print("We reject null hypothesis")
print(H1)
}

OUTPUT:

You might also like