library(foreign)
## Warning: package 'foreign' was built under R version 3.2.5
r=read.dta("C:/Users/BINH THANG/Dropbox/Korea/STudy/Thesis/data management/DataR/dataR5.dta")


r1 <- r

attach(r1)
r1$highper[cost_inc<=60000] <- 0
r1$highper[cost_inc>60000] <- 1


r1$logC1=log10(r1$cost_inc)

r1$b16a[b16a == 5] <- 0


#r1$label1p[l5==1] <- 1
#r1$label1p[l5>1] <- 0


r1$freeEn[c9==1] <- 1
r1$freeEn[c9>1] <- 0


r1$la1[l2==3|l2==4|l2==5] <- 1
r1$la1[l3==2|l3==3|l3==4] <- 1
r1$la1[l4==1] <- 1
r1$la1[l5==1] <- 1
r1$la1[is.na(r1$la1)] <- 0


r1$h1[h1== 1] <- 1
r1$h1[h1== 2] <- 0


r1$c7ad[c7== 2] <- 0
r1$c7ad[c7== 1] <- 1
r1$c7ad[is.na(r1$c7)] <- 0


r1$ant[c5==1& c5==2 & c5==7 & c5==8 & c5==9] <- 0
r1$ant[c5==3|c5==4|c5==5] <- 1
r1$ant[is.na(r1$c5)] <- 0


r1$p1=r1$label1+r1$freeEn+r1$ant+r1$c7ad

r1$p[r1$p1==0] <- 0
r1$p[r1$p1==1] <- 1

r1$p[r1$p1>=2] <- 2





newdata2=r1

define varibles

newdata2$c1=as.factor(newdata2$c1)
newdata2$h1=as.factor(newdata2$h1)

newdata2$policy=as.factor(newdata2$policy)
newdata2$educ2=as.factor(newdata2$educ2)
newdata2$age_group=as.factor(newdata2$age_group)
newdata2$d1a=as.factor(newdata2$d1a)
newdata2$selfhealth=as.factor(newdata2$selfhealth)
newdata2$b18a=as.factor(newdata2$b18a)
newdata2$b16a=as.factor(newdata2$b16a)
newdata2$b6a=as.factor(newdata2$b6a)
newdata2$smostt=as.factor(newdata2$smostt)
newdata2$reasons1=as.factor(newdata2$reasons1)
newdata2$ter_in=as.factor(newdata2$ter_in)
newdata2$group_age1=as.factor(newdata2$group_age1)



newdata2$la1=as.factor(newdata2$la1)

newdata2$p1=as.factor(newdata2$p1)
newdata2$p=as.factor(newdata2$p)
newdata2$label1=as.factor(newdata2$label1)
newdata2$c7ad=as.factor(newdata2$c7ad)
newdata2$freeEn=as.factor(newdata2$freeEn)
newdata2$ant=as.factor(newdata2$ant)
newdata2$p=as.factor(newdata2$p)
newdata2$c=newdata2$cost_inc/1000
bd1=subset(newdata2, select=c(c, age_group, d1a, educ2, ter_in, selfhealth, c1, smostt, b6a, h1, b18a, b16a, label1, ant, freeEn, c7ad, p))

bd1=na.omit(bd1)

ggplot2

library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.2.5
bp0 <- ggplot(data=bd1, aes(x=age_group, y=c, fill=age_group)) + geom_boxplot()

bp0a=bp0+ theme(legend.position="none")
bp1 <- ggplot(data=bd1, aes(x=educ2, y=c, fill=educ2)) + geom_boxplot()
bp1a=bp1 + theme(legend.position="none")
bp2 <- ggplot(data=bd1, aes(x=d1a, y=c, fill=d1a)) + geom_boxplot()
bp2a=bp2 + theme(legend.position="none")
bp3 <- ggplot(data=bd1, aes(x=ter_in, y=c, fill=ter_in)) + geom_boxplot()
bp3a=bp3 + theme(legend.position="none")
bp4 <- ggplot(data=bd1, aes(x=selfhealth, y=c, fill=selfhealth)) + geom_boxplot()
bp4a=bp4 + theme(legend.position="none")
library(ggplot2)
library(grid)
library(gridExtra)
## Warning: package 'gridExtra' was built under R version 3.2.5

graph (demography)

grid.arrange(bp0a,  bp1a, bp2a, bp3a, bp4a, ncol = 3)

graph for smoking

bd1$c1=as.factor(bd1$c1)

bp5 <- ggplot(data=bd1, aes(x=c1, y=c, fill=c1)) + geom_boxplot()

bp5a=bp5+ theme(legend.position="none")
bp6 <- ggplot(data=bd1, aes(x=smostt, y=c, fill=smostt)) + geom_boxplot()
bp6a=bp6+ theme(legend.position="none")
bp7 <- ggplot(data=bd1, aes(x=h1, y=c, fill=h1)) + geom_boxplot()
bp7a=bp7+ theme(legend.position="none")
bp8 <- ggplot(data=bd1, aes(x=b6a, y=c, fill=b6a)) + geom_boxplot()
bp8a=bp8+ theme(legend.position="none")
bp9 <- ggplot(data=bd1, aes(x=b16a, y=c, fill=b16a)) + geom_boxplot()

bp9a=bp9+ theme(legend.position="none")
bp10 <- ggplot(data=bd1, aes(x=b18a, y=c, fill=b18a)) + geom_boxplot()
bp10a=bp10+ theme(legend.position="none")

graph (tobacco msoking)

grid.arrange(bp5a,  bp6a, bp7a, bp8a, bp9a, bp10a,ncol = 3)

policy

bd1$label1=as.factor(bd1$label1)
bd1$c7ad=as.factor(bd1$c7ad)
bd1$freeEn=as.factor(bd1$freeEn)
bd1$ant=as.factor(bd1$ant)
bd1$p=as.factor(bd1$p)
bp11 <- ggplot(data=bd1, aes(x=label1, y=c, fill=label1)) + geom_boxplot()
bp11a=bp11+ theme(legend.position="none")
bp12 <- ggplot(data=bd1, aes(x=ant, y=c, fill=ant)) + geom_boxplot()
bp12a=bp12+ theme(legend.position="none")
bp13 <- ggplot(data=bd1, aes(x=freeEn, y=c, fill=freeEn)) + geom_boxplot()
bp13a=bp13+ theme(legend.position="none")
bp14 <- ggplot(data=bd1, aes(x=c7ad, y=c, fill=c7ad)) + geom_boxplot()
bp14a=bp14+ theme(legend.position="none")
bp15 <- ggplot(data=bd1, aes(x=p, y=c, fill=p)) + geom_boxplot()
bp15a=bp15+ theme(legend.position="none")

graph for policy

grid.arrange(bp11a,  bp12a, bp13a, bp14a, bp15a,ncol = 3)