SDA analysis

PMA 2020-2022 Uganda data

#upload IPUMS extract

library(ipumsr)
library(tidyverse)
Warning: package 'tidyverse' was built under R version 4.4.2
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
ddi=read_ipums_ddi("C:/Users/Rebecca/Downloads/pma_00013.xml")
pmaug=read_ipums_micro(ddi, data_file="C:/Users/Rebecca/Downloads/pma_00013.dat.gz")
Use of data from IPUMS PMA is subject to conditions including that users should cite the data appropriately. Use command `ipums_conditions()` for more details.
unzip("C:/Users/rlutt/Downloads/pma_00013.dat.gz")
Warning in unzip("C:/Users/rlutt/Downloads/pma_00013.dat.gz"): error 1 in
extracting from zip file
#recoding

library(dplyr)

#fertility preference at wave 1: want another child, do not want another child, infertile
pmaug2<-pmaug%>%
  mutate(FERTPREF_1=as.factor(FERTPREF_1))%>%
mutate(wantanotherchild_1= recode(FERTPREF_1, '1' = "have another child" ,'2' = "no more children", '3'="infertile", .default = NA_character_))



#same thing but only for women who were not pregnant at wave 1

pmaug2<-pmaug2%>%
  mutate(FERTPREFNPREG_1=as.factor(FERTPREFNPREG_1))%>%
mutate(wantanotherchildnpregnant_1= recode(FERTPREFNPREG_1, '1' = "have another child" ,'2' = "no more children", '3'="infertile", .default = NA_character_))


#pregnant at wave 2

pmaug2<-pmaug2%>%
  mutate(PREGNANT_2=as.factor(PREGNANT_2))%>%
mutate(pregnant_2= recode(PREGNANT_2, '1' = "pregnant" ,'0' = "not pregnant",.default = NA_character_))

pmaugfilter<-pmaug2%>%
  filter(wantanotherchildnpregnant_1!='infertile')

#pregnancy event at wave 3

pmaugfilter<-pmaugfilter%>%
  mutate(PREGNANT_3=as.factor(PREGNANT_3))%>%
mutate(pregnant_3= recode(PREGNANT_3, '1' = "pregnant" ,'0' = "not pregnant",.default = NA_character_))

#marital status

pmaugfilter<-pmaugfilter%>%
  filter(MARSTAT_1<90)

pmaugfilter<-pmaugfilter%>%
  filter(MARSTAT_2<90)


pmaugfilter<-pmaugfilter%>%
  filter(MARSTAT_3<90)


#age group

pmaugfilter<-pmaugfilter %>%
  mutate(agegroups_1 = case_when(
      AGE_1 >= 15 & AGE_1 <= 19 ~ "15-19",
      AGE_1 >= 20 & AGE_1 <= 25 ~ "20-25",
    AGE_1 >= 26 & AGE_1 <= 30 ~ "26-30",
    AGE_1 >= 31 & AGE_1 <= 35 ~ "31-35",
     AGE_1 >= 36 & AGE_1 <= 40 ~ "36-40",
     AGE_1 >= 41 & AGE_1 <= 44 ~ "41-44",
    AGE_1 >= 45 & AGE_1 <= 49 ~ "45-49"
    ))

pmaugfilter<-pmaugfilter %>%
  mutate(agegroups_2 = case_when(
      AGE_2 >= 15 & AGE_2 <= 19 ~ "15-19",
      AGE_2 >= 20 & AGE_2 <= 25 ~ "20-25",
    AGE_2 >= 26 & AGE_2 <= 30 ~ "26-30",
    AGE_2 >= 31 & AGE_2 <= 35 ~ "31-35",
     AGE_2 >= 36 & AGE_2 <= 40 ~ "36-40",
     AGE_2 >= 41 & AGE_2 <= 44 ~ "41-44",
    AGE_2 >= 45 & AGE_2 <= 49 ~ "45-49"
    ))

pmaugfilter<-pmaugfilter %>%
  mutate(agegroups_3 = case_when(
      AGE_3 >= 15 & AGE_3 <= 19 ~ "15-19",
      AGE_3 >= 20 & AGE_3 <= 25 ~ "20-25",
    AGE_3 >= 26 & AGE_3 <= 30 ~ "26-30",
    AGE_3 >= 31 & AGE_3 <= 35 ~ "31-35",
     AGE_3 >= 36 & AGE_3 <= 40 ~ "36-40",
     AGE_3 >= 41 & AGE_3 <= 44 ~ "41-44",
    AGE_3 >= 45 & AGE_3 <= 49 ~ "45-49"
    ))


#wealth index

pmaugfilter<-pmaugfilter%>%
  mutate(WEALTHQ_1=as.factor(WEALTHQ_1))%>%
mutate(wealth_1= recode(WEALTHQ_1, '1' = "lowest quintile" ,'2' = "lower quintile", '3'= "middle quintile", '4'= 'higher quintile', '5'= 'highest quintile',.default = NA_character_))

pmaugfilter<-pmaugfilter%>%
  mutate(WEALTHQ_2=as.factor(WEALTHQ_2))%>%
mutate(wealth_2= recode(WEALTHQ_2, '1' = "lowest quintile" ,'2' = "lower quintile", '3'= "middle quintile", '4'= 'higher quintile', '5'= 'highest quintile',.default = NA_character_))

pmaugfilter<-pmaugfilter%>%
  mutate(WEALTHQ_3=as.factor(WEALTHQ_3))%>%
mutate(wealth_3= recode(WEALTHQ_3, '1' = "lowest quintile" ,'2' = "lower quintile", '3'= "middle quintile", '4'= 'higher quintile', '5'= 'highest quintile',.default = NA_character_))


#birth events

pmaugfilter<-pmaugfilter%>%
  filter(BIRTHEVENT_2<90)
pmaugfilter<-pmaugfilter%>%
  filter(BIRTHEVENT_3<90)

pmaugfilter$birthbet1and2<-ifelse(pmaugfilter$BIRTHEVENT_2> pmaugfilter$BIRTHEVENT_1, 1, 0)

pmaugfilter$birthbet1and2<-as.factor(pmaugfilter$birthbet1and2)


pmaugfilter$birthbet1and3<-ifelse(pmaugfilter$BIRTHEVENT_3> pmaugfilter$BIRTHEVENT_1, 1, 0)

pmaugfilter$birthbet1and3<-as.factor(pmaugfilter$birthbet1and3)

pmaugfilter$birthbet2and3<-ifelse(pmaugfilter$BIRTHEVENT_3> pmaugfilter$BIRTHEVENT_2, 1, 0)

pmaugfilter$birthbet2and3<-as.factor(pmaugfilter$birthbet2and3)


#decision making: medical

pmaugfilter<-pmaugfilter%>%
  mutate(HHDECMED_1=as.factor(HHDECMED_1))%>%
   mutate(decidemedical_1= recode(HHDECMED_1, '1' = "respondent" ,'2' = "husband/partner", '3'="someone else", .default = NA_character_))

pmaugfilter<-pmaugfilter%>%
  mutate(HHDECMED_2=as.factor(HHDECMED_2))%>%
   mutate(decidemedical_2= recode(HHDECMED_2, '1' = "respondent" ,'2' = "husband/partner", '3'="someone else", .default = NA_character_))

pmaugfilter<-pmaugfilter%>%
  mutate(HHDECMED_3=as.factor(HHDECMED_3))%>%
   mutate(decidemedical_3= recode(HHDECMED_3, '1' = "respondent" ,'2' = "husband/partner", '3'="someone else", .default = NA_character_))

#medical: decision-making events

pmaugfilter$partnerchangedecisionbet1and2med<-ifelse(pmaugfilter$decidemedical_1!= pmaugfilter$decidemedical_2, 1, 0)

pmaugfilter$partnerchangedecisionbet1and2med<-as.factor(pmaugfilter$partnerchangedecisionbet1and2med)

pmaugfilter$partnerchangedecisionbet1and3med<-ifelse(pmaugfilter$decidemedical_1!= pmaugfilter$decidemedical_3, 1, 0)

pmaugfilter$partnerchangedecisionbet1and3med<-as.factor(pmaugfilter$partnerchangedecisionbet1and3med)

pmaugfilter$partnerchangedecisionbet2and3med<-ifelse(pmaugfilter$decidemedical_2!= pmaugfilter$decidemedical_3, 1, 0)

pmaugfilter$partnerchangedecisionbet2and3med<-as.factor(pmaugfilter$partnerchangedecisionbet2and3med)

#decision making: family planning

pmaugfilter<-pmaugfilter%>%
  mutate(FPDECIDER_1=as.factor(FPDECIDER_1))%>%
   mutate(decidefp_1= recode(FPDECIDER_1, '1' = "respondent" ,'2' = "husband/partner", '3'="joint decision", .default = NA_character_))

pmaugfilter<-pmaugfilter%>%
  mutate(FPDECIDER_2=as.factor(FPDECIDER_2))%>%
   mutate(decidefp_2= recode(FPDECIDER_2, '1' = "respondent" ,'2' = "husband/partner", '3'="joint decision", .default = NA_character_))

pmaugfilter<-pmaugfilter%>%
  mutate(FPDECIDER_3=as.factor(FPDECIDER_3))%>%
   mutate(decidefp_3= recode(FPDECIDER_3, '1' = "respondent" ,'2' = "husband/partner", '3'="joint decision", .default = NA_character_))

#FP: decision-making events

pmaugfilter<-pmaugfilter%>%
  filter(BIRTHEVENT_2<90)
pmaugfilter<-pmaugfilter%>%
  filter(BIRTHEVENT_3<90)

pmaugfilter$partnerchangedecisionbet1and2<-ifelse(pmaugfilter$decidefp_1!= pmaugfilter$decidefp_2, 1, 0)

pmaugfilter$partnerchangedecisionbet1and2<-as.factor(pmaugfilter$partnerchangedecisionbet1and2)

pmaugfilter$partnerchangedecisionbet1and3<-ifelse(pmaugfilter$decidefp_1!= pmaugfilter$decidefp_3, 1, 0)

pmaugfilter$partnerchangedecisionbet1and3<-as.factor(pmaugfilter$partnerchangedecisionbet1and3)

pmaugfilter$partnerchangedecisionbet2and3<-ifelse(pmaugfilter$decidefp_2!= pmaugfilter$decidefp_3, 1, 0)

pmaugfilter$partnerchangedecisionbet2and3<-as.factor(pmaugfilter$partnerchangedecisionbet2and3)



#dichotmous variable for having a birth if you wanted one or not having a birth if you didnt want one

pmaugfilter$realizedpreferencebet1and2<-ifelse(pmaugfilter$wantanotherchild_1== "have another child" & pmaugfilter$birthbet1and2==1|pmaugfilter$wantanotherchild_1== "no more children" & pmaugfilter$birthbet1and2==0, 1, 0)

pmaugfilter$realizedpreferencebet1and2<-as.factor(pmaugfilter$realizedpreferencebet1and2)

pmaugfilter$realizedpreferencebet1and3<-ifelse(pmaugfilter$wantanotherchild_1== "have another child"& pmaugfilter$birthbet1and3==0|pmaugfilter$wantanotherchild_1== "no more children" & pmaugfilter$birthbet1and3==0, 1, 0)

pmaugfilter$realizedpreferencebet1and3<-as.factor(pmaugfilter$realizedpreferencebet1and3)


#education

pmaugfilter<-pmaugfilter%>%
  mutate(EDUCATTGEN_1=as.factor(EDUCATTGEN_1))%>%
mutate(educationlevel_1= recode(EDUCATTGEN_1, '1' = "none" ,'2' = "primary/middle school", '3'= "secondary/post-primary", '4'= 'tertiary/ post-secondary',.default = NA_character_))

pmaugfilter<-pmaugfilter%>%
  mutate(EDUCATTGEN_2=as.factor(EDUCATTGEN_2))%>%
mutate(educationlevel_2= recode(EDUCATTGEN_2, '1' = "none" ,'2' = "primary/middle school", '3'= "secondary/post-primary", '4'= 'tertiary/ post-secondary',.default = NA_character_))

pmaugfilter<-pmaugfilter%>%
  mutate(EDUCATTGEN_2=as.factor(EDUCATTGEN_2))%>%
mutate(educationlevel_2= recode(EDUCATTGEN_2, '1' = "none" ,'2' = "primary/middle school", '3'= "secondary/post-primary", '4'= 'tertiary/ post-secondary',.default = NA_character_))

#marital status
pmaugfilter<-pmaugfilter%>%
    mutate(MARSTAT_1=as.factor(MARSTAT_1))%>%
mutate(marital_1= recode(MARSTAT_1, '10' = "never married" ,'20' = "married or living together", '21'= "currently married", '22'= "currently living with partner", '30'="formerly in union",'32'='widow or widower',.default = NA_character_))

#number of children at wave 1
pmaugfilter<-pmaugfilter%>%
  filter(BIRTHEVENT_1<90)


  as.numeric(pmaugfilter$BIRTHEVENT_1)
   [1]  6  7  2  0  5  6  0  0  2  3  4  2  0  5  3  9  2  6  7  3  6  1  2  4
  [25]  1  6  8  0  4  1  4  1  1  4  2  3  2  2  4  0  5  4  0  2  2  1  1  3
  [49]  0  0  0 10  1  1  0  4  2  1  3  6  2  1  2  2  0  5  9  0  2  2  0  3
  [73]  4  0  8  3  9  4  2  8  5  1  1 10  0  0  9  6  3  4  3  2  3  1  2  2
  [97]  2  0  2  0  0  3  3  2  2  3  5  4  0  9  2  9  1 10  0  6  2  2  8  4
 [121]  1  8  6  0  0  0  7  5  2  0  4  3  8  4  8  9  0  4  1  3  0  3  4  3
 [145]  1  7  7  0  6  6  0  1  5  2 10  0  6  0  4  4  7  1  1  4  0  0  1  3
 [169]  1  4  4  9  9  0  1  1  2  6  0  1  9  7  0  7  0  6  3  7  3  8  0  6
 [193]  4  3  1  1  5  5  2  1  6  1  0  1  8  1  4  7  1  9  2  0  7  0  1  2
 [217]  2  6  0  9  3  1  0  6  4  9  5  6  1 10  3  0  7  1  4  2  6  1  1  5
 [241]  1  3  1  0  3  0  1  1  9  3  3  7  0  9  6  1  0  1  4  3  0  2  3  5
 [265]  2  0  1  5  4  4  6  0  4  8  0  7  5  2  2 10  1  0  5  4  6  4  3  0
 [289]  4  3  6  5  5  0  0  8  2  5  2  4  0  4  7  8  1  0  1  9  2  4  7  2
 [313]  2  0  2  8  2  0  4 14  3  2 11  2  0  5  8  3  9  0  0  3  2  1  2  0
 [337]  4  5  5  2  1  4  1  3  0  6  3  1  0  3  0  6  0  8  2  2  2  3  0  6
 [361]  6  3  4  0  0  4  2  4  0  1  6  4  5  0  7  0 10  0  6  4  1  8  2  7
 [385]  0  7  3  6  3  0  0  6  1  1  0  3  6  0  0  1  6  9  0  0  5  6  2  4
 [409]  5  4  0  3  3  5  9  5  2  1  2  4  4  0  3  1  0  5  6  1  2  4  4  3
 [433]  7  4  3  0  0  5  2  4  0  6  0  4  1  4  1  8  4  5  4  2  5  0  0  9
 [457]  2  4  0 10  3  0  1  1 10  9  7  0  3  0  2  2  6  5  0  0  2  1  9  2
 [481]  2  2  3  1  0  5  4  0  0  2  1  1  3  3  6  2  0  2  0  2  5  2  2  2
 [505]  2  3  1  9  3  5  0  4  1  0  8  5  2  1  3  1  1  5  1  1  3  2  0  5
 [529]  6  2  0 12  2  0  1  3  3  0  1  0  7  2 10  6  2  4  2  3  4  8  0  1
 [553]  6  2  7  6  8  7  3  8  5  0  4 11  0  1  2  9  6  5  1  3  2  2  1  3
 [577]  6  4  0  0  0 13  1  0  3  3  9  4  3  3  0  5  1  0  1  3  2  4  0  4
 [601]  6  5  4  8  3  1  8  3  3  3  8  3  3  1  4  3  2  5  0  3  2  6  0  1
 [625]  1  1  3  0  3  0  5  4  0  7  3 10  2  4  0  5  0  0  6  6  0  0  3  3
 [649]  1  4  7  6  1  0  4  2  2  4  3  9  1  4  2  4  8  4  5  1  5  4  8  0
 [673]  8  2  3  4  6  6  0  5  0  2  8 10  5  4  3  0 12  3  2  3  3  3  3  0
 [697]  3  3  9  1  3  9  2  1  9  4  5  2 10  4  3  5  4  2  0  5  1  0  2  0
 [721]  8  7  5  7  0  1  0  1  8  5  0  0  3  3  2  0  0  7  1  2  1  5  1  1
 [745]  0  0  2  1  1  4  4  0  0  0  6  3  1  0  9  4  3  3  0  0  1  6  3  1
 [769]  9  7  3  0  2  1  0  0  5  6  4  7  0  2  2  6  2  9  6  6  0  8  6  4
 [793]  1  5  1  5  2 11  1  5  9  8  2  3  3  0  5 10  7  2  1  5  0  0  1  6
 [817]  2  3  1  2  4  0  3  1  3  8  1 14  1  2  2  2  1  3  5  6  0  7  2  2
 [841]  0  2  0  3  4  1  1  5  2  0  3  4  3  3  1  1  6  5  4  0  2  2  6  1
 [865]  7  4  5  0  7  3  4  2  2 11  0  0 10  0  4 10  1  8  1  7  0  1  5  2
 [889]  2  5  0  2  5  0  0  5  0  4  3  0  2  5  8 11  0  4 11  4  7  8  0  6
 [913]  5  0  3  5  2  5  2  2  1  0 12  0  8  1  8  7  6  0  2  0  1  2  4  4
 [937]  0  2  9  4  6  3  4  2  0  3  7  2  1  6  5 11  0  4  3  3  2  1  1  3
 [961]  6  3  1  6  0  4  4  0  3  7  8  0  1  8  0  6  0  6  5  3  1  2  4  9
 [985]  1  4  3  6  0  3  3  4  2  1  0  1  4  4  3  5  7  0  6  1  2  4  6  6
[1009]  7  2  0  6 11  5  0  4  2  1  1  0  7  1  6  6  6 11  0  0  0  0  8  1
[1033]  6  2  5  7  4  7  3  1  7  4  3  2  9  0  0  1  7  1  8  1  1  8  3  3
[1057]  1  4  0  0  3  0  5  0  3  0  0  6  0  0  4  0  1  6  0  3  1  0  0  8
[1081]  1  2  3  6  4  4  7  1  5  0  0  6  3  5  4  2  1 11  0  7  0  2  2  7
[1105]  6  5  3  2  1  1  1  5  4  8  5  2  0  1  2  7  1  1  3  2  6  2  2  2
[1129]  8  3  1  7  2  2  0  0  5  3  0  0  5  9  0  1  7  5  3  7  4  1  5  3
[1153]  3  1  2  0  0  4  3  0  6  2  3  8  0  1  9  3  0  4  7  3  6  6  6  1
[1177]  5  2  1  0  6  7  3  3  0  0  5  4  6 10  0  6  2  5  1  1  4  2  0  0
[1201]  2  0  6  2  0  9  6  4  0  6  0  2  6  0  6  4  0  2  0  5  2  4  0  5
[1225] 10  7  1  5 10  4  5  2  2 10  9  4  9  6  0  5  0  6  1  1  6  0  3  4
[1249]  2  0  7  5  2  4  4  6  2  1  6  7  4  5  1  7  1  4  1  1  3  1  0  3
[1273]  1 11  3  0  8  1  8  2  6  1  4  3  0  0  7  0  5  2  5 10  0  3  3  4
[1297]  4  2  0  6  6  2  2  1  4  4  0  2  3  1  4  0  6  7  2  8  1  4  3  4
[1321]  5  2  0  1  4  1  2  6  2  0  2  2  4  0  2  1  3  1  0  3  6  2  5  1
[1345]  0  1  1  4  0  0  3  3  0  6  0  2  1  2  4  3  2  1 10  3  0  2  1  5
[1369]  3  4  1  5  1  0  6  0  0  0  2  9 11  1  2  5  2  6  4  3  5  4  0  2
[1393]  0  1  4  6  1  5  4  5  3  1  2  1  3  4  0  4  9  7  4  1  4  3  0  1
[1417]  2  1  9  2  4 10  7  2  0  1  0  3  1  5  4  7  5  3  0  7  0  0  3  2
[1441]  2  2  9  1  5  0  1  4  2  2  0  3  6  0  0  0  8  3  5  2  0  6  1  4
[1465]  4  1  1  0  6  0  2  7  2  6  0  0  3  4  1  1  2  5  4  1 12  0  8  8
[1489]  1  7  6  2  2  8  4  0  1  3  0  0  6  0  0  4  5  4  0  6  4  0  0  0
[1513]  3  2  5 10  3  0  6  2  0  2  2  6  4  1  2  2  7  2  5  5  1  6  1  7
[1537]  3  1  8  0  0  2  0  6  0  5  3  0  5  3  7  4  1  2  5  1  1  3  6  6
[1561]  6  2  1  4  6  5  4  5  0  2  3  5  2  0  6  4  2  6  2  4  4  2  2  7
[1585]  0  2  3  3  3  3  6  1  0  6  5  3  0  5  2  1  2  2  0  8  5  4  0  3
[1609]  1  8  0  6  0  0  0  5  0  5  3  3  1  0  6  0  7  0  7  8  1  2  2  5
[1633]  4  1  1  4  0  0  0  7  1  4  5  5  0  0  4  8  9  0  1  3  0  2  4  2
[1657]  3  2  6  2  3  7  2  0  0  9  0  1  3  0  8  5  7  0  4  5 10  5  4  7
[1681]  0  0  5  5 13  0  4  1  6  4  5  6  4  6  6  0  1  0  7  0  7  2  6  6
[1705]  0  7  1  1  4  3  2  1  1  2  3  8  2  0  0  8  3  8  1  2  8  6  0  7
[1729]  3  3  1  2  0  0  1  0  8  3  7  8  3  3  5  0  7  3  5  2  1  5  4  0
[1753]  1  6  7  0  0  1  0  4  2  3  0  3  1  0  9  2  6  3  6  2  1  2  6  3
[1777] 10  5  7  1  6  4  0  6  7  2  1  7  2  9  1  6  4  4  1  4  5  2  0  3
[1801]  0  4  2  4  9  1  1  2  7  4  4  4  0  0  0  5  2  3  8  1  2  1  3  1
[1825]  3  4  3  0 10  0  0  2  4  9  0  4  7  4  0  2  1  3  0  2  0  4  5  0
[1849]  8  2  0  8  1  1  8  4  5  6  0  7  8  3  4  0  4  4  1  1 11  5  3  4
[1873]  8  5  6  7  0  5  2  3  7  3  3  1  1  5  0  8  3  0  8  8  3  5  9  2
[1897]  5  6  0  3  0  4  0  6  0  0  1  3  1  0  1  7  4  0  1  2  1  3 11  4
[1921]  1  1  2  0  6  7  1  1  4  4  6  3  1  3  5  5  3  1  2  7  2  9  0  8
[1945]  5  1  3  0  4  1  6  4  0  0  0  5  0  0  1  1  1  9  0  3  0  4  0  7
[1969]  0  8  3  0  1  2  6  3  3  3  0  4  2  3  5  0  0  0  4  1  1  2  2  1
[1993]  3  0  1  0  1  0  3  5  1 10  2  3  0  8  1  5  3  1  0  0  1  2  0  7
[2017]  5  8  0  1  4  5
#urban/ rural status
  
pmaugfilter<-pmaugfilter%>%
  mutate(URBAN=as.factor(URBAN))%>%
mutate(urbanrec= recode(URBAN, '0' = "rural" ,'1' = "urban",.default = NA_character_))

#electricity

pmaugfilter<-pmaugfilter%>%
  mutate(ELECTRC_1=as.factor(ELECTRC_1))%>%
mutate(electricity= recode(ELECTRC_1, '0' = "no electricity" ,'1' = "electricity",.default = NA_character_))


#contraceptive use

pmaugfilter<-pmaugfilter%>%
  mutate(MCP_1=as.factor(MCP_1))%>%
mutate(moderncon_1= recode(MCP_1, '1' = "yes" ,'0' = "no",.default = NA_character_))
library(survey)
Loading required package: grid
Loading required package: Matrix

Attaching package: 'Matrix'
The following objects are masked from 'package:tidyr':

    expand, pack, unpack
Loading required package: survival

Attaching package: 'survey'
The following object is masked from 'package:graphics':

    dotchart
library(srvyr)

Attaching package: 'srvyr'
The following object is masked from 'package:stats':

    filter
library(dplyr)

#survey design

design1to2<-pmaugfilter %>%  
as_survey_design( 
weight = PANEL1_2_WEIGHT,  
id = EAWEIGHT_2,  
strata = STRATA_2)


design1to3<-pmaugfilter %>%  
as_survey_design( 
weight = PANEL1_3_WEIGHT,  
id = EAWEIGHT_3,  
strata = STRATA_3)
#calculate means


library(ggplot2)

#fertprefprobs <- svyby(~birthbet1and2, by = ~wantanotherchildnpregnant_1 + pmaugfilter$AGE, design = design, 
                   #FUN = svymean, na.rm = TRUE)

#fertprefprobs$lim <- 2*fertprefprobs$se
#descriptive analysis

library(janitor)

Attaching package: 'janitor'
The following objects are masked from 'package:stats':

    chisq.test, fisher.test
#pregnancy and birth by fertility preferences

tabyl(pmaugfilter, wantanotherchildnpregnant_1, pregnant_2)
 wantanotherchildnpregnant_1 not pregnant pregnant NA_
          have another child         1197      186  22
            no more children          588       23   6
                   infertile            0        0   0
tabyl(pmaugfilter, wantanotherchildnpregnant_1, pregnant_3)
 wantanotherchildnpregnant_1 not pregnant pregnant NA_
          have another child         1235      153  17
            no more children          592       23   2
                   infertile            0        0   0
tabyl(pmaugfilter, wantanotherchildnpregnant_1, birthbet1and2)
 wantanotherchildnpregnant_1    0   1
          have another child 1140 265
            no more children  509 108
                   infertile    0   0
tabyl(pmaugfilter, wantanotherchildnpregnant_1, birthbet1and3)
 wantanotherchildnpregnant_1   0   1
          have another child 874 531
            no more children 452 165
                   infertile   0   0
#pregnancy and birth by decision-making


tabyl(pmaugfilter, decidemedical_1, pregnant_2)
 decidemedical_1 not pregnant pregnant NA_
      respondent          316       33   4
 husband/partner          481       88  11
    someone else          334       37   5
            <NA>          654       51   8
tabyl(pmaugfilter, decidemedical_1, pregnant_3)
 decidemedical_1 not pregnant pregnant NA_
      respondent          319       33   1
 husband/partner          519       53   8
    someone else          333       38   5
            <NA>          656       52   5
tabyl(pmaugfilter, decidemedical_1, birthbet1and2)
 decidemedical_1   0   1
      respondent 277  76
 husband/partner 450 130
    someone else 299  77
            <NA> 623  90
tabyl(pmaugfilter, decidemedical_1, birthbet1and3)
 decidemedical_1   0   1
      respondent 226 127
 husband/partner 324 256
    someone else 240 136
            <NA> 536 177
#visual of those who wanted another child that got pregnant
library(ggplot2)

#calculate the rate of becoming pregnant by fertility preference

library(stats)

#model for becoming pregnant at wave 2 given wave 1 preference

test<-glm(pregnant_2~wantanotherchildnpregnant_1+factor(MARSTAT_1)+ agegroups_1 +decidemedical_1, family=binomial, data=pmaugfilter)

summary(test)

Call:
glm(formula = pregnant_2 ~ wantanotherchildnpregnant_1 + factor(MARSTAT_1) + 
    agegroups_1 + decidemedical_1, family = binomial, data = pmaugfilter)

Coefficients:
                                             Estimate Std. Error z value
(Intercept)                                  -1.34160    0.37123  -3.614
wantanotherchildnpregnant_1no more children  -0.81705    0.29343  -2.784
factor(MARSTAT_1)22                           0.06754    0.18283   0.369
agegroups_120-25                             -0.19695    0.31729  -0.621
agegroups_126-30                             -0.47099    0.33053  -1.425
agegroups_131-35                             -0.81404    0.36622  -2.223
agegroups_136-40                             -0.82431    0.40491  -2.036
agegroups_141-44                             -2.03540    0.80704  -2.522
agegroups_145-49                            -15.59226  450.48912  -0.035
decidemedical_1husband/partner                0.27547    0.22635   1.217
decidemedical_1someone else                  -0.09850    0.25896  -0.380
                                            Pr(>|z|)    
(Intercept)                                 0.000302 ***
wantanotherchildnpregnant_1no more children 0.005361 ** 
factor(MARSTAT_1)22                         0.711832    
agegroups_120-25                            0.534786    
agegroups_126-30                            0.154170    
agegroups_131-35                            0.026226 *  
agegroups_136-40                            0.041773 *  
agegroups_141-44                            0.011667 *  
agegroups_145-49                            0.972389    
decidemedical_1husband/partner              0.223606    
decidemedical_1someone else                 0.703660    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 959.08  on 1288  degrees of freedom
Residual deviance: 881.00  on 1278  degrees of freedom
  (733 observations deleted due to missingness)
AIC: 903

Number of Fisher Scoring iterations: 16
#model for becoming pregnant at wave 3 given wave 1 preference
test2<-glm(pregnant_3~wantanotherchildnpregnant_1+factor(MARSTAT_1)+ agegroups_1+decidemedical_1, family=binomial, data=pmaugfilter)

summary(test2)

Call:
glm(formula = pregnant_3 ~ wantanotherchildnpregnant_1 + factor(MARSTAT_1) + 
    agegroups_1 + decidemedical_1, family = binomial, data = pmaugfilter)

Coefficients:
                                             Estimate Std. Error z value
(Intercept)                                  -1.80932    0.45237  -4.000
wantanotherchildnpregnant_1no more children  -0.28295    0.29286  -0.966
factor(MARSTAT_1)22                          -0.07808    0.19937  -0.392
agegroups_120-25                              0.29457    0.41215   0.715
agegroups_126-30                              0.20275    0.41828   0.485
agegroups_131-35                             -0.21874    0.44909  -0.487
agegroups_136-40                             -0.91550    0.52861  -1.732
agegroups_141-44                             -2.55621    1.10777  -2.308
agegroups_145-49                            -15.35272  450.02501  -0.034
decidemedical_1husband/partner               -0.28652    0.24210  -1.183
decidemedical_1someone else                  -0.03969    0.25710  -0.154
                                            Pr(>|z|)    
(Intercept)                                 6.34e-05 ***
wantanotherchildnpregnant_1no more children   0.3340    
factor(MARSTAT_1)22                           0.6953    
agegroups_120-25                              0.4748    
agegroups_126-30                              0.6279    
agegroups_131-35                              0.6262    
agegroups_136-40                              0.0833 .  
agegroups_141-44                              0.0210 *  
agegroups_145-49                              0.9728    
decidemedical_1husband/partner                0.2366    
decidemedical_1someone else                   0.8773    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 817.53  on 1294  degrees of freedom
Residual deviance: 762.00  on 1284  degrees of freedom
  (727 observations deleted due to missingness)
AIC: 784

Number of Fisher Scoring iterations: 16
#model for having a birth between wave 1 and 2

testa<-glm(birthbet1and2~wantanotherchildnpregnant_1+factor(MARSTAT_1)+ agegroups_1+decidemedical_1, family=binomial, data=pmaugfilter)

summary(testa)

Call:
glm(formula = birthbet1and2 ~ wantanotherchildnpregnant_1 + factor(MARSTAT_1) + 
    agegroups_1 + decidemedical_1, family = binomial, data = pmaugfilter)

Coefficients:
                                             Estimate Std. Error z value
(Intercept)                                 -1.040472   0.330857  -3.145
wantanotherchildnpregnant_1no more children -0.176208   0.183835  -0.959
factor(MARSTAT_1)22                         -0.147040   0.139321  -1.055
agegroups_120-25                            -0.168682   0.313900  -0.537
agegroups_126-30                            -0.099096   0.315774  -0.314
agegroups_131-35                             0.002958   0.325231   0.009
agegroups_136-40                             0.024132   0.343081   0.070
agegroups_141-44                            -0.465347   0.422618  -1.101
agegroups_145-49                            -0.142337   0.439228  -0.324
decidemedical_1husband/partner               0.022820   0.167914   0.136
decidemedical_1someone else                 -0.083076   0.183567  -0.453
                                            Pr(>|z|)   
(Intercept)                                  0.00166 **
wantanotherchildnpregnant_1no more children  0.33780   
factor(MARSTAT_1)22                          0.29124   
agegroups_120-25                             0.59101   
agegroups_126-30                             0.75366   
agegroups_131-35                             0.99274   
agegroups_136-40                             0.94392   
agegroups_141-44                             0.27085   
agegroups_145-49                             0.74589   
decidemedical_1husband/partner               0.89190   
decidemedical_1someone else                  0.65086   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 1366.7  on 1308  degrees of freedom
Residual deviance: 1360.0  on 1298  degrees of freedom
  (713 observations deleted due to missingness)
AIC: 1382

Number of Fisher Scoring iterations: 4
#model for having a birth between wave 1 and 3
testb<-glm(birthbet1and3~wantanotherchildnpregnant_1+factor(MARSTAT_1)+ agegroups_1+decidemedical_1, family=binomial, data=pmaugfilter)

summary(testb)

Call:
glm(formula = birthbet1and3 ~ wantanotherchildnpregnant_1 + factor(MARSTAT_1) + 
    agegroups_1 + decidemedical_1, family = binomial, data = pmaugfilter)

Coefficients:
                                            Estimate Std. Error z value
(Intercept)                                  0.32247    0.28585   1.128
wantanotherchildnpregnant_1no more children -0.36139    0.15926  -2.269
factor(MARSTAT_1)22                         -0.20295    0.12006  -1.690
agegroups_120-25                            -0.36736    0.26783  -1.372
agegroups_126-30                            -0.41191    0.27060  -1.522
agegroups_131-35                            -0.59840    0.28103  -2.129
agegroups_136-40                            -0.82765    0.29912  -2.767
agegroups_141-44                            -1.21473    0.36409  -3.336
agegroups_145-49                            -0.86154    0.38188  -2.256
decidemedical_1husband/partner               0.16741    0.14456   1.158
decidemedical_1someone else                 -0.08556    0.15811  -0.541
                                            Pr(>|z|)    
(Intercept)                                 0.259274    
wantanotherchildnpregnant_1no more children 0.023254 *  
factor(MARSTAT_1)22                         0.090959 .  
agegroups_120-25                            0.170182    
agegroups_126-30                            0.127954    
agegroups_131-35                            0.033227 *  
agegroups_136-40                            0.005658 ** 
agegroups_141-44                            0.000849 ***
agegroups_145-49                            0.024068 *  
decidemedical_1husband/partner              0.246841    
decidemedical_1someone else                 0.588436    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 1758.1  on 1308  degrees of freedom
Residual deviance: 1704.2  on 1298  degrees of freedom
  (713 observations deleted due to missingness)
AIC: 1726.2

Number of Fisher Scoring iterations: 4
#descriptive analysis: maybe group by age, wealth index, religion at first wave

#how many women were pregnant at the first wave, how many children on average did women by age group have at the first wave

#how many pregnant at the second/third wave, how many children on average did women by age group have at the second/third wave

#how many women that indicated they want another child at the first wave acutally have a child, how many women that indicated they did not want anotehr child actually have a child by the second/third wave
#logistic regression

#binomial outcome: had another child between first and second/third wave

#predictors: did want a child, did not want a child, decision making, age
tabyl(pmaugfilter, agegroups_1, wantanotherchildnpregnant_1)
 agegroups_1 have another child no more children infertile
       15-19                334               19         0
       20-25                428               22         0
       26-30                307               63         0
       31-35                191              112         0
       36-40                112              167         0
       41-44                 24              131         0
       45-49                  9              103         0
tabyl(pmaugfilter, agegroups_1, pregnant_2)
 agegroups_1 not pregnant pregnant NA_
       15-19          310       38   5
       20-25          366       74  10
       26-30          317       51   2
       31-35          273       25   5
       36-40          258       18   3
       41-44          150        3   2
       45-49          111        0   1
tabyl(pmaugfilter, agegroups_1, pregnant_3)
 agegroups_1 not pregnant pregnant NA_
       15-19          322       30   1
       20-25          381       59  10
       26-30          320       48   2
       31-35          277       25   1
       36-40          262       13   4
       41-44          153        1   1
       45-49          112        0   0
tabyl(pmaugfilter, agegroups_1, birthbet1and2)
 agegroups_1   0  1
       15-19 314 39
       20-25 366 84
       26-30 295 75
       31-35 233 70
       36-40 214 65
       41-44 135 20
       45-49  92 20
tabyl(pmaugfilter, agegroups_1, birthbet1and3)
 agegroups_1   0   1
       15-19 255  98
       20-25 264 186
       26-30 217 153
       31-35 191 112
       36-40 190  89
       41-44 126  29
       45-49  83  29
#model for having a birth between wave 1 and 2

testx<-glm(birthbet1and2~wantanotherchildnpregnant_1+agegroups_1 +partnerchangedecisionbet1and2+educationlevel_1, family=binomial, data=pmaugfilter)

summary(testx)

Call:
glm(formula = birthbet1and2 ~ wantanotherchildnpregnant_1 + agegroups_1 + 
    partnerchangedecisionbet1and2 + educationlevel_1, family = binomial, 
    data = pmaugfilter)

Coefficients:
                                            Estimate Std. Error z value
(Intercept)                                  -1.4368     0.7438  -1.932
wantanotherchildnpregnant_1no more children  -0.2577     0.3131  -0.823
agegroups_120-25                             -0.3361     0.6207  -0.542
agegroups_126-30                              0.1554     0.5964   0.261
agegroups_131-35                              0.6058     0.6019   1.007
agegroups_136-40                              0.4320     0.6467   0.668
agegroups_141-44                              0.1022     0.7336   0.139
agegroups_145-49                              0.8322     0.7366   1.130
partnerchangedecisionbet1and21                0.2531     0.2348   1.078
educationlevel_1primary/middle school        -0.4461     0.5034  -0.886
educationlevel_1secondary/post-primary       -0.7437     0.5549  -1.340
educationlevel_1tertiary/ post-secondary     -0.2190     0.6263  -0.350
                                            Pr(>|z|)  
(Intercept)                                   0.0534 .
wantanotherchildnpregnant_1no more children   0.4104  
agegroups_120-25                              0.5881  
agegroups_126-30                              0.7944  
agegroups_131-35                              0.3142  
agegroups_136-40                              0.5041  
agegroups_141-44                              0.8892  
agegroups_145-49                              0.2586  
partnerchangedecisionbet1and21                0.2810  
educationlevel_1primary/middle school         0.3755  
educationlevel_1secondary/post-primary        0.1802  
educationlevel_1tertiary/ post-secondary      0.7265  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 501.95  on 573  degrees of freedom
Residual deviance: 489.40  on 562  degrees of freedom
  (1448 observations deleted due to missingness)
AIC: 513.4

Number of Fisher Scoring iterations: 4
#model for having a birth between wave 1 and 3
testy<-glm(birthbet1and3~wantanotherchildnpregnant_1+agegroups_1+decidefp_1 +partnerchangedecisionbet1and3+educationlevel_1, family=binomial, data=pmaugfilter)

summary(testy)

Call:
glm(formula = birthbet1and3 ~ wantanotherchildnpregnant_1 + agegroups_1 + 
    decidefp_1 + partnerchangedecisionbet1and3 + educationlevel_1, 
    family = binomial, data = pmaugfilter)

Coefficients:
                                            Estimate Std. Error z value
(Intercept)                                  0.41895    0.65967   0.635
wantanotherchildnpregnant_1no more children -0.30624    0.29278  -1.046
agegroups_120-25                            -0.74536    0.41492  -1.796
agegroups_126-30                            -0.52652    0.40815  -1.290
agegroups_131-35                            -0.69546    0.44493  -1.563
agegroups_136-40                            -1.30863    0.48309  -2.709
agegroups_141-44                            -1.66527    0.58074  -2.868
agegroups_145-49                            -0.71258    0.58456  -1.219
decidefp_1husband/partner                    0.09538    0.31844   0.300
decidefp_1joint decision                    -0.31702    0.23042  -1.376
partnerchangedecisionbet1and31              -0.17604    0.22542  -0.781
educationlevel_1primary/middle school        0.06492    0.56820   0.114
educationlevel_1secondary/post-primary      -0.04748    0.60277  -0.079
educationlevel_1tertiary/ post-secondary     0.43092    0.66865   0.644
                                            Pr(>|z|)   
(Intercept)                                  0.52537   
wantanotherchildnpregnant_1no more children  0.29559   
agegroups_120-25                             0.07243 . 
agegroups_126-30                             0.19704   
agegroups_131-35                             0.11804   
agegroups_136-40                             0.00675 **
agegroups_141-44                             0.00414 **
agegroups_145-49                             0.22285   
decidefp_1husband/partner                    0.76455   
decidefp_1joint decision                     0.16887   
partnerchangedecisionbet1and31               0.43485   
educationlevel_1primary/middle school        0.90903   
educationlevel_1secondary/post-primary       0.93721   
educationlevel_1tertiary/ post-secondary     0.51928   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 584.57  on 455  degrees of freedom
Residual deviance: 553.41  on 442  degrees of freedom
  (1566 observations deleted due to missingness)
AIC: 581.41

Number of Fisher Scoring iterations: 4

t

#model for having a birth between wave 1 and 2

testw<-glm(birthbet1and2~wantanotherchildnpregnant_1+agegroups_1+partnerchangedecisionbet1and2med, family=binomial, data=pmaugfilter)

summary(testw)

Call:
glm(formula = birthbet1and2 ~ wantanotherchildnpregnant_1 + agegroups_1 + 
    partnerchangedecisionbet1and2med, family = binomial, data = pmaugfilter)

Coefficients:
                                            Estimate Std. Error z value
(Intercept)                                 -1.09126    0.28565  -3.820
wantanotherchildnpregnant_1no more children -0.16665    0.18798  -0.887
agegroups_120-25                            -0.19513    0.31728  -0.615
agegroups_126-30                            -0.12221    0.31581  -0.387
agegroups_131-35                            -0.04989    0.32511  -0.153
agegroups_136-40                            -0.00778    0.34280  -0.023
agegroups_141-44                            -0.53117    0.42244  -1.257
agegroups_145-49                            -0.25089    0.45040  -0.557
partnerchangedecisionbet1and2med1            0.03947    0.13831   0.285
                                            Pr(>|z|)    
(Intercept)                                 0.000133 ***
wantanotherchildnpregnant_1no more children 0.375345    
agegroups_120-25                            0.538552    
agegroups_126-30                            0.698772    
agegroups_131-35                            0.878043    
agegroups_136-40                            0.981892    
agegroups_141-44                            0.208616    
agegroups_145-49                            0.577502    
partnerchangedecisionbet1and2med1           0.775342    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 1310.8  on 1241  degrees of freedom
Residual deviance: 1304.7  on 1233  degrees of freedom
  (780 observations deleted due to missingness)
AIC: 1322.7

Number of Fisher Scoring iterations: 4
#model for having a birth between wave 1 and 3
testv<-glm(birthbet1and3~wantanotherchildnpregnant_1+agegroups_1 +partnerchangedecisionbet1and3med, family=binomial, data=pmaugfilter)

summary(testv)

Call:
glm(formula = birthbet1and3 ~ wantanotherchildnpregnant_1 + agegroups_1 + 
    partnerchangedecisionbet1and3med, family = binomial, data = pmaugfilter)

Coefficients:
                                            Estimate Std. Error z value
(Intercept)                                  0.24401    0.25155   0.970
wantanotherchildnpregnant_1no more children -0.33172    0.16247  -2.042
agegroups_120-25                            -0.33244    0.27647  -1.202
agegroups_126-30                            -0.34480    0.27721  -1.244
agegroups_131-35                            -0.58451    0.28714  -2.036
agegroups_136-40                            -0.80105    0.30314  -2.643
agegroups_141-44                            -1.25303    0.37297  -3.360
agegroups_145-49                            -0.93132    0.39448  -2.361
partnerchangedecisionbet1and3med1            0.01348    0.11983   0.113
                                            Pr(>|z|)    
(Intercept)                                 0.332038    
wantanotherchildnpregnant_1no more children 0.041183 *  
agegroups_120-25                            0.229191    
agegroups_126-30                            0.213561    
agegroups_131-35                            0.041788 *  
agegroups_136-40                            0.008229 ** 
agegroups_141-44                            0.000781 ***
agegroups_145-49                            0.018232 *  
partnerchangedecisionbet1and3med1           0.910427    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 1653.3  on 1226  degrees of freedom
Residual deviance: 1605.0  on 1218  degrees of freedom
  (795 observations deleted due to missingness)
AIC: 1623

Number of Fisher Scoring iterations: 4
# Change reference groups
pmaugfilter$marital_1 <- relevel(pmaugfilter$marital_1, ref = "currently married")


library(gtsummary)

#final analysis: realizing preferences wave 1, wave 1 and 3


testz<-glm(realizedpreferencebet1and2~agegroups_1+marital_1+educationlevel_1+BIRTHEVENT_1+ urbanrec +moderncon_1, family=binomial, data=pmaugfilter)

summary(testz)

Call:
glm(formula = realizedpreferencebet1and2 ~ agegroups_1 + marital_1 + 
    educationlevel_1 + BIRTHEVENT_1 + urbanrec + moderncon_1, 
    family = binomial, data = pmaugfilter)

Coefficients:
                                          Estimate Std. Error z value Pr(>|z|)
(Intercept)                              -1.852147   0.309476  -5.985 2.17e-09
agegroups_120-25                          0.172519   0.225812   0.764 0.444872
agegroups_126-30                          0.330998   0.252293   1.312 0.189533
agegroups_131-35                          0.499809   0.275610   1.813 0.069761
agegroups_136-40                          1.052458   0.296308   3.552 0.000382
agegroups_141-44                          1.734691   0.336933   5.148 2.63e-07
agegroups_145-49                          1.869438   0.376886   4.960 7.04e-07
marital_1never married                   -0.416850   0.226392  -1.841 0.065581
marital_1currently living with partner   -0.019281   0.125369  -0.154 0.877774
marital_1widow or widower                 0.881354   0.376333   2.342 0.019183
educationlevel_1primary/middle school     0.430995   0.208254   2.070 0.038493
educationlevel_1secondary/post-primary    0.233486   0.232619   1.004 0.315511
educationlevel_1tertiary/ post-secondary  0.192912   0.297828   0.648 0.517159
BIRTHEVENT_1                              0.132026   0.034540   3.822 0.000132
urbanrecurban                             0.119500   0.125328   0.954 0.340337
moderncon_1yes                            0.008108   0.116158   0.070 0.944349
                                            
(Intercept)                              ***
agegroups_120-25                            
agegroups_126-30                            
agegroups_131-35                         .  
agegroups_136-40                         ***
agegroups_141-44                         ***
agegroups_145-49                         ***
marital_1never married                   .  
marital_1currently living with partner      
marital_1widow or widower                *  
educationlevel_1primary/middle school    *  
educationlevel_1secondary/post-primary      
educationlevel_1tertiary/ post-secondary    
BIRTHEVENT_1                             ***
urbanrecurban                               
moderncon_1yes                              
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 2343.2  on 1769  degrees of freedom
Residual deviance: 1973.2  on 1754  degrees of freedom
  (252 observations deleted due to missingness)
AIC: 2005.2

Number of Fisher Scoring iterations: 4
tbl_regression(testz, exponentiate = TRUE)
Characteristic OR1 95% CI1 p-value
agegroups_1


    15-19
    20-25 1.19 0.76, 1.86 0.4
    26-30 1.39 0.85, 2.29 0.2
    31-35 1.65 0.96, 2.84 0.070
    36-40 2.86 1.61, 5.14 <0.001
    41-44 5.67 2.95, 11.1 <0.001
    45-49 6.48 3.13, 13.8 <0.001
marital_1


    currently married
    never married 0.66 0.42, 1.02 0.066
    currently living with partner 0.98 0.77, 1.26 0.9
    widow or widower 2.41 1.19, 5.26 0.019
educationlevel_1


    none
    primary/middle school 1.54 1.03, 2.32 0.038
    secondary/post-primary 1.26 0.80, 2.00 0.3
    tertiary/ post-secondary 1.21 0.67, 2.17 0.5
Number of birth events 1.14 1.07, 1.22 <0.001
urbanrec


    rural
    urban 1.13 0.88, 1.44 0.3
moderncon_1


    no
    yes 1.01 0.80, 1.27 >0.9
1 OR = Odds Ratio, CI = Confidence Interval
#model for having a birth between wave 1 and 3
testq<-glm(realizedpreferencebet1and3~agegroups_1+marital_1+educationlevel_1+BIRTHEVENT_1+urbanrec+moderncon_1, family=binomial, data=pmaugfilter)

summary(testq)

Call:
glm(formula = realizedpreferencebet1and3 ~ agegroups_1 + marital_1 + 
    educationlevel_1 + BIRTHEVENT_1 + urbanrec + moderncon_1, 
    family = binomial, data = pmaugfilter)

Coefficients:
                                         Estimate Std. Error z value Pr(>|z|)
(Intercept)                              -0.72995    0.27630  -2.642  0.00824
agegroups_120-25                         -0.21070    0.19357  -1.088  0.27638
agegroups_126-30                         -0.10442    0.22451  -0.465  0.64186
agegroups_131-35                          0.12598    0.25218   0.500  0.61737
agegroups_136-40                          0.39767    0.28010   1.420  0.15569
agegroups_141-44                          0.99796    0.33741   2.958  0.00310
agegroups_145-49                          0.73583    0.35602   2.067  0.03875
marital_1never married                    1.36331    0.20610   6.615 3.72e-11
marital_1currently living with partner    0.20485    0.12109   1.692  0.09071
marital_1widow or widower                 0.64461    0.36744   1.754  0.07937
educationlevel_1primary/middle school     0.44267    0.19223   2.303  0.02129
educationlevel_1secondary/post-primary    0.62337    0.21420   2.910  0.00361
educationlevel_1tertiary/ post-secondary  0.70537    0.27632   2.553  0.01069
BIRTHEVENT_1                              0.05382    0.03361   1.601  0.10938
urbanrecurban                             0.35578    0.11924   2.984  0.00285
moderncon_1yes                            0.26284    0.11156   2.356  0.01847
                                            
(Intercept)                              ** 
agegroups_120-25                            
agegroups_126-30                            
agegroups_131-35                            
agegroups_136-40                            
agegroups_141-44                         ** 
agegroups_145-49                         *  
marital_1never married                   ***
marital_1currently living with partner   .  
marital_1widow or widower                .  
educationlevel_1primary/middle school    *  
educationlevel_1secondary/post-primary   ** 
educationlevel_1tertiary/ post-secondary *  
BIRTHEVENT_1                                
urbanrecurban                            ** 
moderncon_1yes                           *  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 2296.3  on 1769  degrees of freedom
Residual deviance: 2163.7  on 1754  degrees of freedom
  (252 observations deleted due to missingness)
AIC: 2195.7

Number of Fisher Scoring iterations: 4
tbl_regression(testq, exponentiate = TRUE)
Characteristic OR1 95% CI1 p-value
agegroups_1


    15-19
    20-25 0.81 0.55, 1.19 0.3
    26-30 0.90 0.58, 1.40 0.6
    31-35 1.13 0.69, 1.86 0.6
    36-40 1.49 0.86, 2.58 0.2
    41-44 2.71 1.41, 5.31 0.003
    45-49 2.09 1.05, 4.24 0.039
marital_1


    currently married
    never married 3.91 2.62, 5.88 <0.001
    currently living with partner 1.23 0.97, 1.56 0.091
    widow or widower 1.91 0.96, 4.09 0.079
educationlevel_1


    none
    primary/middle school 1.56 1.07, 2.27 0.021
    secondary/post-primary 1.87 1.23, 2.84 0.004
    tertiary/ post-secondary 2.02 1.18, 3.49 0.011
Number of birth events 1.06 0.99, 1.13 0.11
urbanrec


    rural
    urban 1.43 1.13, 1.81 0.003
moderncon_1


    no
    yes 1.30 1.05, 1.62 0.018
1 OR = Odds Ratio, CI = Confidence Interval
#final analysis

#model for having a birth between wave 1 and 2

testu<-glm(birthbet1and2~wantanotherchildnpregnant_1+agegroups_1 +marital_1+educationlevel_1+BIRTHEVENT_1+ urbanrec+moderncon_1, family=binomial, data=pmaugfilter)

summary(testu)

Call:
glm(formula = birthbet1and2 ~ wantanotherchildnpregnant_1 + agegroups_1 + 
    marital_1 + educationlevel_1 + BIRTHEVENT_1 + urbanrec + 
    moderncon_1, family = binomial, data = pmaugfilter)

Coefficients:
                                            Estimate Std. Error z value
(Intercept)                                 -0.91630    0.33503  -2.735
wantanotherchildnpregnant_1no more children -0.15388    0.18629  -0.826
agegroups_120-25                             0.34398    0.25627   1.342
agegroups_126-30                             0.38080    0.28684   1.328
agegroups_131-35                             0.53258    0.31299   1.702
agegroups_136-40                             0.59116    0.34164   1.730
agegroups_141-44                            -0.06771    0.41819  -0.162
agegroups_145-49                             0.16138    0.44197   0.365
marital_1never married                      -1.18269    0.26960  -4.387
marital_1currently living with partner      -0.16428    0.14028  -1.171
marital_1widow or widower                   -0.74960    0.46130  -1.625
educationlevel_1primary/middle school       -0.14807    0.22006  -0.673
educationlevel_1secondary/post-primary      -0.33946    0.25007  -1.357
educationlevel_1tertiary/ post-secondary    -0.52112    0.33760  -1.544
BIRTHEVENT_1                                -0.04500    0.04088  -1.101
urbanrecurban                               -0.25100    0.14628  -1.716
moderncon_1yes                              -0.34705    0.13553  -2.561
                                            Pr(>|z|)    
(Intercept)                                  0.00624 ** 
wantanotherchildnpregnant_1no more children  0.40881    
agegroups_120-25                             0.17951    
agegroups_126-30                             0.18432    
agegroups_131-35                             0.08883 .  
agegroups_136-40                             0.08357 .  
agegroups_141-44                             0.87138    
agegroups_145-49                             0.71500    
marital_1never married                      1.15e-05 ***
marital_1currently living with partner       0.24154    
marital_1widow or widower                    0.10417    
educationlevel_1primary/middle school        0.50102    
educationlevel_1secondary/post-primary       0.17464    
educationlevel_1tertiary/ post-secondary     0.12268    
BIRTHEVENT_1                                 0.27100    
urbanrecurban                                0.08618 .  
moderncon_1yes                               0.01045 *  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 1693.9  on 1769  degrees of freedom
Residual deviance: 1622.6  on 1753  degrees of freedom
  (252 observations deleted due to missingness)
AIC: 1656.6

Number of Fisher Scoring iterations: 5
tbl_regression(testu, exponentiate = TRUE)
Characteristic OR1 95% CI1 p-value
wantanotherchildnpregnant_1


    have another child
    no more children 0.86 0.59, 1.23 0.4
agegroups_1


    15-19
    20-25 1.41 0.86, 2.35 0.2
    26-30 1.46 0.84, 2.58 0.2
    31-35 1.70 0.93, 3.16 0.089
    36-40 1.81 0.93, 3.54 0.084
    41-44 0.93 0.41, 2.10 0.9
    45-49 1.18 0.49, 2.77 0.7
marital_1


    currently married
    never married 0.31 0.18, 0.52 <0.001
    currently living with partner 0.85 0.64, 1.12 0.2
    widow or widower 0.47 0.17, 1.09 0.10
educationlevel_1


    none
    primary/middle school 0.86 0.57, 1.34 0.5
    secondary/post-primary 0.71 0.44, 1.17 0.2
    tertiary/ post-secondary 0.59 0.30, 1.14 0.12
Number of birth events 0.96 0.88, 1.04 0.3
urbanrec


    rural
    urban 0.78 0.58, 1.03 0.086
moderncon_1


    no
    yes 0.71 0.54, 0.92 0.010
1 OR = Odds Ratio, CI = Confidence Interval
#model for having a birth between wave 1 and 3
testi<-glm(birthbet1and3~wantanotherchildnpregnant_1+agegroups_1+marital_1+ +educationlevel_1+BIRTHEVENT_1+ urbanrec+ moderncon_1, family=binomial, data=pmaugfilter)

summary(testi)

Call:
glm(formula = birthbet1and3 ~ wantanotherchildnpregnant_1 + agegroups_1 + 
    marital_1 + +educationlevel_1 + BIRTHEVENT_1 + urbanrec + 
    moderncon_1, family = binomial, data = pmaugfilter)

Coefficients:
                                            Estimate Std. Error z value
(Intercept)                                  0.70067    0.27686   2.531
wantanotherchildnpregnant_1no more children -0.36936    0.15944  -2.317
agegroups_120-25                             0.19200    0.19377   0.991
agegroups_126-30                             0.09293    0.22469   0.414
agegroups_131-35                            -0.09839    0.25250  -0.390
agegroups_136-40                            -0.31881    0.28213  -1.130
agegroups_141-44                            -0.83298    0.34513  -2.414
agegroups_145-49                            -0.55750    0.36539  -1.526
marital_1never married                      -1.32461    0.20677  -6.406
marital_1currently living with partner      -0.20985    0.12139  -1.729
marital_1widow or widower                   -0.56481    0.36877  -1.532
educationlevel_1primary/middle school       -0.42972    0.19275  -2.229
educationlevel_1secondary/post-primary      -0.62078    0.21466  -2.892
educationlevel_1tertiary/ post-secondary    -0.69508    0.27690  -2.510
BIRTHEVENT_1                                -0.02956    0.03521  -0.839
urbanrecurban                               -0.35013    0.11946  -2.931
moderncon_1yes                              -0.24272    0.11205  -2.166
                                            Pr(>|z|)    
(Intercept)                                  0.01138 *  
wantanotherchildnpregnant_1no more children  0.02053 *  
agegroups_120-25                             0.32175    
agegroups_126-30                             0.67918    
agegroups_131-35                             0.69678    
agegroups_136-40                             0.25847    
agegroups_141-44                             0.01580 *  
agegroups_145-49                             0.12707    
marital_1never married                      1.49e-10 ***
marital_1currently living with partner       0.08385 .  
marital_1widow or widower                    0.12562    
educationlevel_1primary/middle school        0.02578 *  
educationlevel_1secondary/post-primary       0.00383 ** 
educationlevel_1tertiary/ post-secondary     0.01207 *  
BIRTHEVENT_1                                 0.40124    
urbanrecurban                                0.00338 ** 
moderncon_1yes                               0.03030 *  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 2296.3  on 1769  degrees of freedom
Residual deviance: 2158.3  on 1753  degrees of freedom
  (252 observations deleted due to missingness)
AIC: 2192.3

Number of Fisher Scoring iterations: 4
tbl_regression(testi, exponentiate = TRUE)
Characteristic OR1 95% CI1 p-value
wantanotherchildnpregnant_1


    have another child
    no more children 0.69 0.50, 0.94 0.021
agegroups_1


    15-19
    20-25 1.21 0.83, 1.77 0.3
    26-30 1.10 0.71, 1.70 0.7
    31-35 0.91 0.55, 1.49 0.7
    36-40 0.73 0.42, 1.26 0.3
    41-44 0.43 0.22, 0.85 0.016
    45-49 0.57 0.28, 1.16 0.13
marital_1


    currently married
    never married 0.27 0.18, 0.40 <0.001
    currently living with partner 0.81 0.64, 1.03 0.084
    widow or widower 0.57 0.26, 1.14 0.13
educationlevel_1


    none
    primary/middle school 0.65 0.45, 0.95 0.026
    secondary/post-primary 0.54 0.35, 0.82 0.004
    tertiary/ post-secondary 0.50 0.29, 0.86 0.012
Number of birth events 0.97 0.91, 1.04 0.4
urbanrec


    rural
    urban 0.70 0.56, 0.89 0.003
moderncon_1


    no
    yes 0.78 0.63, 0.98 0.030
1 OR = Odds Ratio, CI = Confidence Interval
#final analysis: becoming pregnant

#model for becoming pregnant at wave 2 given wave 1 preference

test11<-glm(pregnant_2~wantanotherchildnpregnant_1+agegroups_1 +marital_1+educationlevel_1, family=binomial, data=pmaugfilter)

summary(test11)

Call:
glm(formula = pregnant_2 ~ wantanotherchildnpregnant_1 + agegroups_1 + 
    marital_1 + educationlevel_1, family = binomial, data = pmaugfilter)

Coefficients:
                                             Estimate Std. Error z value
(Intercept)                                  -1.25335    0.38552  -3.251
wantanotherchildnpregnant_1no more children  -0.72179    0.27371  -2.637
agegroups_120-25                              0.12290    0.25268   0.486
agegroups_126-30                             -0.22241    0.28683  -0.775
agegroups_131-35                             -0.60549    0.32737  -1.850
agegroups_136-40                             -0.73206    0.37085  -1.974
agegroups_141-44                             -2.08917    0.78237  -2.670
agegroups_145-49                            -15.40686  395.92850  -0.039
marital_1never married                       -0.75349    0.28112  -2.680
marital_1currently living with partner        0.05889    0.18195   0.324
marital_1widow or widower                    -0.79015    1.04270  -0.758
educationlevel_1primary/middle school        -0.11956    0.29559  -0.404
educationlevel_1secondary/post-primary       -0.50562    0.32252  -1.568
educationlevel_1tertiary/ post-secondary     -0.30418    0.39872  -0.763
                                            Pr(>|z|)   
(Intercept)                                  0.00115 **
wantanotherchildnpregnant_1no more children  0.00836 **
agegroups_120-25                             0.62670   
agegroups_126-30                             0.43809   
agegroups_131-35                             0.06438 . 
agegroups_136-40                             0.04838 * 
agegroups_141-44                             0.00758 **
agegroups_145-49                             0.96896   
marital_1never married                       0.00735 **
marital_1currently living with partner       0.74620   
marital_1widow or widower                    0.44857   
educationlevel_1primary/middle school        0.68587   
educationlevel_1secondary/post-primary       0.11695   
educationlevel_1tertiary/ post-secondary     0.44552   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 1226.0  on 1761  degrees of freedom
Residual deviance: 1135.1  on 1748  degrees of freedom
  (260 observations deleted due to missingness)
AIC: 1163.1

Number of Fisher Scoring iterations: 16
#model for becoming pregnant at wave 3 given wave 1 preference
test12<-glm(pregnant_3~wantanotherchildnpregnant_1+agegroups_1 +marital_1+educationlevel_1, family=binomial, data=pmaugfilter)
Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
summary(test12)

Call:
glm(formula = pregnant_3 ~ wantanotherchildnpregnant_1 + agegroups_1 + 
    marital_1 + educationlevel_1, family = binomial, data = pmaugfilter)

Coefficients:
                                             Estimate Std. Error z value
(Intercept)                                  -1.75558    0.42736  -4.108
wantanotherchildnpregnant_1no more children  -0.34516    0.27511  -1.255
agegroups_120-25                              0.35549    0.28278   1.257
agegroups_126-30                              0.23104    0.31524   0.733
agegroups_131-35                             -0.15337    0.35423  -0.433
agegroups_136-40                             -0.90773    0.44777  -2.027
agegroups_141-44                             -2.54728    1.06401  -2.394
agegroups_145-49                            -16.07775  634.12244  -0.025
marital_1never married                       -0.49856    0.30261  -1.648
marital_1currently living with partner       -0.02557    0.19688  -0.130
marital_1widow or widower                   -15.07703  800.53862  -0.019
educationlevel_1primary/middle school        -0.10448    0.32225  -0.324
educationlevel_1secondary/post-primary       -0.56347    0.35320  -1.595
educationlevel_1tertiary/ post-secondary     -0.53322    0.44533  -1.197
                                            Pr(>|z|)    
(Intercept)                                 3.99e-05 ***
wantanotherchildnpregnant_1no more children   0.2096    
agegroups_120-25                              0.2087    
agegroups_126-30                              0.4636    
agegroups_131-35                              0.6650    
agegroups_136-40                              0.0426 *  
agegroups_141-44                              0.0167 *  
agegroups_145-49                              0.9798    
marital_1never married                        0.0994 .  
marital_1currently living with partner        0.8967    
marital_1widow or widower                     0.9850    
educationlevel_1primary/middle school         0.7458    
educationlevel_1secondary/post-primary        0.1106    
educationlevel_1tertiary/ post-secondary      0.2312    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 1069.41  on 1768  degrees of freedom
Residual deviance:  992.82  on 1755  degrees of freedom
  (253 observations deleted due to missingness)
AIC: 1020.8

Number of Fisher Scoring iterations: 17
#descriptive table

trial2 <- pmaugfilter |> select(wantanotherchildnpregnant_1, agegroups_1, marital_1,educationlevel_1,BIRTHEVENT_1,urbanrec, moderncon_1, birthbet1and2, birthbet1and3,)

trial2 |> tbl_summary()
! Column(s) "BIRTHEVENT_1" are class "haven_labelled".
ℹ This is an intermediate datastructure not meant for analysis.
ℹ Convert columns with `haven::as_factor()`, `labelled::to_factor()`,
  `labelled::unlabelled()`, and `unclass()`. Failure to convert may have
  unintended consequences or result in error.
<https://haven.tidyverse.org/articles/semantics.html>
<https://larmarange.github.io/labelled/articles/intro_labelled.html#unlabelled>
Characteristic N = 2,0221
wantanotherchildnpregnant_1
    have another child 1,405 (69%)
    no more children 617 (31%)
    infertile 0 (0%)
agegroups_1
    15-19 353 (17%)
    20-25 450 (22%)
    26-30 370 (18%)
    31-35 303 (15%)
    36-40 279 (14%)
    41-44 155 (7.7%)
    45-49 112 (5.5%)
marital_1
    currently married 603 (34%)
    never married 403 (23%)
    currently living with partner 729 (41%)
    widow or widower 53 (3.0%)
    Unknown 234
educationlevel_1
    none 152 (7.5%)
    primary/middle school 1,177 (58%)
    secondary/post-primary 548 (27%)
    tertiary/ post-secondary 145 (7.2%)
Number of birth events 3 (1, 5)
urbanrec
    rural 1,347 (67%)
    urban 675 (33%)
moderncon_1 725 (36%)
    Unknown 21
birthbet1and2
    0 1,649 (82%)
    1 373 (18%)
birthbet1and3
    0 1,326 (66%)
    1 696 (34%)
1 n (%); Median (Q1, Q3)
#with survey design

#birth1to2sd<-svyglm(birthbet1and3~wantanotherchildnpregnant_1+agegroups_1 +marital_1+educationlevel_1+BIRTHEVENT_1+ urbanrec, family=binomial, design=design1to3)
# 2.5.2025 updates

#create code for change in status between waves: had a birth*, marital status, contraceptive use

#add polygamy variable to the mix: already in the dataset