describle data

outcome: WTP ()

bayesian logistics regression for all (90-10)

It results in a likelihood maximized when a parameter is extremely large, and causes trouble with ordinary maximum likelihood approached.) Another option is to use Bayesian methods. Here we focus on Markov chain Monte Carlo (MCMC) approaches to Bayesian analysis.

steps to solve

Establish models

Establish models

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/dataR3.dta")

r1 <- subset(r)

attach(r1)


r1$wtp[cost1 == 0] <- 0
r1$wtp[cost1 == 1] <- 0

r1$wtp[is.na(r1$wtp)] <- 1


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


attach(r1)
## The following objects are masked from r1 (pos = 3):
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, branch3, c1, c2, c23a, c3, c4, c5,
##     c6, c7, c8, c9, COST, cost_inc, cost1, costincrease, ct, d1,
##     d10, d11, d1a, d2, d3, d3a, d4, d5, d6, d7, d8, d9, Decision,
##     e1, e2, edu, educ1, educ2, f1, ghi1, ghi2, ghiro2,
##     giadinhkoUH, group_age, group_age1, h1, h10a, h10a1, h10a10,
##     h10a11, h10a11a, h10a2, h10a3, h10a4, h10a5, h10a6, h10a7,
##     h10a8, h10a9, h12, h12a, h12a_1, h12log, h13, h2, h3, h4, h5,
##     h6, h7, h8, h9, ha000, itc1, itc2, l1, l2, l3, l4, l5, l6,
##     label1, label2, moneyspent, msdt, n01, n02, n03, n05, n06,
##     n07, n08, n1, n10, n100, n101, n102, n103, n11, n12, n13, n14,
##     n15, n16, n1b, n2, n3, n35, n36, n37, n38, n39, n3posterb, n4,
##     n40, n41, n42, n43, n44, n45, n46, n47, n48, n49, n5, n50,
##     n51, n52, n53, n54, n55, n56, n57, n58, n59, n6, n60, n61,
##     n61a, n62, n62a, n63, n63a, n64, n64a, n65, n65a, n66, n66a,
##     n67, n67a, n68, n68a, n7, n77, n78, n7tren, n8, n88, n89,
##     n8nha, n9, n97, n98, n99, n9khach, noEnvi2, occup1, policy,
##     policy_a, policy2, reasons, reasons1, s1, Screening,
##     selfhealth, SH1, smostt, ter_fa1, ter_in, tertile_fa,
##     tertile_indi, test, unitsdiffi1, var242, w1, w2, w3, w4
mytable <- table(wtp, c1)
print(mytable)
##    c1
## wtp   1   2
##   0 398  63
##   1 329  32
prop.table(mytable)
##    c1
## wtp          1          2
##   0 0.48418491 0.07664234
##   1 0.40024331 0.03892944
newdata2=r1

attach(newdata2 )
## The following objects are masked from r1 (pos = 3):
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, branch3, c1, c2, c23a, c3, c4, c5,
##     c6, c7, c8, c9, COST, cost_inc, cost1, costincrease, ct, d1,
##     d10, d11, d1a, d2, d3, d3a, d4, d5, d6, d7, d8, d9, Decision,
##     e1, e2, edu, educ1, educ2, f1, ghi1, ghi2, ghiro2,
##     giadinhkoUH, group_age, group_age1, h1, h10a, h10a1, h10a10,
##     h10a11, h10a11a, h10a2, h10a3, h10a4, h10a5, h10a6, h10a7,
##     h10a8, h10a9, h12, h12a, h12a_1, h12log, h13, h2, h3, h4, h5,
##     h6, h7, h8, h9, ha000, itc1, itc2, l1, l2, l3, l4, l5, l6,
##     label1, label2, moneyspent, msdt, n01, n02, n03, n05, n06,
##     n07, n08, n1, n10, n100, n101, n102, n103, n11, n12, n13, n14,
##     n15, n16, n1b, n2, n3, n35, n36, n37, n38, n39, n3posterb, n4,
##     n40, n41, n42, n43, n44, n45, n46, n47, n48, n49, n5, n50,
##     n51, n52, n53, n54, n55, n56, n57, n58, n59, n6, n60, n61,
##     n61a, n62, n62a, n63, n63a, n64, n64a, n65, n65a, n66, n66a,
##     n67, n67a, n68, n68a, n7, n77, n78, n7tren, n8, n88, n89,
##     n8nha, n9, n97, n98, n99, n9khach, noEnvi2, occup1, policy,
##     policy_a, policy2, reasons, reasons1, s1, Screening,
##     selfhealth, SH1, smostt, ter_fa1, ter_in, tertile_fa,
##     tertile_indi, test, unitsdiffi1, var242, w1, w2, w3, w4, wtp
## The following objects are masked from r1 (pos = 4):
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, branch3, c1, c2, c23a, c3, c4, c5,
##     c6, c7, c8, c9, COST, cost_inc, cost1, costincrease, ct, d1,
##     d10, d11, d1a, d2, d3, d3a, d4, d5, d6, d7, d8, d9, Decision,
##     e1, e2, edu, educ1, educ2, f1, ghi1, ghi2, ghiro2,
##     giadinhkoUH, group_age, group_age1, h1, h10a, h10a1, h10a10,
##     h10a11, h10a11a, h10a2, h10a3, h10a4, h10a5, h10a6, h10a7,
##     h10a8, h10a9, h12, h12a, h12a_1, h12log, h13, h2, h3, h4, h5,
##     h6, h7, h8, h9, ha000, itc1, itc2, l1, l2, l3, l4, l5, l6,
##     label1, label2, moneyspent, msdt, n01, n02, n03, n05, n06,
##     n07, n08, n1, n10, n100, n101, n102, n103, n11, n12, n13, n14,
##     n15, n16, n1b, n2, n3, n35, n36, n37, n38, n39, n3posterb, n4,
##     n40, n41, n42, n43, n44, n45, n46, n47, n48, n49, n5, n50,
##     n51, n52, n53, n54, n55, n56, n57, n58, n59, n6, n60, n61,
##     n61a, n62, n62a, n63, n63a, n64, n64a, n65, n65a, n66, n66a,
##     n67, n67a, n68, n68a, n7, n77, n78, n7tren, n8, n88, n89,
##     n8nha, n9, n97, n98, n99, n9khach, noEnvi2, occup1, policy,
##     policy_a, policy2, reasons, reasons1, s1, Screening,
##     selfhealth, SH1, smostt, ter_fa1, ter_in, tertile_fa,
##     tertile_indi, test, unitsdiffi1, var242, w1, w2, w3, w4

define varibles

newdata2$cost1=as.factor(newdata2$cost1)
newdata2$c1=as.factor(newdata2$c1)
newdata2$h1=as.factor(newdata2$h1)
newdata2$label1=as.factor(newdata2$label1)
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)

re-organise our data by sampling .

newdata2=newdata2[sample(1:nrow(newdata2), 822, replace=F),]

Create new data frame (90%-10%) (N=822)

Train data

train=newdata2[1:740, ]
View(train)

Test data

test=newdata2[741:822, ]
View(test)
  1. Binh Thang - binhthang1001@gmail.com

Install packages and library

library("brms")
## Warning: package 'brms' was built under R version 3.2.5
## Loading required package: Rcpp
## Warning: package 'Rcpp' was built under R version 3.2.5
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.2.5
## Loading 'brms' package (version 1.5.1). Useful instructions 
## can be found by typing help('brms'). A more detailed introduction 
## to the package is available through vignette('brms_overview').
library("caret")
## Warning: package 'caret' was built under R version 3.2.5
## Loading required package: lattice
library("coda")
## Warning: package 'coda' was built under R version 3.2.5
library("rstan", lib.loc="~/R/win-library/3.2")
## Warning: package 'rstan' was built under R version 3.2.5
## Loading required package: StanHeaders
## Warning: package 'StanHeaders' was built under R version 3.2.5
## rstan (Version 2.14.1, packaged: 2016-12-28 14:55:41 UTC, GitRev: 5fa1e80eb817)
## For execution on a local, multicore CPU with excess RAM we recommend calling
## rstan_options(auto_write = TRUE)
## options(mc.cores = parallel::detectCores())
## 
## Attaching package: 'rstan'
## The following object is masked from 'package:coda':
## 
##     traceplot
options(mc.cores = parallel::detectCores())

set up for fomulation

prior=get_prior(formula=wtp~age_group+h1+ d1a+educ2+ter_in+selfhealth+smostt
               +b16a+b18a+label1+anticam+noEnvi2, family="bernoulli", data=train)
## Warning: Rows containing NAs were excluded from the model
set.seed(1234) 
 
baybin=brm(formula=wtp~age_group+h1+ d1a+educ2+ter_in+selfhealth+smostt
               +b16a+b18a+label1+anticam+noEnvi2, family="bernoulli", data=train, chains=5, iter=2000, warmup=1000, prior=prior)
## Warning: Rows containing NAs were excluded from the model
## Compiling the C++ model
## Start sampling

Summary model using train data - this is main results for model

summary(baybin)
##  Family: bernoulli (logit) 
## Formula: wtp ~ age_group + h1 + d1a + educ2 + ter_in + selfhealth + smostt + b16a + b18a + label1 + anticam + noEnvi2 
##    Data: train (Number of observations: 708) 
## Samples: 5 chains, each with iter = 2000; warmup = 1000; thin = 1; 
##          total post-warmup samples = 5000
##    WAIC: Not computed
##  
## Population-Level Effects: 
##                 Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
## Intercept           0.28      0.36    -0.44     1.00       5000    1
## age_groupgr3039     0.46      0.27    -0.06     0.99       3805    1
## age_groupgr4049     0.64      0.29     0.07     1.21       3359    1
## age_groupgr5059     0.56      0.31    -0.04     1.17       3181    1
## age_group60plus     0.95      0.40     0.16     1.73       4107    1
## h12                 0.06      0.17    -0.28     0.40       5000    1
## d1amarried         -0.30      0.24    -0.76     0.17       3913    1
## educ22             -0.37      0.21    -0.78     0.04       5000    1
## educ23             -0.06      0.25    -0.55     0.43       4311    1
## ter_in2             0.32      0.21    -0.09     0.75       5000    1
## ter_in3             0.48      0.21     0.07     0.88       5000    1
## selfhealthgood      0.06      0.17    -0.27     0.40       5000    1
## smosttmedium       -0.29      0.22    -0.72     0.14       5000    1
## smosttheavy         0.26      0.22    -0.18     0.71       5000    1
## b16a1              -0.88      0.22    -1.32    -0.47       5000    1
## b18abad            -0.38      0.17    -0.72    -0.05       5000    1
## label11             0.42      0.24    -0.07     0.88       5000    1
## anticam             0.00      0.22    -0.45     0.43       5000    1
## noEnvi2            -0.22      0.22    -0.66     0.22       5000    1
## 
## Samples were drawn using sampling(NUTS). For each parameter, Eff.Sample 
## is a crude measure of effective sample size, and Rhat is the potential 
## scale reduction factor on split chains (at convergence, Rhat = 1).
WAIC(baybin)
##    WAIC    SE
##  937.89 18.19

Notes: If you wanna report OR and 95%CI, you might need the exp funtion (using in R or in excel)

fit model

baybin$fit
## Inference for Stan model: bernoulli(logit) brms-model.
## 5 chains, each with iter=2000; warmup=1000; thin=1; 
## post-warmup draws per chain=1000, total post-warmup draws=5000.
## 
##                      mean se_mean   sd    2.5%     25%     50%     75%
## b_Intercept          0.28    0.01 0.36   -0.44    0.04    0.28    0.53
## b_age_groupgr3039    0.46    0.00 0.27   -0.06    0.28    0.46    0.65
## b_age_groupgr4049    0.64    0.01 0.29    0.07    0.44    0.63    0.84
## b_age_groupgr5059    0.56    0.01 0.31   -0.04    0.34    0.56    0.77
## b_age_group60plus    0.95    0.01 0.40    0.16    0.68    0.95    1.21
## b_h12                0.06    0.00 0.17   -0.28   -0.05    0.06    0.18
## b_d1amarried        -0.30    0.00 0.24   -0.76   -0.46   -0.30   -0.13
## b_educ22            -0.37    0.00 0.21   -0.78   -0.51   -0.36   -0.23
## b_educ23            -0.06    0.00 0.25   -0.55   -0.23   -0.06    0.11
## b_ter_in2            0.32    0.00 0.21   -0.09    0.17    0.32    0.46
## b_ter_in3            0.48    0.00 0.21    0.07    0.34    0.48    0.63
## b_selfhealthgood     0.06    0.00 0.17   -0.27   -0.05    0.06    0.18
## b_smosttmedium      -0.29    0.00 0.22   -0.72   -0.45   -0.29   -0.14
## b_smosttheavy        0.26    0.00 0.22   -0.18    0.11    0.25    0.41
## b_b16a1             -0.88    0.00 0.22   -1.32   -1.02   -0.88   -0.74
## b_b18abad           -0.38    0.00 0.17   -0.72   -0.50   -0.38   -0.26
## b_label11            0.42    0.00 0.24   -0.07    0.25    0.42    0.58
## b_anticam            0.00    0.00 0.22   -0.45   -0.16    0.00    0.15
## b_noEnvi2           -0.22    0.00 0.22   -0.66   -0.37   -0.22   -0.08
## lp__              -458.73    0.07 3.11 -465.70 -460.63 -458.42 -456.43
##                     97.5% n_eff Rhat
## b_Intercept          1.00  5000    1
## b_age_groupgr3039    0.99  3805    1
## b_age_groupgr4049    1.21  3359    1
## b_age_groupgr5059    1.17  3181    1
## b_age_group60plus    1.73  4107    1
## b_h12                0.40  5000    1
## b_d1amarried         0.17  3913    1
## b_educ22             0.04  5000    1
## b_educ23             0.43  4311    1
## b_ter_in2            0.75  5000    1
## b_ter_in3            0.88  5000    1
## b_selfhealthgood     0.40  5000    1
## b_smosttmedium       0.14  5000    1
## b_smosttheavy        0.71  5000    1
## b_b16a1             -0.47  5000    1
## b_b18abad           -0.05  5000    1
## b_label11            0.88  5000    1
## b_anticam            0.43  5000    1
## b_noEnvi2            0.22  5000    1
## lp__              -453.68  2277    1
## 
## Samples were drawn using NUTS(diag_e) at Wed Mar 22 00:17:12 2017.
## For each parameter, n_eff is a crude measure of effective sample size,
## and Rhat is the potential scale reduction factor on split chains (at 
## convergence, Rhat=1).

next steps, ww will check the accuracy of model using ROC

establish command using TEST data

prob=as.data.frame(predict(baybin,test,type="response"))

library(e1071)
## Warning: package 'e1071' was built under R version 3.2.5
pred=ifelse(prob$Estimate> 0.5, 1, 0)

Results

confusionMatrix(data=pred,reference=test$wtp)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 31 26
##          1 13 12
##                                           
##                Accuracy : 0.5244          
##                  95% CI : (0.4111, 0.6359)
##     No Information Rate : 0.5366          
##     P-Value [Acc > NIR] : 0.63092         
##                                           
##                   Kappa : 0.0208          
##  Mcnemar's Test P-Value : 0.05466         
##                                           
##             Sensitivity : 0.7045          
##             Specificity : 0.3158          
##          Pos Pred Value : 0.5439          
##          Neg Pred Value : 0.4800          
##              Prevalence : 0.5366          
##          Detection Rate : 0.3780          
##    Detection Prevalence : 0.6951          
##       Balanced Accuracy : 0.5102          
##                                           
##        'Positive' Class : 0               
## 

model for age>=40

library(foreign)
r=read.dta("C:/Users/BINH THANG/Dropbox/Korea/STudy/Thesis/data management/DataR/dataR2.dta")

r1 <- subset(r, group_age1==1)

attach(r1)
## The following object is masked _by_ .GlobalEnv:
## 
##     test
## The following objects are masked from newdata2:
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, c1, c2, c23a, c3, c4, c5, c6, c7,
##     c8, c9, COST, cost1, costincrease, ct, d1, d10, d11, d1a, d2,
##     d3, d3a, d4, d5, d6, d7, d8, d9, Decision, e1, e2, edu, educ1,
##     educ2, f1, ghi1, ghi2, ghiro2, giadinhkoUH, group_age,
##     group_age1, h1, h10a, h10a1, h10a10, h10a11, h10a11a, h10a2,
##     h10a3, h10a4, h10a5, h10a6, h10a7, h10a8, h10a9, h12, h12a,
##     h12a_1, h12log, h13, h2, h3, h4, h5, h6, h7, h8, h9, ha000,
##     itc1, itc2, l1, l2, l3, l4, l5, l6, label1, label2,
##     moneyspent, msdt, n01, n02, n03, n05, n06, n07, n08, n1, n10,
##     n100, n101, n102, n103, n11, n12, n13, n14, n15, n16, n1b, n2,
##     n3, n35, n36, n37, n38, n39, n3posterb, n4, n40, n41, n42,
##     n43, n44, n45, n46, n47, n48, n49, n5, n50, n51, n52, n53,
##     n54, n55, n56, n57, n58, n59, n6, n60, n61, n61a, n62, n62a,
##     n63, n63a, n64, n64a, n65, n65a, n66, n66a, n67, n67a, n68,
##     n68a, n7, n77, n78, n7tren, n8, n88, n89, n8nha, n9, n97, n98,
##     n99, n9khach, noEnvi2, occup1, policy, policy_a, policy2,
##     reasons, reasons1, s1, Screening, selfhealth, SH1, smostt,
##     ter_fa1, ter_in, tertile_fa, tertile_indi, test, unitsdiffi1,
##     w1, w2, w3, w4
## The following objects are masked from r1 (pos = 13):
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, c1, c2, c23a, c3, c4, c5, c6, c7,
##     c8, c9, COST, cost1, costincrease, ct, d1, d10, d11, d1a, d2,
##     d3, d3a, d4, d5, d6, d7, d8, d9, Decision, e1, e2, edu, educ1,
##     educ2, f1, ghi1, ghi2, ghiro2, giadinhkoUH, group_age,
##     group_age1, h1, h10a, h10a1, h10a10, h10a11, h10a11a, h10a2,
##     h10a3, h10a4, h10a5, h10a6, h10a7, h10a8, h10a9, h12, h12a,
##     h12a_1, h12log, h13, h2, h3, h4, h5, h6, h7, h8, h9, ha000,
##     itc1, itc2, l1, l2, l3, l4, l5, l6, label1, label2,
##     moneyspent, msdt, n01, n02, n03, n05, n06, n07, n08, n1, n10,
##     n100, n101, n102, n103, n11, n12, n13, n14, n15, n16, n1b, n2,
##     n3, n35, n36, n37, n38, n39, n3posterb, n4, n40, n41, n42,
##     n43, n44, n45, n46, n47, n48, n49, n5, n50, n51, n52, n53,
##     n54, n55, n56, n57, n58, n59, n6, n60, n61, n61a, n62, n62a,
##     n63, n63a, n64, n64a, n65, n65a, n66, n66a, n67, n67a, n68,
##     n68a, n7, n77, n78, n7tren, n8, n88, n89, n8nha, n9, n97, n98,
##     n99, n9khach, noEnvi2, occup1, policy, policy_a, policy2,
##     reasons, reasons1, s1, Screening, selfhealth, SH1, smostt,
##     ter_fa1, ter_in, tertile_fa, tertile_indi, test, unitsdiffi1,
##     w1, w2, w3, w4
## The following objects are masked from r1 (pos = 14):
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, c1, c2, c23a, c3, c4, c5, c6, c7,
##     c8, c9, COST, cost1, costincrease, ct, d1, d10, d11, d1a, d2,
##     d3, d3a, d4, d5, d6, d7, d8, d9, Decision, e1, e2, edu, educ1,
##     educ2, f1, ghi1, ghi2, ghiro2, giadinhkoUH, group_age,
##     group_age1, h1, h10a, h10a1, h10a10, h10a11, h10a11a, h10a2,
##     h10a3, h10a4, h10a5, h10a6, h10a7, h10a8, h10a9, h12, h12a,
##     h12a_1, h12log, h13, h2, h3, h4, h5, h6, h7, h8, h9, ha000,
##     itc1, itc2, l1, l2, l3, l4, l5, l6, label1, label2,
##     moneyspent, msdt, n01, n02, n03, n05, n06, n07, n08, n1, n10,
##     n100, n101, n102, n103, n11, n12, n13, n14, n15, n16, n1b, n2,
##     n3, n35, n36, n37, n38, n39, n3posterb, n4, n40, n41, n42,
##     n43, n44, n45, n46, n47, n48, n49, n5, n50, n51, n52, n53,
##     n54, n55, n56, n57, n58, n59, n6, n60, n61, n61a, n62, n62a,
##     n63, n63a, n64, n64a, n65, n65a, n66, n66a, n67, n67a, n68,
##     n68a, n7, n77, n78, n7tren, n8, n88, n89, n8nha, n9, n97, n98,
##     n99, n9khach, noEnvi2, occup1, policy, policy_a, policy2,
##     reasons, reasons1, s1, Screening, selfhealth, SH1, smostt,
##     ter_fa1, ter_in, tertile_fa, tertile_indi, test, unitsdiffi1,
##     w1, w2, w3, w4
r1$wtp[cost1 == 0] <- 0
r1$wtp[cost1 == 1] <- 0

r1$wtp[is.na(r1$wtp)] <- 1


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


attach(r1)
## The following object is masked _by_ .GlobalEnv:
## 
##     test
## The following objects are masked from r1 (pos = 3):
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, c1, c2, c23a, c3, c4, c5, c6, c7,
##     c8, c9, COST, cost1, cost2, costincrease, ct, d1, d10, d11,
##     d1a, d2, d3, d3a, d4, d5, d6, d7, d8, d9, Decision, e1, e2,
##     edu, educ1, educ2, f1, ghi1, ghi2, ghiro2, giadinhkoUH,
##     group_age, group_age1, h1, h10a, h10a1, h10a10, h10a11,
##     h10a11a, h10a2, h10a3, h10a4, h10a5, h10a6, h10a7, h10a8,
##     h10a9, h12, h12a, h12a_1, h12log, h13, h2, h3, h4, h5, h6, h7,
##     h8, h9, ha000, itc1, itc2, l1, l2, l3, l4, l5, l6, label1,
##     label2, moneyspent, msdt, n01, n02, n03, n05, n06, n07, n08,
##     n1, n10, n100, n101, n102, n103, n11, n12, n13, n14, n15, n16,
##     n1b, n2, n3, n35, n36, n37, n38, n39, n3posterb, n4, n40, n41,
##     n42, n43, n44, n45, n46, n47, n48, n49, n5, n50, n51, n52,
##     n53, n54, n55, n56, n57, n58, n59, n6, n60, n61, n61a, n62,
##     n62a, n63, n63a, n64, n64a, n65, n65a, n66, n66a, n67, n67a,
##     n68, n68a, n7, n77, n78, n7tren, n8, n88, n89, n8nha, n9, n97,
##     n98, n99, n9khach, noEnvi2, occup1, policy, policy_a, policy2,
##     reasons, reasons1, s1, Screening, selfhealth, SH1, smostt,
##     ter_fa1, ter_in, tertile_fa, tertile_indi, test, unitsdiffi1,
##     w1, w2, w3, w4
## The following objects are masked from newdata2:
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, c1, c2, c23a, c3, c4, c5, c6, c7,
##     c8, c9, COST, cost1, costincrease, ct, d1, d10, d11, d1a, d2,
##     d3, d3a, d4, d5, d6, d7, d8, d9, Decision, e1, e2, edu, educ1,
##     educ2, f1, ghi1, ghi2, ghiro2, giadinhkoUH, group_age,
##     group_age1, h1, h10a, h10a1, h10a10, h10a11, h10a11a, h10a2,
##     h10a3, h10a4, h10a5, h10a6, h10a7, h10a8, h10a9, h12, h12a,
##     h12a_1, h12log, h13, h2, h3, h4, h5, h6, h7, h8, h9, ha000,
##     itc1, itc2, l1, l2, l3, l4, l5, l6, label1, label2,
##     moneyspent, msdt, n01, n02, n03, n05, n06, n07, n08, n1, n10,
##     n100, n101, n102, n103, n11, n12, n13, n14, n15, n16, n1b, n2,
##     n3, n35, n36, n37, n38, n39, n3posterb, n4, n40, n41, n42,
##     n43, n44, n45, n46, n47, n48, n49, n5, n50, n51, n52, n53,
##     n54, n55, n56, n57, n58, n59, n6, n60, n61, n61a, n62, n62a,
##     n63, n63a, n64, n64a, n65, n65a, n66, n66a, n67, n67a, n68,
##     n68a, n7, n77, n78, n7tren, n8, n88, n89, n8nha, n9, n97, n98,
##     n99, n9khach, noEnvi2, occup1, policy, policy_a, policy2,
##     reasons, reasons1, s1, Screening, selfhealth, SH1, smostt,
##     ter_fa1, ter_in, tertile_fa, tertile_indi, test, unitsdiffi1,
##     w1, w2, w3, w4, wtp
## The following objects are masked from r1 (pos = 14):
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, c1, c2, c23a, c3, c4, c5, c6, c7,
##     c8, c9, COST, cost1, costincrease, ct, d1, d10, d11, d1a, d2,
##     d3, d3a, d4, d5, d6, d7, d8, d9, Decision, e1, e2, edu, educ1,
##     educ2, f1, ghi1, ghi2, ghiro2, giadinhkoUH, group_age,
##     group_age1, h1, h10a, h10a1, h10a10, h10a11, h10a11a, h10a2,
##     h10a3, h10a4, h10a5, h10a6, h10a7, h10a8, h10a9, h12, h12a,
##     h12a_1, h12log, h13, h2, h3, h4, h5, h6, h7, h8, h9, ha000,
##     itc1, itc2, l1, l2, l3, l4, l5, l6, label1, label2,
##     moneyspent, msdt, n01, n02, n03, n05, n06, n07, n08, n1, n10,
##     n100, n101, n102, n103, n11, n12, n13, n14, n15, n16, n1b, n2,
##     n3, n35, n36, n37, n38, n39, n3posterb, n4, n40, n41, n42,
##     n43, n44, n45, n46, n47, n48, n49, n5, n50, n51, n52, n53,
##     n54, n55, n56, n57, n58, n59, n6, n60, n61, n61a, n62, n62a,
##     n63, n63a, n64, n64a, n65, n65a, n66, n66a, n67, n67a, n68,
##     n68a, n7, n77, n78, n7tren, n8, n88, n89, n8nha, n9, n97, n98,
##     n99, n9khach, noEnvi2, occup1, policy, policy_a, policy2,
##     reasons, reasons1, s1, Screening, selfhealth, SH1, smostt,
##     ter_fa1, ter_in, tertile_fa, tertile_indi, test, unitsdiffi1,
##     w1, w2, w3, w4, wtp
## The following objects are masked from r1 (pos = 15):
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, c1, c2, c23a, c3, c4, c5, c6, c7,
##     c8, c9, COST, cost1, costincrease, ct, d1, d10, d11, d1a, d2,
##     d3, d3a, d4, d5, d6, d7, d8, d9, Decision, e1, e2, edu, educ1,
##     educ2, f1, ghi1, ghi2, ghiro2, giadinhkoUH, group_age,
##     group_age1, h1, h10a, h10a1, h10a10, h10a11, h10a11a, h10a2,
##     h10a3, h10a4, h10a5, h10a6, h10a7, h10a8, h10a9, h12, h12a,
##     h12a_1, h12log, h13, h2, h3, h4, h5, h6, h7, h8, h9, ha000,
##     itc1, itc2, l1, l2, l3, l4, l5, l6, label1, label2,
##     moneyspent, msdt, n01, n02, n03, n05, n06, n07, n08, n1, n10,
##     n100, n101, n102, n103, n11, n12, n13, n14, n15, n16, n1b, n2,
##     n3, n35, n36, n37, n38, n39, n3posterb, n4, n40, n41, n42,
##     n43, n44, n45, n46, n47, n48, n49, n5, n50, n51, n52, n53,
##     n54, n55, n56, n57, n58, n59, n6, n60, n61, n61a, n62, n62a,
##     n63, n63a, n64, n64a, n65, n65a, n66, n66a, n67, n67a, n68,
##     n68a, n7, n77, n78, n7tren, n8, n88, n89, n8nha, n9, n97, n98,
##     n99, n9khach, noEnvi2, occup1, policy, policy_a, policy2,
##     reasons, reasons1, s1, Screening, selfhealth, SH1, smostt,
##     ter_fa1, ter_in, tertile_fa, tertile_indi, test, unitsdiffi1,
##     w1, w2, w3, w4
mytable <- table(wtp, c1)
print(mytable)
##    c1
## wtp   1   2
##   0 180  14
##   1 181   5
prop.table(mytable)
##    c1
## wtp          1          2
##   0 0.47368421 0.03684211
##   1 0.47631579 0.01315789
newdata2=r1

attach(newdata2 )
## The following object is masked _by_ .GlobalEnv:
## 
##     test
## The following objects are masked from r1 (pos = 3):
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, c1, c2, c23a, c3, c4, c5, c6, c7,
##     c8, c9, COST, cost1, cost2, costincrease, ct, d1, d10, d11,
##     d1a, d2, d3, d3a, d4, d5, d6, d7, d8, d9, Decision, e1, e2,
##     edu, educ1, educ2, f1, ghi1, ghi2, ghiro2, giadinhkoUH,
##     group_age, group_age1, h1, h10a, h10a1, h10a10, h10a11,
##     h10a11a, h10a2, h10a3, h10a4, h10a5, h10a6, h10a7, h10a8,
##     h10a9, h12, h12a, h12a_1, h12log, h13, h2, h3, h4, h5, h6, h7,
##     h8, h9, ha000, itc1, itc2, l1, l2, l3, l4, l5, l6, label1,
##     label2, moneyspent, msdt, n01, n02, n03, n05, n06, n07, n08,
##     n1, n10, n100, n101, n102, n103, n11, n12, n13, n14, n15, n16,
##     n1b, n2, n3, n35, n36, n37, n38, n39, n3posterb, n4, n40, n41,
##     n42, n43, n44, n45, n46, n47, n48, n49, n5, n50, n51, n52,
##     n53, n54, n55, n56, n57, n58, n59, n6, n60, n61, n61a, n62,
##     n62a, n63, n63a, n64, n64a, n65, n65a, n66, n66a, n67, n67a,
##     n68, n68a, n7, n77, n78, n7tren, n8, n88, n89, n8nha, n9, n97,
##     n98, n99, n9khach, noEnvi2, occup1, policy, policy_a, policy2,
##     reasons, reasons1, s1, Screening, selfhealth, SH1, smostt,
##     ter_fa1, ter_in, tertile_fa, tertile_indi, test, unitsdiffi1,
##     w1, w2, w3, w4, wtp
## The following objects are masked from r1 (pos = 4):
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, c1, c2, c23a, c3, c4, c5, c6, c7,
##     c8, c9, COST, cost1, cost2, costincrease, ct, d1, d10, d11,
##     d1a, d2, d3, d3a, d4, d5, d6, d7, d8, d9, Decision, e1, e2,
##     edu, educ1, educ2, f1, ghi1, ghi2, ghiro2, giadinhkoUH,
##     group_age, group_age1, h1, h10a, h10a1, h10a10, h10a11,
##     h10a11a, h10a2, h10a3, h10a4, h10a5, h10a6, h10a7, h10a8,
##     h10a9, h12, h12a, h12a_1, h12log, h13, h2, h3, h4, h5, h6, h7,
##     h8, h9, ha000, itc1, itc2, l1, l2, l3, l4, l5, l6, label1,
##     label2, moneyspent, msdt, n01, n02, n03, n05, n06, n07, n08,
##     n1, n10, n100, n101, n102, n103, n11, n12, n13, n14, n15, n16,
##     n1b, n2, n3, n35, n36, n37, n38, n39, n3posterb, n4, n40, n41,
##     n42, n43, n44, n45, n46, n47, n48, n49, n5, n50, n51, n52,
##     n53, n54, n55, n56, n57, n58, n59, n6, n60, n61, n61a, n62,
##     n62a, n63, n63a, n64, n64a, n65, n65a, n66, n66a, n67, n67a,
##     n68, n68a, n7, n77, n78, n7tren, n8, n88, n89, n8nha, n9, n97,
##     n98, n99, n9khach, noEnvi2, occup1, policy, policy_a, policy2,
##     reasons, reasons1, s1, Screening, selfhealth, SH1, smostt,
##     ter_fa1, ter_in, tertile_fa, tertile_indi, test, unitsdiffi1,
##     w1, w2, w3, w4
## The following objects are masked from newdata2 (pos = 14):
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, c1, c2, c23a, c3, c4, c5, c6, c7,
##     c8, c9, COST, cost1, costincrease, ct, d1, d10, d11, d1a, d2,
##     d3, d3a, d4, d5, d6, d7, d8, d9, Decision, e1, e2, edu, educ1,
##     educ2, f1, ghi1, ghi2, ghiro2, giadinhkoUH, group_age,
##     group_age1, h1, h10a, h10a1, h10a10, h10a11, h10a11a, h10a2,
##     h10a3, h10a4, h10a5, h10a6, h10a7, h10a8, h10a9, h12, h12a,
##     h12a_1, h12log, h13, h2, h3, h4, h5, h6, h7, h8, h9, ha000,
##     itc1, itc2, l1, l2, l3, l4, l5, l6, label1, label2,
##     moneyspent, msdt, n01, n02, n03, n05, n06, n07, n08, n1, n10,
##     n100, n101, n102, n103, n11, n12, n13, n14, n15, n16, n1b, n2,
##     n3, n35, n36, n37, n38, n39, n3posterb, n4, n40, n41, n42,
##     n43, n44, n45, n46, n47, n48, n49, n5, n50, n51, n52, n53,
##     n54, n55, n56, n57, n58, n59, n6, n60, n61, n61a, n62, n62a,
##     n63, n63a, n64, n64a, n65, n65a, n66, n66a, n67, n67a, n68,
##     n68a, n7, n77, n78, n7tren, n8, n88, n89, n8nha, n9, n97, n98,
##     n99, n9khach, noEnvi2, occup1, policy, policy_a, policy2,
##     reasons, reasons1, s1, Screening, selfhealth, SH1, smostt,
##     ter_fa1, ter_in, tertile_fa, tertile_indi, test, unitsdiffi1,
##     w1, w2, w3, w4, wtp
## The following objects are masked from r1 (pos = 15):
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, c1, c2, c23a, c3, c4, c5, c6, c7,
##     c8, c9, COST, cost1, costincrease, ct, d1, d10, d11, d1a, d2,
##     d3, d3a, d4, d5, d6, d7, d8, d9, Decision, e1, e2, edu, educ1,
##     educ2, f1, ghi1, ghi2, ghiro2, giadinhkoUH, group_age,
##     group_age1, h1, h10a, h10a1, h10a10, h10a11, h10a11a, h10a2,
##     h10a3, h10a4, h10a5, h10a6, h10a7, h10a8, h10a9, h12, h12a,
##     h12a_1, h12log, h13, h2, h3, h4, h5, h6, h7, h8, h9, ha000,
##     itc1, itc2, l1, l2, l3, l4, l5, l6, label1, label2,
##     moneyspent, msdt, n01, n02, n03, n05, n06, n07, n08, n1, n10,
##     n100, n101, n102, n103, n11, n12, n13, n14, n15, n16, n1b, n2,
##     n3, n35, n36, n37, n38, n39, n3posterb, n4, n40, n41, n42,
##     n43, n44, n45, n46, n47, n48, n49, n5, n50, n51, n52, n53,
##     n54, n55, n56, n57, n58, n59, n6, n60, n61, n61a, n62, n62a,
##     n63, n63a, n64, n64a, n65, n65a, n66, n66a, n67, n67a, n68,
##     n68a, n7, n77, n78, n7tren, n8, n88, n89, n8nha, n9, n97, n98,
##     n99, n9khach, noEnvi2, occup1, policy, policy_a, policy2,
##     reasons, reasons1, s1, Screening, selfhealth, SH1, smostt,
##     ter_fa1, ter_in, tertile_fa, tertile_indi, test, unitsdiffi1,
##     w1, w2, w3, w4, wtp
## The following objects are masked from r1 (pos = 16):
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, c1, c2, c23a, c3, c4, c5, c6, c7,
##     c8, c9, COST, cost1, costincrease, ct, d1, d10, d11, d1a, d2,
##     d3, d3a, d4, d5, d6, d7, d8, d9, Decision, e1, e2, edu, educ1,
##     educ2, f1, ghi1, ghi2, ghiro2, giadinhkoUH, group_age,
##     group_age1, h1, h10a, h10a1, h10a10, h10a11, h10a11a, h10a2,
##     h10a3, h10a4, h10a5, h10a6, h10a7, h10a8, h10a9, h12, h12a,
##     h12a_1, h12log, h13, h2, h3, h4, h5, h6, h7, h8, h9, ha000,
##     itc1, itc2, l1, l2, l3, l4, l5, l6, label1, label2,
##     moneyspent, msdt, n01, n02, n03, n05, n06, n07, n08, n1, n10,
##     n100, n101, n102, n103, n11, n12, n13, n14, n15, n16, n1b, n2,
##     n3, n35, n36, n37, n38, n39, n3posterb, n4, n40, n41, n42,
##     n43, n44, n45, n46, n47, n48, n49, n5, n50, n51, n52, n53,
##     n54, n55, n56, n57, n58, n59, n6, n60, n61, n61a, n62, n62a,
##     n63, n63a, n64, n64a, n65, n65a, n66, n66a, n67, n67a, n68,
##     n68a, n7, n77, n78, n7tren, n8, n88, n89, n8nha, n9, n97, n98,
##     n99, n9khach, noEnvi2, occup1, policy, policy_a, policy2,
##     reasons, reasons1, s1, Screening, selfhealth, SH1, smostt,
##     ter_fa1, ter_in, tertile_fa, tertile_indi, test, unitsdiffi1,
##     w1, w2, w3, w4

define varibles

newdata2$cost1=as.factor(newdata2$cost1)
newdata2$c1=as.factor(newdata2$c1)
newdata2$h1=as.factor(newdata2$h1)
newdata2$label1=as.factor(newdata2$label1)
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)

re-organise our data by sampling .

newdata2=newdata2[sample(1:nrow(newdata2), 380, replace=F),]

Create new data frame (90%-10%) (N=822)

Train data

train=newdata2[1:342, ]
View(train)

Test data

test=newdata2[343:380, ]
View(test)

Install packages and library

library("brms")
library("caret")
library("coda")

library("rstan", lib.loc="~/R/win-library/3.2")
options(mc.cores = parallel::detectCores())

set up for fomulation

prior=get_prior(formula=wtp~h1+ d1a+educ2+ter_in+selfhealth+smostt
               +b16a+b18a+label1+anticam+noEnvi2, family="bernoulli", data=train)
## Warning: Rows containing NAs were excluded from the model
set.seed(1234) 
 
baybin=brm(formula=wtp~h1+ d1a+educ2+ter_in+selfhealth+smostt
               +b16a+b18a+label1+anticam+noEnvi2, family="bernoulli", data=train, chains=5, iter=2000, warmup=1000, prior=prior)
## Warning: Rows containing NAs were excluded from the model
## Compiling the C++ model
## Start sampling

Summary model using train data - this is main results for model

summary(baybin)
##  Family: bernoulli (logit) 
## Formula: wtp ~ h1 + d1a + educ2 + ter_in + selfhealth + smostt + b16a + b18a + label1 + anticam + noEnvi2 
##    Data: train (Number of observations: 329) 
## Samples: 5 chains, each with iter = 2000; warmup = 1000; thin = 1; 
##          total post-warmup samples = 5000
##    WAIC: Not computed
##  
## Population-Level Effects: 
##                Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
## Intercept          0.66      0.61    -0.55     1.87       5000    1
## h12                0.31      0.26    -0.21     0.81       5000    1
## d1amarried        -0.37      0.44    -1.25     0.52       5000    1
## educ22            -0.22      0.27    -0.75     0.31       5000    1
## educ23             0.64      0.42    -0.18     1.46       5000    1
## ter_in2            0.32      0.31    -0.27     0.93       5000    1
## ter_in3            0.35      0.29    -0.25     0.93       5000    1
## selfhealthgood     0.22      0.25    -0.26     0.72       5000    1
## smosttmedium      -0.29      0.35    -0.99     0.39       5000    1
## smosttheavy        0.13      0.34    -0.53     0.78       5000    1
## b16a1             -0.92      0.30    -1.49    -0.33       5000    1
## b18abad           -0.42      0.27    -0.96     0.09       5000    1
## label11            0.42      0.35    -0.25     1.11       5000    1
## anticam            0.29      0.33    -0.34     0.94       5000    1
## noEnvi2           -0.01      0.33    -0.66     0.65       5000    1
## 
## Samples were drawn using sampling(NUTS). For each parameter, Eff.Sample 
## is a crude measure of effective sample size, and Rhat is the potential 
## scale reduction factor on split chains (at convergence, Rhat = 1).
WAIC(baybin)
## Warning: 1 (0.3%) p_waic estimates greater than 0.4.
## We recommend trying loo() instead.
##    WAIC    SE
##  449.81 13.62

Notes: If you wanna report OR and 95%CI, you might need the exp funtion (using in R or in excel)

fit model

baybin$fit
## Inference for Stan model: bernoulli(logit) brms-model.
## 5 chains, each with iter=2000; warmup=1000; thin=1; 
## post-warmup draws per chain=1000, total post-warmup draws=5000.
## 
##                     mean se_mean   sd    2.5%     25%     50%     75%
## b_Intercept         0.66    0.01 0.61   -0.55    0.26    0.66    1.06
## b_h12               0.31    0.00 0.26   -0.21    0.14    0.32    0.48
## b_d1amarried       -0.37    0.01 0.44   -1.25   -0.65   -0.36   -0.09
## b_educ22           -0.22    0.00 0.27   -0.75   -0.39   -0.22   -0.03
## b_educ23            0.64    0.01 0.42   -0.18    0.36    0.64    0.92
## b_ter_in2           0.32    0.00 0.31   -0.27    0.11    0.32    0.53
## b_ter_in3           0.35    0.00 0.29   -0.25    0.16    0.35    0.54
## b_selfhealthgood    0.22    0.00 0.25   -0.26    0.05    0.22    0.40
## b_smosttmedium     -0.29    0.01 0.35   -0.99   -0.53   -0.29   -0.05
## b_smosttheavy       0.13    0.00 0.34   -0.53   -0.10    0.14    0.36
## b_b16a1            -0.92    0.00 0.30   -1.49   -1.11   -0.92   -0.71
## b_b18abad          -0.42    0.00 0.27   -0.96   -0.60   -0.42   -0.25
## b_label11           0.42    0.00 0.35   -0.25    0.19    0.42    0.66
## b_anticam           0.29    0.00 0.33   -0.34    0.07    0.29    0.51
## b_noEnvi2          -0.01    0.00 0.33   -0.66   -0.24   -0.02    0.20
## lp__             -216.00    0.06 2.84 -222.12 -217.68 -215.66 -213.93
##                    97.5% n_eff Rhat
## b_Intercept         1.87  5000    1
## b_h12               0.81  5000    1
## b_d1amarried        0.52  5000    1
## b_educ22            0.31  5000    1
## b_educ23            1.46  5000    1
## b_ter_in2           0.93  5000    1
## b_ter_in3           0.93  5000    1
## b_selfhealthgood    0.72  5000    1
## b_smosttmedium      0.39  5000    1
## b_smosttheavy       0.78  5000    1
## b_b16a1            -0.33  5000    1
## b_b18abad           0.09  5000    1
## b_label11           1.11  5000    1
## b_anticam           0.94  5000    1
## b_noEnvi2           0.65  5000    1
## lp__             -211.43  2099    1
## 
## Samples were drawn using NUTS(diag_e) at Wed Mar 22 00:18:52 2017.
## For each parameter, n_eff is a crude measure of effective sample size,
## and Rhat is the potential scale reduction factor on split chains (at 
## convergence, Rhat=1).

next steps, ww will check the accuracy of model using ROC

establish command using TEST data

prob=as.data.frame(predict(baybin,test,type="response"))

library(e1071)
pred=ifelse(prob$Estimate> 0.5, 1, 0)

Results

confusionMatrix(data=pred,reference=test$wtp)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 12 10
##          1  5 11
##                                           
##                Accuracy : 0.6053          
##                  95% CI : (0.4339, 0.7596)
##     No Information Rate : 0.5526          
##     P-Value [Acc > NIR] : 0.3142          
##                                           
##                   Kappa : 0.2234          
##  Mcnemar's Test P-Value : 0.3017          
##                                           
##             Sensitivity : 0.7059          
##             Specificity : 0.5238          
##          Pos Pred Value : 0.5455          
##          Neg Pred Value : 0.6875          
##              Prevalence : 0.4474          
##          Detection Rate : 0.3158          
##    Detection Prevalence : 0.5789          
##       Balanced Accuracy : 0.6148          
##                                           
##        'Positive' Class : 0               
## 

model for age<40

library(foreign)
r=read.dta("C:/Users/BINH THANG/Dropbox/Korea/STudy/Thesis/data management/DataR/dataR2.dta")

r1 <- subset(r, group_age1==0)

attach(r1)
## The following object is masked _by_ .GlobalEnv:
## 
##     test
## The following objects are masked from newdata2 (pos = 3):
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, c1, c2, c23a, c3, c4, c5, c6, c7,
##     c8, c9, COST, cost1, cost2, costincrease, ct, d1, d10, d11,
##     d1a, d2, d3, d3a, d4, d5, d6, d7, d8, d9, Decision, e1, e2,
##     edu, educ1, educ2, f1, ghi1, ghi2, ghiro2, giadinhkoUH,
##     group_age, group_age1, h1, h10a, h10a1, h10a10, h10a11,
##     h10a11a, h10a2, h10a3, h10a4, h10a5, h10a6, h10a7, h10a8,
##     h10a9, h12, h12a, h12a_1, h12log, h13, h2, h3, h4, h5, h6, h7,
##     h8, h9, ha000, itc1, itc2, l1, l2, l3, l4, l5, l6, label1,
##     label2, moneyspent, msdt, n01, n02, n03, n05, n06, n07, n08,
##     n1, n10, n100, n101, n102, n103, n11, n12, n13, n14, n15, n16,
##     n1b, n2, n3, n35, n36, n37, n38, n39, n3posterb, n4, n40, n41,
##     n42, n43, n44, n45, n46, n47, n48, n49, n5, n50, n51, n52,
##     n53, n54, n55, n56, n57, n58, n59, n6, n60, n61, n61a, n62,
##     n62a, n63, n63a, n64, n64a, n65, n65a, n66, n66a, n67, n67a,
##     n68, n68a, n7, n77, n78, n7tren, n8, n88, n89, n8nha, n9, n97,
##     n98, n99, n9khach, noEnvi2, occup1, policy, policy_a, policy2,
##     reasons, reasons1, s1, Screening, selfhealth, SH1, smostt,
##     ter_fa1, ter_in, tertile_fa, tertile_indi, test, unitsdiffi1,
##     w1, w2, w3, w4
## The following objects are masked from r1 (pos = 4):
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, c1, c2, c23a, c3, c4, c5, c6, c7,
##     c8, c9, COST, cost1, cost2, costincrease, ct, d1, d10, d11,
##     d1a, d2, d3, d3a, d4, d5, d6, d7, d8, d9, Decision, e1, e2,
##     edu, educ1, educ2, f1, ghi1, ghi2, ghiro2, giadinhkoUH,
##     group_age, group_age1, h1, h10a, h10a1, h10a10, h10a11,
##     h10a11a, h10a2, h10a3, h10a4, h10a5, h10a6, h10a7, h10a8,
##     h10a9, h12, h12a, h12a_1, h12log, h13, h2, h3, h4, h5, h6, h7,
##     h8, h9, ha000, itc1, itc2, l1, l2, l3, l4, l5, l6, label1,
##     label2, moneyspent, msdt, n01, n02, n03, n05, n06, n07, n08,
##     n1, n10, n100, n101, n102, n103, n11, n12, n13, n14, n15, n16,
##     n1b, n2, n3, n35, n36, n37, n38, n39, n3posterb, n4, n40, n41,
##     n42, n43, n44, n45, n46, n47, n48, n49, n5, n50, n51, n52,
##     n53, n54, n55, n56, n57, n58, n59, n6, n60, n61, n61a, n62,
##     n62a, n63, n63a, n64, n64a, n65, n65a, n66, n66a, n67, n67a,
##     n68, n68a, n7, n77, n78, n7tren, n8, n88, n89, n8nha, n9, n97,
##     n98, n99, n9khach, noEnvi2, occup1, policy, policy_a, policy2,
##     reasons, reasons1, s1, Screening, selfhealth, SH1, smostt,
##     ter_fa1, ter_in, tertile_fa, tertile_indi, test, unitsdiffi1,
##     w1, w2, w3, w4
## The following objects are masked from r1 (pos = 5):
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, c1, c2, c23a, c3, c4, c5, c6, c7,
##     c8, c9, COST, cost1, cost2, costincrease, ct, d1, d10, d11,
##     d1a, d2, d3, d3a, d4, d5, d6, d7, d8, d9, Decision, e1, e2,
##     edu, educ1, educ2, f1, ghi1, ghi2, ghiro2, giadinhkoUH,
##     group_age, group_age1, h1, h10a, h10a1, h10a10, h10a11,
##     h10a11a, h10a2, h10a3, h10a4, h10a5, h10a6, h10a7, h10a8,
##     h10a9, h12, h12a, h12a_1, h12log, h13, h2, h3, h4, h5, h6, h7,
##     h8, h9, ha000, itc1, itc2, l1, l2, l3, l4, l5, l6, label1,
##     label2, moneyspent, msdt, n01, n02, n03, n05, n06, n07, n08,
##     n1, n10, n100, n101, n102, n103, n11, n12, n13, n14, n15, n16,
##     n1b, n2, n3, n35, n36, n37, n38, n39, n3posterb, n4, n40, n41,
##     n42, n43, n44, n45, n46, n47, n48, n49, n5, n50, n51, n52,
##     n53, n54, n55, n56, n57, n58, n59, n6, n60, n61, n61a, n62,
##     n62a, n63, n63a, n64, n64a, n65, n65a, n66, n66a, n67, n67a,
##     n68, n68a, n7, n77, n78, n7tren, n8, n88, n89, n8nha, n9, n97,
##     n98, n99, n9khach, noEnvi2, occup1, policy, policy_a, policy2,
##     reasons, reasons1, s1, Screening, selfhealth, SH1, smostt,
##     ter_fa1, ter_in, tertile_fa, tertile_indi, test, unitsdiffi1,
##     w1, w2, w3, w4
## The following objects are masked from newdata2 (pos = 15):
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, c1, c2, c23a, c3, c4, c5, c6, c7,
##     c8, c9, COST, cost1, costincrease, ct, d1, d10, d11, d1a, d2,
##     d3, d3a, d4, d5, d6, d7, d8, d9, Decision, e1, e2, edu, educ1,
##     educ2, f1, ghi1, ghi2, ghiro2, giadinhkoUH, group_age,
##     group_age1, h1, h10a, h10a1, h10a10, h10a11, h10a11a, h10a2,
##     h10a3, h10a4, h10a5, h10a6, h10a7, h10a8, h10a9, h12, h12a,
##     h12a_1, h12log, h13, h2, h3, h4, h5, h6, h7, h8, h9, ha000,
##     itc1, itc2, l1, l2, l3, l4, l5, l6, label1, label2,
##     moneyspent, msdt, n01, n02, n03, n05, n06, n07, n08, n1, n10,
##     n100, n101, n102, n103, n11, n12, n13, n14, n15, n16, n1b, n2,
##     n3, n35, n36, n37, n38, n39, n3posterb, n4, n40, n41, n42,
##     n43, n44, n45, n46, n47, n48, n49, n5, n50, n51, n52, n53,
##     n54, n55, n56, n57, n58, n59, n6, n60, n61, n61a, n62, n62a,
##     n63, n63a, n64, n64a, n65, n65a, n66, n66a, n67, n67a, n68,
##     n68a, n7, n77, n78, n7tren, n8, n88, n89, n8nha, n9, n97, n98,
##     n99, n9khach, noEnvi2, occup1, policy, policy_a, policy2,
##     reasons, reasons1, s1, Screening, selfhealth, SH1, smostt,
##     ter_fa1, ter_in, tertile_fa, tertile_indi, test, unitsdiffi1,
##     w1, w2, w3, w4
## The following objects are masked from r1 (pos = 16):
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, c1, c2, c23a, c3, c4, c5, c6, c7,
##     c8, c9, COST, cost1, costincrease, ct, d1, d10, d11, d1a, d2,
##     d3, d3a, d4, d5, d6, d7, d8, d9, Decision, e1, e2, edu, educ1,
##     educ2, f1, ghi1, ghi2, ghiro2, giadinhkoUH, group_age,
##     group_age1, h1, h10a, h10a1, h10a10, h10a11, h10a11a, h10a2,
##     h10a3, h10a4, h10a5, h10a6, h10a7, h10a8, h10a9, h12, h12a,
##     h12a_1, h12log, h13, h2, h3, h4, h5, h6, h7, h8, h9, ha000,
##     itc1, itc2, l1, l2, l3, l4, l5, l6, label1, label2,
##     moneyspent, msdt, n01, n02, n03, n05, n06, n07, n08, n1, n10,
##     n100, n101, n102, n103, n11, n12, n13, n14, n15, n16, n1b, n2,
##     n3, n35, n36, n37, n38, n39, n3posterb, n4, n40, n41, n42,
##     n43, n44, n45, n46, n47, n48, n49, n5, n50, n51, n52, n53,
##     n54, n55, n56, n57, n58, n59, n6, n60, n61, n61a, n62, n62a,
##     n63, n63a, n64, n64a, n65, n65a, n66, n66a, n67, n67a, n68,
##     n68a, n7, n77, n78, n7tren, n8, n88, n89, n8nha, n9, n97, n98,
##     n99, n9khach, noEnvi2, occup1, policy, policy_a, policy2,
##     reasons, reasons1, s1, Screening, selfhealth, SH1, smostt,
##     ter_fa1, ter_in, tertile_fa, tertile_indi, test, unitsdiffi1,
##     w1, w2, w3, w4
## The following objects are masked from r1 (pos = 17):
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, c1, c2, c23a, c3, c4, c5, c6, c7,
##     c8, c9, COST, cost1, costincrease, ct, d1, d10, d11, d1a, d2,
##     d3, d3a, d4, d5, d6, d7, d8, d9, Decision, e1, e2, edu, educ1,
##     educ2, f1, ghi1, ghi2, ghiro2, giadinhkoUH, group_age,
##     group_age1, h1, h10a, h10a1, h10a10, h10a11, h10a11a, h10a2,
##     h10a3, h10a4, h10a5, h10a6, h10a7, h10a8, h10a9, h12, h12a,
##     h12a_1, h12log, h13, h2, h3, h4, h5, h6, h7, h8, h9, ha000,
##     itc1, itc2, l1, l2, l3, l4, l5, l6, label1, label2,
##     moneyspent, msdt, n01, n02, n03, n05, n06, n07, n08, n1, n10,
##     n100, n101, n102, n103, n11, n12, n13, n14, n15, n16, n1b, n2,
##     n3, n35, n36, n37, n38, n39, n3posterb, n4, n40, n41, n42,
##     n43, n44, n45, n46, n47, n48, n49, n5, n50, n51, n52, n53,
##     n54, n55, n56, n57, n58, n59, n6, n60, n61, n61a, n62, n62a,
##     n63, n63a, n64, n64a, n65, n65a, n66, n66a, n67, n67a, n68,
##     n68a, n7, n77, n78, n7tren, n8, n88, n89, n8nha, n9, n97, n98,
##     n99, n9khach, noEnvi2, occup1, policy, policy_a, policy2,
##     reasons, reasons1, s1, Screening, selfhealth, SH1, smostt,
##     ter_fa1, ter_in, tertile_fa, tertile_indi, test, unitsdiffi1,
##     w1, w2, w3, w4
r1$wtp[cost1 == 0] <- 0
r1$wtp[cost1 == 1] <- 0

r1$wtp[is.na(r1$wtp)] <- 1


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


attach(r1)
## The following object is masked _by_ .GlobalEnv:
## 
##     test
## The following objects are masked from r1 (pos = 3):
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, c1, c2, c23a, c3, c4, c5, c6, c7,
##     c8, c9, COST, cost1, cost2, costincrease, ct, d1, d10, d11,
##     d1a, d2, d3, d3a, d4, d5, d6, d7, d8, d9, Decision, e1, e2,
##     edu, educ1, educ2, f1, ghi1, ghi2, ghiro2, giadinhkoUH,
##     group_age, group_age1, h1, h10a, h10a1, h10a10, h10a11,
##     h10a11a, h10a2, h10a3, h10a4, h10a5, h10a6, h10a7, h10a8,
##     h10a9, h12, h12a, h12a_1, h12log, h13, h2, h3, h4, h5, h6, h7,
##     h8, h9, ha000, itc1, itc2, l1, l2, l3, l4, l5, l6, label1,
##     label2, moneyspent, msdt, n01, n02, n03, n05, n06, n07, n08,
##     n1, n10, n100, n101, n102, n103, n11, n12, n13, n14, n15, n16,
##     n1b, n2, n3, n35, n36, n37, n38, n39, n3posterb, n4, n40, n41,
##     n42, n43, n44, n45, n46, n47, n48, n49, n5, n50, n51, n52,
##     n53, n54, n55, n56, n57, n58, n59, n6, n60, n61, n61a, n62,
##     n62a, n63, n63a, n64, n64a, n65, n65a, n66, n66a, n67, n67a,
##     n68, n68a, n7, n77, n78, n7tren, n8, n88, n89, n8nha, n9, n97,
##     n98, n99, n9khach, noEnvi2, occup1, policy, policy_a, policy2,
##     reasons, reasons1, s1, Screening, selfhealth, SH1, smostt,
##     ter_fa1, ter_in, tertile_fa, tertile_indi, test, unitsdiffi1,
##     w1, w2, w3, w4
## The following objects are masked from newdata2 (pos = 4):
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, c1, c2, c23a, c3, c4, c5, c6, c7,
##     c8, c9, COST, cost1, cost2, costincrease, ct, d1, d10, d11,
##     d1a, d2, d3, d3a, d4, d5, d6, d7, d8, d9, Decision, e1, e2,
##     edu, educ1, educ2, f1, ghi1, ghi2, ghiro2, giadinhkoUH,
##     group_age, group_age1, h1, h10a, h10a1, h10a10, h10a11,
##     h10a11a, h10a2, h10a3, h10a4, h10a5, h10a6, h10a7, h10a8,
##     h10a9, h12, h12a, h12a_1, h12log, h13, h2, h3, h4, h5, h6, h7,
##     h8, h9, ha000, itc1, itc2, l1, l2, l3, l4, l5, l6, label1,
##     label2, moneyspent, msdt, n01, n02, n03, n05, n06, n07, n08,
##     n1, n10, n100, n101, n102, n103, n11, n12, n13, n14, n15, n16,
##     n1b, n2, n3, n35, n36, n37, n38, n39, n3posterb, n4, n40, n41,
##     n42, n43, n44, n45, n46, n47, n48, n49, n5, n50, n51, n52,
##     n53, n54, n55, n56, n57, n58, n59, n6, n60, n61, n61a, n62,
##     n62a, n63, n63a, n64, n64a, n65, n65a, n66, n66a, n67, n67a,
##     n68, n68a, n7, n77, n78, n7tren, n8, n88, n89, n8nha, n9, n97,
##     n98, n99, n9khach, noEnvi2, occup1, policy, policy_a, policy2,
##     reasons, reasons1, s1, Screening, selfhealth, SH1, smostt,
##     ter_fa1, ter_in, tertile_fa, tertile_indi, test, unitsdiffi1,
##     w1, w2, w3, w4, wtp
## The following objects are masked from r1 (pos = 5):
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, c1, c2, c23a, c3, c4, c5, c6, c7,
##     c8, c9, COST, cost1, cost2, costincrease, ct, d1, d10, d11,
##     d1a, d2, d3, d3a, d4, d5, d6, d7, d8, d9, Decision, e1, e2,
##     edu, educ1, educ2, f1, ghi1, ghi2, ghiro2, giadinhkoUH,
##     group_age, group_age1, h1, h10a, h10a1, h10a10, h10a11,
##     h10a11a, h10a2, h10a3, h10a4, h10a5, h10a6, h10a7, h10a8,
##     h10a9, h12, h12a, h12a_1, h12log, h13, h2, h3, h4, h5, h6, h7,
##     h8, h9, ha000, itc1, itc2, l1, l2, l3, l4, l5, l6, label1,
##     label2, moneyspent, msdt, n01, n02, n03, n05, n06, n07, n08,
##     n1, n10, n100, n101, n102, n103, n11, n12, n13, n14, n15, n16,
##     n1b, n2, n3, n35, n36, n37, n38, n39, n3posterb, n4, n40, n41,
##     n42, n43, n44, n45, n46, n47, n48, n49, n5, n50, n51, n52,
##     n53, n54, n55, n56, n57, n58, n59, n6, n60, n61, n61a, n62,
##     n62a, n63, n63a, n64, n64a, n65, n65a, n66, n66a, n67, n67a,
##     n68, n68a, n7, n77, n78, n7tren, n8, n88, n89, n8nha, n9, n97,
##     n98, n99, n9khach, noEnvi2, occup1, policy, policy_a, policy2,
##     reasons, reasons1, s1, Screening, selfhealth, SH1, smostt,
##     ter_fa1, ter_in, tertile_fa, tertile_indi, test, unitsdiffi1,
##     w1, w2, w3, w4, wtp
## The following objects are masked from r1 (pos = 6):
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, c1, c2, c23a, c3, c4, c5, c6, c7,
##     c8, c9, COST, cost1, cost2, costincrease, ct, d1, d10, d11,
##     d1a, d2, d3, d3a, d4, d5, d6, d7, d8, d9, Decision, e1, e2,
##     edu, educ1, educ2, f1, ghi1, ghi2, ghiro2, giadinhkoUH,
##     group_age, group_age1, h1, h10a, h10a1, h10a10, h10a11,
##     h10a11a, h10a2, h10a3, h10a4, h10a5, h10a6, h10a7, h10a8,
##     h10a9, h12, h12a, h12a_1, h12log, h13, h2, h3, h4, h5, h6, h7,
##     h8, h9, ha000, itc1, itc2, l1, l2, l3, l4, l5, l6, label1,
##     label2, moneyspent, msdt, n01, n02, n03, n05, n06, n07, n08,
##     n1, n10, n100, n101, n102, n103, n11, n12, n13, n14, n15, n16,
##     n1b, n2, n3, n35, n36, n37, n38, n39, n3posterb, n4, n40, n41,
##     n42, n43, n44, n45, n46, n47, n48, n49, n5, n50, n51, n52,
##     n53, n54, n55, n56, n57, n58, n59, n6, n60, n61, n61a, n62,
##     n62a, n63, n63a, n64, n64a, n65, n65a, n66, n66a, n67, n67a,
##     n68, n68a, n7, n77, n78, n7tren, n8, n88, n89, n8nha, n9, n97,
##     n98, n99, n9khach, noEnvi2, occup1, policy, policy_a, policy2,
##     reasons, reasons1, s1, Screening, selfhealth, SH1, smostt,
##     ter_fa1, ter_in, tertile_fa, tertile_indi, test, unitsdiffi1,
##     w1, w2, w3, w4
## The following objects are masked from newdata2 (pos = 16):
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, c1, c2, c23a, c3, c4, c5, c6, c7,
##     c8, c9, COST, cost1, costincrease, ct, d1, d10, d11, d1a, d2,
##     d3, d3a, d4, d5, d6, d7, d8, d9, Decision, e1, e2, edu, educ1,
##     educ2, f1, ghi1, ghi2, ghiro2, giadinhkoUH, group_age,
##     group_age1, h1, h10a, h10a1, h10a10, h10a11, h10a11a, h10a2,
##     h10a3, h10a4, h10a5, h10a6, h10a7, h10a8, h10a9, h12, h12a,
##     h12a_1, h12log, h13, h2, h3, h4, h5, h6, h7, h8, h9, ha000,
##     itc1, itc2, l1, l2, l3, l4, l5, l6, label1, label2,
##     moneyspent, msdt, n01, n02, n03, n05, n06, n07, n08, n1, n10,
##     n100, n101, n102, n103, n11, n12, n13, n14, n15, n16, n1b, n2,
##     n3, n35, n36, n37, n38, n39, n3posterb, n4, n40, n41, n42,
##     n43, n44, n45, n46, n47, n48, n49, n5, n50, n51, n52, n53,
##     n54, n55, n56, n57, n58, n59, n6, n60, n61, n61a, n62, n62a,
##     n63, n63a, n64, n64a, n65, n65a, n66, n66a, n67, n67a, n68,
##     n68a, n7, n77, n78, n7tren, n8, n88, n89, n8nha, n9, n97, n98,
##     n99, n9khach, noEnvi2, occup1, policy, policy_a, policy2,
##     reasons, reasons1, s1, Screening, selfhealth, SH1, smostt,
##     ter_fa1, ter_in, tertile_fa, tertile_indi, test, unitsdiffi1,
##     w1, w2, w3, w4, wtp
## The following objects are masked from r1 (pos = 17):
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, c1, c2, c23a, c3, c4, c5, c6, c7,
##     c8, c9, COST, cost1, costincrease, ct, d1, d10, d11, d1a, d2,
##     d3, d3a, d4, d5, d6, d7, d8, d9, Decision, e1, e2, edu, educ1,
##     educ2, f1, ghi1, ghi2, ghiro2, giadinhkoUH, group_age,
##     group_age1, h1, h10a, h10a1, h10a10, h10a11, h10a11a, h10a2,
##     h10a3, h10a4, h10a5, h10a6, h10a7, h10a8, h10a9, h12, h12a,
##     h12a_1, h12log, h13, h2, h3, h4, h5, h6, h7, h8, h9, ha000,
##     itc1, itc2, l1, l2, l3, l4, l5, l6, label1, label2,
##     moneyspent, msdt, n01, n02, n03, n05, n06, n07, n08, n1, n10,
##     n100, n101, n102, n103, n11, n12, n13, n14, n15, n16, n1b, n2,
##     n3, n35, n36, n37, n38, n39, n3posterb, n4, n40, n41, n42,
##     n43, n44, n45, n46, n47, n48, n49, n5, n50, n51, n52, n53,
##     n54, n55, n56, n57, n58, n59, n6, n60, n61, n61a, n62, n62a,
##     n63, n63a, n64, n64a, n65, n65a, n66, n66a, n67, n67a, n68,
##     n68a, n7, n77, n78, n7tren, n8, n88, n89, n8nha, n9, n97, n98,
##     n99, n9khach, noEnvi2, occup1, policy, policy_a, policy2,
##     reasons, reasons1, s1, Screening, selfhealth, SH1, smostt,
##     ter_fa1, ter_in, tertile_fa, tertile_indi, test, unitsdiffi1,
##     w1, w2, w3, w4, wtp
## The following objects are masked from r1 (pos = 18):
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, c1, c2, c23a, c3, c4, c5, c6, c7,
##     c8, c9, COST, cost1, costincrease, ct, d1, d10, d11, d1a, d2,
##     d3, d3a, d4, d5, d6, d7, d8, d9, Decision, e1, e2, edu, educ1,
##     educ2, f1, ghi1, ghi2, ghiro2, giadinhkoUH, group_age,
##     group_age1, h1, h10a, h10a1, h10a10, h10a11, h10a11a, h10a2,
##     h10a3, h10a4, h10a5, h10a6, h10a7, h10a8, h10a9, h12, h12a,
##     h12a_1, h12log, h13, h2, h3, h4, h5, h6, h7, h8, h9, ha000,
##     itc1, itc2, l1, l2, l3, l4, l5, l6, label1, label2,
##     moneyspent, msdt, n01, n02, n03, n05, n06, n07, n08, n1, n10,
##     n100, n101, n102, n103, n11, n12, n13, n14, n15, n16, n1b, n2,
##     n3, n35, n36, n37, n38, n39, n3posterb, n4, n40, n41, n42,
##     n43, n44, n45, n46, n47, n48, n49, n5, n50, n51, n52, n53,
##     n54, n55, n56, n57, n58, n59, n6, n60, n61, n61a, n62, n62a,
##     n63, n63a, n64, n64a, n65, n65a, n66, n66a, n67, n67a, n68,
##     n68a, n7, n77, n78, n7tren, n8, n88, n89, n8nha, n9, n97, n98,
##     n99, n9khach, noEnvi2, occup1, policy, policy_a, policy2,
##     reasons, reasons1, s1, Screening, selfhealth, SH1, smostt,
##     ter_fa1, ter_in, tertile_fa, tertile_indi, test, unitsdiffi1,
##     w1, w2, w3, w4
mytable <- table(wtp, c1)
print(mytable)
##    c1
## wtp   1   2
##   0 218  49
##   1 148  27
prop.table(mytable)
##    c1
## wtp          1          2
##   0 0.49321267 0.11085973
##   1 0.33484163 0.06108597
newdata2=r1

attach(newdata2 )
## The following object is masked _by_ .GlobalEnv:
## 
##     test
## The following objects are masked from r1 (pos = 3):
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, c1, c2, c23a, c3, c4, c5, c6, c7,
##     c8, c9, COST, cost1, cost2, costincrease, ct, d1, d10, d11,
##     d1a, d2, d3, d3a, d4, d5, d6, d7, d8, d9, Decision, e1, e2,
##     edu, educ1, educ2, f1, ghi1, ghi2, ghiro2, giadinhkoUH,
##     group_age, group_age1, h1, h10a, h10a1, h10a10, h10a11,
##     h10a11a, h10a2, h10a3, h10a4, h10a5, h10a6, h10a7, h10a8,
##     h10a9, h12, h12a, h12a_1, h12log, h13, h2, h3, h4, h5, h6, h7,
##     h8, h9, ha000, itc1, itc2, l1, l2, l3, l4, l5, l6, label1,
##     label2, moneyspent, msdt, n01, n02, n03, n05, n06, n07, n08,
##     n1, n10, n100, n101, n102, n103, n11, n12, n13, n14, n15, n16,
##     n1b, n2, n3, n35, n36, n37, n38, n39, n3posterb, n4, n40, n41,
##     n42, n43, n44, n45, n46, n47, n48, n49, n5, n50, n51, n52,
##     n53, n54, n55, n56, n57, n58, n59, n6, n60, n61, n61a, n62,
##     n62a, n63, n63a, n64, n64a, n65, n65a, n66, n66a, n67, n67a,
##     n68, n68a, n7, n77, n78, n7tren, n8, n88, n89, n8nha, n9, n97,
##     n98, n99, n9khach, noEnvi2, occup1, policy, policy_a, policy2,
##     reasons, reasons1, s1, Screening, selfhealth, SH1, smostt,
##     ter_fa1, ter_in, tertile_fa, tertile_indi, test, unitsdiffi1,
##     w1, w2, w3, w4, wtp
## The following objects are masked from r1 (pos = 4):
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, c1, c2, c23a, c3, c4, c5, c6, c7,
##     c8, c9, COST, cost1, cost2, costincrease, ct, d1, d10, d11,
##     d1a, d2, d3, d3a, d4, d5, d6, d7, d8, d9, Decision, e1, e2,
##     edu, educ1, educ2, f1, ghi1, ghi2, ghiro2, giadinhkoUH,
##     group_age, group_age1, h1, h10a, h10a1, h10a10, h10a11,
##     h10a11a, h10a2, h10a3, h10a4, h10a5, h10a6, h10a7, h10a8,
##     h10a9, h12, h12a, h12a_1, h12log, h13, h2, h3, h4, h5, h6, h7,
##     h8, h9, ha000, itc1, itc2, l1, l2, l3, l4, l5, l6, label1,
##     label2, moneyspent, msdt, n01, n02, n03, n05, n06, n07, n08,
##     n1, n10, n100, n101, n102, n103, n11, n12, n13, n14, n15, n16,
##     n1b, n2, n3, n35, n36, n37, n38, n39, n3posterb, n4, n40, n41,
##     n42, n43, n44, n45, n46, n47, n48, n49, n5, n50, n51, n52,
##     n53, n54, n55, n56, n57, n58, n59, n6, n60, n61, n61a, n62,
##     n62a, n63, n63a, n64, n64a, n65, n65a, n66, n66a, n67, n67a,
##     n68, n68a, n7, n77, n78, n7tren, n8, n88, n89, n8nha, n9, n97,
##     n98, n99, n9khach, noEnvi2, occup1, policy, policy_a, policy2,
##     reasons, reasons1, s1, Screening, selfhealth, SH1, smostt,
##     ter_fa1, ter_in, tertile_fa, tertile_indi, test, unitsdiffi1,
##     w1, w2, w3, w4
## The following objects are masked from newdata2 (pos = 5):
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, c1, c2, c23a, c3, c4, c5, c6, c7,
##     c8, c9, COST, cost1, cost2, costincrease, ct, d1, d10, d11,
##     d1a, d2, d3, d3a, d4, d5, d6, d7, d8, d9, Decision, e1, e2,
##     edu, educ1, educ2, f1, ghi1, ghi2, ghiro2, giadinhkoUH,
##     group_age, group_age1, h1, h10a, h10a1, h10a10, h10a11,
##     h10a11a, h10a2, h10a3, h10a4, h10a5, h10a6, h10a7, h10a8,
##     h10a9, h12, h12a, h12a_1, h12log, h13, h2, h3, h4, h5, h6, h7,
##     h8, h9, ha000, itc1, itc2, l1, l2, l3, l4, l5, l6, label1,
##     label2, moneyspent, msdt, n01, n02, n03, n05, n06, n07, n08,
##     n1, n10, n100, n101, n102, n103, n11, n12, n13, n14, n15, n16,
##     n1b, n2, n3, n35, n36, n37, n38, n39, n3posterb, n4, n40, n41,
##     n42, n43, n44, n45, n46, n47, n48, n49, n5, n50, n51, n52,
##     n53, n54, n55, n56, n57, n58, n59, n6, n60, n61, n61a, n62,
##     n62a, n63, n63a, n64, n64a, n65, n65a, n66, n66a, n67, n67a,
##     n68, n68a, n7, n77, n78, n7tren, n8, n88, n89, n8nha, n9, n97,
##     n98, n99, n9khach, noEnvi2, occup1, policy, policy_a, policy2,
##     reasons, reasons1, s1, Screening, selfhealth, SH1, smostt,
##     ter_fa1, ter_in, tertile_fa, tertile_indi, test, unitsdiffi1,
##     w1, w2, w3, w4, wtp
## The following objects are masked from r1 (pos = 6):
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, c1, c2, c23a, c3, c4, c5, c6, c7,
##     c8, c9, COST, cost1, cost2, costincrease, ct, d1, d10, d11,
##     d1a, d2, d3, d3a, d4, d5, d6, d7, d8, d9, Decision, e1, e2,
##     edu, educ1, educ2, f1, ghi1, ghi2, ghiro2, giadinhkoUH,
##     group_age, group_age1, h1, h10a, h10a1, h10a10, h10a11,
##     h10a11a, h10a2, h10a3, h10a4, h10a5, h10a6, h10a7, h10a8,
##     h10a9, h12, h12a, h12a_1, h12log, h13, h2, h3, h4, h5, h6, h7,
##     h8, h9, ha000, itc1, itc2, l1, l2, l3, l4, l5, l6, label1,
##     label2, moneyspent, msdt, n01, n02, n03, n05, n06, n07, n08,
##     n1, n10, n100, n101, n102, n103, n11, n12, n13, n14, n15, n16,
##     n1b, n2, n3, n35, n36, n37, n38, n39, n3posterb, n4, n40, n41,
##     n42, n43, n44, n45, n46, n47, n48, n49, n5, n50, n51, n52,
##     n53, n54, n55, n56, n57, n58, n59, n6, n60, n61, n61a, n62,
##     n62a, n63, n63a, n64, n64a, n65, n65a, n66, n66a, n67, n67a,
##     n68, n68a, n7, n77, n78, n7tren, n8, n88, n89, n8nha, n9, n97,
##     n98, n99, n9khach, noEnvi2, occup1, policy, policy_a, policy2,
##     reasons, reasons1, s1, Screening, selfhealth, SH1, smostt,
##     ter_fa1, ter_in, tertile_fa, tertile_indi, test, unitsdiffi1,
##     w1, w2, w3, w4, wtp
## The following objects are masked from r1 (pos = 7):
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, c1, c2, c23a, c3, c4, c5, c6, c7,
##     c8, c9, COST, cost1, cost2, costincrease, ct, d1, d10, d11,
##     d1a, d2, d3, d3a, d4, d5, d6, d7, d8, d9, Decision, e1, e2,
##     edu, educ1, educ2, f1, ghi1, ghi2, ghiro2, giadinhkoUH,
##     group_age, group_age1, h1, h10a, h10a1, h10a10, h10a11,
##     h10a11a, h10a2, h10a3, h10a4, h10a5, h10a6, h10a7, h10a8,
##     h10a9, h12, h12a, h12a_1, h12log, h13, h2, h3, h4, h5, h6, h7,
##     h8, h9, ha000, itc1, itc2, l1, l2, l3, l4, l5, l6, label1,
##     label2, moneyspent, msdt, n01, n02, n03, n05, n06, n07, n08,
##     n1, n10, n100, n101, n102, n103, n11, n12, n13, n14, n15, n16,
##     n1b, n2, n3, n35, n36, n37, n38, n39, n3posterb, n4, n40, n41,
##     n42, n43, n44, n45, n46, n47, n48, n49, n5, n50, n51, n52,
##     n53, n54, n55, n56, n57, n58, n59, n6, n60, n61, n61a, n62,
##     n62a, n63, n63a, n64, n64a, n65, n65a, n66, n66a, n67, n67a,
##     n68, n68a, n7, n77, n78, n7tren, n8, n88, n89, n8nha, n9, n97,
##     n98, n99, n9khach, noEnvi2, occup1, policy, policy_a, policy2,
##     reasons, reasons1, s1, Screening, selfhealth, SH1, smostt,
##     ter_fa1, ter_in, tertile_fa, tertile_indi, test, unitsdiffi1,
##     w1, w2, w3, w4
## The following objects are masked from newdata2 (pos = 17):
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, c1, c2, c23a, c3, c4, c5, c6, c7,
##     c8, c9, COST, cost1, costincrease, ct, d1, d10, d11, d1a, d2,
##     d3, d3a, d4, d5, d6, d7, d8, d9, Decision, e1, e2, edu, educ1,
##     educ2, f1, ghi1, ghi2, ghiro2, giadinhkoUH, group_age,
##     group_age1, h1, h10a, h10a1, h10a10, h10a11, h10a11a, h10a2,
##     h10a3, h10a4, h10a5, h10a6, h10a7, h10a8, h10a9, h12, h12a,
##     h12a_1, h12log, h13, h2, h3, h4, h5, h6, h7, h8, h9, ha000,
##     itc1, itc2, l1, l2, l3, l4, l5, l6, label1, label2,
##     moneyspent, msdt, n01, n02, n03, n05, n06, n07, n08, n1, n10,
##     n100, n101, n102, n103, n11, n12, n13, n14, n15, n16, n1b, n2,
##     n3, n35, n36, n37, n38, n39, n3posterb, n4, n40, n41, n42,
##     n43, n44, n45, n46, n47, n48, n49, n5, n50, n51, n52, n53,
##     n54, n55, n56, n57, n58, n59, n6, n60, n61, n61a, n62, n62a,
##     n63, n63a, n64, n64a, n65, n65a, n66, n66a, n67, n67a, n68,
##     n68a, n7, n77, n78, n7tren, n8, n88, n89, n8nha, n9, n97, n98,
##     n99, n9khach, noEnvi2, occup1, policy, policy_a, policy2,
##     reasons, reasons1, s1, Screening, selfhealth, SH1, smostt,
##     ter_fa1, ter_in, tertile_fa, tertile_indi, test, unitsdiffi1,
##     w1, w2, w3, w4, wtp
## The following objects are masked from r1 (pos = 18):
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, c1, c2, c23a, c3, c4, c5, c6, c7,
##     c8, c9, COST, cost1, costincrease, ct, d1, d10, d11, d1a, d2,
##     d3, d3a, d4, d5, d6, d7, d8, d9, Decision, e1, e2, edu, educ1,
##     educ2, f1, ghi1, ghi2, ghiro2, giadinhkoUH, group_age,
##     group_age1, h1, h10a, h10a1, h10a10, h10a11, h10a11a, h10a2,
##     h10a3, h10a4, h10a5, h10a6, h10a7, h10a8, h10a9, h12, h12a,
##     h12a_1, h12log, h13, h2, h3, h4, h5, h6, h7, h8, h9, ha000,
##     itc1, itc2, l1, l2, l3, l4, l5, l6, label1, label2,
##     moneyspent, msdt, n01, n02, n03, n05, n06, n07, n08, n1, n10,
##     n100, n101, n102, n103, n11, n12, n13, n14, n15, n16, n1b, n2,
##     n3, n35, n36, n37, n38, n39, n3posterb, n4, n40, n41, n42,
##     n43, n44, n45, n46, n47, n48, n49, n5, n50, n51, n52, n53,
##     n54, n55, n56, n57, n58, n59, n6, n60, n61, n61a, n62, n62a,
##     n63, n63a, n64, n64a, n65, n65a, n66, n66a, n67, n67a, n68,
##     n68a, n7, n77, n78, n7tren, n8, n88, n89, n8nha, n9, n97, n98,
##     n99, n9khach, noEnvi2, occup1, policy, policy_a, policy2,
##     reasons, reasons1, s1, Screening, selfhealth, SH1, smostt,
##     ter_fa1, ter_in, tertile_fa, tertile_indi, test, unitsdiffi1,
##     w1, w2, w3, w4, wtp
## The following objects are masked from r1 (pos = 19):
## 
##     a1, advice, advice1, ag, age_group, anticam, anticam2, b1,
##     b10, b10a1, b10a2, b10a3, b10a4, b10a5, b10a6, b10a7, b11,
##     b11a, b11a2, b11a3, b12, b12a, b13, b14, b15, b16, b16a, b17,
##     b18, b18a, b1a, b2, b2a1, b3, b4, b5, b5a, b6, b6a, b6a1,
##     b6a2, b6a3, b6a4, b6a5, b6a6, b6a7, b6a7a, b7, b8, b9, br,
##     branch, branch1, branch2, c1, c2, c23a, c3, c4, c5, c6, c7,
##     c8, c9, COST, cost1, costincrease, ct, d1, d10, d11, d1a, d2,
##     d3, d3a, d4, d5, d6, d7, d8, d9, Decision, e1, e2, edu, educ1,
##     educ2, f1, ghi1, ghi2, ghiro2, giadinhkoUH, group_age,
##     group_age1, h1, h10a, h10a1, h10a10, h10a11, h10a11a, h10a2,
##     h10a3, h10a4, h10a5, h10a6, h10a7, h10a8, h10a9, h12, h12a,
##     h12a_1, h12log, h13, h2, h3, h4, h5, h6, h7, h8, h9, ha000,
##     itc1, itc2, l1, l2, l3, l4, l5, l6, label1, label2,
##     moneyspent, msdt, n01, n02, n03, n05, n06, n07, n08, n1, n10,
##     n100, n101, n102, n103, n11, n12, n13, n14, n15, n16, n1b, n2,
##     n3, n35, n36, n37, n38, n39, n3posterb, n4, n40, n41, n42,
##     n43, n44, n45, n46, n47, n48, n49, n5, n50, n51, n52, n53,
##     n54, n55, n56, n57, n58, n59, n6, n60, n61, n61a, n62, n62a,
##     n63, n63a, n64, n64a, n65, n65a, n66, n66a, n67, n67a, n68,
##     n68a, n7, n77, n78, n7tren, n8, n88, n89, n8nha, n9, n97, n98,
##     n99, n9khach, noEnvi2, occup1, policy, policy_a, policy2,
##     reasons, reasons1, s1, Screening, selfhealth, SH1, smostt,
##     ter_fa1, ter_in, tertile_fa, tertile_indi, test, unitsdiffi1,
##     w1, w2, w3, w4

define varibles

newdata2$cost1=as.factor(newdata2$cost1)
newdata2$c1=as.factor(newdata2$c1)
newdata2$h1=as.factor(newdata2$h1)
newdata2$label1=as.factor(newdata2$label1)
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)

re-organise our data by sampling .

newdata2=newdata2[sample(1:nrow(newdata2), 442, replace=F),]

Create new data frame (90%-10%) (N=822)

Train data

train=newdata2[1:396, ]
View(train)

Test data

test=newdata2[397:442, ]
View(test)

Install packages and library

library("brms")
library("caret")
library("coda")

library("rstan", lib.loc="~/R/win-library/3.2")
options(mc.cores = parallel::detectCores())

set up for fomulation

prior=get_prior(formula=wtp~h1+ d1a+educ2+ter_in+selfhealth+smostt
               +b16a+b18a+label1+anticam+noEnvi2, family="bernoulli", data=train)
## Warning: Rows containing NAs were excluded from the model
set.seed(1234) 
 
baybin=brm(formula=wtp~h1+ d1a+educ2+ter_in+selfhealth+smostt
               +b16a+b18a+label1+anticam+noEnvi2, family="bernoulli", data=train, chains=5, iter=2000, warmup=1000, prior=prior)
## Warning: Rows containing NAs were excluded from the model
## Compiling the C++ model
## Start sampling

Summary model using train data - this is main results for model

summary(baybin)
##  Family: bernoulli (logit) 
## Formula: wtp ~ h1 + d1a + educ2 + ter_in + selfhealth + smostt + b16a + b18a + label1 + anticam + noEnvi2 
##    Data: train (Number of observations: 381) 
## Samples: 5 chains, each with iter = 2000; warmup = 1000; thin = 1; 
##          total post-warmup samples = 5000
##    WAIC: Not computed
##  
## Population-Level Effects: 
##                Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
## Intercept          0.69      0.55    -0.38     1.79       5000    1
## h12               -0.30      0.25    -0.78     0.18       5000    1
## d1amarried        -0.45      0.26    -0.96     0.05       5000    1
## educ22            -0.89      0.38    -1.65    -0.13       4130    1
## educ23            -0.41      0.40    -1.19     0.39       4120    1
## ter_in2            0.48      0.31    -0.10     1.08       5000    1
## ter_in3            0.79      0.30     0.19     1.38       5000    1
## selfhealthgood    -0.09      0.23    -0.56     0.36       5000    1
## smosttmedium      -0.24      0.30    -0.83     0.33       4174    1
## smosttheavy        0.75      0.31     0.15     1.35       4199    1
## b16a1             -0.69      0.31    -1.29    -0.09       5000    1
## b18abad           -0.44      0.25    -0.94     0.08       5000    1
## label11            1.06      0.38     0.33     1.81       4527    1
## anticam           -0.47      0.36    -1.19     0.22       4365    1
## noEnvi2           -0.56      0.32    -1.19     0.06       5000    1
## 
## Samples were drawn using sampling(NUTS). For each parameter, Eff.Sample 
## is a crude measure of effective sample size, and Rhat is the potential 
## scale reduction factor on split chains (at convergence, Rhat = 1).
WAIC(baybin)
##    WAIC    SE
##  494.09 15.94

Notes: If you wanna report OR and 95%CI, you might need the exp funtion (using in R or in excel)

fit model

baybin$fit
## Inference for Stan model: bernoulli(logit) brms-model.
## 5 chains, each with iter=2000; warmup=1000; thin=1; 
## post-warmup draws per chain=1000, total post-warmup draws=5000.
## 
##                     mean se_mean   sd    2.5%     25%     50%     75%
## b_Intercept         0.69    0.01 0.55   -0.38    0.32    0.70    1.05
## b_h12              -0.30    0.00 0.25   -0.78   -0.47   -0.30   -0.14
## b_d1amarried       -0.45    0.00 0.26   -0.96   -0.63   -0.45   -0.27
## b_educ22           -0.89    0.01 0.38   -1.65   -1.15   -0.88   -0.62
## b_educ23           -0.41    0.01 0.40   -1.19   -0.69   -0.41   -0.15
## b_ter_in2           0.48    0.00 0.31   -0.10    0.27    0.47    0.68
## b_ter_in3           0.79    0.00 0.30    0.19    0.59    0.79    1.00
## b_selfhealthgood   -0.09    0.00 0.23   -0.56   -0.25   -0.09    0.07
## b_smosttmedium     -0.24    0.00 0.30   -0.83   -0.45   -0.24   -0.04
## b_smosttheavy       0.75    0.00 0.31    0.15    0.55    0.75    0.95
## b_b16a1            -0.69    0.00 0.31   -1.29   -0.90   -0.69   -0.49
## b_b18abad          -0.44    0.00 0.25   -0.94   -0.61   -0.44   -0.27
## b_label11           1.06    0.01 0.38    0.33    0.81    1.06    1.32
## b_anticam          -0.47    0.01 0.36   -1.19   -0.70   -0.46   -0.23
## b_noEnvi2          -0.56    0.00 0.32   -1.19   -0.77   -0.56   -0.35
## lp__             -238.71    0.06 2.74 -244.88 -240.36 -238.33 -236.75
##                    97.5% n_eff Rhat
## b_Intercept         1.79  5000    1
## b_h12               0.18  5000    1
## b_d1amarried        0.05  5000    1
## b_educ22           -0.13  4130    1
## b_educ23            0.39  4120    1
## b_ter_in2           1.08  5000    1
## b_ter_in3           1.38  5000    1
## b_selfhealthgood    0.36  5000    1
## b_smosttmedium      0.33  4174    1
## b_smosttheavy       1.35  4199    1
## b_b16a1            -0.09  5000    1
## b_b18abad           0.08  5000    1
## b_label11           1.81  4527    1
## b_anticam           0.22  4365    1
## b_noEnvi2           0.06  5000    1
## lp__             -234.35  2178    1
## 
## Samples were drawn using NUTS(diag_e) at Wed Mar 22 00:21:05 2017.
## For each parameter, n_eff is a crude measure of effective sample size,
## and Rhat is the potential scale reduction factor on split chains (at 
## convergence, Rhat=1).

next steps, ww will check the accuracy of model using ROC

establish command using TEST data

prob=as.data.frame(predict(baybin,test,type="response"))

library(e1071)
pred=ifelse(prob$Estimate> 0.5, 1, 0)

Results

confusionMatrix(data=pred,reference=test$wtp)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 22 15
##          1  5  4
##                                           
##                Accuracy : 0.5652          
##                  95% CI : (0.4111, 0.7107)
##     No Information Rate : 0.587           
##     P-Value [Acc > NIR] : 0.67562         
##                                           
##                   Kappa : 0.0275          
##  Mcnemar's Test P-Value : 0.04417         
##                                           
##             Sensitivity : 0.8148          
##             Specificity : 0.2105          
##          Pos Pred Value : 0.5946          
##          Neg Pred Value : 0.4444          
##              Prevalence : 0.5870          
##          Detection Rate : 0.4783          
##    Detection Prevalence : 0.8043          
##       Balanced Accuracy : 0.5127          
##                                           
##        'Positive' Class : 0               
##