Homework 2

Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.

Running Code

When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:

1 + 1
[1] 2

You can add options to executable code like this

[1] 4

The echo: false option disables the printing of code (only output is displayed).

##load libraries
library(haven)
library(survival)
library(car)
Loading required package: carData
library(muhaz)
library(tidyverse)
── Attaching packages
───────────────────────────────────────
tidyverse 1.3.2 ──
✔ ggplot2 3.3.6     ✔ purrr   0.3.4
✔ tibble  3.1.8     ✔ dplyr   1.0.9
✔ tidyr   1.2.0     ✔ stringr 1.4.1
✔ readr   2.1.2     ✔ forcats 0.5.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
✖ dplyr::recode() masks car::recode()
✖ purrr::some()   masks car::some()
library(ipumsr)
##load data


nhis9 <- read_stata("C:/Users/okabe/OneDrive/Pictures/Stats 2/nhis_00010.dta.gz")
nhis9<-zap_labels(nhis9)
#iew(nhis9)
##filter only respondants
nhis9 <- nhis9 %>%
  filter(mortelig == 1)
##Recode variables
#poor

nhis9$poor<-Recode(nhis9$pooryn, recodes ="7:9=NA; 1=0;2=1",as.factor=T)

##Alcohol

nhis9$alcohol<-Recode(nhis9$alclife, recodes ="1=1;2=0;7:9=NA;0=NA")

##smoking

nhis9$smoke<-Recode(nhis9$smokev, recodes ="7:9=NA;1=1;2=0; 0=NA")

##usborn
nhis9$born<-Recode(nhis9$usborn, recodes ="96:99=NA; 20=0;10:12=1;else=NA",as.factor=T)

##educ
nhis9$education<-Recode(nhis9$educ, recodes="101='0Prim'; 101:204='1somehs'; 300:302='2hsgrad'; 400:401='3somecol'; 500='4colgrad';600:603='5masteranddoc';402:403='6Techandacademic';604:999=NA;000=NA;100=NA; else= 'other'", as.factor=T)

##Recode
##citizen
nhis9$mycitizen<-Recode(nhis9$citizen, recodes ="7:9=NA; 1=1;2=0;else=NA",as.factor=T)

##Marital status

nhis9$marital<-Recode(nhis9$marstat, recodes="10='married'; 30='divorced'; 20='widowed'; 40='separated'; 50='nm'; else=NA", as.factor=T)
nhis9$marital<-relevel(nhis9$marital, ref='married')

#Age cut into intervals 


nhis9$agec<-cut(nhis9$age, breaks = c( 30, 40, 50, 60, 70, 80, 100), include.lowest = T)

#race/ethnicity
nhis9$race<-Recode(nhis9$racesr, recodes="100='white'; 200='black'; else='other'", as.factor=T)
nhis9$race<-relevel(nhis9$race, ref = "white")


##Hispanic
nhis9$ethnicity<-Recode(nhis9$hispeth, recodes="10='not hispanic'; 20:70='hispanic'; else=NA", as.factor=T)

##disease
#nhis2$disease<- Recode(nhis2$mortucodld, recodes= "01= 0; 02=1; 03=2; 04=3;05=4; 06=5; 07=6; 08= 7; 09=8; 10=9; else=NA")

#nhis2$disease<- Recode(nhis2$mortucodld, recodes= "01= 'Diseases of heart'; 02='Malignant neoplasms'; 03='Chronic lower respiratory diseases'; 04='Accidents';05='Cerebrovascular disease'; 06='Alzheimers disease'; 07='Diabetes mellitus'; 08= 'Influenza and pneumonia'; 09='Nephritis'; 10='All other causes'; 96= NA; else=NA",as.factor=T )


##sex
nhis9$male<-as.factor(ifelse(nhis9$sex==1, "Male", "Female"))
nhis9$male<-relevel(nhis9$male, ref='Male')

##Earnings
#nhis9$inc<-Recode(brfss_17$incomg, recodes = "9= NA;1='1_lt15k'; 2='2_15-25k';3='3_25-35k';4='4_35-50k';5='5_50kplus'", as.factor = T)

##employment status
nhis9$employed<- Recode(nhis9$empstat, recodes= " 100:122='unemployed'; 200:217='Employed';220='notinlaborforce'; else=NA", as.factor=T)

Submit either a link to an Rpub that has your homework published, or as a emailed Word document

Please answer the following questions, use appropriate figures and statistical output to answer the questions.

 

  1. Define your event variable

Death/mortality

  1. Define a duration or time variable

Data is collected from 2018. The age at death variable is if a respondent experienced the event based on age of of the survey, they are experiencing the event, if not, they are censored,this enables m to look at the variance between mortality rates for immigrants and US citizens based on age

  1. Define a censoring indicator

The respondent is censored if they did not experience the event. The of death is the censoring indicator

  1. Estimate the survival function for your outcome and plot it
        ##age at death
        nhis9 <- nhis9 %>%
          mutate(death_age = ifelse( mortstat ==1, 
                                     mortdody - (year - age), 
                                     2010 - (year - age)), 
                 d.event = ifelse(mortstat == 1, 1, 0))

        library(survival)

        age_fit <- survfit(Surv(death_age, d.event)~ 1, 
                           data = nhis9)

        library(ggsurvfit)

        age_fit %>%
          ggsurvfit() +
          add_confidence_interval(type = "ribbon") +
          add_quantile() 

This shows that the median age for mortality is about 80-85 years of age. It also shows that the probability of survival declines the older the participants get

  1. Carry out the following analysis
  1. Kaplan-Meier survival analysis of the outcome
            summary(age_fit)
Call: survfit(formula = Surv(death_age, d.event) ~ 1, data = nhis9)

 time n.risk n.event survival  std.err lower 95% CI upper 95% CI
   18  63776       3   1.0000 2.72e-05       0.9999       1.0000
   19  62539       1   0.9999 3.15e-05       0.9999       1.0000
   20  61335       1   0.9999 3.55e-05       0.9999       1.0000
   21  60212       3   0.9999 4.57e-05       0.9998       1.0000
   22  59085       5   0.9998 5.93e-05       0.9997       0.9999
   23  57928       6   0.9997 7.28e-05       0.9995       0.9998
   24  56696       9   0.9995 9.00e-05       0.9993       0.9997
   25  55534       7   0.9994 1.02e-04       0.9992       0.9996
   26  54347       8   0.9993 1.14e-04       0.9990       0.9995
   27  53241       6   0.9991 1.23e-04       0.9989       0.9994
   28  52013      12   0.9989 1.40e-04       0.9986       0.9992
   29  50800       8   0.9988 1.51e-04       0.9985       0.9990
   30  49604      17   0.9984 1.72e-04       0.9981       0.9987
   31  48369      14   0.9981 1.88e-04       0.9977       0.9985
   32  47233       9   0.9979 1.99e-04       0.9975       0.9983
   33  46077      12   0.9977 2.12e-04       0.9973       0.9981
   34  44905      10   0.9974 2.24e-04       0.9970       0.9979
   35  43763      14   0.9971 2.39e-04       0.9967       0.9976
   36  42589      18   0.9967 2.59e-04       0.9962       0.9972
   37  41480      15   0.9963 2.75e-04       0.9958       0.9969
   38  40304      18   0.9959 2.94e-04       0.9953       0.9965
   39  39149      22   0.9953 3.17e-04       0.9947       0.9960
   40  37937      14   0.9950 3.32e-04       0.9943       0.9956
   41  36629      21   0.9944 3.54e-04       0.9937       0.9951
   42  35523      14   0.9940 3.70e-04       0.9933       0.9947
   43  34377      27   0.9932 3.99e-04       0.9925       0.9940
   44  33208      23   0.9925 4.23e-04       0.9917       0.9934
   45  32006      28   0.9917 4.54e-04       0.9908       0.9926
   46  30774      34   0.9906 4.91e-04       0.9896       0.9915
   47  29592      24   0.9898 5.17e-04       0.9888       0.9908
   48  28421      24   0.9889 5.44e-04       0.9879       0.9900
   49  27175      38   0.9876 5.88e-04       0.9864       0.9887
   50  25957      46   0.9858 6.41e-04       0.9846       0.9871
   51  24712      34   0.9845 6.81e-04       0.9831       0.9858
   52  23571      47   0.9825 7.37e-04       0.9810       0.9839
   53  22417      63   0.9797 8.13e-04       0.9781       0.9813
   54  21231      57   0.9771 8.82e-04       0.9754       0.9788
   55  20115      76   0.9734 9.75e-04       0.9715       0.9753
   56  18946      64   0.9701 1.06e-03       0.9681       0.9722
   57  17892      63   0.9667 1.14e-03       0.9645       0.9689
   58  16901      66   0.9629 1.22e-03       0.9605       0.9653
   59  15950      77   0.9583 1.33e-03       0.9557       0.9609
   60  15102      88   0.9527 1.45e-03       0.9499       0.9555
   61  14146      84   0.9470 1.56e-03       0.9440       0.9501
   62  13295      78   0.9415 1.68e-03       0.9382       0.9448
   63  12399     106   0.9334 1.84e-03       0.9298       0.9370
   64  11462      86   0.9264 1.97e-03       0.9226       0.9303
   65  10724     101   0.9177 2.13e-03       0.9135       0.9219
   66  10022     113   0.9074 2.32e-03       0.9028       0.9119
   67   9325      84   0.8992 2.47e-03       0.8944       0.9040
   68   8668     121   0.8866 2.68e-03       0.8814       0.8919
   69   8049     114   0.8741 2.89e-03       0.8684       0.8798
   70   7518     137   0.8581 3.14e-03       0.8520       0.8643
   71   6965     104   0.8453 3.34e-03       0.8388       0.8519
   72   6472     119   0.8298 3.57e-03       0.8228       0.8368
   73   5997     125   0.8125 3.81e-03       0.8051       0.8200
   74   5552     136   0.7926 4.08e-03       0.7846       0.8006
   75   5118     133   0.7720 4.35e-03       0.7635       0.7806
   76   4689     113   0.7534 4.58e-03       0.7445       0.7624
   77   4325     135   0.7299 4.87e-03       0.7204       0.7395
   78   3989     130   0.7061 5.14e-03       0.6961       0.7162
   79   3694     117   0.6837 5.37e-03       0.6733       0.6943
   80   3389     130   0.6575 5.64e-03       0.6465       0.6686
   81   3092     160   0.6235 5.95e-03       0.6119       0.6353
   82   2804     159   0.5881 6.24e-03       0.5760       0.6005
   83   2526     156   0.5518 6.50e-03       0.5392       0.5647
   84   2269     156   0.5139 6.72e-03       0.5008       0.5272
   85   2027     211   0.4604 6.96e-03       0.4469       0.4742
   86   1502     265   0.3791 7.30e-03       0.3651       0.3937
   87   1237     275   0.2949 7.24e-03       0.2810       0.3094
   88    962     235   0.2228 6.83e-03       0.2098       0.2366
   89    727     184   0.1664 6.24e-03       0.1546       0.1791
   90    543     157   0.1183 5.49e-03       0.1080       0.1296
   91    386     142   0.0748 4.53e-03       0.0664       0.0842
   92    244     115   0.0395 3.38e-03       0.0334       0.0468
   93    129      81   0.0147 2.10e-03       0.0111       0.0195
   94     48      48   0.0000      NaN           NA           NA
  1. Define a grouping variable, this can be dichotomous or categorical.

The grouping variable I would like to use is citizenship status(citizen) to see if mortality varies between non citizens and US citizens

  1. Do you have a research hypothesis about the survival patterns for the levels of the categorical variable?  State it.

The hypothesis I would like to state is that overtime mortality rates of non-citizens mirrors that of US born the longer they reside in the United States. This takes into account SES(in this case living above or below the poverty threshold)

  1. Comparison of Kaplan-Meier survival across grouping variables in your data. Interpret your results
library(survival)

citfit <-survfit(Surv(death_age, d.event) ~ mycitizen+poor,
                   data = nhis9)
summary(citfit)
Call: survfit(formula = Surv(death_age, d.event) ~ mycitizen + poor, 
    data = nhis9)

7832 observations deleted due to missingness 
                mycitizen=0, poor=0 
 time n.risk n.event survival  std.err lower 95% CI upper 95% CI
   18  42337       2   1.0000 3.34e-05       0.9999       1.0000
   20  40900       1   0.9999 4.14e-05       0.9998       1.0000
   21  40302       2   0.9999 5.43e-05       0.9998       1.0000
   22  39689       4   0.9998 7.40e-05       0.9996       0.9999
   23  39017       3   0.9997 8.63e-05       0.9995       0.9999
   24  38289       6   0.9995 1.07e-04       0.9993       0.9998
   25  37596       5   0.9994 1.23e-04       0.9992       0.9997
   26  36880       4   0.9993 1.34e-04       0.9990       0.9996
   27  36173       2   0.9992 1.40e-04       0.9990       0.9995
   28  35403       6   0.9991 1.56e-04       0.9988       0.9994
   29  34634       2   0.9990 1.61e-04       0.9987       0.9993
   30  33878       8   0.9988 1.81e-04       0.9984       0.9991
   31  33144       5   0.9986 1.93e-04       0.9983       0.9990
   32  32447       4   0.9985 2.03e-04       0.9981       0.9989
   33  31747       9   0.9982 2.24e-04       0.9978       0.9987
   34  31012       7   0.9980 2.39e-04       0.9975       0.9985
   35  30295       9   0.9977 2.59e-04       0.9972       0.9982
   36  29580       9   0.9974 2.78e-04       0.9969       0.9979
   37  28892      11   0.9970 3.00e-04       0.9964       0.9976
   38  28147      10   0.9967 3.21e-04       0.9960       0.9973
   39  27432      14   0.9962 3.48e-04       0.9955       0.9968
   40  26638       8   0.9959 3.64e-04       0.9951       0.9966
   41  25772      14   0.9953 3.91e-04       0.9946       0.9961
   42  25040       6   0.9951 4.03e-04       0.9943       0.9959
   43  24274      18   0.9943 4.39e-04       0.9935       0.9952
   44  23485      13   0.9938 4.64e-04       0.9929       0.9947
   45  22652      18   0.9930 5.00e-04       0.9920       0.9940
   46  21831      22   0.9920 5.43e-04       0.9909       0.9931
   47  21038      14   0.9913 5.70e-04       0.9902       0.9925
   48  20238      16   0.9906 6.03e-04       0.9894       0.9917
   49  19360      18   0.9896 6.40e-04       0.9884       0.9909
   50  18499      28   0.9881 6.99e-04       0.9868       0.9895
   51  17616      18   0.9871 7.38e-04       0.9857       0.9886
   52  16775      36   0.9850 8.16e-04       0.9834       0.9866
   53  15941      43   0.9824 9.09e-04       0.9806       0.9841
   54  15092      37   0.9799 9.89e-04       0.9780       0.9819
   55  14271      43   0.9770 1.08e-03       0.9749       0.9791
   56  13436      37   0.9743 1.17e-03       0.9720       0.9766
   57  12666      40   0.9712 1.26e-03       0.9688       0.9737
   58  11930      39   0.9681 1.36e-03       0.9654       0.9707
   59  11235      42   0.9644 1.46e-03       0.9616       0.9673
   60  10620      55   0.9594 1.60e-03       0.9563       0.9626
   61   9913      58   0.9538 1.75e-03       0.9504       0.9573
   62   9266      52   0.9485 1.89e-03       0.9448       0.9522
   63   8590      77   0.9400 2.11e-03       0.9358       0.9441
   64   7873      62   0.9326 2.29e-03       0.9281       0.9371
   65   7323      75   0.9230 2.52e-03       0.9181       0.9280
   66   6820      79   0.9123 2.76e-03       0.9069       0.9178
   67   6307      59   0.9038 2.95e-03       0.8980       0.9096
   68   5810      89   0.8899 3.25e-03       0.8836       0.8963
   69   5384      76   0.8774 3.51e-03       0.8705       0.8843
   70   5019      94   0.8610 3.83e-03       0.8535       0.8685
   71   4645      76   0.8469 4.10e-03       0.8389       0.8549
   72   4286      87   0.8297 4.41e-03       0.8211       0.8384
   73   3956      93   0.8102 4.75e-03       0.8009       0.8195
   74   3655      92   0.7898 5.08e-03       0.7799       0.7998
   75   3359      99   0.7665 5.44e-03       0.7559       0.7772
   76   3061      84   0.7455 5.76e-03       0.7343       0.7568
   77   2802      97   0.7197 6.13e-03       0.7078       0.7318
   78   2574      89   0.6948 6.46e-03       0.6822       0.7075
   79   2371      86   0.6696 6.77e-03       0.6564       0.6830
   80   2166      97   0.6396 7.12e-03       0.6258       0.6537
   81   1969     100   0.6071 7.46e-03       0.5927       0.6219
   82   1788     113   0.5687 7.81e-03       0.5536       0.5843
   83   1606     100   0.5333 8.09e-03       0.5177       0.5494
   84   1438     105   0.4944 8.34e-03       0.4783       0.5110
   85   1279     136   0.4418 8.59e-03       0.4253       0.4590
   86    984     172   0.3646 8.88e-03       0.3476       0.3824
   87    812     189   0.2797 8.70e-03       0.2632       0.2973
   88    623     149   0.2128 8.16e-03       0.1974       0.2294
   89    474     113   0.1621 7.48e-03       0.1481       0.1774
   90    361     113   0.1114 6.49e-03       0.0993       0.1248
   91    248      91   0.0705 5.34e-03       0.0608       0.0818
   92    157      73   0.0377 4.00e-03       0.0306       0.0464
   93     84      52   0.0144 2.51e-03       0.0102       0.0202
   94     32      32   0.0000      NaN           NA           NA

                mycitizen=0, poor=1 
 time n.risk n.event survival  std.err lower 95% CI upper 95% CI
   18   6437       1   0.9998 0.000155      0.99954       1.0000
   21   5657       1   0.9997 0.000235      0.99921       1.0000
   23   5227       2   0.9993 0.000358      0.99858       1.0000
   24   5029       3   0.9987 0.000497      0.99772       0.9997
   25   4847       1   0.9985 0.000538      0.99743       0.9995
   26   4696       1   0.9983 0.000578      0.99714       0.9994
   27   4564       2   0.9978 0.000655      0.99655       0.9991
   28   4440       3   0.9972 0.000762      0.99567       0.9987
   29   4318       2   0.9967 0.000828      0.99507       0.9983
   30   4189       6   0.9953 0.001012      0.99329       0.9973
   31   4059       3   0.9945 0.001096      0.99239       0.9967
   32   3936       2   0.9940 0.001153      0.99177       0.9963
   33   3815       2   0.9935 0.001210      0.99114       0.9959
   34   3714       2   0.9930 0.001267      0.99049       0.9955
   35   3611       1   0.9927 0.001296      0.99016       0.9952
   36   3514       3   0.9918 0.001384      0.98914       0.9946
   37   3413       1   0.9916 0.001414      0.98879       0.9943
   38   3304       5   0.9901 0.001563      0.98700       0.9931
   39   3201       3   0.9891 0.001651      0.98590       0.9924
   40   3112       2   0.9885 0.001710      0.98515       0.9919
   41   3017       2   0.9878 0.001770      0.98438       0.9913
   42   2911       4   0.9865 0.001893      0.98278       0.9902
   43   2826       6   0.9844 0.002074      0.98033       0.9885
   44   2734       5   0.9826 0.002221      0.97825       0.9869
   45   2641       6   0.9804 0.002395      0.97567       0.9851
   46   2547       5   0.9784 0.002540      0.97346       0.9834
   47   2433       4   0.9768 0.002661      0.97162       0.9821
   48   2323       6   0.9743 0.002846      0.96874       0.9799
   49   2211      10   0.9699 0.003156      0.96373       0.9761
   50   2106       7   0.9667 0.003372      0.96008       0.9733
   51   2024       6   0.9638 0.003560      0.95685       0.9708
   52   1940       8   0.9598 0.003812      0.95239       0.9673
   53   1854       8   0.9557 0.004067      0.94775       0.9637
   54   1762      11   0.9497 0.004422      0.94109       0.9584
   55   1681      19   0.9390 0.005011      0.92922       0.9489
   56   1584      18   0.9283 0.005549      0.91750       0.9393
   57   1488       6   0.9246 0.005734      0.91340       0.9359
   58   1419      12   0.9168 0.006113      0.90485       0.9288
   59   1338      20   0.9031 0.006746      0.88992       0.9164
   60   1273      21   0.8882 0.007377      0.87381       0.9027
   61   1200      13   0.8785 0.007764      0.86345       0.8939
   62   1143      15   0.8670 0.008213      0.85105       0.8833
   63   1070      16   0.8540 0.008707      0.83714       0.8713
   64   1002      14   0.8421 0.009150      0.82436       0.8602
   65    953      10   0.8333 0.009471      0.81491       0.8520
   66    914      16   0.8187 0.009983      0.79935       0.8385
   67    860      10   0.8092 0.010311      0.78920       0.8296
   68    817      20   0.7894 0.010969      0.76815       0.8111
   69    760      17   0.7717 0.011529      0.74943       0.7946
   70    717      19   0.7512 0.012141      0.72783       0.7754
   71    664      13   0.7365 0.012569      0.71231       0.7616
   72    625      14   0.7200 0.013038      0.69494       0.7461
   73    575      13   0.7038 0.013503      0.67779       0.7307
   74    527      15   0.6837 0.014074      0.65670       0.7119
   75    486      17   0.6598 0.014729      0.63157       0.6893
   76    445      13   0.6405 0.015238      0.61136       0.6711
   77    410      12   0.6218 0.015724      0.59173       0.6534
   78    386      21   0.5880 0.016510      0.55648       0.6212
   79    356       9   0.5731 0.016820      0.54106       0.6070
   80    331      12   0.5523 0.017246      0.51953       0.5872
   81    309      18   0.5201 0.017831      0.48635       0.5563
   82    274      17   0.4879 0.018363      0.45318       0.5252
   83    242      23   0.4415 0.018993      0.40581       0.4803
   84    206      15   0.4094 0.019339      0.37316       0.4491
   85    187      14   0.3787 0.019549      0.34227       0.4190
   86    137      24   0.3124 0.020280      0.27505       0.3548
   87    113      33   0.2211 0.019613      0.18586       0.2631
   88     80      13   0.1852 0.018788      0.15182       0.2259
   89     67      21   0.1272 0.016630      0.09841       0.1643
   90     46      13   0.0912 0.014615      0.06664       0.1249
   91     33       8   0.0691 0.012996      0.04780       0.0999
   92     25      10   0.0415 0.010327      0.02545       0.0676
   93     15       9   0.0166 0.006676      0.00754       0.0365
   94      6       6   0.0000      NaN           NA           NA

                mycitizen=1, poor=0 
 time n.risk n.event survival  std.err lower 95% CI upper 95% CI
   22   4600       1   0.9998 0.000217      0.99936        1.000
   25   4306       1   0.9996 0.000318      0.99893        1.000
   28   3899       1   0.9993 0.000408      0.99849        1.000
   29   3764       1   0.9990 0.000487      0.99807        1.000
   30   3630       1   0.9988 0.000559      0.99766        1.000
   31   3480       2   0.9982 0.000691      0.99683        1.000
   35   2857       1   0.9978 0.000774      0.99631        0.999
   36   2697       2   0.9971 0.000933      0.99526        0.999
   37   2549       2   0.9963 0.001084      0.99418        0.998
   38   2404       1   0.9959 0.001160      0.99362        0.998
   39   2260       1   0.9955 0.001241      0.99302        0.998
   40   2124       3   0.9940 0.001481      0.99115        0.997
   41   1987       2   0.9930 0.001640      0.98984        0.996
   42   1874       3   0.9915 0.001876      0.98779        0.995
   43   1763       2   0.9903 0.002036      0.98635        0.994
   44   1647       1   0.9897 0.002122      0.98558        0.994
   45   1540       1   0.9891 0.002215      0.98475        0.993
   46   1437       3   0.9870 0.002511      0.98211        0.992
   47   1344       1   0.9863 0.002615      0.98118        0.991
   48   1261       1   0.9855 0.002727      0.98018        0.991
   49   1148       4   0.9821 0.003213      0.97580        0.988
   50   1073       2   0.9802 0.003458      0.97349        0.987
   51    991       3   0.9773 0.003848      0.96976        0.985
   53    852       3   0.9738 0.004317      0.96541        0.982
   54    781       1   0.9726 0.004488      0.96383        0.981
   55    711       5   0.9657 0.005399      0.95522        0.976
   56    643       2   0.9627 0.005785      0.95147        0.974
   57    588       4   0.9562 0.006608      0.94333        0.969
   58    533       3   0.9508 0.007265      0.93668        0.965
   59    482       3   0.9449 0.007983      0.92938        0.961
   60    448       3   0.9386 0.008725      0.92162        0.956
   61    408       4   0.9294 0.009778      0.91040        0.949
   62    376       2   0.9244 0.010332      0.90439        0.945
   63    349       1   0.9218 0.010636      0.90116        0.943
   65    288       4   0.9090 0.012264      0.88525        0.933
   66    259       2   0.9020 0.013136      0.87657        0.928
   67    237       4   0.8867 0.014957      0.85789        0.917
   68    216       2   0.8785 0.015906      0.84789        0.910
   69    193       3   0.8649 0.017504      0.83123        0.900
   70    173       1   0.8599 0.018102      0.82510        0.896
   71    157       2   0.8489 0.019458      0.81162        0.888
   74    117       5   0.8126 0.024473      0.76605        0.862
   75    102       5   0.7728 0.029042      0.71792        0.832
   77     81       3   0.7442 0.032328      0.68343        0.810
   78     72       1   0.7338 0.033490      0.67105        0.803
   79     65       3   0.7000 0.037218      0.63069        0.777
   80     59       2   0.6762 0.039558      0.60299        0.758
   81     51       5   0.6099 0.045453      0.52706        0.706
   83     40       2   0.5794 0.048024      0.49257        0.682
   84     36       3   0.5312 0.051482      0.43926        0.642
   85     29       3   0.4762 0.055070      0.37963        0.597
   87     15       3   0.3810 0.066029      0.27124        0.535
   88     12       3   0.2857 0.068704      0.17835        0.458
   89      9       5   0.1270 0.056322      0.05324        0.303
   90      4       2   0.0635 0.042437      0.01713        0.235
   92      2       1   0.0317 0.030890      0.00472        0.214
   94      1       1   0.0000      NaN           NA           NA

                mycitizen=1, poor=1 
 time n.risk n.event survival  std.err lower 95% CI upper 95% CI
   27   1852       2   0.9989 0.000763       0.9974        1.000
   30   1637       1   0.9983 0.000977       0.9964        1.000
   31   1551       1   0.9977 0.001169       0.9954        1.000
   34   1336       1   0.9969 0.001386       0.9942        1.000
   35   1256       2   0.9953 0.001782       0.9918        0.999
   36   1176       1   0.9945 0.001971       0.9906        0.998
   37   1104       1   0.9936 0.002165       0.9894        0.998
   39    962       2   0.9915 0.002607       0.9864        0.997
   41    827       1   0.9903 0.002866       0.9847        0.996
   43    720       1   0.9889 0.003175       0.9827        0.995
   44    661       3   0.9845 0.004084       0.9765        0.992
   46    553       2   0.9809 0.004782       0.9716        0.990
   47    504       2   0.9770 0.005499       0.9663        0.988
   50    403       1   0.9746 0.005996       0.9629        0.986
   51    370       1   0.9719 0.006533       0.9592        0.985
   52    345       1   0.9691 0.007095       0.9553        0.983
   53    314       3   0.9599 0.008814       0.9427        0.977
   54    289       1   0.9565 0.009389       0.9383        0.975
   55    274       2   0.9496 0.010538       0.9291        0.970
   56    256       1   0.9459 0.011131       0.9243        0.968
   58    226       1   0.9417 0.011842       0.9187        0.965
   59    206       1   0.9371 0.012636       0.9127        0.962
   61    169       1   0.9316 0.013724       0.9050        0.959
   62    155       1   0.9255 0.014894       0.8968        0.955
   64    133       3   0.9047 0.018813       0.8685        0.942
   67     99       1   0.8955 0.020724       0.8558        0.937
   68     91       1   0.8857 0.022713       0.8423        0.931
   69     82       1   0.8749 0.024872       0.8275        0.925
   70     74       1   0.8631 0.027201       0.8114        0.918
   72     60       2   0.8343 0.033037       0.7720        0.902
   73     56       2   0.8045 0.037986       0.7334        0.883
   74     52       2   0.7736 0.042360       0.6948        0.861
   76     43       2   0.7376 0.047418       0.6503        0.837
   77     37       1   0.7176 0.050152       0.6258        0.823
   78     32       1   0.6952 0.053364       0.5981        0.808
   79     28       1   0.6704 0.056942       0.5676        0.792
   80     25       1   0.6436 0.060651       0.5350        0.774
   81     21       1   0.6129 0.065046       0.4978        0.755
   82     18       1   0.5789 0.069778       0.4571        0.733
   84     15       1   0.5403 0.075043       0.4115        0.709
   86      8       2   0.4052 0.100046       0.2498        0.657
   87      6       1   0.3377 0.103691       0.1850        0.616
   90      5       1   0.2701 0.102615       0.1283        0.569
   91      4       1   0.2026 0.096664       0.0795        0.516
   92      3       2   0.0675 0.063866       0.0106        0.431
   94      1       1   0.0000      NaN           NA           NA

Important to note:

Poor is coded, 0 for above the poverty threshold and 1 for below the poverty threshold Being a U.S citizen is coded 0 as being a citizen and 1 for not being a citizen

Interpretation of the results:

The data shows that U.S citizens both above and below the poverty line have a lesser probability of survival compared to non- U.S citizens above and below the poverty line.

The results including the graph below, also show that citizens below the poverty line have a lower probability of survival while non-citizens below the poverty threshold outlive both citizens and non-citizens above the poverty threshold. This therefore shows that my hypothesis is null because non-citizens out live U.S citizens. The paradox could probably support these results where we see immigrants living longer with low SES

##comparing the difference be difference among groups

survdiff(Surv(death_age, d.event) ~ mycitizen+poor,
                   data = nhis9)
Call:
survdiff(formula = Surv(death_age, d.event) ~ mycitizen + poor, 
    data = nhis9)

n=55944, 7832 observations deleted due to missingness.

                        N Observed Expected (O-E)^2/E (O-E)^2/V
mycitizen=0, poor=0 42337     3847   4036.0    8.8470   61.8517
mycitizen=0, poor=1  6437      743    554.0   64.4437   80.9583
mycitizen=1, poor=0  4861      136    137.8    0.0231    0.0253
mycitizen=1, poor=1  2309       64     62.2    0.0513    0.0568

 Chisq= 81.5  on 3 degrees of freedom, p= <2e-16 

This shows that there is a difference in survival status first among those living above and below the poverty threshold as well as those who are citizens and not citizens. This is seen by looking at the differences between the observed v. expected deaths as well as by looking at the huge difference between the Chisq and the p value

e.Plot the survival function for the analysis for each level of the group variable

    citfit%>%
      ggsurvfit(xlim=c(0,12),
    ylim=c(.90, 4),
    conf.int=T,
    title="Survival Function for mortality - citizen vs non citizen")
Warning: Ignoring unknown parameters: xlim, ylim, conf.int, title

Citations: Lynn A. Blewett, Julia A. Rivera Drew, Miriam L. King, Kari C.W. Williams, Natalie Del Ponte and Pat Convey. IPUMS Health Surveys: National Health Interview Survey, Version 7.1 [dataset]. Minneapolis, MN: IPUMS, 2021. https://doi.org/10.18128/D070.V7.1