maling factor variables
mdr<- factor(mydata$mdr,
levels = c(0,1),
labels = c("NO","Yes"))
boil_milk<- factor(mydata$boil_milk,
levels = c(0, 1),
labels = c( "NO","Yes"))
DAE<- factor(mydata$diseased_animals_eaten,
levels = c(0, 1),
labels = c( "NO","Yes"))
education <- factor(mydata$education,
levels = c(0, 1),
labels = c( "NO","Yes"))
Toilet_type<- factor(mydata$X2type,
levels = c(1, 2),
labels = c( "Bush","Toilet"))
abruse<- factor(mydata$abruse,
levels = c( 1,2,3,4,5,6))
#labels = c( "NO","Yes"))
Chi square test for association
mytable1=table(mdr,boil_milk)
chisq.test(mytable1)
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: mytable1
## X-squared = 663.96, df = 1, p-value < 2.2e-16
mytable2=table(mdr,DAE)
chisq.test(mytable2)
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: mytable2
## X-squared = 359.28, df = 1, p-value < 2.2e-16
mytable3=table(mdr,education)
chisq.test(mytable3)
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: mytable3
## X-squared = 6.6324, df = 1, p-value = 0.01001
mytable4=table(mdr,Toilet_type)
chisq.test(mytable4)
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: mytable4
## X-squared = 74.736, df = 1, p-value < 2.2e-16
mytable5=table(mdr,abruse)
chisq.test(mytable5)
##
## Pearson's Chi-squared test
##
## data: mytable5
## X-squared = 145.71, df = 5, p-value < 2.2e-16
Fitting gernalized linear model(Logistic regression)
glm.fit <- glm(mdr~ factor(education)+factor(boil_milk)+factor(diseased_animals_eaten)+factor(X2type)+factor(abruse)+
milk_consume+village_distance_km, data = mydata, family = binomial)
glm.fit
##
## Call: glm(formula = mdr ~ factor(education) + factor(boil_milk) + factor(diseased_animals_eaten) +
## factor(X2type) + factor(abruse) + milk_consume + village_distance_km,
## family = binomial, data = mydata)
##
## Coefficients:
## (Intercept) factor(education)1
## -1.139769 0.261926
## factor(boil_milk)1 factor(diseased_animals_eaten)1
## -2.744316 0.866216
## factor(X2type)2 factor(abruse)2
## -0.377066 0.577429
## factor(abruse)3 factor(abruse)4
## 0.518943 0.561710
## factor(abruse)5 factor(abruse)6
## 0.417516 0.720746
## milk_consume village_distance_km
## 0.544859 0.005144
##
## Degrees of Freedom: 4185 Total (i.e. Null); 4174 Residual
## Null Deviance: 5787
## Residual Deviance: 3659 AIC: 3683
summary(glm.fit)
##
## Call:
## glm(formula = mdr ~ factor(education) + factor(boil_milk) + factor(diseased_animals_eaten) +
## factor(X2type) + factor(abruse) + milk_consume + village_distance_km,
## family = binomial, data = mydata)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.4766 -0.7512 0.1554 0.6719 2.5596
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.139769 0.418128 -2.726 0.00641 **
## factor(education)1 0.261926 0.089904 2.913 0.00358 **
## factor(boil_milk)1 -2.744316 0.104976 -26.142 < 2e-16 ***
## factor(diseased_animals_eaten)1 0.866216 0.090218 9.601 < 2e-16 ***
## factor(X2type)2 -0.377066 0.140912 -2.676 0.00745 **
## factor(abruse)2 0.577429 0.426831 1.353 0.17611
## factor(abruse)3 0.518943 0.429004 1.210 0.22641
## factor(abruse)4 0.561710 0.434939 1.291 0.19654
## factor(abruse)5 0.417516 0.481290 0.867 0.38567
## factor(abruse)6 0.720746 0.786753 0.916 0.35961
## milk_consume 0.544859 0.022308 24.424 < 2e-16 ***
## village_distance_km 0.005144 0.008304 0.619 0.53565
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 5786.6 on 4185 degrees of freedom
## Residual deviance: 3659.4 on 4174 degrees of freedom
## AIC: 3683.4
##
## Number of Fisher Scoring iterations: 5
descriptive statistics
summary(mdr)
## NO Yes
## 1962 2224
summary(boil_milk)
## NO Yes
## 1689 2497
summary(education)
## NO Yes
## 2774 1412
summary(Toilet_type)
## Bush Toilet
## 3761 425
summary(DAE)
## NO Yes
## 2447 1739
making bar charts
df = cbind(mdr, boil_milk, education,Toilet_type,abruse,diseased_animals_eaten,milk_consume,village_distance_km)
df = as.data.frame(df)
class(df)
## [1] "data.frame"
attach(df)
## The following objects are masked _by_ .GlobalEnv:
##
## abruse, boil_milk, education, mdr, Toilet_type
## The following objects are masked from mydata:
##
## abruse, boil_milk, diseased_animals_eaten, education, mdr,
## milk_consume, village_distance_km
names(mydata)
## [1] "id" "mdr" "abruse"
## [4] "boil_milk" "diseased_animals_eaten" "education"
## [7] "X2type" "milk_consume" "village_distance_km"
ggplot(mydata, aes(x = abruse, fill = abruse)) +
geom_bar(position = "dodge")

ggplot(mydata, aes(x = mdr, y= milk_consume)) +
geom_bar(stat = "identity")

ggplot(mydata, aes(x = mdr, y= village_distance_km)) +
geom_bar(stat = "identity")

df=data.frame(table(mydata$mdr,mydata$boil_milk))
names(df) = c("mdr","boil_milk","count")
ggplot(data=df, aes(x = mdr, y= count,fill=boil_milk)) +
geom_bar(stat = "identity")

df=data.frame(table(mydata$mdr,mydata$education))
names(df) = c("mdr","education","count")
ggplot(data=df, aes(x = mdr, y= count,fill=education)) +
geom_bar(stat = "identity")
