Chapter 2- pregnancy decision-making, age, union status and contraceptive use

Author

R. Luttinen

Pregnancy Decision-making

This chapter explores panel data from the performance monitoring for action sample in Uganda collected from 2020-2022. I employ random effects for individual to study the pregnancy exercise of choice scale over time. I pay special attention to interactions between age group and marital status as well as age group and usage of modern contraception.

#read in data


library(ipumsr)
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   4.0.3     ✔ tibble    3.2.1
✔ lubridate 1.9.4     ✔ tidyr     1.3.2
✔ purrr     1.2.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
#read in long format

#person-period dataset


pmauglong <- read_ipums_micro(
  ddi = "C:/Users/Rebecca/Downloads/pma_00029.xml",
  data = "C:/Users/Rebecca/Downloads/pma_00029.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.
#filter for 2022 only

#pmauglong<-pmauglong%>%
 # filter(YEAR==2021)
#recoding and filtering

#check distribution of variables of interest


library(janitor)

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

    chisq.test, fisher.test
tabyl(pmauglong$NEGOTIATEKIDS)
 pmauglong$NEGOTIATEKIDS    n      percent
                       1  560 3.949781e-02
                       2 1303 9.190295e-02
                       3  608 4.288334e-02
                       4 7435 5.244040e-01
                       5 3995 2.817746e-01
                      97  236 1.664551e-02
                      98   40 2.821272e-03
                      99    1 7.053181e-05
tabyl(pmauglong$PTRDISCKIDSTART)
 pmauglong$PTRDISCKIDSTART     n     percent
                         1   106 0.007476372
                         2   331 0.023346029
                         3   170 0.011990408
                         4  1925 0.135773734
                         5  1066 0.075186909
                        97   154 0.010861899
                        98    31 0.002186486
                        99 10395 0.733178163
tabyl(pmauglong$PTRDISCKIDSTOP)
 pmauglong$PTRDISCKIDSTOP    n      percent
                        1  470 0.0331499506
                        2 1011 0.0713076598
                        3  421 0.0296938919
                        4 5469 0.3857384680
                        5 2946 0.2077867118
                       97   63 0.0044435040
                       98   14 0.0009874453
                       99 3784 0.2668923685
tabyl(pmauglong$STARTKIDDEC)
 pmauglong$STARTKIDDEC    n      percent
                     1  508 0.0358301594
                     2 1659 0.1170122725
                     3  372 0.0262378333
                     4 5430 0.3829877275
                     5 2386 0.1682888983
                    97   31 0.0021864861
                    98    8 0.0005642545
                    99 3784 0.2668923685
tabyl(pmauglong$STARTKIDDECWILL)
 pmauglong$STARTKIDDECWILL     n      percent
                         1    69 0.0048666949
                         2   279 0.0196783749
                         3    95 0.0067005219
                         4  1826 0.1287910848
                         5  1436 0.1012836789
                        97    69 0.0048666949
                        98     9 0.0006347863
                        99 10395 0.7331781634
tabyl(pmauglong$EDUCPREKID)
 pmauglong$EDUCPREKID     n      percent
                    1   122 0.0086048808
                    2   287 0.0202426294
                    3    86 0.0060657356
                    4  1083 0.0763859501
                    5  1114 0.0785724362
                   97     9 0.0006347863
                   99 11477 0.8094935816
tabyl(pmauglong$EDUCPREKIDWILL)
 pmauglong$EDUCPREKIDWILL     n      percent
                        1    80 0.0056425448
                        2   109 0.0076879673
                        3    36 0.0025391452
                        4   601 0.0423896177
                        5  1679 0.1184229087
                       97     9 0.0006347863
                       98    12 0.0008463817
                       99 11652 0.8218366483
tabyl(pmauglong$PREGREST)
 pmauglong$PREGREST    n     percent
                  1  127 0.008957540
                  2  222 0.015658062
                  3  150 0.010579771
                  4 6858 0.483707152
                  5 6724 0.474255889
                 97   72 0.005078290
                 98   25 0.001763295
tabyl(pmauglong$PTRDISCKIDSTOPWILL)
 pmauglong$PTRDISCKIDSTOPWILL     n     percent
                            1    90 0.006347863
                            2   292 0.020595288
                            3   187 0.013189448
                            4  1966 0.138665538
                            5  1049 0.073987869
                           97   173 0.012202003
                           98    26 0.001833827
                           99 10395 0.733178163
#make index: exercise of choice

library(janitor)


pmauglong <- pmauglong %>%
  mutate(startstop = case_when(
    STARTKIDDEC == 1 | PTRDISCKIDSTOPWILL== 1 ~ '1',
    STARTKIDDEC == 2 | PTRDISCKIDSTOPWILL== 2 ~ '2',
    STARTKIDDEC == 3 | PTRDISCKIDSTOPWILL== 3 ~ '3',
    STARTKIDDEC == 4 | PTRDISCKIDSTOPWILL== 4~ '4',
    STARTKIDDEC == 5 | PTRDISCKIDSTOPWILL== 5 ~ '5'
  ))

tabyl(pmauglong$startstop)
 pmauglong$startstop    n    percent valid_percent
                   1  598 0.04217802    0.04290121
                   2 1951 0.13760756    0.13996700
                   3  559 0.03942728    0.04010331
                   4 7396 0.52165327    0.53059760
                   5 3435 0.24227677    0.24643088
                <NA>  239 0.01685710            NA
pmauglong <- pmauglong %>%
  mutate(havekid = case_when(
    STARTKIDDECWILL == 1 | NEXTKIDDEC== 1 ~ '1',
    STARTKIDDECWILL == 2 | NEXTKIDDEC== 2 ~ '2',
    STARTKIDDECWILL == 3 | NEXTKIDDEC== 3 ~ '3',
    STARTKIDDECWILL == 4 | NEXTKIDDEC== 4~ '4',
    STARTKIDDECWILL == 5 | NEXTKIDDEC== 5 ~ '5'
  ))


tabyl(pmauglong$havekid)
 pmauglong$havekid    n     percent valid_percent
                 1  343 0.024192411    0.02441455
                 2 1267 0.089363803    0.09018435
                 3  446 0.031457187    0.03174603
                 4 7312 0.515728594    0.52046409
                 5 4681 0.330159402    0.33319097
              <NA>  129 0.009098603            NA
pmauglong <- pmauglong %>%
  mutate(negotiate = case_when(
    PTRDISCKIDSTOP == 1 | PTRDISCKIDSTART== 1 ~ '1',
    PTRDISCKIDSTOP == 2 | PTRDISCKIDSTART== 2 ~ '2',
    PTRDISCKIDSTOP == 3 | PTRDISCKIDSTART== 3 ~ '3',
    PTRDISCKIDSTOP == 4 | PTRDISCKIDSTART== 4~ '4',
    PTRDISCKIDSTOP == 5 | PTRDISCKIDSTART== 5 ~ '5'
  ))

tabyl(pmauglong$negotiate)
 pmauglong$negotiate    n    percent valid_percent
                   1  576 0.04062632    0.04139418
                   2 1342 0.09465369    0.09644269
                   3  591 0.04168430    0.04247215
                   4 7394 0.52151220    0.53136903
                   5 4012 0.28297362    0.28832195
                <NA>  263 0.01854987            NA
#make index existence of choice

pmauglong <- pmauglong %>%
  mutate(restbtw = case_when(
    PREGREST == 1 ~ '1',
    PREGREST == 2 ~ '2',
    PREGREST == 3 ~ '3',
    PREGREST == 4 ~ '4',
    PREGREST == 5~ '5'
  ))

tabyl(pmauglong$restbtw)
 pmauglong$restbtw    n     percent valid_percent
                 1  127 0.008957540   0.009019246
                 2  222 0.015658062   0.015765926
                 3  150 0.010579771   0.010652653
                 4 6858 0.483707152   0.487039273
                 5 6724 0.474255889   0.477522903
              <NA>   97 0.006841586            NA
pmauglong <- pmauglong %>%
  mutate(edufirst = case_when(
    EDUCPREKID == 1 | EDUCPREKIDWILL== 1 ~ '1',
    EDUCPREKID == 2 | EDUCPREKIDWILL== 2 ~ '2',
    EDUCPREKID == 3 | EDUCPREKIDWILL== 3 ~ '3',
    EDUCPREKID == 4 | EDUCPREKIDWILL== 4~ '4',
    EDUCPREKID == 5 | EDUCPREKIDWILL== 5 ~ '5'
  ))

tabyl(pmauglong$edufirst)
 pmauglong$edufirst    n     percent valid_percent
                  1  202 0.014247426    0.03886858
                  2  396 0.027930597    0.07619781
                  3  122 0.008604881    0.02347508
                  4 1684 0.118775568    0.32403310
                  5 2793 0.196995345    0.53742544
               <NA> 8981 0.633446184            NA
#don't use because it isn't validated



#drop na's

pmauglongfilter2 <- pmauglong %>%
  filter(!is.na(startstop),
         !is.na(havekid),
         !is.na(negotiate))



pmauglongfilter2$startstopnum<-as.numeric(pmauglongfilter2$startstop)
pmauglongfilter2$havekidnum<-as.numeric(pmauglongfilter2$havekid)
pmauglongfilter2$negotiatenum<-as.numeric(pmauglongfilter2$negotiate)

#create summary variables: sum

pmauglongfilter2 <- pmauglongfilter2 %>%
  mutate(summary_score = (startstopnum + havekidnum + negotiatenum) )


tabyl(pmauglongfilter2$summary_score)
 pmauglongfilter2$summary_score    n     percent
                              3   67 0.004861413
                              4   68 0.004933972
                              5   98 0.007110724
                              6  342 0.024814976
                              7  273 0.019808446
                              8  632 0.045856915
                              9  514 0.037295022
                             10 1357 0.098461762
                             11  959 0.069583515
                             12 4528 0.328544478
                             13 1734 0.125816282
                             14 1330 0.096502685
                             15 1880 0.136409810
hist(pmauglongfilter2$summary_score)

#average

pmauglongfilter2 <- pmauglongfilter2 %>%
  mutate(summary_score_av = (startstopnum + havekidnum + negotiatenum)/3 )

tabyl(pmauglongfilter2$summary_score_av)
 pmauglongfilter2$summary_score_av    n     percent
                          1.000000   67 0.004861413
                          1.333333   68 0.004933972
                          1.666667   98 0.007110724
                          2.000000  342 0.024814976
                          2.333333  273 0.019808446
                          2.666667  632 0.045856915
                          3.000000  514 0.037295022
                          3.333333 1357 0.098461762
                          3.666667  959 0.069583515
                          4.000000 4528 0.328544478
                          4.333333 1734 0.125816282
                          4.666667 1330 0.096502685
                          5.000000 1880 0.136409810
hist(pmauglongfilter2$summary_score_av)

#marital status 

pmauglongfilter2<-pmauglongfilter2%>%
  mutate(MARSTAT=as.factor(MARSTAT))%>%
  mutate(maritalstatus= recode(MARSTAT, '10' = "never married" ,'20' = "married or living together", '21'= "currently married", '22'= "currently living with partner", '31'="formerly in union",'32'='widow or widower',.default = NA_character_))

tabyl(pmauglongfilter2, maritalstatus)
                 maritalstatus    n      percent valid_percent
                 never married 3251 0.2358873893    0.23593875
             currently married 3384 0.2455376578    0.24559112
 currently living with partner 4934 0.3580031926    0.35808114
             formerly in union 1897 0.1376433029    0.13767327
              widow or widower  313 0.0227107822    0.02271573
                          <NA>    3 0.0002176752            NA
#age-group

pmauglongfilter2<-pmauglongfilter2 %>%
  mutate(agegroup = case_when(
    AGE >= 15 & AGE <= 19 ~ "15-19",
    AGE >= 20 & AGE <= 24 ~ "20-24",
    AGE >= 25 & AGE <= 29 ~ "25-29",
    AGE >= 30 & AGE <= 34 ~ "30-34",
    AGE >= 35 & AGE <= 39 ~ "35-39",
    AGE >= 40 & AGE <= 44 ~ "40-44",
    AGE >= 45 & AGE <= 49 ~ "45-49"
  ))



pmauglongfilter2<-pmauglongfilter2%>%
  filter(BIRTHEVENT<90)

pmauglongfilter2<-pmauglongfilter2 %>%
  mutate(AGE=as.numeric(AGE))


pmauglongfilter2<-pmauglongfilter2 %>%
  mutate(URBAN=as.factor(URBAN))

#contraceptive use

pmauglongfilter2<-pmauglongfilter2%>%
  mutate(MCP=as.factor(MCP))%>%
  mutate(usingmoderncon= recode(MCP, '1' = "yes" ,'0' = "no",.default = NA_character_))

tabyl(pmauglongfilter2, usingmoderncon)
 usingmoderncon    n    percent valid_percent
             no 8955 0.64990202     0.6594256
            yes 4625 0.33565571     0.3405744
           <NA>  199 0.01444227            NA
#education

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

tabyl(pmauglongfilter2, educationlevel)
           educationlevel    n      percent valid_percent
                     none  797 0.0578416431    0.05785004
    primary/middle school 7666 0.5563538718    0.55643464
   secondary/post-primary 4165 0.3022715727    0.30231545
 tertiary/ post-secondary 1149 0.0833877640    0.08339987
                     <NA>    2 0.0001451484            NA
#collapse marital status variable

pmauglongfilter2<-pmauglongfilter2%>%
  mutate(maritalcombined= recode(maritalstatus, 'formerly in union'= 'not in a union' ,'never married' = "not in a union", 'widow or widower'= "not in a union", 'currently married'= "in a union", 'currently living with partner'="in a union"))
#bivariate testing

contingency_table <- table(pmauglongfilter2$summary_score_av, pmauglongfilter2$maritalcombined)

# Chi-squared test
chi_test_results <- chisq.test(contingency_table)

print(chi_test_results)

    Pearson's Chi-squared test

data:  contingency_table
X-squared = 90.599, df = 12, p-value = 3.78e-14
contingency_table2 <- table(pmauglongfilter2$summary_score_av, pmauglongfilter2$agegroup)

# Chi-squared test
chi_test_results2 <- chisq.test(contingency_table2)
Warning in stats::chisq.test(x, y, ...): Chi-squared approximation may be
incorrect
print(chi_test_results2)

    Pearson's Chi-squared test

data:  contingency_table2
X-squared = 180.28, df = 72, p-value = 2.949e-11
contingency_table3 <- table(pmauglongfilter2$summary_score_av, pmauglongfilter2$usingmoderncon)

# Chi-squared test
chi_test_results3 <- chisq.test(contingency_table3)

print(chi_test_results3)

    Pearson's Chi-squared test

data:  contingency_table3
X-squared = 85.21, df = 12, p-value = 4.148e-13
library(lme4)
Loading required package: Matrix

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

    expand, pack, unpack
#filter out cases with a missing wealth index 

pmauglongfilter3<-pmauglongfilter2%>%
  filter(WEALTHQ!=99)

#filter out any infertile or missing values for fertility preferences

pmauglongfilter3<-pmauglongfilter3%>%
  filter(!FERTPREF %in% c(3, 97, 98, 99))


#begin multivariate testing for the average score


#base model


lm_model <- lmer(summary_score_av ~ agegroup + maritalcombined + usingmoderncon+ educationlevel+ BIRTHEVENT+ factor(URBAN)+ SCORE+ factor(YEAR)+ factor(FERTPREF)+ (1|FQINSTID) , 
                 data = pmauglongfilter3)


summary(lm_model)
Linear mixed model fit by REML ['lmerMod']
Formula: summary_score_av ~ agegroup + maritalcombined + usingmoderncon +  
    educationlevel + BIRTHEVENT + factor(URBAN) + SCORE + factor(YEAR) +  
    factor(FERTPREF) + (1 | FQINSTID)
   Data: pmauglongfilter3

REML criterion at convergence: 26130.3

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-4.0836 -0.4252  0.0967  0.5978  2.7144 

Random effects:
 Groups   Name        Variance Std.Dev.
 FQINSTID (Intercept) 0.1511   0.3887  
 Residual             0.4294   0.6553  
Number of obs: 11516, groups:  FQINSTID, 6930

Fixed effects:
                                        Estimate Std. Error t value
(Intercept)                             3.889354   0.038342 101.439
agegroup20-24                          -0.017550   0.024465  -0.717
agegroup25-29                           0.004917   0.028544   0.172
agegroup30-34                           0.060063   0.033340   1.802
agegroup35-39                           0.023171   0.038986   0.594
agegroup40-44                           0.007372   0.044944   0.164
agegroup45-49                           0.033609   0.049520   0.679
maritalcombinedin a union              -0.056571   0.018614  -3.039
usingmodernconyes                       0.114311   0.015783   7.242
educationlevelprimary/middle school     0.001430   0.033537   0.043
educationlevelsecondary/post-primary    0.215301   0.037116   5.801
educationleveltertiary/ post-secondary  0.236454   0.045574   5.188
BIRTHEVENT                             -0.014452   0.005486  -2.634
factor(URBAN)1                         -0.113544   0.018709  -6.069
SCORE                                   0.040521   0.003627  11.173
factor(YEAR)2021                        0.069118   0.015994   4.322
factor(YEAR)2022                        0.014716   0.016350   0.900
factor(FERTPREF)2                      -0.039629   0.022720  -1.744

Correlation matrix not shown by default, as p = 18 > 12.
Use print(x, correlation=TRUE)  or
    vcov(x)        if you need it
#interaction between union status and age group



lm_modelint <- lmer(summary_score_av ~ agegroup + maritalcombined + usingmoderncon+ educationlevel+ BIRTHEVENT+ agegroup*maritalcombined+ factor(URBAN)+ SCORE+ factor(YEAR)+ factor(FERTPREF)+ (1|FQINSTID), 
                 data = pmauglongfilter3)


summary(lm_modelint)
Linear mixed model fit by REML ['lmerMod']
Formula: summary_score_av ~ agegroup + maritalcombined + usingmoderncon +  
    educationlevel + BIRTHEVENT + agegroup * maritalcombined +  
    factor(URBAN) + SCORE + factor(YEAR) + factor(FERTPREF) +  
    (1 | FQINSTID)
   Data: pmauglongfilter3

REML criterion at convergence: 26131.6

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-4.0958 -0.4241  0.0924  0.6012  2.7227 

Random effects:
 Groups   Name        Variance Std.Dev.
 FQINSTID (Intercept) 0.1499   0.3872  
 Residual             0.4296   0.6554  
Number of obs: 11516, groups:  FQINSTID, 6930

Fixed effects:
                                         Estimate Std. Error t value
(Intercept)                              3.914472   0.038989 100.399
agegroup20-24                           -0.060843   0.032363  -1.880
agegroup25-29                           -0.103069   0.041473  -2.485
agegroup30-34                            0.013144   0.049292   0.267
agegroup35-39                            0.060883   0.058314   1.044
agegroup40-44                            0.018586   0.061355   0.303
agegroup45-49                           -0.032120   0.067546  -0.476
maritalcombinedin a union               -0.184068   0.040244  -4.574
usingmodernconyes                        0.117610   0.015801   7.443
educationlevelprimary/middle school      0.001528   0.033520   0.046
educationlevelsecondary/post-primary     0.211516   0.037118   5.699
educationleveltertiary/ post-secondary   0.239650   0.045628   5.252
BIRTHEVENT                              -0.015686   0.005518  -2.843
factor(URBAN)1                          -0.111441   0.018698  -5.960
SCORE                                    0.040169   0.003630  11.066
factor(YEAR)2021                         0.068837   0.015994   4.304
factor(YEAR)2022                         0.015245   0.016348   0.933
factor(FERTPREF)2                       -0.038943   0.022707  -1.715
agegroup20-24:maritalcombinedin a union  0.156064   0.051567   3.026
agegroup25-29:maritalcombinedin a union  0.241218   0.057604   4.188
agegroup30-34:maritalcombinedin a union  0.160018   0.062918   2.543
agegroup35-39:maritalcombinedin a union  0.054901   0.068598   0.800
agegroup40-44:maritalcombinedin a union  0.083634   0.072247   1.158
agegroup45-49:maritalcombinedin a union  0.193261   0.079360   2.435

Correlation matrix not shown by default, as p = 24 > 12.
Use print(x, correlation=TRUE)  or
    vcov(x)        if you need it
library(ggeffects)


#visualize the interaction

predictions <- predict_response(lm_modelint, terms = c("agegroup", "maritalcombined"))

predictions %>% 
  as.data.frame() %>% 
  mutate(x = fct_relevel(x, "15-19", "20-24", "25-29", "30-34", "35-39", "40-44", "45-49")) %>% 
  ggplot(aes(x = x, y = predicted, color = group, group = group)) +
  geom_point(position = position_dodge(0.4), size = 2.5) +
  geom_errorbar(aes(ymin = conf.low, ymax = conf.high), position = position_dodge(0.4), width = 0.2) +
  labs(
    title = "Marginal Effect of Age Group and Marital Status on Summary Score",
    x = "Age Group",
    y = "Predicted Summary Score Average",
    color = "Marital Status"
  ) +
  theme_minimal()

#interaction between contraceptive use and age group

lm_modelint2 <- lmer(summary_score_av ~ agegroup + maritalcombined + BIRTHEVENT+ usingmoderncon+ educationlevel+agegroup*usingmoderncon+ SCORE+ factor(URBAN)+ factor(FERTPREF)+ factor(YEAR)+ (1|FQINSTID), 
                    data = pmauglongfilter3)


summary(lm_modelint2)
Linear mixed model fit by REML ['lmerMod']
Formula: summary_score_av ~ agegroup + maritalcombined + BIRTHEVENT +  
    usingmoderncon + educationlevel + agegroup * usingmoderncon +  
    SCORE + factor(URBAN) + factor(FERTPREF) + factor(YEAR) +  
    (1 | FQINSTID)
   Data: pmauglongfilter3

REML criterion at convergence: 26144.7

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-4.1048 -0.4256  0.0950  0.5964  2.7018 

Random effects:
 Groups   Name        Variance Std.Dev.
 FQINSTID (Intercept) 0.1511   0.3888  
 Residual             0.4292   0.6551  
Number of obs: 11516, groups:  FQINSTID, 6930

Fixed effects:
                                         Estimate Std. Error t value
(Intercept)                             3.9034987  0.0388528 100.469
agegroup20-24                          -0.0326606  0.0278180  -1.174
agegroup25-29                          -0.0350815  0.0326955  -1.073
agegroup30-34                           0.0252457  0.0377519   0.669
agegroup35-39                           0.0257682  0.0432223   0.596
agegroup40-44                          -0.0101196  0.0497806  -0.203
agegroup45-49                           0.0390336  0.0535310   0.729
maritalcombinedin a union              -0.0535157  0.0186622  -2.868
BIRTHEVENT                             -0.0144993  0.0054886  -2.642
usingmodernconyes                       0.0347671  0.0413920   0.840
educationlevelprimary/middle school    -0.0003673  0.0335460  -0.011
educationlevelsecondary/post-primary    0.2132533  0.0371227   5.745
educationleveltertiary/ post-secondary  0.2359105  0.0455767   5.176
SCORE                                   0.0404994  0.0036298  11.157
factor(URBAN)1                         -0.1143626  0.0187100  -6.112
factor(FERTPREF)2                      -0.0386582  0.0227670  -1.698
factor(YEAR)2021                        0.0686766  0.0159927   4.294
factor(YEAR)2022                        0.0157025  0.0163516   0.960
agegroup20-24:usingmodernconyes         0.0814731  0.0522722   1.559
agegroup25-29:usingmodernconyes         0.1393398  0.0536561   2.597
agegroup30-34:usingmodernconyes         0.1269057  0.0566997   2.238
agegroup35-39:usingmodernconyes         0.0355551  0.0596435   0.596
agegroup40-44:usingmodernconyes         0.0839669  0.0675302   1.243
agegroup45-49:usingmodernconyes         0.0120335  0.0772381   0.156

Correlation matrix not shown by default, as p = 24 > 12.
Use print(x, correlation=TRUE)  or
    vcov(x)        if you need it
#visualize the interaction




library(ggeffects)

# Generate predictions based on the interaction model
predictions <- predict_response(lm_modelint2, terms = c("agegroup", "usingmoderncon"))

predictions %>% 
  as.data.frame() %>% 
  # Ensure chronological order
  mutate(x = fct_relevel(x, "15-19", "20-24", "25-29", "30-34", "35-39", "40-44", "45-49")) %>% 
  ggplot(aes(x = x, y = predicted, color = group, group = group)) +
  geom_point(position = position_dodge(0.4), size = 2.5) +
  geom_errorbar(aes(ymin = conf.low, ymax = conf.high), position = position_dodge(0.4), width = 0.2) +
  labs(
    title = "Marginal Effect of Age Group and Contraceptive Use on Summary Score",
    x = "Age Group",
    y = "Predicted Summary Score Average",
    color = "Using MCP"
  ) +
  theme_minimal()

#take a look at cell values

tabyl(pmauglongfilter3, agegroup, maritalcombined)
 agegroup not in a union in a union NA_
    15-19           2107        488   1
    20-24            914       1499   0
    25-29            504       1534   0
    30-34            360       1268   1
    35-39            266       1102   0
    40-44            257        664   0
    45-49            204        525   0
#take a look at cell values

tabyl(pmauglongfilter3, agegroup, usingmoderncon)
 agegroup   no yes NA_
    15-19 2148 406  42
    20-24 1541 835  37
    25-29 1151 857  30
    30-34  936 670  23
    35-39  809 542  17
    40-44  550 358  13
    45-49  497 220  12
#make a list to put into model summary

# 3. Put your models into a named list for the table columns
models_list_con <- list(
  "Model 1: Main Effects" = lm_model,
  "Model 2: Age × Marital" = lm_modelint,
  "Model 3: Age × Contraception" = lm_modelint2
)
library(modelsummary)
library(marginaleffects)

Attaching package: 'marginaleffects'
The following object is masked from 'package:lme4':

    refit
modelsummary(
  models_list_con,
  slope = "b",
  stars = TRUE,
  title = "Regression Table: Average Marginal Effects",
  gof_map = NA  
)
Regression Table: Average Marginal Effects
Model 1: Main Effects Model 2: Age × Marital Model 3: Age × Contraception
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001
(Intercept) 3.889*** 3.914*** 3.903***
(0.038) (0.039) (0.039)
agegroup20-24 -0.018 -0.061+ -0.033
(0.024) (0.032) (0.028)
agegroup25-29 0.005 -0.103* -0.035
(0.029) (0.041) (0.033)
agegroup30-34 0.060+ 0.013 0.025
(0.033) (0.049) (0.038)
agegroup35-39 0.023 0.061 0.026
(0.039) (0.058) (0.043)
agegroup40-44 0.007 0.019 -0.010
(0.045) (0.061) (0.050)
agegroup45-49 0.034 -0.032 0.039
(0.050) (0.068) (0.054)
maritalcombinedin a union -0.057** -0.184*** -0.054**
(0.019) (0.040) (0.019)
usingmodernconyes 0.114*** 0.118*** 0.035
(0.016) (0.016) (0.041)
educationlevelprimary/middle school 0.001 0.002 -0.000
(0.034) (0.034) (0.034)
educationlevelsecondary/post-primary 0.215*** 0.212*** 0.213***
(0.037) (0.037) (0.037)
educationleveltertiary/ post-secondary 0.236*** 0.240*** 0.236***
(0.046) (0.046) (0.046)
BIRTHEVENT -0.014** -0.016** -0.014**
(0.005) (0.006) (0.005)
factor(URBAN)1 -0.114*** -0.111*** -0.114***
(0.019) (0.019) (0.019)
SCORE 0.041*** 0.040*** 0.040***
(0.004) (0.004) (0.004)
factor(YEAR)2021 0.069*** 0.069*** 0.069***
(0.016) (0.016) (0.016)
factor(YEAR)2022 0.015 0.015 0.016
(0.016) (0.016) (0.016)
factor(FERTPREF)2 -0.040+ -0.039+ -0.039+
(0.023) (0.023) (0.023)
agegroup20-24 × maritalcombinedin a union 0.156**
(0.052)
agegroup25-29 × maritalcombinedin a union 0.241***
(0.058)
agegroup30-34 × maritalcombinedin a union 0.160*
(0.063)
agegroup35-39 × maritalcombinedin a union 0.055
(0.069)
agegroup40-44 × maritalcombinedin a union 0.084
(0.072)
agegroup45-49 × maritalcombinedin a union 0.193*
(0.079)
agegroup20-24 × usingmodernconyes 0.081
(0.052)
agegroup25-29 × usingmodernconyes 0.139**
(0.054)
agegroup30-34 × usingmodernconyes 0.127*
(0.057)
agegroup35-39 × usingmodernconyes 0.036
(0.060)
agegroup40-44 × usingmodernconyes 0.084
(0.068)
agegroup45-49 × usingmodernconyes 0.012
(0.077)
SD (Intercept FQINSTID) 0.389 0.387 0.389
SD (Observations) 0.655 0.655 0.655