Msc_Dataset_Eliza <- read_dta("thesis_database.dta") 
new_column_names <- c("Cluster", "HHNumber", "sampleweight", "year","age","evermarried","psu","stratum","region","urbanrural","highestedu","hieghestyredu","edusingleyrs","eduattain","literacy","childevrborn", "birthfveyrs","birthpastyr","ageatfirstbirth","currPreg","termipreg","contraKnowl","currentcontraceptive","wealthindex", "maritualstatus")

Msc_Dataset_Eliza <- Msc_Dataset_Eliza %>%
  rename_at(vars("v001","v002","v005","v007","v012","v020","v021","v022", "v024","v025","v106","v107","v133","v149","v155","v201","v208","v209","v212","v213","v228","v301a","v312","v190","v501"),
~new_column_names) 
labels_res <- c("Urban", "Rural")
labels_educat <- c("No_education", "Primary", "Secondary", "higher") 
labels_wealthcat <- c("Poorest", "Poorer", "Poor", "Rich", "Richest") 
labels_marital <- c("Never_married", "Married", "Living_with_partner", "Widowed", "Divorced", "Not_living_together")
labels_region <- c("Northern", "Central", "Southern")
labels_yesno <- c("No", "Yes")

Msc_Dataset_Eliza $urbanrural <- factor(Msc_Dataset_Eliza $urbanrural, levels = c(1, 2),
            labels = labels_res)

Msc_Dataset_Eliza $hieghestedu <- factor(Msc_Dataset_Eliza $highestedu, levels = c(0, 1, 2, 3),
             labels = labels_educat)

Msc_Dataset_Eliza $wealthindex <- factor(Msc_Dataset_Eliza $wealthindex, levels = c(1, 2, 3, 4, 5),
              labels = labels_wealthcat)

Msc_Dataset_Eliza $maritualstatus <- factor(Msc_Dataset_Eliza$maritualstatus, levels = c(0, 1, 2, 3, 4, 5),
              labels = labels_marital)

Msc_Dataset_Eliza $region <- factor(Msc_Dataset_Eliza$region, levels = c(1, 2, 3),
              labels = labels_region)
                                
Msc_Dataset_Eliza $everpreg <- factor(Msc_Dataset_Eliza$everpreg, levels = c(0, 1),
              labels = labels_yesno)

Msc_Dataset_Eliza $contraKnowl <- factor(Msc_Dataset_Eliza$contraKnowl, levels = c(0,1),
              labels = labels_yesno)

Msc_Dataset_Eliza$psu <- as.factor(Msc_Dataset_Eliza$psu)
Msc_Dataset_Eliza <- Msc_Dataset_Eliza %>% mutate(wt= sampleweight/1000000)

options(survey.adjust.domain.lonely=TRUE)
options(survey.lonely.psu="adjust")

Datasetwt <- svydesign(
  ids = ~psu, strata= ~stratum, weights= ~wt,
  nest=TRUE, data=Msc_Dataset_Eliza
  )
tab1_summary <- svyCreateTableOne2(
  vars = c("region", "everpreg", "maritualstatus", "hieghestedu", "wealthindex", "contraKnowl", "age"),
  strata = "urbanrural", data = Datasetwt,
  factorVars = c("region", "everpreg", "maritualstatus", "hieghestedu", "wealthindex", "contraKnowl")
)
# view the table
tab1_summary
##                    level                 Urban             Rural             
## n                  ""                    " 2320.6"         " 10339.0"        
## region (%)         "Northern"            "  253.2 (10.9) " "  1325.4 (12.8) "
##                    "Central"             "  950.8 (41.0) " "  4459.5 (43.1) "
##                    "Southern"            " 1116.7 (48.1) " "  4554.1 (44.0) "
## everpreg (%)       "No"                  " 1818.7 (78.4) " "  7216.8 (69.8) "
##                    "Yes"                 "  501.9 (21.6) " "  3122.2 (30.2) "
## maritualstatus (%) "Never_married"       " 1886.2 (81.3) " "  7183.3 (69.5) "
##                    "Married"             "  321.8 (13.9) " "  2499.4 (24.2) "
##                    "Living_with_partner" "   67.7 ( 2.9) " "   304.7 ( 2.9) "
##                    "Widowed"             "    3.8 ( 0.2) " "    19.4 ( 0.2) "
##                    "Divorced"            "   16.2 ( 0.7) " "   126.0 ( 1.2) "
##                    "Not_living_together" "   24.8 ( 1.1) " "   206.3 ( 2.0) "
## hieghestedu (%)    "No_education"        "   33.6 ( 1.4) " "   371.1 ( 3.6) "
##                    "Primary"             " 1061.1 (45.7) " "  8098.1 (78.3) "
##                    "Secondary"           " 1177.2 (50.7) " "  1854.5 (17.9) "
##                    "higher"              "   48.7 ( 2.1) " "    15.2 ( 0.1) "
## wealthindex (%)    "Poorest"             "   63.5 ( 2.7) " "  2184.7 (21.1) "
##                    "Poorer"              "   42.6 ( 1.8) " "  2263.1 (21.9) "
##                    "Poor"                "  123.8 ( 5.3) " "  2352.5 (22.8) "
##                    "Rich"                "  311.0 (13.4) " "  2202.0 (21.3) "
##                    "Richest"             " 1779.7 (76.7) " "  1336.6 (12.9) "
## contraKnowl (%)    "No"                  "   92.3 ( 4.0) " "   932.1 ( 9.0) "
##                    "Yes"                 " 2228.3 (96.0) " "  9407.0 (91.0) "
## age                ""                    "  16.93 ± 1.43"  "   16.90 ± 1.43" 
##                    p        test sig 
## n                  ""       ""   NA  
## region (%)         " 0.323" ""   ""  
##                    ""       ""   NA  
##                    ""       ""   NA  
## everpreg (%)       "<0.001" ""   "**"
##                    ""       ""   NA  
## maritualstatus (%) "<0.001" ""   "**"
##                    ""       ""   NA  
##                    ""       ""   NA  
##                    ""       ""   NA  
##                    ""       ""   NA  
##                    ""       ""   NA  
## hieghestedu (%)    "<0.001" ""   "**"
##                    ""       ""   NA  
##                    ""       ""   NA  
##                    ""       ""   NA  
## wealthindex (%)    "<0.001" ""   "**"
##                    ""       ""   NA  
##                    ""       ""   NA  
##                    ""       ""   NA  
##                    ""       ""   NA  
## contraKnowl (%)    "<0.001" ""   "**"
##                    ""       ""   NA  
## age                " 0.465" ""   ""
#to save the file
html_table <- as_huxtable(tab1_summary)
# Add some formatting
html_table <- set_all_borders(html_table, 1)
#html_table <- set_bold(html_table, where = "top-left-bottom-right", TRUE)
html_table <- set_font_size(html_table, 12)


# Print the formatted HTML table
print(html_table, output = "quarto")
##          ┌────────────┬────────────┬────────────┬────────┬─────┬─────┐
##          │            │ 2320.6     │ 10339.0    │        │     │     │
##          ├────────────┼────────────┼────────────┼────────┼─────┼─────┤
##          │ Northern   │ 253.2      │ 1325.4     │ 0.323  │     │     │
##          │            │ (10.9)     │ (12.8)     │        │     │     │
##          ├────────────┼────────────┼────────────┼────────┼─────┼─────┤
##          │ Central    │ 950.8      │ 4459.5     │        │     │     │
##          │            │ (41.0)     │ (43.1)     │        │     │     │
##          ├────────────┼────────────┼────────────┼────────┼─────┼─────┤
##          │ Southern   │ 1116.7     │ 4554.1     │        │     │     │
##          │            │ (48.1)     │ (44.0)     │        │     │     │
##          ├────────────┼────────────┼────────────┼────────┼─────┼─────┤
##          │ No         │ 1818.7     │ 7216.8     │ <0.001 │     │ **  │
##          │            │ (78.4)     │ (69.8)     │        │     │     │
##          ├────────────┼────────────┼────────────┼────────┼─────┼─────┤
##          │ Yes        │ 501.9      │ 3122.2     │        │     │     │
##          │            │ (21.6)     │ (30.2)     │        │     │     │
##          ├────────────┼────────────┼────────────┼────────┼─────┼─────┤
##          │ Never_marr │ 1886.2     │ 7183.3     │ <0.001 │     │ **  │
##          │ ied        │ (81.3)     │ (69.5)     │        │     │     │
##          ├────────────┼────────────┼────────────┼────────┼─────┼─────┤
##          │ Married    │ 321.8      │ 2499.4     │        │     │     │
##          │            │ (13.9)     │ (24.2)     │        │     │     │
##          ├────────────┼────────────┼────────────┼────────┼─────┼─────┤
##          │ Living_wit │ 67.7 (     │ 304.7 (    │        │     │     │
##          │ h_partner  │ 2.9)       │ 2.9)       │        │     │     │
##          ├────────────┼────────────┼────────────┼────────┼─────┼─────┤
##          │ Widowed    │ 3.8 (      │ 19.4 (     │        │     │     │
##          │            │ 0.2)       │ 0.2)       │        │     │     │
##          ├────────────┼────────────┼────────────┼────────┼─────┼─────┤
##          │ Divorced   │ 16.2 (     │ 126.0 (    │        │     │     │
##          │            │ 0.7)       │ 1.2)       │        │     │     │
##          ├────────────┼────────────┼────────────┼────────┼─────┼─────┤
##          │ Not_living │ 24.8 (     │ 206.3 (    │        │     │     │
##          │ _together  │ 1.1)       │ 2.0)       │        │     │     │
##          ├────────────┼────────────┼────────────┼────────┼─────┼─────┤
##          │ No_educati │ 33.6 (     │ 371.1 (    │ <0.001 │     │ **  │
##          │ on         │ 1.4)       │ 3.6)       │        │     │     │
##          ├────────────┼────────────┼────────────┼────────┼─────┼─────┤
##          │ Primary    │ 1061.1     │ 8098.1     │        │     │     │
##          │            │ (45.7)     │ (78.3)     │        │     │     │
##          ├────────────┼────────────┼────────────┼────────┼─────┼─────┤
##          │ Secondary  │ 1177.2     │ 1854.5     │        │     │     │
##          │            │ (50.7)     │ (17.9)     │        │     │     │
##          ├────────────┼────────────┼────────────┼────────┼─────┼─────┤
##          │ higher     │ 48.7 (     │ 15.2 (     │        │     │     │
##          │            │ 2.1)       │ 0.1)       │        │     │     │
##          ├────────────┼────────────┼────────────┼────────┼─────┼─────┤
##          │ Poorest    │ 63.5 (     │ 2184.7     │ <0.001 │     │ **  │
##          │            │ 2.7)       │ (21.1)     │        │     │     │
##          ├────────────┼────────────┼────────────┼────────┼─────┼─────┤
##          │ Poorer     │ 42.6 (     │ 2263.1     │        │     │     │
##          │            │ 1.8)       │ (21.9)     │        │     │     │
##          ├────────────┼────────────┼────────────┼────────┼─────┼─────┤
##          │ Poor       │ 123.8 (    │ 2352.5     │        │     │     │
##          │            │ 5.3)       │ (22.8)     │        │     │     │
##          ├────────────┼────────────┼────────────┼────────┼─────┼─────┤
##          │ Rich       │ 311.0      │ 2202.0     │        │     │     │
##          │            │ (13.4)     │ (21.3)     │        │     │     │
##          ├────────────┼────────────┼────────────┼────────┼─────┼─────┤
##          │ Richest    │ 1779.7     │ 1336.6     │        │     │     │
##          │            │ (76.7)     │ (12.9)     │        │     │     │
##          ├────────────┼────────────┼────────────┼────────┼─────┼─────┤
##          │ No         │ 92.3 (     │ 932.1 (    │ <0.001 │     │ **  │
##          │            │ 4.0)       │ 9.0)       │        │     │     │
##          ├────────────┼────────────┼────────────┼────────┼─────┼─────┤
##          │ Yes        │ 2228.3     │ 9407.0     │        │     │     │
##          │            │ (96.0)     │ (91.0)     │        │     │     │
##          ├────────────┼────────────┼────────────┼────────┼─────┼─────┤
##          │            │ 16.93 ±    │ 16.90 ±    │ 0.465  │     │     │
##          │            │ 1.43       │ 1.43       │        │     │     │
##          └────────────┴────────────┴────────────┴────────┴─────┴─────┘
## 
## Column names: level, Urban, Rural, p, test, sig
write.csv(tab1_summary, "tab1_demographics.csv")