Driver Behavior

Driver Behavior

setwd("C:/Users/asr171/OneDrive - Texas State University/Das, Subasish's files - PewSurvey/W152")

library(readxl)
library(ggplot2)
library(dplyr)
library(compareGroups)
library(vtree)
library(DT)

df= read.csv("W152_Driver.csv")
df1 =df[, c(1:14, 19)]
head(df1)
##            F_METRO F_CREGION F_USR_SELFID F_AGECAT F_GENDER
## 1 Non-metropolitan   Midwest        Rural      65+    A man
## 2     Metropolitan     South     Suburban      65+    A man
## 3 Non-metropolitan   Midwest        Rural    50-64    A man
## 4     Metropolitan      West     Suburban    50-64  A woman
## 5 Non-metropolitan     South        Rural    50-64  A woman
## 6     Metropolitan   Midwest     Suburban      65+  A woman
##               F_EDUCCAT F_HISP_ORIGIN       F_RACETHNMOD
## 1          Some College   No Response White non-Hispanic
## 2          Some College   No Response White non-Hispanic
## 3 H.S. graduate or less   No Response White non-Hispanic
## 4     College graduate+   No Response White non-Hispanic
## 5     College graduate+   No Response White non-Hispanic
## 6     College graduate+   No Response White non-Hispanic
##                           F_BIRTHPLACE          F_MARITAL      F_PARTYLN_FINAL
## 1 U.S. 50 states, District of Columbia            Married          No Response
## 2 U.S. 50 states, District of Columbia            Married          No Response
## 3 U.S. 50 states, District of Columbia            Married          No Response
## 4 U.S. 50 states, District of Columbia            Married The Republican Party
## 5 U.S. 50 states, District of Columbia Never been married The Republican Party
## 6 U.S. 50 states, District of Columbia            Married          No Response
##                      F_INC_SDT1       F_IDEO           F_INTFREQ
## 1  $80,000 to less than $90,000     Moderate    About once a day
## 2              $100,000 or more Conservative          Less often
## 3                       Refused Conservative Several times a day
## 4 $90,000 to less than $100,000     Moderate Several times a day
## 5              $100,000 or more     Moderate   Almost constantly
## 6              $100,000 or more Conservative Several times a day
##                  DRIVE2_d_W152
## 1                Major problem
## 2                Minor problem
## 3                Not a problem
## 4                Major problem
## 5                Major problem
## 6 Don't know/Refused/Web blank

Compare Groups

res1 <- compareGroups(DRIVE2_d_W152 ~ ., df1, ref = 1, max.ylev = 50,
                      max.xlev = 50,  chisq.test.perm=TRUE, chisq.test.B=10000)
res2= createTable(res1, show.ratio = TRUE)
res2
## 
## --------Summary descriptives table by 'DRIVE2_d_W152'---------
## 
## _________________________________________________________________________________________________________________________ 
##                                          Don't know/Refused/Web blank Major problem Minor problem Not a problem p.overall 
##                                                      N=75                N=1963        N=2476         N=896               
## ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ 
## F_METRO:                                                                                                          0.004   
##     Metropolitan                                  64 (85.3%)          1748 (89.0%)  2152 (86.9%)   772 (86.2%)            
##     No Response                                   1 (1.33%)             0 (0.00%)     2 (0.08%)     0 (0.00%)             
##     Non-metropolitan                              10 (13.3%)           215 (11.0%)   322 (13.0%)   124 (13.8%)            
## F_CREGION:                                                                                                        0.003   
##     Midwest                                       16 (21.3%)           362 (18.4%)   518 (20.9%)   193 (21.5%)            
##     No Response                                   1 (1.33%)             0 (0.00%)     2 (0.08%)     0 (0.00%)             
##     Northeast                                     16 (21.3%)           339 (17.3%)   436 (17.6%)   144 (16.1%)            
##     South                                         27 (36.0%)           760 (38.7%)   916 (37.0%)   373 (41.6%)            
##     West                                          15 (20.0%)           502 (25.6%)   604 (24.4%)   186 (20.8%)            
## F_USR_SELFID:                                                                                                     0.001   
##     Refused                                       0 (0.00%)            22 (1.12%)    20 (0.81%)    15 (1.67%)             
##     Rural                                         23 (30.7%)           429 (21.9%)   558 (22.5%)   225 (25.1%)            
##     Suburban                                      32 (42.7%)           979 (49.9%)  1358 (54.8%)   447 (49.9%)            
##     Urban                                         20 (26.7%)           533 (27.2%)   540 (21.8%)   209 (23.3%)            
## F_AGECAT:                                                                                                        <0.001   
##     18-29                                         9 (12.0%)            244 (12.4%)   360 (14.5%)   150 (16.7%)            
##     30-49                                         11 (14.7%)           665 (33.9%)   797 (32.2%)   410 (45.8%)            
##     50-64                                         13 (17.3%)           543 (27.7%)   628 (25.4%)   158 (17.6%)            
##     65+                                           40 (53.3%)           497 (25.3%)   684 (27.6%)   174 (19.4%)            
##     Refused                                       2 (2.67%)            14 (0.71%)     7 (0.28%)     4 (0.45%)             
## F_GENDER:                                                                                                         0.001   
##     A man                                         21 (28.0%)           914 (46.6%)  1279 (51.7%)   475 (53.0%)            
##     A woman                                       52 (69.3%)          1035 (52.7%)  1172 (47.3%)   408 (45.5%)            
##     In some other way                             0 (0.00%)             5 (0.25%)    17 (0.69%)     9 (1.00%)             
##     Refused                                       2 (2.67%)             9 (0.46%)     8 (0.32%)     4 (0.45%)             
## F_EDUCCAT:                                                                                                       <0.001   
##     College graduate+                             32 (42.7%)           733 (37.3%)  1175 (47.5%)   369 (41.2%)            
##     H.S. graduate or less                         27 (36.0%)           607 (30.9%)   577 (23.3%)   247 (27.6%)            
##     Refused                                       2 (2.67%)             4 (0.20%)     4 (0.16%)     3 (0.33%)             
##     Some College                                  14 (18.7%)           619 (31.5%)   720 (29.1%)   277 (30.9%)            
## F_HISP_ORIGIN:                                                                                                   <0.001   
##     Cuban                                         0 (0.00%)            28 (1.43%)    22 (0.89%)     2 (0.22%)             
##     Dominican                                     1 (1.33%)            13 (0.66%)    11 (0.44%)     4 (0.45%)             
##     Mexican                                       8 (10.7%)            204 (10.4%)   169 (6.83%)   54 (6.03%)             
##     No Response                                   60 (80.0%)          1560 (79.5%)  2165 (87.4%)   787 (87.8%)            
##     Other Central American                        1 (1.33%)            25 (1.27%)    11 (0.44%)     5 (0.56%)             
##     Other country                                 1 (1.33%)             9 (0.46%)     9 (0.36%)     3 (0.33%)             
##     Other South American                          2 (2.67%)            45 (2.29%)    32 (1.29%)    11 (1.23%)             
##     Puerto Rican                                  0 (0.00%)            38 (1.94%)    27 (1.09%)    14 (1.56%)             
##     Refused                                       2 (2.67%)             8 (0.41%)     6 (0.24%)     1 (0.11%)             
##     Salvadoran                                    0 (0.00%)            14 (0.71%)     8 (0.32%)     3 (0.33%)             
##     Spanish                                       0 (0.00%)            19 (0.97%)    16 (0.65%)    12 (1.34%)             
## F_RACETHNMOD:                                                                                                    <0.001   
##     Asian non-Hispanic                            6 (8.00%)            217 (11.1%)   234 (9.45%)   94 (10.5%)             
##     Black non-Hispanic                            8 (10.7%)            285 (14.5%)   231 (9.33%)   114 (12.7%)            
##     Hispanic                                      15 (20.0%)           403 (20.5%)   311 (12.6%)   109 (12.2%)            
##     Other                                         2 (2.67%)            65 (3.31%)    98 (3.96%)    46 (5.13%)             
##     Refused                                       0 (0.00%)            16 (0.82%)    19 (0.77%)    11 (1.23%)             
##     White non-Hispanic                            44 (58.7%)           977 (49.8%)  1583 (63.9%)   522 (58.3%)            
## F_BIRTHPLACE:                                                                                                    <0.001   
##     Another country                               10 (13.3%)           432 (22.0%)   340 (13.7%)   100 (11.2%)            
##     Refused                                       2 (2.67%)            20 (1.02%)     9 (0.36%)     3 (0.33%)             
##     U.S. 50 states, District of Columbia          61 (81.3%)          1465 (74.6%)  2100 (84.8%)   780 (87.1%)            
##     U.S. other territory                          1 (1.33%)            23 (1.17%)    13 (0.53%)     5 (0.56%)             
##     U.S. Puerto Rico                              1 (1.33%)            23 (1.17%)    14 (0.57%)     8 (0.89%)             
## F_MARITAL:                                                                                                       <0.001   
##     Divorced                                      9 (12.0%)            201 (10.2%)   210 (8.48%)   74 (8.26%)             
##     Living with a partner                         4 (5.33%)            173 (8.81%)   210 (8.48%)   111 (12.4%)            
##     Married                                       37 (49.3%)          1082 (55.1%)  1362 (55.0%)   455 (50.8%)            
##     Never been married                            14 (18.7%)           370 (18.8%)   514 (20.8%)   212 (23.7%)            
##     Refused                                       4 (5.33%)            12 (0.61%)     8 (0.32%)     5 (0.56%)             
##     Separated                                     2 (2.67%)            32 (1.63%)    47 (1.90%)    11 (1.23%)             
##     Widowed                                       5 (6.67%)            93 (4.74%)    125 (5.05%)   28 (3.12%)             
## F_PARTYLN_FINAL:                                                                                                  0.003   
##     No Response                                   39 (52.0%)          1124 (57.3%)  1427 (57.6%)   455 (50.8%)            
##     Refused                                       5 (6.67%)            79 (4.02%)    80 (3.23%)    33 (3.68%)             
##     The Democratic Party                          19 (25.3%)           377 (19.2%)   522 (21.1%)   230 (25.7%)            
##     The Republican Party                          12 (16.0%)           383 (19.5%)   447 (18.1%)   178 (19.9%)            
## F_INC_SDT1:                                                                                                      <0.001   
##     $100,000 or more                              15 (20.0%)           548 (27.9%)   897 (36.2%)   281 (31.4%)            
##     $30,000 to less than $40,000                  2 (2.67%)            193 (9.83%)   173 (6.99%)   68 (7.59%)             
##     $40,000 to less than $50,000                  7 (9.33%)            155 (7.90%)   188 (7.59%)   66 (7.37%)             
##     $50,000 to less than $60,000                  4 (5.33%)            144 (7.34%)   176 (7.11%)   54 (6.03%)             
##     $60,000 to less than $70,000                  3 (4.00%)            124 (6.32%)   158 (6.38%)   84 (9.38%)             
##     $70,000 to less than $80,000                  4 (5.33%)            123 (6.27%)   172 (6.95%)   71 (7.92%)             
##     $80,000 to less than $90,000                  2 (2.67%)            115 (5.86%)   147 (5.94%)   40 (4.46%)             
##     $90,000 to less than $100,000                 3 (4.00%)            121 (6.16%)   152 (6.14%)   51 (5.69%)             
##     Less than $30,000                             21 (28.0%)           330 (16.8%)   309 (12.5%)   149 (16.6%)            
##     Refused                                       14 (18.7%)           110 (5.60%)   104 (4.20%)   32 (3.57%)             
## F_IDEO:                                                                                                          <0.001   
##     Conservative                                  10 (13.3%)           524 (26.7%)   614 (24.8%)   166 (18.5%)            
##     Liberal                                       15 (20.0%)           276 (14.1%)   495 (20.0%)   196 (21.9%)            
##     Moderate                                      27 (36.0%)           810 (41.3%)   940 (38.0%)   350 (39.1%)            
##     Refused                                       8 (10.7%)            42 (2.14%)    40 (1.62%)    18 (2.01%)             
##     Very conservative                             12 (16.0%)           212 (10.8%)   183 (7.39%)   57 (6.36%)             
##     Very liberal                                  3 (4.00%)            99 (5.04%)    204 (8.24%)   109 (12.2%)            
## F_INTFREQ:                                                                                                       <0.001   
##     About once a day                              5 (6.67%)            83 (4.23%)    109 (4.40%)   30 (3.35%)             
##     Almost constantly                             24 (32.0%)           976 (49.7%)  1147 (46.3%)   482 (53.8%)            
##     Do not use the internet                       5 (6.67%)            36 (1.83%)    30 (1.21%)    13 (1.45%)             
##     Less often                                    7 (9.33%)            45 (2.29%)    28 (1.13%)    12 (1.34%)             
##     Refused                                       2 (2.67%)             9 (0.46%)     9 (0.36%)     3 (0.33%)             
##     Several times a day                           32 (42.7%)           769 (39.2%)  1100 (44.4%)   335 (37.4%)            
##     Several times a week                          0 (0.00%)            45 (2.29%)    53 (2.14%)    21 (2.34%)             
## ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
##export2xls(res2, file='tabledem_Driver1.xlsx')


df3= df[, c(16:21, 4)]
res1 <- compareGroups(F_AGECAT ~ ., df3, ref = 1, max.ylev = 50,
                      max.xlev = 50,  chisq.test.perm=TRUE, chisq.test.B=10000)
res2= createTable(res1, show.ratio = TRUE)
res2
## 
## --------Summary descriptives table by 'F_AGECAT'---------
## 
## ________________________________________________________________________________________________________ 
##                                     18-29       30-49        50-64         65+       Refused   p.overall 
##                                     N=763       N=1883       N=1342       N=1395       N=27              
## ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ 
## DRIVE2_a_W152:                                                                                   0.003   
##     Don't know/Refused/Web blank  5 (0.66%)   2 (0.11%)    2 (0.15%)    7 (0.50%)   1 (3.70%)            
##     Major problem                456 (59.8%) 1122 (59.6%) 864 (64.4%)  911 (65.3%)  19 (70.4%)           
##     Minor problem                240 (31.5%) 628 (33.4%)  422 (31.4%)  405 (29.0%)  5 (18.5%)            
##     Not a problem                62 (8.13%)  131 (6.96%)   54 (4.02%)   72 (5.16%)  2 (7.41%)            
## DRIVE2_b_W152:                                                                                   0.011   
##     Don't know/Refused/Web blank  5 (0.66%)   3 (0.16%)    1 (0.07%)    5 (0.36%)   1 (3.70%)            
##     Major problem                483 (63.3%) 1162 (61.7%) 887 (66.1%)  881 (63.2%)  18 (66.7%)           
##     Minor problem                220 (28.8%) 606 (32.2%)  386 (28.8%)  423 (30.3%)  7 (25.9%)            
##     Not a problem                55 (7.21%)  112 (5.95%)   68 (5.07%)   86 (6.16%)  1 (3.70%)            
## DRIVE2_c_W152:                                                                                  <0.001   
##     Don't know/Refused/Web blank  5 (0.66%)   7 (0.37%)    7 (0.52%)    18 (1.29%)  2 (7.41%)            
##     Major problem                387 (50.7%) 957 (50.8%)  656 (48.9%)  660 (47.3%)  17 (63.0%)           
##     Minor problem                289 (37.9%) 775 (41.2%)  613 (45.7%)  642 (46.0%)  3 (11.1%)            
##     Not a problem                82 (10.7%)  144 (7.65%)   66 (4.92%)   75 (5.38%)  5 (18.5%)            
## DRIVE2_d_W152:                                                                                  <0.001   
##     Don't know/Refused/Web blank  9 (1.18%)   11 (0.58%)   13 (0.97%)   40 (2.87%)  2 (7.41%)            
##     Major problem                244 (32.0%) 665 (35.3%)  543 (40.5%)  497 (35.6%)  14 (51.9%)           
##     Minor problem                360 (47.2%) 797 (42.3%)  628 (46.8%)  684 (49.0%)  7 (25.9%)            
##     Not a problem                150 (19.7%) 410 (21.8%)  158 (11.8%)  174 (12.5%)  4 (14.8%)            
## DRIVE2_e_W152:                                                                                   0.001   
##     Don't know/Refused/Web blank  4 (0.52%)   3 (0.16%)    3 (0.22%)    2 (0.14%)   1 (3.70%)            
##     Major problem                575 (75.4%) 1491 (79.2%) 1103 (82.2%) 1062 (76.1%) 22 (81.5%)           
##     Minor problem                152 (19.9%) 330 (17.5%)  208 (15.5%)  303 (21.7%)  3 (11.1%)            
##     Not a problem                32 (4.19%)   59 (3.13%)   28 (2.09%)   28 (2.01%)  1 (3.70%)            
## DRIVE2_f_W152:                                                                                   0.001   
##     Don't know/Refused/Web blank  5 (0.66%)   2 (0.11%)    4 (0.30%)    7 (0.50%)   2 (7.41%)            
##     Major problem                350 (45.9%) 907 (48.2%)  637 (47.5%)  634 (45.4%)  12 (44.4%)           
##     Minor problem                312 (40.9%) 761 (40.4%)  573 (42.7%)  616 (44.2%)  9 (33.3%)            
##     Not a problem                96 (12.6%)  213 (11.3%)  128 (9.54%)  138 (9.89%)  4 (14.8%)            
## ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
##export2xls(res2, file='tabledem_Driver1.xlsx')

df3= df[, c(16:21, 15)]
res1 <- compareGroups(DRIVE1_W152 ~ ., df3, ref = 1, max.ylev = 50,
                      max.xlev = 50,  chisq.test.perm=TRUE, chisq.test.B=10000)
res2= createTable(res1, show.ratio = TRUE)
res2
## 
## --------Summary descriptives table by 'DRIVE1_W152'---------
## 
## ___________________________________________________________________________________________________________________________________________________________________________________ 
##                                  A lot less safely A lot more safely Neither more nor less safely  Not sure   Refused/Web blank Somewhat less safely Somewhat more safely p.overall 
##                                       N=1245             N=93                   N=1922               N=326           N=8               N=1431               N=385                   
## ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ 
## DRIVE2_a_W152:                                                                                                                                                             <0.001   
##     Don't know/Refused/Web blank     1 (0.08%)         0 (0.00%)              5 (0.26%)            3 (0.92%)      2 (25.0%)          3 (0.21%)            3 (0.78%)                 
##     Major problem                  1061 (85.2%)       49 (52.7%)             905 (47.1%)          170 (52.1%)     3 (37.5%)         1001 (70.0%)         183 (47.5%)                
##     Minor problem                   165 (13.3%)       36 (38.7%)             845 (44.0%)          108 (33.1%)     2 (25.0%)         395 (27.6%)          149 (38.7%)                
##     Not a problem                   18 (1.45%)         8 (8.60%)             167 (8.69%)          45 (13.8%)      1 (12.5%)          32 (2.24%)           50 (13.0%)                
## DRIVE2_b_W152:                                                                                                                                                             <0.001   
##     Don't know/Refused/Web blank     2 (0.16%)         0 (0.00%)              3 (0.16%)            4 (1.23%)      2 (25.0%)          2 (0.14%)            2 (0.52%)                 
##     Major problem                  1103 (88.6%)       49 (52.7%)             928 (48.3%)          177 (54.3%)     2 (25.0%)         999 (69.8%)          173 (44.9%)                
##     Minor problem                   127 (10.2%)       33 (35.5%)             822 (42.8%)          105 (32.2%)     3 (37.5%)         388 (27.1%)          164 (42.6%)                
##     Not a problem                   13 (1.04%)        11 (11.8%)             169 (8.79%)          40 (12.3%)      1 (12.5%)          42 (2.94%)           46 (11.9%)                
## DRIVE2_c_W152:                                                                                                                                                             <0.001   
##     Don't know/Refused/Web blank     5 (0.40%)         0 (0.00%)              10 (0.52%)           9 (2.76%)      2 (25.0%)          9 (0.63%)            4 (1.04%)                 
##     Major problem                   796 (63.9%)       46 (49.5%)             818 (42.6%)          163 (50.0%)     2 (25.0%)         697 (48.7%)          155 (40.3%)                
##     Minor problem                   405 (32.5%)       34 (36.6%)             926 (48.2%)          106 (32.5%)     3 (37.5%)         653 (45.6%)          195 (50.6%)                
##     Not a problem                   39 (3.13%)        13 (14.0%)             168 (8.74%)          48 (14.7%)      1 (12.5%)          72 (5.03%)           31 (8.05%)                
## DRIVE2_d_W152:                                                                                                                                                             <0.001   
##     Don't know/Refused/Web blank    11 (0.88%)         0 (0.00%)              27 (1.40%)          11 (3.37%)      2 (25.0%)          16 (1.12%)           8 (2.08%)                 
##     Major problem                   611 (49.1%)       36 (38.7%)             551 (28.7%)          123 (37.7%)     1 (12.5%)         520 (36.3%)          121 (31.4%)                
##     Minor problem                   484 (38.9%)       37 (39.8%)             962 (50.1%)          113 (34.7%)     4 (50.0%)         701 (49.0%)          175 (45.5%)                
##     Not a problem                   139 (11.2%)       20 (21.5%)             382 (19.9%)          79 (24.2%)      1 (12.5%)         194 (13.6%)           81 (21.0%)                
## DRIVE2_e_W152:                                                                                                                                                             <0.001   
##     Don't know/Refused/Web blank     0 (0.00%)         0 (0.00%)              6 (0.31%)            1 (0.31%)      2 (25.0%)          3 (0.21%)            1 (0.26%)                 
##     Major problem                  1162 (93.3%)       60 (64.5%)             1350 (70.2%)         227 (69.6%)     3 (37.5%)         1198 (83.7%)         253 (65.7%)                
##     Minor problem                   73 (5.86%)        27 (29.0%)             505 (26.3%)          67 (20.6%)      2 (25.0%)         210 (14.7%)          112 (29.1%)                
##     Not a problem                   10 (0.80%)         6 (6.45%)              61 (3.17%)          31 (9.51%)      1 (12.5%)          20 (1.40%)           19 (4.94%)                
## DRIVE2_f_W152:                                                                                                                                                             <0.001   
##     Don't know/Refused/Web blank     2 (0.16%)         0 (0.00%)              5 (0.26%)            4 (1.23%)      2 (25.0%)          4 (0.28%)            3 (0.78%)                 
##     Major problem                   884 (71.0%)       40 (43.0%)             637 (33.1%)          149 (45.7%)     2 (25.0%)         698 (48.8%)          130 (33.8%)                
##     Minor problem                   323 (25.9%)       35 (37.6%)             967 (50.3%)          117 (35.9%)     4 (50.0%)         637 (44.5%)          188 (48.8%)                
##     Not a problem                   36 (2.89%)        18 (19.4%)             313 (16.3%)          56 (17.2%)      0 (0.00%)          92 (6.43%)           64 (16.6%)                
## ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
df3= df[, c(16:21, 22)]
res1 <- compareGroups(DRIVE3_W152 ~ ., df3, ref = 1, max.ylev = 50,
                      max.xlev = 50,  chisq.test.perm=TRUE, chisq.test.B=10000)
res2= createTable(res1, show.ratio = TRUE)
res2
## 
## --------Summary descriptives table by 'DRIVE3_W152'---------
## 
## _______________________________________________________________________________________________________________________________________ 
##                                  Don't know/Refused/Web blank Extremely often   Never      Rarely     Sometimes   Very often  p.overall 
##                                              N=11                  N=373        N=185      N=1334       N=2446      N=1061              
## ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ 
## DRIVE2_a_W152:                                                                                                                 <0.001   
##     Don't know/Refused/Web blank          4 (36.4%)              2 (0.54%)    1 (0.54%)   3 (0.22%)   4 (0.16%)    3 (0.28%)            
##     Major problem                         6 (54.5%)             326 (87.4%)   67 (36.2%) 555 (41.6%) 1566 (64.0%) 852 (80.3%)           
##     Minor problem                         0 (0.00%)             33 (8.85%)    76 (41.1%) 631 (47.3%) 776 (31.7%)  184 (17.3%)           
##     Not a problem                         1 (9.09%)             12 (3.22%)    41 (22.2%) 145 (10.9%) 100 (4.09%)  22 (2.07%)            
## DRIVE2_b_W152:                                                                                                                 <0.001   
##     Don't know/Refused/Web blank          5 (45.5%)              2 (0.54%)    1 (0.54%)   3 (0.22%)   3 (0.12%)    1 (0.09%)            
##     Major problem                         4 (36.4%)             345 (92.5%)   62 (33.5%) 491 (36.8%) 1624 (66.4%) 905 (85.3%)           
##     Minor problem                         1 (9.09%)             15 (4.02%)    63 (34.1%) 685 (51.3%) 738 (30.2%)  140 (13.2%)           
##     Not a problem                         1 (9.09%)             11 (2.95%)    59 (31.9%) 155 (11.6%)  81 (3.31%)  15 (1.41%)            
## DRIVE2_c_W152:                                                                                                                 <0.001   
##     Don't know/Refused/Web blank          5 (45.5%)              0 (0.00%)    0 (0.00%)  14 (1.05%)   14 (0.57%)   6 (0.57%)            
##     Major problem                         3 (27.3%)             282 (75.6%)   65 (35.1%) 404 (30.3%) 1240 (50.7%) 683 (64.4%)           
##     Minor problem                         1 (9.09%)             77 (20.6%)    71 (38.4%) 770 (57.7%) 1069 (43.7%) 334 (31.5%)           
##     Not a problem                         2 (18.2%)             14 (3.75%)    49 (26.5%) 146 (10.9%) 123 (5.03%)  38 (3.58%)            
## DRIVE2_d_W152:                                                                                                                 <0.001   
##     Don't know/Refused/Web blank          5 (45.5%)              2 (0.54%)    4 (2.16%)  24 (1.80%)   30 (1.23%)  10 (0.94%)            
##     Major problem                         3 (27.3%)             214 (57.4%)   45 (24.3%) 287 (21.5%) 901 (36.8%)  513 (48.4%)           
##     Minor problem                         2 (18.2%)             108 (29.0%)   65 (35.1%) 733 (54.9%) 1170 (47.8%) 398 (37.5%)           
##     Not a problem                         1 (9.09%)             49 (13.1%)    71 (38.4%) 290 (21.7%) 345 (14.1%)  140 (13.2%)           
## DRIVE2_e_W152:                                                                                                                 <0.001   
##     Don't know/Refused/Web blank          4 (36.4%)              1 (0.27%)    0 (0.00%)   2 (0.15%)   3 (0.12%)    3 (0.28%)            
##     Major problem                         6 (54.5%)             341 (91.4%)   95 (51.4%) 884 (66.3%) 1977 (80.8%) 950 (89.5%)           
##     Minor problem                         1 (9.09%)             23 (6.17%)    64 (34.6%) 402 (30.1%) 416 (17.0%)  90 (8.48%)            
##     Not a problem                         0 (0.00%)              8 (2.14%)    26 (14.1%) 46 (3.45%)   50 (2.04%)  18 (1.70%)            
## DRIVE2_f_W152:                                                                                                                 <0.001   
##     Don't know/Refused/Web blank          5 (45.5%)              1 (0.27%)    1 (0.54%)   3 (0.22%)   8 (0.33%)    2 (0.19%)            
##     Major problem                         4 (36.4%)             295 (79.1%)   55 (29.7%) 359 (26.9%) 1134 (46.4%) 693 (65.3%)           
##     Minor problem                         1 (9.09%)             61 (16.4%)    65 (35.1%) 728 (54.6%) 1103 (45.1%) 313 (29.5%)           
##     Not a problem                         1 (9.09%)             16 (4.29%)    64 (34.6%) 244 (18.3%) 201 (8.22%)  53 (5.00%)            
## ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
df3= df[, c(16:21, 23)]
res1 <- compareGroups(DRIVER_W152 ~ ., df3, ref = 1, max.ylev = 50,
                      max.xlev = 50,  chisq.test.perm=TRUE, chisq.test.B=10000)
res2= createTable(res1, show.ratio = TRUE)
res2
## 
## --------Summary descriptives table by 'DRIVER_W152'---------
## 
## _______________________________________________________________________________________________________________________________________________________________ 
##                                  A few times a month A few times a week    Daily     Don't know/Refused/Web blank    Never    Once a week   Seldom    p.overall 
##                                         N=168              N=1201          N=3384                N=23                N=276       N=161       N=197              
## ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ 
## DRIVE2_a_W152:                                                                                                                                         <0.001   
##     Don't know/Refused/Web blank      1 (0.60%)          2 (0.17%)       5 (0.15%)            3 (13.0%)            3 (1.09%)   1 (0.62%)   2 (1.02%)            
##     Major problem                    101 (60.1%)        725 (60.4%)     2156 (63.7%)          14 (60.9%)          160 (58.0%) 89 (55.3%)  127 (64.5%)           
##     Minor problem                    45 (26.8%)         413 (34.4%)     1046 (30.9%)          5 (21.7%)           78 (28.3%)  61 (37.9%)  52 (26.4%)            
##     Not a problem                    21 (12.5%)          61 (5.08%)     177 (5.23%)           1 (4.35%)           35 (12.7%)  10 (6.21%)  16 (8.12%)            
## DRIVE2_b_W152:                                                                                                                                         <0.001   
##     Don't know/Refused/Web blank      0 (0.00%)          3 (0.25%)       4 (0.12%)            3 (13.0%)            3 (1.09%)   1 (0.62%)   1 (0.51%)            
##     Major problem                    91 (54.2%)         755 (62.9%)     2212 (65.4%)          13 (56.5%)          160 (58.0%) 82 (50.9%)  118 (59.9%)           
##     Minor problem                    61 (36.3%)         381 (31.7%)     995 (29.4%)           6 (26.1%)           71 (25.7%)  67 (41.6%)  61 (31.0%)            
##     Not a problem                    16 (9.52%)          62 (5.16%)     173 (5.11%)           1 (4.35%)           42 (15.2%)  11 (6.83%)  17 (8.63%)            
## DRIVE2_c_W152:                                                                                                                                         <0.001   
##     Don't know/Refused/Web blank      0 (0.00%)          13 (1.08%)      14 (0.41%)           3 (13.0%)            4 (1.45%)   1 (0.62%)   4 (2.03%)            
##     Major problem                    78 (46.4%)         545 (45.4%)     1724 (50.9%)          12 (52.2%)          148 (53.6%) 69 (42.9%)  101 (51.3%)           
##     Minor problem                    64 (38.1%)         557 (46.4%)     1451 (42.9%)          8 (34.8%)           88 (31.9%)  78 (48.4%)  76 (38.6%)            
##     Not a problem                    26 (15.5%)          86 (7.16%)     195 (5.76%)           0 (0.00%)           36 (13.0%)  13 (8.07%)  16 (8.12%)            
## DRIVE2_d_W152:                                                                                                                                         <0.001   
##     Don't know/Refused/Web blank      1 (0.60%)          30 (2.50%)      27 (0.80%)           3 (13.0%)            7 (2.54%)   3 (1.86%)   4 (2.03%)            
##     Major problem                    61 (36.3%)         390 (32.5%)     1293 (38.2%)          10 (43.5%)          91 (33.0%)  52 (32.3%)  66 (33.5%)            
##     Minor problem                    70 (41.7%)         585 (48.7%)     1541 (45.5%)          7 (30.4%)           105 (38.0%) 80 (49.7%)  88 (44.7%)            
##     Not a problem                    36 (21.4%)         196 (16.3%)     523 (15.5%)           3 (13.0%)           73 (26.4%)  26 (16.1%)  39 (19.8%)            
## DRIVE2_e_W152:                                                                                                                                         <0.001   
##     Don't know/Refused/Web blank      0 (0.00%)          2 (0.17%)       5 (0.15%)            3 (13.0%)            2 (0.72%)   0 (0.00%)   1 (0.51%)            
##     Major problem                    108 (64.3%)        919 (76.5%)     2780 (82.2%)          16 (69.6%)          180 (65.2%) 110 (68.3%) 140 (71.1%)           
##     Minor problem                    47 (28.0%)         250 (20.8%)     541 (16.0%)           3 (13.0%)           70 (25.4%)  43 (26.7%)  42 (21.3%)            
##     Not a problem                    13 (7.74%)          30 (2.50%)      58 (1.71%)           1 (4.35%)           24 (8.70%)   8 (4.97%)  14 (7.11%)            
## DRIVE2_f_W152:                                                                                                                                         <0.001   
##     Don't know/Refused/Web blank      0 (0.00%)          6 (0.50%)       6 (0.18%)            3 (13.0%)            3 (1.09%)   0 (0.00%)   2 (1.02%)            
##     Major problem                    87 (51.8%)         527 (43.9%)     1594 (47.1%)          9 (39.1%)           149 (54.0%) 72 (44.7%)  102 (51.8%)           
##     Minor problem                    59 (35.1%)         547 (45.5%)     1432 (42.3%)          10 (43.5%)          78 (28.3%)  66 (41.0%)  79 (40.1%)            
##     Not a problem                    22 (13.1%)         121 (10.1%)     352 (10.4%)           1 (4.35%)           46 (16.7%)  23 (14.3%)  14 (7.11%)            
## ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

Chi-sq Test

g <- "DRIVE2_d_W152"   
vars <- c("F_METRO", "F_CREGION", "F_USR_SELFID", "F_AGECAT", "F_GENDER", "F_EDUCCAT", "F_HISP_ORIGIN", "F_RACETHNMOD", 
          "F_BIRTHPLACE", "F_MARITAL", "F_PARTYLN_FINAL", "F_INC_SDT1", "F_IDEO", "F_INTFREQ") 

chisq_info <- lapply(vars, function(v){
  tab <- table(df1[[v]], df1[[g]])
  ct  <- suppressWarnings(chisq.test(tab, correct = FALSE))
  data.frame(
    var   = v,
    chisq = unname(ct$statistic),
    df    = unname(ct$parameter),
    p     = ct$p.value
  )
})
chisq_info <- do.call(rbind, chisq_info)
chisq_info
##                var     chisq df            p
## 1          F_METRO  30.40942  6 3.285353e-05
## 2        F_CREGION  40.45042 12 6.052041e-05
## 3     F_USR_SELFID  31.54610  9 2.384843e-04
## 4         F_AGECAT 131.70692 12 2.803137e-22
## 5         F_GENDER  47.07426  9 3.801620e-07
## 6        F_EDUCCAT  78.02606  9 3.982282e-13
## 7    F_HISP_ORIGIN  96.86910 30 5.758835e-09
## 8     F_RACETHNMOD 128.32107 15 4.530162e-20
## 9     F_BIRTHPLACE 107.76114 12 1.657185e-17
## 10       F_MARITAL  70.81264 18 3.291239e-08
## 11 F_PARTYLN_FINAL  24.86014  9 3.129855e-03
## 12      F_INC_SDT1 124.43681 27 1.841005e-14
## 13          F_IDEO 153.04878 15 5.976382e-25
## 14       F_INTFREQ  88.46223 18 2.723581e-11

vtree

names(df1)
##  [1] "F_METRO"         "F_CREGION"       "F_USR_SELFID"    "F_AGECAT"       
##  [5] "F_GENDER"        "F_EDUCCAT"       "F_HISP_ORIGIN"   "F_RACETHNMOD"   
##  [9] "F_BIRTHPLACE"    "F_MARITAL"       "F_PARTYLN_FINAL" "F_INC_SDT1"     
## [13] "F_IDEO"          "F_INTFREQ"       "DRIVE2_d_W152"
vtree(df1,"F_GENDER DRIVE2_d_W152", showlegend=TRUE, shownodelabels=FALSE)

vtree(df1,"F_GENDER DRIVE2_d_W152", sameline=TRUE, prunesmaller=25)

#### TABLE FOR VTREE BELOW (Gender and Educat)
df2= df1[, c(6, 15)]
res1 <- compareGroups(F_EDUCCAT ~ ., df2, ref = 1, max.ylev = 50,
                      max.xlev = 50,  chisq.test.perm=TRUE, chisq.test.B=10000)
res2= createTable(res1, show.ratio = TRUE)
res2
## 
## --------Summary descriptives table by 'F_EDUCCAT'---------
## 
## _________________________________________________________________________________________________________ 
##                                  College graduate+ H.S. graduate or less  Refused  Some College p.overall 
##                                       N=2309              N=1458           N=13       N=1630              
## ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ 
## DRIVE2_d_W152:                                                                                   <0.001   
##     Don't know/Refused/Web blank    32 (1.39%)          27 (1.85%)       2 (15.4%)  14 (0.86%)            
##     Major problem                   733 (31.7%)         607 (41.6%)      4 (30.8%) 619 (38.0%)            
##     Minor problem                  1175 (50.9%)         577 (39.6%)      4 (30.8%) 720 (44.2%)            
##     Not a problem                   369 (16.0%)         247 (16.9%)      3 (23.1%) 277 (17.0%)            
## ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
vtree(df1,"F_EDUCCAT DRIVE2_d_W152", sameline=TRUE, prunesmaller=100)

df2= df1[, c(5, 15)]
res1 <- compareGroups(F_GENDER ~ ., df2, ref = 1, max.ylev = 50,
                      max.xlev = 50,  chisq.test.perm=TRUE, chisq.test.B=10000)
res2= createTable(res1, show.ratio = TRUE)
res2
## 
## --------Summary descriptives table by 'F_GENDER'---------
## 
## ________________________________________________________________________________________________ 
##                                     A man       A woman    In some other way  Refused  p.overall 
##                                     N=2689       N=2667          N=31          N=23              
## ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ 
## DRIVE2_d_W152:                                                                          <0.001   
##     Don't know/Refused/Web blank  21 (0.78%)   52 (1.95%)      0 (0.00%)     2 (8.70%)           
##     Major problem                914 (34.0%)  1035 (38.8%)     5 (16.1%)     9 (39.1%)           
##     Minor problem                1279 (47.6%) 1172 (43.9%)    17 (54.8%)     8 (34.8%)           
##     Not a problem                475 (17.7%)  408 (15.3%)      9 (29.0%)     4 (17.4%)           
## ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
vtree(df1,"F_GENDER DRIVE2_d_W152", sameline=TRUE, prunesmaller=100)

### Combined Both
vtree(df1,"F_GENDER F_EDUCCAT DRIVE2_d_W152", sameline=TRUE, prunesmaller=100)

################################

vtree(df1,"F_AGECAT F_EDUCCAT DRIVE2_d_W152", sameline=TRUE, prunesmaller=200)

vtree(df1,"F_GENDER DRIVE2_d_W152", pattern=TRUE)

vtree(df1,"F_USR_SELFID F_INC_SDT1  DRIVE2_d_W152", pattern=TRUE , prunesmaller=75)