#filter for women in fertility years
library(plyr)
## ------------------------------------------------------------------------------
## You have loaded plyr after dplyr - this is likely to cause problems.
## If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
## library(plyr); library(dplyr)
## ------------------------------------------------------------------------------
##
## Attaching package: 'plyr'
## The following objects are masked from 'package:dplyr':
##
## arrange, count, desc, failwith, id, mutate, rename, summarise,
## summarize
## The following object is masked from 'package:purrr':
##
## compact
library(dplyr)
datawomen<-mutate(pr, cbrwomen = (SEX== 2))
cbrpri <- filter(datawomen, AGE >= 15, AGE<= 44)
cbrpr<-filter(cbrpri, HISPAN==2)
#apply weights to get total number of women
cbrpr<-cbrpr%>%
filter(SEX==2)
cbrpr$cbrprwomen <- cbrpr$PERWT
cbrpr$ages <- as.numeric(cbrpr$AGE)
#apply weights to get total number of births
cbrpr$Fertility<-as.numeric(cbrpr$FERTYR)
cbrpr$Fertility2<-recode(cbrpr$Fertility,'1'=0, '2'=1, '0'=5, '8'=5)
cbrpr<-cbrpr%>%
filter(Fertility2<5)
cbrpr$births<-cbrpr$Fertility2*cbrpr$PERWT
library(dplyr)
agegroups<-cbrpr %>%
mutate(ages = case_when(
ages >= 15 & ages <= 19 ~ "15-19",
ages >= 20 & ages <= 25 ~ "20-25",
ages >= 26 & ages <= 30 ~ "26-30",
ages >= 31 & ages <= 35 ~ "31-35",
ages >= 36 & ages <= 40 ~ "36-40",
ages >= 41 & ages <= 44 ~ "41-44"))
#total number of puerto rican women and births to puerto rican women by year and age group
totalwomen<-aggregate(x=agegroups$cbrprwomen, by=list(agegroups$YEAR,agegroups$ages), FUN=sum)
totalbirths<-aggregate(x=agegroups$births, by=list(agegroups$YEAR,agegroups$ages), FUN=sum)
#divide number of children per women to get age specific fertility rate
#number births/number of women in given year
#unit is per 1,000
15-19
## [1] -4
20-25
## [1] -5
26-30
## [1] -4
31-35
## [1] -4
36-40
## [1] -4
41-44
## [1] -3
#sum fertility age specific fertility rates and divide by total groups to get TFR per year
#sum of ASFRs x5
#mimic this for
#filter for only Puerto Rican samples
#PR women living in PR
PRwomen<-filter(agegroups, SAMPLE==201502 | 201602 | 201702 | 201802 | 201902)
PRwomen2<-filter(PRwomen, SEX==2)
totalwomenPR<-aggregate(x=PRwomen2$cbrprwomen, by=list(PRwomen2$YEAR,PRwomen2$ages), FUN=sum)
totalbirthsPR<-aggregate(x=PRwomen2$births, by=list(PRwomen2$YEAR,PRwomen2$ages), FUN=sum)
#what % of women living in PR were born in the mainland US and what % was born on the island
totalwomenPR
## Group.1 Group.2 x
## 1 2015 15-19 340310
## 2 2016 15-19 344347
## 3 2017 15-19 334439
## 4 2018 15-19 320392
## 5 2019 15-19 323276
## 6 2021 15-19 308077
## 7 2015 20-25 397136
## 8 2016 20-25 392508
## 9 2017 20-25 393567
## 10 2018 20-25 387311
## 11 2019 20-25 382884
## 12 2021 20-25 385484
## 13 2015 26-30 311074
## 14 2016 26-30 313410
## 15 2017 26-30 320047
## 16 2018 26-30 320761
## 17 2019 26-30 310966
## 18 2021 26-30 320323
## 19 2015 31-35 314478
## 20 2016 31-35 293880
## 21 2017 31-35 301952
## 22 2018 31-35 290507
## 23 2019 31-35 302002
## 24 2021 31-35 297205
## 25 2015 36-40 302084
## 26 2016 36-40 309330
## 27 2017 36-40 305923
## 28 2018 36-40 300766
## 29 2019 36-40 313575
## 30 2021 36-40 298722
## 31 2015 41-44 231238
## 32 2016 41-44 234096
## 33 2017 41-44 218247
## 34 2018 41-44 230746
## 35 2019 41-44 227956
## 36 2021 41-44 242010
totalbirthsPR
## Group.1 Group.2 x
## 1 2015 15-19 9593
## 2 2016 15-19 9139
## 3 2017 15-19 4726
## 4 2018 15-19 6857
## 5 2019 15-19 4793
## 6 2021 15-19 2462
## 7 2015 20-25 33909
## 8 2016 20-25 33137
## 9 2017 20-25 28269
## 10 2018 20-25 26071
## 11 2019 20-25 27401
## 12 2021 20-25 24902
## 13 2015 26-30 28784
## 14 2016 26-30 29089
## 15 2017 26-30 24007
## 16 2018 26-30 26814
## 17 2019 26-30 27543
## 18 2021 26-30 26990
## 19 2015 31-35 20032
## 20 2016 31-35 20113
## 21 2017 31-35 22788
## 22 2018 31-35 19507
## 23 2019 31-35 21391
## 24 2021 31-35 19005
## 25 2015 36-40 8427
## 26 2016 36-40 10361
## 27 2017 36-40 11216
## 28 2018 36-40 11048
## 29 2019 36-40 14118
## 30 2021 36-40 10975
## 31 2015 41-44 2753
## 32 2016 41-44 2937
## 33 2017 41-44 2700
## 34 2018 41-44 3483
## 35 2019 41-44 2314
## 36 2021 41-44 3043
#filter for US Samples only and nativity as PR
#PR women born in PR living in US
PRwomenbornUS<-filter(agegroups, SAMPLE==201501 | 201601 | 201701 | 201802 | 201901)
PRwomenbornus2<-filter(PRwomenbornUS, SEX==2)
PRwomeninUSbornPR<-filter(PRwomenbornus2, BPL==110)
totalwomenUSbornPR<-aggregate(x=PRwomeninUSbornPR$cbrprwomen, by=list(PRwomeninUSbornPR$YEAR,PRwomeninUSbornPR$ages), FUN=sum)
totalbirthsUSbornPR<-aggregate(x=PRwomeninUSbornPR$births, by=list(PRwomeninUSbornPR$YEAR,PRwomeninUSbornPR$ages), FUN=sum)
totalwomenUSbornPR
## Group.1 Group.2 x
## 1 2015 15-19 144659
## 2 2016 15-19 145668
## 3 2017 15-19 140992
## 4 2018 15-19 131214
## 5 2019 15-19 128227
## 6 2021 15-19 120919
## 7 2015 20-25 182562
## 8 2016 20-25 179779
## 9 2017 20-25 179499
## 10 2018 20-25 178246
## 11 2019 20-25 178242
## 12 2021 20-25 174772
## 13 2015 26-30 145911
## 14 2016 26-30 149244
## 15 2017 26-30 149690
## 16 2018 26-30 144748
## 17 2019 26-30 146062
## 18 2021 26-30 154694
## 19 2015 31-35 164043
## 20 2016 31-35 153347
## 21 2017 31-35 152774
## 22 2018 31-35 141147
## 23 2019 31-35 147799
## 24 2021 31-35 137503
## 25 2015 36-40 163509
## 26 2016 36-40 179464
## 27 2017 36-40 175903
## 28 2018 36-40 158441
## 29 2019 36-40 160211
## 30 2021 36-40 160302
## 31 2015 41-44 136278
## 32 2016 41-44 135121
## 33 2017 41-44 132845
## 34 2018 41-44 133557
## 35 2019 41-44 132726
## 36 2021 41-44 140716
totalbirthsUSbornPR
## Group.1 Group.2 x
## 1 2015 15-19 5434
## 2 2016 15-19 4585
## 3 2017 15-19 2941
## 4 2018 15-19 3502
## 5 2019 15-19 1170
## 6 2021 15-19 1111
## 7 2015 20-25 14458
## 8 2016 20-25 15273
## 9 2017 20-25 9405
## 10 2018 20-25 12084
## 11 2019 20-25 10854
## 12 2021 20-25 8546
## 13 2015 26-30 10800
## 14 2016 26-30 11840
## 15 2017 26-30 8654
## 16 2018 26-30 10200
## 17 2019 26-30 12641
## 18 2021 26-30 9044
## 19 2015 31-35 8369
## 20 2016 31-35 10053
## 21 2017 31-35 9609
## 22 2018 31-35 8413
## 23 2019 31-35 8377
## 24 2021 31-35 7873
## 25 2015 36-40 3840
## 26 2016 36-40 4976
## 27 2017 36-40 4970
## 28 2018 36-40 3941
## 29 2019 36-40 7405
## 30 2021 36-40 3454
## 31 2015 41-44 1379
## 32 2016 41-44 1691
## 33 2017 41-44 1863
## 34 2018 41-44 2363
## 35 2019 41-44 1351
## 36 2021 41-44 1595
#filter for US Samples only nativity as US
#PR women born in US but ethnically PR
PRwomen<-filter(agegroups, SAMPLE==201501 | 201601 | 201701 | 201802 | 20190)
PRwomen2<-filter(PRwomen, SEX==2)
PRwomenbornUS<-filter(PRwomen2, BPL>=001 | BPL>2 | BPL<= 056)
totalwomenPRbornUS<-aggregate(x=PRwomenbornUS$cbrprwomen, by=list(PRwomenbornUS$YEAR,PRwomenbornUS$ages), FUN=sum)
totalbirthsPRbornUS<-aggregate(x=PRwomenbornUS$births, by=list(PRwomenbornUS$YEAR,PRwomenbornUS$ages), FUN=sum)
totalwomenPRbornUS
## Group.1 Group.2 x
## 1 2015 15-19 340310
## 2 2016 15-19 344347
## 3 2017 15-19 334439
## 4 2018 15-19 320392
## 5 2019 15-19 323276
## 6 2021 15-19 308077
## 7 2015 20-25 397136
## 8 2016 20-25 392508
## 9 2017 20-25 393567
## 10 2018 20-25 387311
## 11 2019 20-25 382884
## 12 2021 20-25 385484
## 13 2015 26-30 311074
## 14 2016 26-30 313410
## 15 2017 26-30 320047
## 16 2018 26-30 320761
## 17 2019 26-30 310966
## 18 2021 26-30 320323
## 19 2015 31-35 314478
## 20 2016 31-35 293880
## 21 2017 31-35 301952
## 22 2018 31-35 290507
## 23 2019 31-35 302002
## 24 2021 31-35 297205
## 25 2015 36-40 302084
## 26 2016 36-40 309330
## 27 2017 36-40 305923
## 28 2018 36-40 300766
## 29 2019 36-40 313575
## 30 2021 36-40 298722
## 31 2015 41-44 231238
## 32 2016 41-44 234096
## 33 2017 41-44 218247
## 34 2018 41-44 230746
## 35 2019 41-44 227956
## 36 2021 41-44 242010
totalbirthsPRbornUS
## Group.1 Group.2 x
## 1 2015 15-19 9593
## 2 2016 15-19 9139
## 3 2017 15-19 4726
## 4 2018 15-19 6857
## 5 2019 15-19 4793
## 6 2021 15-19 2462
## 7 2015 20-25 33909
## 8 2016 20-25 33137
## 9 2017 20-25 28269
## 10 2018 20-25 26071
## 11 2019 20-25 27401
## 12 2021 20-25 24902
## 13 2015 26-30 28784
## 14 2016 26-30 29089
## 15 2017 26-30 24007
## 16 2018 26-30 26814
## 17 2019 26-30 27543
## 18 2021 26-30 26990
## 19 2015 31-35 20032
## 20 2016 31-35 20113
## 21 2017 31-35 22788
## 22 2018 31-35 19507
## 23 2019 31-35 21391
## 24 2021 31-35 19005
## 25 2015 36-40 8427
## 26 2016 36-40 10361
## 27 2017 36-40 11216
## 28 2018 36-40 11048
## 29 2019 36-40 14118
## 30 2021 36-40 10975
## 31 2015 41-44 2753
## 32 2016 41-44 2937
## 33 2017 41-44 2700
## 34 2018 41-44 3483
## 35 2019 41-44 2314
## 36 2021 41-44 3043