How does health insurance status impact the survival time of individuals diagnosed with prostate-related conditions, and what role do other factors such as education, income, and race/ethnicity play in this context?

Interpretation: Multiple datasets from NHANES are merged here to create a comprehensive dataset. This approach ensures that all relevant variables are included for the analysis, focusing on factors impacting the survival of individuals with prostate-related conditions.

# Load necessary libraries
library(survival)
library(ggplot2)
library(survminer)
## Loading required package: ggpubr
## 
## Attaching package: 'survminer'
## The following object is masked from 'package:survival':
## 
##     myeloma
library(ggpubr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(mice)
## 
## Attaching package: 'mice'
## The following object is masked from 'package:stats':
## 
##     filter
## The following objects are masked from 'package:base':
## 
##     cbind, rbind
library(tidyr)
library(dplyr)
library(haven)
library(Amelia)
## Loading required package: Rcpp
## ## 
## ## Amelia II: Multiple Imputation
## ## (Version 1.8.1, built: 2022-11-18)
## ## Copyright (C) 2005-2023 James Honaker, Gary King and Matthew Blackwell
## ## Refer to http://gking.harvard.edu/amelia/ for more information
## ##
library(purrr)
library(readr)
library(survey)
## Loading required package: grid
## Loading required package: Matrix
## 
## Attaching package: 'Matrix'
## The following objects are masked from 'package:tidyr':
## 
##     expand, pack, unpack
## 
## Attaching package: 'survey'
## The following object is masked from 'package:graphics':
## 
##     dotchart
# Read the XPT files with the complete file paths
nhanes_data <- read_xpt("/Users/christacrumrine/Desktop/Ad_methods/DEMO_D.XPT")
nhanes_data1 <- read_xpt("/Users/christacrumrine/Desktop/Ad_methods/ALQ_D.XPT")
nhanes_data2 <- read_xpt("/Users/christacrumrine/Desktop/Ad_methods/HIQ_D.XPT")
nhanes_data3 <- read_xpt("/Users/christacrumrine/Desktop/Ad_methods/KIQ_P_D.XPT")
nhanes_data4 <- read_xpt("/Users/christacrumrine/Desktop/Ad_methods/MCQ_D.XPT")
nhanes_data5 <- read_xpt("/Users/christacrumrine/Desktop/Ad_methods/PSA_D.XPT")
nhanes_data6 <- read_xpt("/Users/christacrumrine/Desktop/Ad_methods/PSQ_D.XPT")



# Combine all ways
#merged_data <- left_join(nhanes_data1, nhanes_data, by="SEQN")
#merged_data <- left_join(merged_data, nhanes_data2, by="SEQN")
#merged_data <- left_join(merged_data, nhanes_data3, by="SEQN")
#merged_data <- left_join(merged_data, nhanes_data4, by="SEQN")
#merged_data <- left_join(merged_data, nhanes_data5, by="SEQN")
#merged_data <- left_join(merged_data, nhanes_data6, by="SEQN")
#merged_data <- left_join(merged_data, nhanes_data7, by="SEQN")
#merged_data <- left_join(merged_data, NHANES_2006, by="SEQN")

df = list(nhanes_data,nhanes_data6, nhanes_data2,nhanes_data3, nhanes_data4, nhanes_data5, nhanes_data6)
final_df <- df %>% reduce(full_join,
                          by  = 'SEQN')


# Merge the datasets using bind_rows
#merged_data <- bind_rows(nhanes_data, nhanes_data1, nhanes_data2, nhanes_data3, nhanes_data4)
srvyin <- paste("NHANES_2005_2006_MORT_2019_PUBLIC (1).dat")   # full .DAT name here
srvyout <- "NHANES_2006" # shorthand dataset name here


# read in the fixed-width format ASCII file
dsn <- read_fwf(file=srvyin,
                col_types = "iiiiiiii",
                fwf_cols(seqn = c(1,6),
                         eligstat = c(15,15),
                         mortstat = c(16,16),
                         ucod_leading = c(17,19),
                         diabetes = c(20,20),
                         hyperten = c(21,21),
                         permth_int = c(43,45),
                         permth_exm = c(46,48)
                ),
                na = c("", ".")
)

dsn <- dsn %>%
  rename(SEQN = seqn)

Interpretation: In this step, key variables relevant to the study, such as education, race/ethnicity, income, and health metrics, are selected and cleaned. This includes handling missing values and transforming some variables to the appropriate format for analysis.

# Create a new dataset with selected variables
selected_data <- final_df %>%
  left_join(dsn, by = "SEQN") %>%
  select(
    Education = DMDEDUC2, #categorical
    Race_Ethnicity = RIDRETH1, #categorical
    Income = INDFMINC, #categorical
    AGE_TOLD_PROSTATE = KID221, #continuous
    DIAGNOSED_PROSTATE = KIQ201, #categorical
    PROSTATE_ENLARGE = KIQ121, #continuous
    AGE_PSA_TEST = MCQ320, 
    PSA_TOTAL = LBXP1, #continuous
    AGE_CURRENT = RIDAGEYR, #continuous
    CITIZEN_STATUS = DMDCITZN, #categorical
    HEALTH_INSURANCE = HIQ011, #categorical
    CURRENT_AGE = RIDAGEYR, #continuous
    #RADIATION_TX = KIQ301,
    #MED_TX = KIQ311,
    mortstat,  # Adding mortstat from dsn,
    permth_exm,
    ucod_leading,
#AGE_AT_DEATH is a continuous variable
  )

 #Replace NA with 0 in all columns of select_data
selected_data <- selected_data %>%
  mutate_all(funs(ifelse(is.na(.), 0, .)))
## Warning: `funs()` was deprecated in dplyr 0.8.0.
## ℹ Please use a list of either functions or lambdas:
## 
## # Simple named list: list(mean = mean, median = median)
## 
## # Auto named with `tibble::lst()`: tibble::lst(mean, median)
## 
## # Using lambdas list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
selected_data <- selected_data %>%
  mutate(AGE_TOLD_PROSTATE = as.character(AGE_TOLD_PROSTATE),
         AGE_TOLD_PROSTATE = ifelse(AGE_TOLD_PROSTATE == "85 or greater", "85", AGE_TOLD_PROSTATE),
         AGE_TOLD_PROSTATE = as.numeric(as.character(AGE_TOLD_PROSTATE)))
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `AGE_TOLD_PROSTATE =
##   as.numeric(as.character(AGE_TOLD_PROSTATE))`.
## Caused by warning:
## ! NAs introduced by coercion
# First, filter out deaths not caused by ucod_leading == 2
selected_data_filtered <- selected_data %>%
  filter(!(mortstat == 1 & ucod_leading != 2))

#0= non-white and 1=white
selected_data <- selected_data %>%
  mutate(Race_Ethnicity = ifelse(Race_Ethnicity == 1, 1, 0))

selected_data <- na.omit(selected_data)

#0=no insurance, 1=insurance 
selected_data <- selected_data %>%
  mutate(HEALTH_INSURANCE = ifelse(HEALTH_INSURANCE == 1, 1, 0))

#0= no college and 1=college
selected_data <- selected_data %>%
  mutate(Education = case_when(
    Education %in% c(1, 2, 3) ~ 0,
    Education %in% c(4, 5) ~ 1,
    TRUE ~ NA_real_ # Assign NA to values that are not 1, 2, 3, 4, or 5
  )) %>%
  na.omit() # Remove rows with any NA values
selected_data <- na.omit(selected_data)

selected_data <- selected_data %>%
  mutate(Education = ifelse(Education == 99, NA, Education),
         Income = ifelse(Income == 99, NA, Income),
         Race_Ethnicity = ifelse(Race_Ethnicity == 99, NA, Race_Ethnicity),
         # ... add other variables as needed
        ) %>%
  na.omit() # Remove rows with any NA values

Education has been turned into a dummy variable with 0 equaling high school or less and 1 equaling some college for more.

Health insurance is the focal variable with 0 meaning no health insurance and 1 with health insurance

Race ethnicity is 0 for non-white and 1 for NH-white

library(dplyr)

# Filter to keep only those diagnosed with prostate cancer
selected_data <- selected_data %>%
  filter(DIAGNOSED_PROSTATE == 1)

# Create or update the Age_or_Status variable
selected_data <- selected_data %>%
  mutate(Age_or_Status = ifelse(mortstat == 0, 
                                paste0(CURRENT_AGE, "+"), # Current age with a '+' if alive
                                as.character(floor(CURRENT_AGE + permth_exm / 12)))) # Rounded down age at death if dead

# This code first filters the selected_data to include only individuals diagnosed with prostate cancer.
# Then, for these individuals:
# - If they died from ucod_leading == 2, it calculates their age at death.
# - If they are still alive, it shows their current age with a '+'.
library(stringr)
library(dplyr)

# Selecting CURRENT_AGE and Age_or_Status from the filtered data
age_status_table <- selected_data %>%
  select(CURRENT_AGE, Age_or_Status)

# Create a numeric variable from Age_or_Status
selected_data <- selected_data %>%
  mutate(Age_or_Status_Numeric = as.numeric(str_replace(Age_or_Status, "\\+", "")))
selected_data <- selected_data %>%
  mutate(permth_exm = floor(permth_exm / 12))

selected_data <- selected_data %>%
  mutate(Age_At_Death = AGE_CURRENT + permth_exm)

Subset to Diagnosed Men

Interpretation: The dataset is filtered to include only individuals diagnosed with prostate conditions.

selected_data <- selected_data %>% filter(DIAGNOSED_PROSTATE ==1)

selected_data
## # A tibble: 57 × 18
##    Education Race_Ethnicity Income AGE_TOLD_PROSTATE DIAGNOSED_PROSTATE
##        <dbl>          <dbl>  <dbl>             <dbl>              <dbl>
##  1         1              0      5                79                  1
##  2         1              0      7                65                  1
##  3         1              0      6                61                  1
##  4         1              0      7                64                  1
##  5         0              0     10                62                  1
##  6         1              0     11                54                  1
##  7         0              0      7                64                  1
##  8         1              0     11                80                  1
##  9         0              0      5                70                  1
## 10         0              0     11                59                  1
## # ℹ 47 more rows
## # ℹ 13 more variables: PROSTATE_ENLARGE <dbl>, AGE_PSA_TEST <dbl>,
## #   PSA_TOTAL <dbl>, AGE_CURRENT <dbl>, CITIZEN_STATUS <dbl>,
## #   HEALTH_INSURANCE <dbl>, CURRENT_AGE <dbl>, mortstat <dbl>,
## #   permth_exm <dbl>, ucod_leading <dbl>, Age_or_Status <chr>,
## #   Age_or_Status_Numeric <dbl>, Age_At_Death <dbl>

Interpretation: This section provides a detailed summary of key variables, including demographic and health-related factors.

# Summary statistics of key variables
summary(selected_data$Age_At_Death)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   65.00   79.00   85.00   83.19   89.00   94.00
summary(selected_data$Education)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  0.0000  0.0000  1.0000  0.5439  1.0000  1.0000
summary(selected_data$Income)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   2.000   6.000   7.000   8.368   8.000  77.000
summary(selected_data$Race_Ethnicity)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
## 0.00000 0.00000 0.00000 0.03509 0.00000 1.00000
summary(selected_data$Citizenship_Status)
## Warning: Unknown or uninitialised column: `Citizenship_Status`.
## Length  Class   Mode 
##      0   NULL   NULL
summary(selected_data$Health_Insurance)
## Warning: Unknown or uninitialised column: `Health_Insurance`.
## Length  Class   Mode 
##      0   NULL   NULL
dim(selected_data)  # Check dimensions of the dataframe
## [1] 57 18
summary(selected_data)
##    Education      Race_Ethnicity        Income       AGE_TOLD_PROSTATE
##  Min.   :0.0000   Min.   :0.00000   Min.   : 2.000   Min.   :54.00    
##  1st Qu.:0.0000   1st Qu.:0.00000   1st Qu.: 6.000   1st Qu.:64.00    
##  Median :1.0000   Median :0.00000   Median : 7.000   Median :70.00    
##  Mean   :0.5439   Mean   :0.03509   Mean   : 8.368   Mean   :69.84    
##  3rd Qu.:1.0000   3rd Qu.:0.00000   3rd Qu.: 8.000   3rd Qu.:75.00    
##  Max.   :1.0000   Max.   :1.00000   Max.   :77.000   Max.   :85.00    
##  DIAGNOSED_PROSTATE PROSTATE_ENLARGE  AGE_PSA_TEST     PSA_TOTAL
##  Min.   :1          Min.   :0.000    Min.   :  0.0   Min.   :0  
##  1st Qu.:1          1st Qu.:1.000    1st Qu.: 55.0   1st Qu.:0  
##  Median :1          Median :1.000    Median : 66.0   Median :0  
##  Mean   :1          Mean   :1.263    Mean   :144.3   Mean   :0  
##  3rd Qu.:1          3rd Qu.:1.000    3rd Qu.: 72.0   3rd Qu.:0  
##  Max.   :1          Max.   :9.000    Max.   :999.0   Max.   :0  
##   AGE_CURRENT    CITIZEN_STATUS HEALTH_INSURANCE  CURRENT_AGE   
##  Min.   :57.00   Min.   :1      Min.   :0.0000   Min.   :57.00  
##  1st Qu.:69.00   1st Qu.:1      1st Qu.:1.0000   1st Qu.:69.00  
##  Median :75.00   Median :1      Median :1.0000   Median :75.00  
##  Mean   :74.53   Mean   :1      Mean   :0.9825   Mean   :74.53  
##  3rd Qu.:80.00   3rd Qu.:1      3rd Qu.:1.0000   3rd Qu.:80.00  
##  Max.   :85.00   Max.   :1      Max.   :1.0000   Max.   :85.00  
##     mortstat        permth_exm      ucod_leading    Age_or_Status     
##  Min.   :0.0000   Min.   : 0.000   Min.   : 0.000   Length:57         
##  1st Qu.:0.0000   1st Qu.: 5.000   1st Qu.: 0.000   Class :character  
##  Median :1.0000   Median : 9.000   Median : 2.000   Mode  :character  
##  Mean   :0.7018   Mean   : 8.667   Mean   : 2.684                     
##  3rd Qu.:1.0000   3rd Qu.:13.000   3rd Qu.: 2.000                     
##  Max.   :1.0000   Max.   :14.000   Max.   :10.000                     
##  Age_or_Status_Numeric  Age_At_Death  
##  Min.   :57.00         Min.   :65.00  
##  1st Qu.:73.00         1st Qu.:79.00  
##  Median :80.00         Median :85.00  
##  Mean   :79.18         Mean   :83.19  
##  3rd Qu.:87.00         3rd Qu.:89.00  
##  Max.   :93.00         Max.   :94.00
sapply(selected_data, function(x) sum(is.na(x)))  # Counts NAs for each variable
##             Education        Race_Ethnicity                Income 
##                     0                     0                     0 
##     AGE_TOLD_PROSTATE    DIAGNOSED_PROSTATE      PROSTATE_ENLARGE 
##                     0                     0                     0 
##          AGE_PSA_TEST             PSA_TOTAL           AGE_CURRENT 
##                     0                     0                     0 
##        CITIZEN_STATUS      HEALTH_INSURANCE           CURRENT_AGE 
##                     0                     0                     0 
##              mortstat            permth_exm          ucod_leading 
##                     0                     0                     0 
##         Age_or_Status Age_or_Status_Numeric          Age_At_Death 
##                     0                     0                     0
sapply(selected_data, class)  # Displays the data type of each variable
##             Education        Race_Ethnicity                Income 
##             "numeric"             "numeric"             "numeric" 
##     AGE_TOLD_PROSTATE    DIAGNOSED_PROSTATE      PROSTATE_ENLARGE 
##             "numeric"             "numeric"             "numeric" 
##          AGE_PSA_TEST             PSA_TOTAL           AGE_CURRENT 
##             "numeric"             "numeric"             "numeric" 
##        CITIZEN_STATUS      HEALTH_INSURANCE           CURRENT_AGE 
##             "numeric"             "numeric"             "numeric" 
##              mortstat            permth_exm          ucod_leading 
##             "numeric"             "numeric"             "numeric" 
##         Age_or_Status Age_or_Status_Numeric          Age_At_Death 
##           "character"             "numeric"             "numeric"
# Summary Statistics for Education
summary(selected_data$Education)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  0.0000  0.0000  1.0000  0.5439  1.0000  1.0000
# Histogram for Education
hist(selected_data$Education, main = "Histogram of Education", xlab = "Education")

# Structure of the dataset
str(selected_data$Education)
##  num [1:57] 1 1 1 1 0 1 0 1 0 0 ...
library(ggplot2)
# Summary Statistics for Education
summary(selected_data$Education)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  0.0000  0.0000  1.0000  0.5439  1.0000  1.0000
# Summary Statistics for Income
summary(selected_data$Income)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   2.000   6.000   7.000   8.368   8.000  77.000
# Histogram for Income
hist(selected_data$Income, main = "Histogram of Income", xlab = "Income")

# Bar Plot for Income (if it's a categorical variable)
ggplot(selected_data, aes(x = Income)) +
  geom_bar() +
  labs(
    title = "Distribution of Income ",
    x = "Income ",
    y = "Frequency"
  )

# Summary Statistics for Race_Ethnicity
summary(selected_data$Race_Ethnicity)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
## 0.00000 0.00000 0.00000 0.03509 0.00000 1.00000
# Histogram for Race_Ethnicity
hist(selected_data$Race_Ethnicity, main = "Histogram of Race_Ethnicity", xlab = "Race_Ethnicity")

# Bar Plot for Race_Ethnicity (if it's a categorical variable)
ggplot(selected_data, aes(x = Race_Ethnicity)) +
  geom_bar() +
  labs(
    title = "Distribution of Race_Ethnicity ",
    x = "Race_Ethnicity ",
    y = "Frequency"
  )

# Summary Statistics for HEALTH_INSURANCE
summary(selected_data$HEALTH_INSURANCE)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  0.0000  1.0000  1.0000  0.9825  1.0000  1.0000
# Histogram for HEALTH_INSURANCE
hist(selected_data$HEALTH_INSURANCE, main = "Histogram of HEALTH_INSURANCE", xlab = "HEALTH_INSURANCE")

# Bar Plot for HEALTH_INSURANCE (if it's a categorical variable)
ggplot(selected_data, aes(x = HEALTH_INSURANCE)) +
  geom_bar() +
  labs(
    title = "Distribution of HEALTH_INSURANCE ",
    x = "HEALTH_INSURANCE ",
    y = "Frequency"
  )

The histogram shows a distribution with multiple peaks, suggesting that the ages at death are concentrated around certain values. There appears to be a relatively uniform distribution with slight increases in frequency at certain age intervals, notably in the late 70s to early 90s. This could indicate common ages at which death occurs in the studied population, possibly due to common age-related factors.

The histogram does not show a smooth, bell-shaped curve, which suggests that the age at death is not normally distributed in this sample. This could be due to a variety of reasons such as the sample size, the presence of outliers, or the natural life course events that don’t follow a normal distribution pattern.

# Summary Statistics for Age_At_Death
summary(selected_data$Age_At_Death)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   65.00   79.00   85.00   83.19   89.00   94.00
# Histogram for Age_At_Death
hist(selected_data$Age_At_Death, main = "Histogram of Age_At_Death", xlab = "mortstat")

# Box Plot for Age_At_Death
#boxplot(selected_data$Age_At_Death, main = "Box Plot of Age_At_Death", ylab = "mortstat")

# Density Plot for Age_At_Death
#plot(density(selected_data$Age_At_Death), main = "Density Plot of Age_At_Death", xlab = "Age_At_Death")

# Q-Q Plot for Age_At_Death (if it's a continuous variable)
#qqnorm(selected_data$Age_At_Death)
#qqline(selected_data$Age_At_Death)

# Bar Plot for Age_At_Death (if it's a categorical variable)
ggplot(selected_data, aes(x = Age_At_Death)) +
  geom_bar() +
  labs(
    title = "Distribution of Age_At_Death ",
    x = "Age_At_Death ",
    y = "Frequency"
  )

#tabyl(selected_data$Age_At_Death) 

x <-selected_data %>% 
  
  filter(mortstat ==1 )
selected_data <- selected_data %>%
  mutate(ageevent = AGE_CURRENT + permth_exm) # Calculate ageevent

# Assuming you want to perform a similar analysis as with tabyl
library(gtsummary)

# Create a tbl_summary object for ageevent
tbl_ageevent <- selected_data %>%
  select(ageevent) %>%
  tbl_summary()

# Print the table for ageevent
tbl_ageevent
Characteristic N = 571
ageevent 85 (79, 89)
1 Median (IQR)

Interpretation: A Kaplan-Meier survival curve is created here to estimate the survival function from lifetime data. This is a non-parametric statistic used to estimate the survival probability from observed survival times.

From this curve, we can infer that survival decreases over time, as expected. The curve’s shape could be analyzed to understand specific periods where there may be a higher risk of death post-diagnosis. For instance, since there is a steep drop at a, this might indicate a period of higher mortality risk.

# Calculate Age_At_Death using AGE_CURRENT and permth_exm
selected_data <- selected_data %>%
  mutate(Age_At_Death = AGE_CURRENT + permth_exm)

# Load the survival library (if not already loaded)
library(survival)

surv_obj <- Surv(time = selected_data$Age_At_Death, event = selected_data$mortstat)

# Create a Kaplan-Meier survival curve
km_curve <- survfit(surv_obj ~ 1)  # Assuming you want to compare all individuals

# Plot the Kaplan-Meier survival curve
plot(km_curve, main = "Kaplan-Meier Survival Curve",
     xlab = "Time (Age at Death from Diagnosis)",
     ylab = "Survival Probability")

A Pearson’s Chi-squared test with Yates’ correction, is used to see if different categories are related to each other. The warning about the Chi-squared approximation indicates that your results might not reliable because of the small number of observations. In such cases with small sample sizes, the test will not work well, leading to potentially misleading conclusions. Therefore, while the test suggests that there’s no significant relationship, this conclusion should be taken with caution due to the small sample size involved.

# Create a contingency table between two categorical variables (e.g., Education and Race_Ethnicity)
contingency_table <- table(selected_data$Education, selected_data$Race_Ethnicity)

# Perform a chi-square test
chi_square_test <- chisq.test(contingency_table)
## Warning in chisq.test(contingency_table): Chi-squared approximation may be
## incorrect
# Print the chi-square test results
print(chi_square_test)
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  contingency_table
## X-squared = 0.7215, df = 1, p-value = 0.3957
# Calculate correlation matrix for numeric variables
numeric_vars <- select_if(selected_data, is.numeric)
correlation_matrix <- cor(numeric_vars)
## Warning in cor(numeric_vars): the standard deviation is zero
# Print the correlation matrix
print(correlation_matrix)
##                          Education Race_Ethnicity      Income AGE_TOLD_PROSTATE
## Education              1.000000000    -0.20822258  0.15328706      -0.020060846
## Race_Ethnicity        -0.208222581     1.00000000 -0.03729134       0.215817929
## Income                 0.153287062    -0.03729134  1.00000000       0.013072284
## AGE_TOLD_PROSTATE     -0.020060846     0.21581793  0.01307228       1.000000000
## DIAGNOSED_PROSTATE              NA             NA          NA                NA
## PROSTATE_ENLARGE      -0.065374206    -0.12516755  0.15236220      -0.009695577
## AGE_PSA_TEST          -0.220936233    -0.04515625  0.01152214       0.341176911
## PSA_TOTAL                       NA             NA          NA                NA
## AGE_CURRENT            0.008085813     0.23323799 -0.02103637       0.860773887
## CITIZEN_STATUS                  NA             NA          NA                NA
## HEALTH_INSURANCE      -0.122380385     0.02548236  0.03312565       0.163922073
## CURRENT_AGE            0.008085813     0.23323799 -0.02103637       0.860773887
## mortstat              -0.212069238     0.12431631 -0.22772475       0.177243951
## permth_exm             0.074475738    -0.02879970  0.14143248      -0.240399170
## ucod_leading          -0.235260851     0.30483144 -0.07916560       0.168455473
## Age_or_Status_Numeric -0.104946992     0.26485260 -0.09615718       0.707536517
## Age_At_Death           0.051965824     0.21211888  0.06293263       0.703560050
## ageevent               0.051965824     0.21211888  0.06293263       0.703560050
##                       DIAGNOSED_PROSTATE PROSTATE_ENLARGE AGE_PSA_TEST
## Education                             NA     -0.065374206 -0.220936233
## Race_Ethnicity                        NA     -0.125167548 -0.045156254
## Income                                NA      0.152362198  0.011522138
## AGE_TOLD_PROSTATE                     NA     -0.009695577  0.341176911
## DIAGNOSED_PROSTATE                     1               NA           NA
## PROSTATE_ENLARGE                      NA      1.000000000 -0.007563358
## AGE_PSA_TEST                          NA     -0.007563358  1.000000000
## PSA_TOTAL                             NA               NA           NA
## AGE_CURRENT                           NA      0.092766443  0.242860454
## CITIZEN_STATUS                        NA               NA           NA
## HEALTH_INSURANCE                      NA      0.030245870  0.041968060
## CURRENT_AGE                           NA      0.092766443  0.242860454
## mortstat                              NA     -0.017359437  0.205753871
## permth_exm                            NA     -0.003418333 -0.215773556
## ucod_leading                          NA     -0.119171969  0.242808804
## Age_or_Status_Numeric                 NA      0.063543730  0.236114351
## Age_At_Death                          NA      0.089116845  0.111053644
## ageevent                              NA      0.089116845  0.111053644
##                       PSA_TOTAL  AGE_CURRENT CITIZEN_STATUS HEALTH_INSURANCE
## Education                    NA  0.008085813             NA      -0.12238038
## Race_Ethnicity               NA  0.233237988             NA       0.02548236
## Income                       NA -0.021036372             NA       0.03312565
## AGE_TOLD_PROSTATE            NA  0.860773887             NA       0.16392207
## DIAGNOSED_PROSTATE           NA           NA             NA               NA
## PROSTATE_ENLARGE             NA  0.092766443             NA       0.03024587
## AGE_PSA_TEST                 NA  0.242860454             NA       0.04196806
## PSA_TOTAL                     1           NA             NA               NA
## AGE_CURRENT                  NA  1.000000000             NA       0.22815195
## CITIZEN_STATUS               NA           NA              1               NA
## HEALTH_INSURANCE             NA  0.228151954             NA       1.00000000
## CURRENT_AGE                  NA  1.000000000             NA       0.22815195
## mortstat                     NA  0.271521326             NA      -0.08711651
## permth_exm                   NA -0.270854190             NA      -0.01009091
## ucod_leading                 NA  0.240513957             NA       0.02749502
## Age_or_Status_Numeric        NA  0.873837786             NA       0.12311647
## Age_At_Death                 NA  0.822339532             NA       0.21818078
## ageevent                     NA  0.822339532             NA       0.21818078
##                        CURRENT_AGE    mortstat   permth_exm ucod_leading
## Education              0.008085813 -0.21206924  0.074475738  -0.23526085
## Race_Ethnicity         0.233237988  0.12431631 -0.028799699   0.30483144
## Income                -0.021036372 -0.22772475  0.141432479  -0.07916560
## AGE_TOLD_PROSTATE      0.860773887  0.17724395 -0.240399170   0.16845547
## DIAGNOSED_PROSTATE              NA          NA           NA           NA
## PROSTATE_ENLARGE       0.092766443 -0.01735944 -0.003418333  -0.11917197
## AGE_PSA_TEST           0.242860454  0.20575387 -0.215773556   0.24280880
## PSA_TOTAL                       NA          NA           NA           NA
## AGE_CURRENT            1.000000000  0.27152133 -0.270854190   0.24051396
## CITIZEN_STATUS                  NA          NA           NA           NA
## HEALTH_INSURANCE       0.228151954 -0.08711651 -0.010090909   0.02749502
## CURRENT_AGE            1.000000000  0.27152133 -0.270854190   0.24051396
## mortstat               0.271521326  1.00000000 -0.709473098   0.52622245
## permth_exm            -0.270854190 -0.70947310  1.000000000  -0.28445004
## ucod_leading           0.240513957  0.52622245 -0.284450037   1.00000000
## Age_or_Status_Numeric  0.873837786  0.56605391 -0.221107578   0.42281329
## Age_At_Death           0.822339532 -0.15261058  0.324994095   0.06815424
## ageevent               0.822339532 -0.15261058  0.324994095   0.06815424
##                       Age_or_Status_Numeric Age_At_Death    ageevent
## Education                       -0.10494699   0.05196582  0.05196582
## Race_Ethnicity                   0.26485260   0.21211888  0.21211888
## Income                          -0.09615718   0.06293263  0.06293263
## AGE_TOLD_PROSTATE                0.70753652   0.70356005  0.70356005
## DIAGNOSED_PROSTATE                       NA           NA          NA
## PROSTATE_ENLARGE                 0.06354373   0.08911685  0.08911685
## AGE_PSA_TEST                     0.23611435   0.11105364  0.11105364
## PSA_TOTAL                                NA           NA          NA
## AGE_CURRENT                      0.87383779   0.82233953  0.82233953
## CITIZEN_STATUS                           NA           NA          NA
## HEALTH_INSURANCE                 0.12311647   0.21818078  0.21818078
## CURRENT_AGE                      0.87383779   0.82233953  0.82233953
## mortstat                         0.56605391  -0.15261058 -0.15261058
## permth_exm                      -0.22110758   0.32499410  0.32499410
## ucod_leading                     0.42281329   0.06815424  0.06815424
## Age_or_Status_Numeric            1.00000000   0.72779764  0.72779764
## Age_At_Death                     0.72779764   1.00000000  1.00000000
## ageevent                         0.72779764   1.00000000  1.00000000
# Visualize the correlation matrix using a heatmap
library(corrplot)
## corrplot 0.92 loaded
corrplot(correlation_matrix, method = "color", type = "upper", tl.col = "black", tl.srt = 45)

The overall fit of the model can be assessed by the Concordance statistic (C-index), Likelihood ratio test, Wald test, and Score (logrank) test. A C-index of 0.554 suggests that the model is not particularly strong at predicting outcomes (a C-index of 0.5 suggests no predictive power, and 1.0 suggests perfect prediction).

All the p-values from the tests are not significant (all above 0.05), suggesting that the model does not have strong evidence for the effects of DIAGNOSED_PROSTATE, Education, and Income on the age at death. However, it is worth noting that with a sample size of 59 and 41 events, the study may be underpowered to detect all but the largest effects.

# Assuming selected_data contains your dataset
model_data <- selected_data %>%
  select(Age_At_Death, DIAGNOSED_PROSTATE, Education, Income, mortstat)

# Create a binary event variable
model_data$event <- ifelse(model_data$mortstat == 1, 1, 0)

# Fit a Cox Proportional Hazards regression model
cox_model <- coxph(Surv(Age_At_Death, event) ~ DIAGNOSED_PROSTATE + Education + Income, data = model_data)

summary(cox_model)
## Call:
## coxph(formula = Surv(Age_At_Death, event) ~ DIAGNOSED_PROSTATE + 
##     Education + Income, data = model_data)
## 
##   n= 57, number of events= 40 
## 
##                        coef exp(coef) se(coef)      z Pr(>|z|)
## DIAGNOSED_PROSTATE       NA        NA  0.00000     NA       NA
## Education          -0.20804   0.81218  0.31935 -0.651    0.515
## Income             -0.02265   0.97760  0.02748 -0.824    0.410
## 
##                    exp(coef) exp(-coef) lower .95 upper .95
## DIAGNOSED_PROSTATE        NA         NA        NA        NA
## Education             0.8122      1.231    0.4343     1.519
## Income                0.9776      1.023    0.9263     1.032
## 
## Concordance= 0.549  (se = 0.045 )
## Likelihood ratio test= 1.8  on 2 df,   p=0.4
## Wald test            = 1.24  on 2 df,   p=0.5
## Score (logrank) test = 1.39  on 2 df,   p=0.5
# Calculate time_to_event by adding AGE_TOLD_PROSTATE and Age_At_Death
selected_data <- selected_data %>%
  mutate(time_to_event = Age_At_Death)

# Fit a Cox Proportional Hazards regression model using Race_Ethnicity as a predictor
cox_model_race_ethnicity <- coxph(Surv(time_to_event, mortstat) ~ Race_Ethnicity, data = selected_data)

# View the summary of the model
summary(cox_model_race_ethnicity)
## Call:
## coxph(formula = Surv(time_to_event, mortstat) ~ Race_Ethnicity, 
##     data = selected_data)
## 
##   n= 57, number of events= 40 
## 
##                   coef exp(coef) se(coef)      z Pr(>|z|)
## Race_Ethnicity -0.5759    0.5622   0.7330 -0.786    0.432
## 
##                exp(coef) exp(-coef) lower .95 upper .95
## Race_Ethnicity    0.5622      1.779    0.1336     2.365
## 
## Concordance= 0.526  (se = 0.018 )
## Likelihood ratio test= 0.73  on 1 df,   p=0.4
## Wald test            = 0.62  on 1 df,   p=0.4
## Score (logrank) test = 0.63  on 1 df,   p=0.4

The Cox Proportional Hazards regression model summary attempts to understand the impact of race/ethnicity on survival time, using data from 57 individuals with 40 events (deaths). The analysis resulted in a coefficient of -0.5759 for race/ethnicity, suggesting a decrease in risk. The hazard ratio of 0.5622 implies that the risk of the event is about 56% for one group compared to another. Thee model does not find this relationship statistically significant, as evidenced by p-values around 0.4 in various tests, suggesting that race/ethnicity is not a significant predictor of survival time of prostate cancer diagnosis. The concordance of 0.526 indicates only a slightly better predictive ability than a random guess. These findings, especially given the small sample size, suggest limited reliability and generalizability, highlighting the need for caution in interpreting these results.

#model age to death, then censor people

# Load required libraries
library(survival)
library(survminer)

# Simulated survival data with demographic variables
set.seed(123)
n <- 100  # Number of observations
time_to_event <- rexp(n, rate = 0.02)  # Simulated survival times
status <- rbinom(n, size = 1, prob = 0.7)  # Simulated censoring (1=event, 0=censored)
HEALTH_INSURANCE <- factor(sample(c("1", "2"), n, replace = TRUE))
CURRENT_AGE <- rnorm(n, mean = 40, sd = 9)

# Create a survival object
surv_data <- Surv(selected_data$time_to_event,  selected_data$mortstat)

# Kaplan-Meier survival curves by HEALTH INSURANCE STATUS
#survfit_HEALTH_INSURANCE <- survfit(surv_data ~ HEALTH_INSURANCE)

# Plot Kaplan-Meier curves
#ggsurvplot(survfit_HEALTH_INSURANCE, data = surv_data, title = "Kaplan-Meier Survival Curves by HEALTH INSURANCE STATUS")

# Perform a log-rank test to compare survival curves
#log_rank_test <- survdiff(surv_data ~ HEALTH_INSURANCE)
#print(log_rank_test)

# Fit a Cox Proportional-Hazards model
#cox_model <- coxph(surv_data ~ HEALTH_INSURANCE + time_to_event)
#summary(cox_model)
surv_data  <- survfit(formula = Surv(time_to_event, mortstat) ~ HEALTH_INSURANCE, 
    data = selected_data) 
library(ggplot2)

# Calculate mean and standard deviation of CURRENT_AGE
mean_age <- mean(selected_data$CURRENT_AGE, na.rm = TRUE)
sd_age <- sd(selected_data$CURRENT_AGE, na.rm = TRUE)

# Creating a histogram of CURRENT_AGE and adding mean and SD
ggplot(selected_data, aes(x = CURRENT_AGE)) +
  geom_histogram(binwidth = 1, fill = "blue", color = "black") +
  geom_vline(aes(xintercept = mean_age), color = "red", linetype = "dashed", size = 1) +
  geom_vline(aes(xintercept = mean_age + sd_age), color = "green", linetype = "dashed", size = 1) +
  geom_vline(aes(xintercept = mean_age - sd_age), color = "green", linetype = "dashed", size = 1) +
  labs(title = "Histogram of Current Age", x = "Current Age", y = "Frequency") +
  theme_minimal() +
  annotate("text", x = mean_age, y = Inf, label = paste("Mean:", round(mean_age, 2)), vjust = 2, color = "red") +
  annotate("text", x = mean_age + sd_age, y = Inf, label = paste("Mean + SD:", round(mean_age + sd_age, 2)), vjust = 2, color = "green") +
  annotate("text", x = mean_age - sd_age, y = Inf, label = paste("Mean - SD:", round(mean_age - sd_age, 2)), vjust = 2, color = "green")
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

Interpretation: This section includes checking the proportional hazards assumption, which is crucial for the validity of Cox regression models. Visual and statistical methods are used for this purpose.

# Load the survminer library
library(survminer)

# Check the proportional hazards assumption visually
ggsurvplot(survfit(cox_model), data = model_data, pval = TRUE)
## Warning in .pvalue(fit, data = data, method = method, pval = pval, pval.coord = pval.coord, : There are no survival curves to be compared. 
##  This is a null model.

# Test the proportional hazards assumption using Schoenfeld residuals
#cox_zph <- cox.zph(cox_model)
#plot(cox_zph)

# Assuming 'cox_model' is your Cox regression model
# Check the proportional hazards assumption for a continuous variable (e.g., 'Age_At_Death')
cox_zph <- cox.zph(cox_model, transform = "identity")

# Print the results
print(cox_zph)
##            chisq df    p
## Education 0.0552  1 0.81
## Income    0.0184  1 0.89
## GLOBAL    0.0672  2 0.97
# Calculate mean and standard deviation
mean_age_status <- mean(selected_data$Age_or_Status_Numeric, na.rm = TRUE)
sd_age_status <- sd(selected_data$Age_or_Status_Numeric, na.rm = TRUE)

# Creating a histogram of Age_or_Status_Numeric and adding mean and SD
ggplot(selected_data, aes(x = Age_or_Status_Numeric)) +
  geom_histogram(binwidth = 1, fill = "blue", color = "black") +
  geom_vline(aes(xintercept = mean_age_status), color = "red", linetype = "dashed", size = 1) +
  geom_vline(aes(xintercept = mean_age_status + sd_age_status), color = "green", linetype = "dashed", size = 1) +
  geom_vline(aes(xintercept = mean_age_status - sd_age_status), color = "green", linetype = "dashed", size = 1) +
  labs(title = "Histogram of Age or Status", x = "Age or Status", y = "Frequency") +
  theme_minimal() +
  annotate("text", x = mean_age_status, y = Inf, label = paste("Mean:", round(mean_age_status, 2)), vjust = 2, color = "red") +
  annotate("text", x = mean_age_status + sd_age_status, y = Inf, label = paste("Mean + SD:", round(mean_age_status + sd_age_status, 2)), vjust = 2, color = "green") +
  annotate("text", x = mean_age_status - sd_age_status, y = Inf, label = paste("Mean - SD:", round(mean_age_status - sd_age_status, 2)), vjust = 2, color = "green")

model_data <- selected_data %>%
  select(Age_At_Death, DIAGNOSED_PROSTATE, Education, Income, HEALTH_INSURANCE, CITIZEN_STATUS, Race_Ethnicity, mortstat)

cox_model <- coxph(Surv(Age_At_Death, mortstat) ~ 
                    DIAGNOSED_PROSTATE + Education + Income + Race_Ethnicity + CITIZEN_STATUS + HEALTH_INSURANCE, 
                  data = model_data)
summary(cox_model)
## Call:
## coxph(formula = Surv(Age_At_Death, mortstat) ~ DIAGNOSED_PROSTATE + 
##     Education + Income + Race_Ethnicity + CITIZEN_STATUS + HEALTH_INSURANCE, 
##     data = model_data)
## 
##   n= 57, number of events= 40 
## 
##                        coef exp(coef) se(coef)      z Pr(>|z|)  
## DIAGNOSED_PROSTATE       NA        NA  0.00000     NA       NA  
## Education          -0.36541   0.69392  0.33268 -1.098   0.2720  
## Income             -0.02028   0.97992  0.02614 -0.776   0.4379  
## Race_Ethnicity     -0.78068   0.45809  0.75185 -1.038   0.2991  
## CITIZEN_STATUS           NA        NA  0.00000     NA       NA  
## HEALTH_INSURANCE   -2.49240   0.08271  1.11113 -2.243   0.0249 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                    exp(coef) exp(-coef) lower .95 upper .95
## DIAGNOSED_PROSTATE        NA         NA        NA        NA
## Education            0.69392      1.441  0.361515    1.3319
## Income               0.97992      1.020  0.930984    1.0314
## Race_Ethnicity       0.45809      2.183  0.104949    1.9995
## CITIZEN_STATUS            NA         NA        NA        NA
## HEALTH_INSURANCE     0.08271     12.090  0.009371    0.7301
## 
## Concordance= 0.607  (se = 0.046 )
## Likelihood ratio test= 6.03  on 4 df,   p=0.2
## Wald test            = 7.27  on 4 df,   p=0.1
## Score (logrank) test = 10.15  on 4 df,   p=0.04
# Create a dataset with Age_At_Death and censoring status (mortstat)
model_data <- selected_data %>%
  select(Age_At_Death, mortstat)

# Fit a Cox Proportional Hazards regression model
cox_model <- coxph(Surv(Age_At_Death, mortstat) ~ 1, data = model_data)

# View the summary of the Cox model
summary(cox_model)
## Call:  coxph(formula = Surv(Age_At_Death, mortstat) ~ 1, data = model_data)
## 
## Null model
##   log likelihood= -128.6335 
##   n= 57
# Create a dataset with Age_At_Death
model_data <- selected_data %>%
  select(Age_At_Death)

# Fit a Cox Proportional Hazards regression model without censoring
cox_model <- coxph(Surv(Age_At_Death) ~ 1, data = model_data)

# View the summary of the Cox model
summary(cox_model)
## Call:  coxph(formula = Surv(Age_At_Death) ~ 1, data = model_data)
## 
## Null model
##   log likelihood= -176.3958 
##   n= 57

ageevent age at event if they died and age of survey if they did not die

library(janitor)
## 
## Attaching package: 'janitor'
## The following objects are masked from 'package:stats':
## 
##     chisq.test, fisher.test
# Create ageevent variable
selected_data <- selected_data %>%
  mutate(ageevent = ifelse(mortstat == 1, Age_At_Death, CURRENT_AGE))

# Check the first few rows of the dataset to verify the ageevent variable
head(selected_data)
## # A tibble: 6 × 20
##   Education Race_Ethnicity Income AGE_TOLD_PROSTATE DIAGNOSED_PROSTATE
##       <dbl>          <dbl>  <dbl>             <dbl>              <dbl>
## 1         1              0      5                79                  1
## 2         1              0      7                65                  1
## 3         1              0      6                61                  1
## 4         1              0      7                64                  1
## 5         0              0     10                62                  1
## 6         1              0     11                54                  1
## # ℹ 15 more variables: PROSTATE_ENLARGE <dbl>, AGE_PSA_TEST <dbl>,
## #   PSA_TOTAL <dbl>, AGE_CURRENT <dbl>, CITIZEN_STATUS <dbl>,
## #   HEALTH_INSURANCE <dbl>, CURRENT_AGE <dbl>, mortstat <dbl>,
## #   permth_exm <dbl>, ucod_leading <dbl>, Age_or_Status <chr>,
## #   Age_or_Status_Numeric <dbl>, Age_At_Death <dbl>, ageevent <dbl>,
## #   time_to_event <dbl>
tabyl(selected_data$ageevent)
##  selected_data$ageevent n    percent
##                      57 1 0.01754386
##                      60 1 0.01754386
##                      65 1 0.01754386
##                      66 2 0.03508772
##                      68 2 0.03508772
##                      69 4 0.07017544
##                      70 1 0.01754386
##                      71 2 0.03508772
##                      73 3 0.05263158
##                      74 1 0.01754386
##                      76 3 0.05263158
##                      77 2 0.03508772
##                      78 4 0.07017544
##                      79 1 0.01754386
##                      80 3 0.05263158
##                      81 2 0.03508772
##                      82 1 0.01754386
##                      83 2 0.03508772
##                      84 2 0.03508772
##                      85 1 0.01754386
##                      87 4 0.07017544
##                      88 4 0.07017544
##                      89 4 0.07017544
##                      90 1 0.01754386
##                      91 2 0.03508772
##                      92 1 0.01754386
##                      93 2 0.03508772
# Create a survival object
surv_obj <- Surv(time = selected_data$ageevent, event = selected_data$mortstat)
# Fit a Cox Proportional Hazards regression model using ageevent
cox_model_ageevent <- coxph(Surv(ageevent, mortstat) ~ Education + Race_Ethnicity + Income + DIAGNOSED_PROSTATE + HEALTH_INSURANCE + CITIZEN_STATUS, data = selected_data)

# View the summary of the Cox model
summary(cox_model_ageevent)
## Call:
## coxph(formula = Surv(ageevent, mortstat) ~ Education + Race_Ethnicity + 
##     Income + DIAGNOSED_PROSTATE + HEALTH_INSURANCE + CITIZEN_STATUS, 
##     data = selected_data)
## 
##   n= 57, number of events= 40 
## 
##                         coef exp(coef)  se(coef)      z Pr(>|z|)  
## Education          -0.319806  0.726290  0.340317 -0.940   0.3474  
## Race_Ethnicity     -1.434644  0.238200  0.778808 -1.842   0.0655 .
## Income              0.002653  1.002657  0.030922  0.086   0.9316  
## DIAGNOSED_PROSTATE        NA        NA  0.000000     NA       NA  
## HEALTH_INSURANCE   -2.413374  0.089513  1.112921 -2.169   0.0301 *
## CITIZEN_STATUS            NA        NA  0.000000     NA       NA  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                    exp(coef) exp(-coef) lower .95 upper .95
## Education            0.72629     1.3769   0.37276    1.4151
## Race_Ethnicity       0.23820     4.1982   0.05176    1.0961
## Income               1.00266     0.9974   0.94369    1.0653
## DIAGNOSED_PROSTATE        NA         NA        NA        NA
## HEALTH_INSURANCE     0.08951    11.1716   0.01011    0.7929
## CITIZEN_STATUS            NA         NA        NA        NA
## 
## Concordance= 0.597  (se = 0.067 )
## Likelihood ratio test= 7.31  on 4 df,   p=0.1
## Wald test            = 7.86  on 4 df,   p=0.1
## Score (logrank) test = 10.5  on 4 df,   p=0.03
# Assuming your dataset is named 'data'
# and mortstat = 1 means the individual is alive, 
# while mortstat = 0 means the individual is deceased.

# Creating the time_to_event variable
selected_data$time_to_event <- selected_data$permth_exm

# Creating the status variable based on mortstat
# If mortstat = 0 (alive), then status = 0 (censored)
# If mortstat = 0 (deceased), then status = 1 (event occurred)
selected_data$status <- ifelse(selected_data$mortstat == 0, 1, 1)


# Creating the survival object
surv_object1 <- Surv(time = selected_data$time_to_event, event = selected_data$status)

surv_object <- Surv(time = selected_data$time_to_event, event = selected_data$status)

km_fit <- survfit(surv_object ~ 1, data = selected_data)

ggsurvplot(km_fit, 
           data = selected_data, 
           xlab = "Years", 
           ylab = "Survival probability",
           title = "Kaplan-Meier Survival Curve")

cox_model <- coxph(surv_object ~ AGE_TOLD_PROSTATE, data = selected_data)
summary(cox_model)
## Call:
## coxph(formula = surv_object ~ AGE_TOLD_PROSTATE, data = selected_data)
## 
##   n= 57, number of events= 57 
## 
##                      coef exp(coef) se(coef)     z Pr(>|z|)
## AGE_TOLD_PROSTATE 0.01927   1.01945  0.02089 0.922    0.356
## 
##                   exp(coef) exp(-coef) lower .95 upper .95
## AGE_TOLD_PROSTATE     1.019     0.9809    0.9785     1.062
## 
## Concordance= 0.579  (se = 0.047 )
## Likelihood ratio test= 0.85  on 1 df,   p=0.4
## Wald test            = 0.85  on 1 df,   p=0.4
## Score (logrank) test = 0.85  on 1 df,   p=0.4
cox.zph(cox_model)
##                   chisq df     p
## AGE_TOLD_PROSTATE  3.79  1 0.052
## GLOBAL             3.79  1 0.052
# Assuming 'surv_object' is your survival object
KM_fit <- survfit(surv_object ~ 1, data = selected_data)

plot(KM_fit)

# Convert HEALTH_INSURANCE to a factor if it is not already
selected_data$HEALTH_INSURANCE <- as.factor(selected_data$HEALTH_INSURANCE)

# Create the survival object
surv_object <- Surv(time = selected_data$ageevent, event = selected_data$status)

# Fit the Cox proportional hazards model
cox_model <- coxph(surv_object ~ HEALTH_INSURANCE, data = selected_data)

# View the summary of the Cox model
summary(cox_model)
## Call:
## coxph(formula = surv_object ~ HEALTH_INSURANCE, data = selected_data)
## 
##   n= 57, number of events= 57 
## 
##                      coef exp(coef) se(coef)      z Pr(>|z|)
## HEALTH_INSURANCE1 -1.3739    0.2531   1.0384 -1.323    0.186
## 
##                   exp(coef) exp(-coef) lower .95 upper .95
## HEALTH_INSURANCE1    0.2531      3.951   0.03307     1.937
## 
## Concordance= 0.51  (se = 0.01 )
## Likelihood ratio test= 1.21  on 1 df,   p=0.3
## Wald test            = 1.75  on 1 df,   p=0.2
## Score (logrank) test = 2.04  on 1 df,   p=0.2
# Pull specific columns
# pull columns named 'Age_At_Death' and 'Age_Told_Prostate'
extracted_columns <- selected_data %>% select(Age_or_Status, AGE_TOLD_PROSTATE)

# Pull specific rows
# For example, to pull rows 1 to 5
extracted_rows <- selected_data %>% slice(1:5)

# If you want to pull rows based on a condition
# pull rows where Age_At_Death is greater than 70
extracted_condition_rows <- selected_data %>% filter(Age_At_Death > 70)

# Display the extracted data
print(extracted_columns)
## # A tibble: 57 × 2
##    Age_or_Status AGE_TOLD_PROSTATE
##    <chr>                     <dbl>
##  1 93                           79
##  2 78                           65
##  3 71                           61
##  4 77                           64
##  5 68                           62
##  6 57+                          54
##  7 74                           64
##  8 89                           80
##  9 88                           70
## 10 69                           59
## # ℹ 47 more rows
print(extracted_rows)
## # A tibble: 5 × 21
##   Education Race_Ethnicity Income AGE_TOLD_PROSTATE DIAGNOSED_PROSTATE
##       <dbl>          <dbl>  <dbl>             <dbl>              <dbl>
## 1         1              0      5                79                  1
## 2         1              0      7                65                  1
## 3         1              0      6                61                  1
## 4         1              0      7                64                  1
## 5         0              0     10                62                  1
## # ℹ 16 more variables: PROSTATE_ENLARGE <dbl>, AGE_PSA_TEST <dbl>,
## #   PSA_TOTAL <dbl>, AGE_CURRENT <dbl>, CITIZEN_STATUS <dbl>,
## #   HEALTH_INSURANCE <fct>, CURRENT_AGE <dbl>, mortstat <dbl>,
## #   permth_exm <dbl>, ucod_leading <dbl>, Age_or_Status <chr>,
## #   Age_or_Status_Numeric <dbl>, Age_At_Death <dbl>, ageevent <dbl>,
## #   time_to_event <dbl>, status <dbl>
print(extracted_condition_rows)
## # A tibble: 51 × 21
##    Education Race_Ethnicity Income AGE_TOLD_PROSTATE DIAGNOSED_PROSTATE
##        <dbl>          <dbl>  <dbl>             <dbl>              <dbl>
##  1         1              0      5                79                  1
##  2         1              0      7                65                  1
##  3         1              0      6                61                  1
##  4         1              0      7                64                  1
##  5         0              0      7                64                  1
##  6         1              0     11                80                  1
##  7         0              0      5                70                  1
##  8         1              0      6                78                  1
##  9         1              0      4                73                  1
## 10         1              0     11                75                  1
## # ℹ 41 more rows
## # ℹ 16 more variables: PROSTATE_ENLARGE <dbl>, AGE_PSA_TEST <dbl>,
## #   PSA_TOTAL <dbl>, AGE_CURRENT <dbl>, CITIZEN_STATUS <dbl>,
## #   HEALTH_INSURANCE <fct>, CURRENT_AGE <dbl>, mortstat <dbl>,
## #   permth_exm <dbl>, ucod_leading <dbl>, Age_or_Status <chr>,
## #   Age_or_Status_Numeric <dbl>, Age_At_Death <dbl>, ageevent <dbl>,
## #   time_to_event <dbl>, status <dbl>
# Load necessary libraries
library(survival)
library(survminer)

extracted_condition_rows$time_to_event <- as.numeric(extracted_condition_rows$time_to_event)


# Assuming 'selected_data' is your dataset
# Ensure 'ageevent' is numeric and 'status' is a binary indicator (0 or 1)

# Create the survival object
surv_object <- Surv(time = extracted_condition_rows$time_to_event, event = extracted_condition_rows$status)


# Fit the Kaplan-Meier model
km_fit <- survfit(surv_object ~ 1)

# Plot the Kaplan-Meier survival curve
ggsurvplot(km_fit, 
           data = extracted_condition_rows, 
           xlab = "Years since Diagnosis", 
           ylab = "Survival Probability",
           title = "Kaplan-Meier Survival Curve",
           xlim = c(0, max(extracted_condition_rows$time_to_event))) # Adjust the x-axis limit as needed

# start from 0 (year of diagnosis) ensure that 'ageevent' is calculated as the time since diagnosis

The purpose of the analysis is to visualize how the probability to survival changes over time for those diagnosed with prostate cancer.

# Ensure that the 'Education' variable is a factor
selected_data$Education <- as.factor(selected_data$Education)

# Create the survival object
surv_obj <- Surv(time = selected_data$time_to_event, event = selected_data$status)

# Fit the Kaplan-Meier model for the Education variable
km_fit_education <- survfit(surv_obj ~ Education, data = selected_data)

# Plot the Kaplan-Meier survival curve
ggsurvplot(km_fit_education, 
           data = selected_data, 
           pval = TRUE, # Show p-value of log-rank test
           conf.int = TRUE, # Show confidence intervals
           palette = "Dark2", # Color palette
           xlab = "Time", 
           ylab = "Survival probability",
           title = "Kaplan-Meier Survival Curve by Education Level")

# time_to_event and status variables are correctly defined in your dataset
# Fit the Cox Proportional Hazards regression model
cox_model <- coxph(Surv(time_to_event, status) ~ HEALTH_INSURANCE + Education + Income + Race_Ethnicity, data = selected_data)

# Create a dataframe of the model's summary
cox_summary <- broom::tidy(cox_model)

# Create a plot of hazard ratios
ggplot(cox_summary, aes(x = term, y = estimate)) +
  geom_point() +
  geom_errorbar(aes(ymin = estimate - std.error, ymax = estimate + std.error), width = 0.1) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  theme_minimal() +
  labs(title = "Hazard Ratios from Cox Proportional Hazards Model",
       x = "Covariates",
       y = "Hazard Ratio Estimate")

# Note: This plot will show the point estimates of hazard ratios and their corresponding confidence intervals.
# Fit the Kaplan-Meier model
km_fit <- survfit(Surv(time_to_event, status) ~ Education, data = extracted_condition_rows)

# Plot the Kaplan-Meier survival curve
ggsurvplot(km_fit, 
           data = extracted_condition_rows, 
           xlab = "Years since Diagnosis", 
           ylab = "Survival Probability",
           title = "Kaplan-Meier Survival Curve",
           xlim = c(0, max(extracted_condition_rows$time_to_event))) # Adjust the x-axis limit as needed

# start from 0 (year of diagnosis) ensure that 'ageevent' is calculated as the time since diagnosis
# Fit the Kaplan-Meier model
km_fit <- survfit(Surv(time_to_event, status) ~ HEALTH_INSURANCE, data = extracted_condition_rows)

# Plot the Kaplan-Meier survival curve
ggsurvplot(km_fit, 
           data = extracted_condition_rows, 
           xlab = "Years since Diagnosis", 
           ylab = "Survival Probability",
           title = "Kaplan-Meier Survival Curve",
           xlim = c(0, max(extracted_condition_rows$time_to_event))) # Adjust the x-axis limit as needed

# start from 0 (year of diagnosis) ensure that 'ageevent' is calculated as the time since diagnosis
# Load the libraries
library(survival)
library(survminer)

# Assuming your data frame is called 'selected_data'
# and you have 'time_to_event' as the time variable, 'status' as the event indicator
# and 'HEALTH_INSURANCE' as the health insurance status variable

# Create the survival object
surv_obj <- Surv(time = selected_data$time_to_event, event = selected_data$status)

# Fit the Kaplan-Meier model stratified by health insurance status
km_fit <- survfit(surv_obj ~ HEALTH_INSURANCE, data = selected_data)

# Plot the Kaplan-Meier curves
ggsurvplot(km_fit, 
           data = selected_data, 
           pval = TRUE, 
           conf.int = TRUE,
           palette = "Dark2",
           xlab = "Time", 
           ylab = "Survival probability",
           title = "Kaplan-Meier Survival Curves by Health Insurance Status")

# Fit the Kaplan-Meier model
km_fit <- survfit(Surv(time_to_event, status) ~ Race_Ethnicity, data = extracted_condition_rows)

# Plot the Kaplan-Meier survival curve
ggsurvplot(km_fit, 
           data = extracted_condition_rows, 
           xlab = "Years since Diagnosis", 
           ylab = "Survival Probability",
           title = "Kaplan-Meier Survival Curve",
           xlim = c(0, max(extracted_condition_rows$time_to_event))) # Adjust the x-axis limit as needed

# start from 0 (year of diagnosis) ensure that 'ageevent' is calculated as the time since diagnosis
library(table1)
## 
## Attaching package: 'table1'
## The following objects are masked from 'package:base':
## 
##     units, units<-
extracted_condition_rows$Race_Ethnicity <- factor(extracted_condition_rows$Race_Ethnicity, 
                                       levels = c(0, 1), 
                                       labels = c("Nonwhite", "White"))

extracted_condition_rows$mortstat <- factor(extracted_condition_rows$mortstat, 
                                 levels = c(0, 1), 
                                 labels = c("Alive", "Deceased"))

extracted_condition_rows$HEALTH_INSURANCE <- factor(extracted_condition_rows$HEALTH_INSURANCE, 
                                         levels = c(0, 1), 
                                         labels = c("Uninsured", "Insured"))

extracted_condition_rows$Education <- as.factor(extracted_condition_rows$Education)
# Fit the Cox Proportional Hazards regression model
cox_model <- coxph(Surv(time_to_event, status) ~ HEALTH_INSURANCE + Education + Income + Race_Ethnicity, data = selected_data)
summary(cox_model)
## Call:
## coxph(formula = Surv(time_to_event, status) ~ HEALTH_INSURANCE + 
##     Education + Income + Race_Ethnicity, data = selected_data)
## 
##   n= 57, number of events= 57 
## 
##                        coef exp(coef)  se(coef)      z Pr(>|z|)
## HEALTH_INSURANCE1 -0.600402  0.548591  1.031498 -0.582    0.561
## Education1        -0.269114  0.764056  0.283167 -0.950    0.342
## Income            -0.001964  0.998038  0.013401 -0.147    0.883
## Race_Ethnicity     0.553411  1.739176  0.751883  0.736    0.462
## 
##                   exp(coef) exp(-coef) lower .95 upper .95
## HEALTH_INSURANCE1    0.5486      1.823   0.07265     4.142
## Education1           0.7641      1.309   0.43862     1.331
## Income               0.9980      1.002   0.97216     1.025
## Race_Ethnicity       1.7392      0.575   0.39842     7.592
## 
## Concordance= 0.548  (se = 0.043 )
## Likelihood ratio test= 1.92  on 4 df,   p=0.8
## Wald test            = 2.06  on 4 df,   p=0.7
## Score (logrank) test = 2.11  on 4 df,   p=0.7

This analysis attempts to see if factors like health insurance, education, income, and race/ethnicity play a significant role in influencing the time to a certain event in a group of 57 people. The results suggest no strong evidence that any of these factors are significant predictors in this specific dataset.

# Check the proportional hazards assumption
cox_zph <- cox.zph(cox_model)
plot(cox_zph)

table1::table1(~mortstat + time_to_event + AGE_TOLD_PROSTATE + Income + Race_Ethnicity + Education|HEALTH_INSURANCE, data= extracted_condition_rows)
Uninsured
(N=1)
Insured
(N=50)
Overall
(N=51)
mortstat
Alive 0 (0%) 16 (32.0%) 16 (31.4%)
Deceased 1 (100%) 34 (68.0%) 35 (68.6%)
time_to_event
Mean (SD) 9.00 (NA) 9.16 (4.25) 9.16 (4.21)
Median [Min, Max] 9.00 [9.00, 9.00] 10.0 [0, 14.0] 10.0 [0, 14.0]
AGE_TOLD_PROSTATE
Mean (SD) 61.0 (NA) 71.1 (6.74) 70.9 (6.82)
Median [Min, Max] 61.0 [61.0, 61.0] 71.0 [58.0, 85.0] 71.0 [58.0, 85.0]
Income
Mean (SD) 6.00 (NA) 8.38 (10.3) 8.33 (10.2)
Median [Min, Max] 6.00 [6.00, 6.00] 7.00 [2.00, 77.0] 7.00 [2.00, 77.0]
Race_Ethnicity
Nonwhite 1 (100%) 48 (96.0%) 49 (96.1%)
White 0 (0%) 2 (4.0%) 2 (3.9%)
Education
0 0 (0%) 22 (44.0%) 22 (43.1%)
1 1 (100%) 28 (56.0%) 29 (56.9%)
# Create a crosstab of the mortstat variable
mortstat_table <- table(selected_data$mortstat)
# 'AGE_TOLD_PROSTATE' is the age at diagnosis (ensure it's numeric)
# 'Age_or_Status' is the age at last interview or death (ensure it's numeric)

# Convert AGE_TOLD_PROSTATE and Age_or_Status to numeric 
selected_data$AGE_TOLD_PROSTATE <- as.numeric(as.character(selected_data$AGE_TOLD_PROSTATE))
selected_data$Age_or_Status <- as.numeric(as.character(selected_data$Age_or_Status))
## Warning: NAs introduced by coercion
# Calculate the time at risk for each individual
selected_data$time_at_risk <- selected_data$Age_or_Status - selected_data$AGE_TOLD_PROSTATE

# Handle any NAs that might have been introduced during conversion
selected_data$time_at_risk[is.na(selected_data$time_at_risk)] <- 0

# Calculate total person-years
total_person_years <- sum(selected_data$time_at_risk)

# Output the total person-years
total_person_years
## [1] 471
# Define age groups
age_breaks <- c(40, 49, 59, 69, 79, Inf)  # Define your age groups here
age_labels <- c("40-49", "50-59", "60-69", "70-79", "80+")  # Labels for age groups

# Categorize each individual into an age group
selected_data$age_group <- cut(selected_data$AGE_TOLD_PROSTATE, 
                               breaks = age_breaks, 
                               labels = age_labels, 
                               right = FALSE)

# Calculate time at risk for each individual
selected_data$time_at_risk <- selected_data$Age_or_Status - selected_data$AGE_TOLD_PROSTATE
selected_data$time_at_risk[is.na(selected_data$time_at_risk)] <- 0

# Calculate total person-years by age group
total_person_years_by_age_group <- aggregate(time_at_risk ~ age_group, data = selected_data, sum)

# View the result
print(total_person_years_by_age_group)
##   age_group time_at_risk
## 1     50-59            0
## 2     60-69          213
## 3     70-79          217
## 4       80+           41

The total person-years at risk in this dataset is 471. This means when you add up all the time periods that each individual has been at risk since their diagnosis or awareness of prostate issues, it totals to 471 years.

For age group 50-59 the time as risk is 0. This could suggest that individuals in this age group were either diagnosed and reached the end of the study period very quickly, or this age group is not represented in the data set. For the age group 60 to 69 the time at risk is 213, meaning that when summing up the time each person in this age group spent at risk from their diagnosis until the end of the study period, it totals 213 years. For 70 to 79 age had the highest accumulated risk time in your data set, suggesting either a larger number of individuals in this age group, longer survival times post-diagnosis. Finally, the 80+ age group is 41, the lower number could be due to fewer individuals in this age group, shorter survival times post-diagnosis, or a combination of both.

# Display the crosstab
print(mortstat_table)
## 
##  0  1 
## 17 40
summary(selected_data$Education)
##  0  1 
## 26 31
summary(selected_data$HEALTH_INSURANCE)
##  0  1 
##  1 56
summary(selected_data$Income)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   2.000   6.000   7.000   8.368   8.000  77.000
summary(selected_data$Race_Ethnicity)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
## 0.00000 0.00000 0.00000 0.03509 0.00000 1.00000
ggplot(selected_data, aes(x = Education)) + 
  geom_bar() + 
  labs(title = "Distribution of Education Levels", x = "Education", y = "Count")

ggplot(selected_data, aes(x = HEALTH_INSURANCE)) + 
  geom_bar() + 
  labs(title = "Distribution of Health Insurance Status", x = "Health Insurance", y = "Count")

ggplot(selected_data, aes(x = Race_Ethnicity)) + 
  geom_bar() + 
  labs(title = "Distribution of Race/Ethnicity", x = "Race/Ethnicity", y = "Count")

ggplot(selected_data, aes(x = Income)) + 
  geom_histogram(binwidth = 1, fill = "blue", color = "black") + 
  labs(title = "Histogram of Income", x = "Income", y = "Frequency")

table(selected_data$Education)
## 
##  0  1 
## 26 31
table(selected_data$HEALTH_INSURANCE)
## 
##  0  1 
##  1 56
table(selected_data$Race_Ethnicity)
## 
##  0  1 
## 55  2
selected_data %>%
  summarise(
    Mean_Income = mean(Income, na.rm = TRUE),
    Median_Income = median(Income, na.rm = TRUE),
    SD_Income = sd(Income, na.rm = TRUE)
  )
## # A tibble: 1 × 3
##   Mean_Income Median_Income SD_Income
##         <dbl>         <dbl>     <dbl>
## 1        8.37             7      9.64
library(ggplot2)

# Histogram or Bar plot for Health Insurance
ggplot(extracted_condition_rows, aes(x = HEALTH_INSURANCE)) +
  geom_bar() +
  labs(title = "Distribution of Health Insurance", x = "Health Insurance", y = "Count")

# Histogram or Bar plot for Education
ggplot(extracted_condition_rows, aes(x = Education)) +
  geom_bar() +
  labs(title = "Distribution of Education", x = "Education", y = "Count")

# Histogram or Bar plot for Income
ggplot(extracted_condition_rows, aes(x = Income)) +
  geom_bar() +
  labs(title = "Distribution of Income", x = "Income", y = "Count")

# Histogram or Bar plot for Race/Ethnicity
ggplot(extracted_condition_rows, aes(x = Race_Ethnicity)) +
  geom_bar() +
  labs(title = "Distribution of Race/Ethnicity", x = "Race/Ethnicity", y = "Count")

# Cox model for Health Insurance
cox_model_health_insurance <- coxph(Surv(time_to_event, status) ~ HEALTH_INSURANCE, data = extracted_condition_rows)
summary(cox_model_health_insurance)
## Call:
## coxph(formula = Surv(time_to_event, status) ~ HEALTH_INSURANCE, 
##     data = extracted_condition_rows)
## 
##   n= 51, number of events= 51 
## 
##                            coef exp(coef) se(coef)     z Pr(>|z|)
## HEALTH_INSURANCEInsured -0.5725    0.5641   1.0224 -0.56    0.576
## 
##                         exp(coef) exp(-coef) lower .95 upper .95
## HEALTH_INSURANCEInsured    0.5641      1.773   0.07604     4.185
## 
## Concordance= 0.503  (se = 0.004 )
## Likelihood ratio test= 0.26  on 1 df,   p=0.6
## Wald test            = 0.31  on 1 df,   p=0.6
## Score (logrank) test = 0.32  on 1 df,   p=0.6
# Cox model for Education
cox_model_education <- coxph(Surv(time_to_event, status) ~ Education, data = extracted_condition_rows)
summary(cox_model_education)
## Call:
## coxph(formula = Surv(time_to_event, status) ~ Education, data = extracted_condition_rows)
## 
##   n= 51, number of events= 51 
## 
##               coef exp(coef) se(coef)      z Pr(>|z|)
## Education1 -0.1877    0.8289   0.2865 -0.655    0.512
## 
##            exp(coef) exp(-coef) lower .95 upper .95
## Education1    0.8289      1.206    0.4727     1.453
## 
## Concordance= 0.507  (se = 0.044 )
## Likelihood ratio test= 0.43  on 1 df,   p=0.5
## Wald test            = 0.43  on 1 df,   p=0.5
## Score (logrank) test = 0.43  on 1 df,   p=0.5
# Cox model for Income
cox_model_income <- coxph(Surv(time_to_event, status) ~ Income, data = extracted_condition_rows)
summary(cox_model_income)
## Call:
## coxph(formula = Surv(time_to_event, status) ~ Income, data = extracted_condition_rows)
## 
##   n= 51, number of events= 51 
## 
##             coef exp(coef)  se(coef)      z Pr(>|z|)
## Income -0.004654  0.995357  0.013769 -0.338    0.735
## 
##        exp(coef) exp(-coef) lower .95 upper .95
## Income    0.9954      1.005    0.9689     1.023
## 
## Concordance= 0.544  (se = 0.049 )
## Likelihood ratio test= 0.13  on 1 df,   p=0.7
## Wald test            = 0.11  on 1 df,   p=0.7
## Score (logrank) test = 0.12  on 1 df,   p=0.7
# Cox model for Race/Ethnicity
cox_model_race_ethnicity <- coxph(Surv(time_to_event, status) ~ Race_Ethnicity, data = extracted_condition_rows)
summary(cox_model_race_ethnicity)
## Call:
## coxph(formula = Surv(time_to_event, status) ~ Race_Ethnicity, 
##     data = extracted_condition_rows)
## 
##   n= 51, number of events= 51 
## 
##                       coef exp(coef) se(coef)     z Pr(>|z|)
## Race_EthnicityWhite 0.8706    2.3885   0.7470 1.166    0.244
## 
##                     exp(coef) exp(-coef) lower .95 upper .95
## Race_EthnicityWhite     2.388     0.4187    0.5525     10.33
## 
## Concordance= 0.513  (se = 0.01 )
## Likelihood ratio test= 1.08  on 1 df,   p=0.3
## Wald test            = 1.36  on 1 df,   p=0.2
## Score (logrank) test = 1.45  on 1 df,   p=0.2

The Cox model attempted to assess whether having health insurance affects the survival time of prostate cancer patients. The analysis found no significant evidence that health insurance status significantly alters the risk of death or censoring in this group of 51 individuals. The model’s predictive power is also very limited, as indicated by the concordance statistic of .503.

The Cox model looking at education resulted in an education coefficient of -0.1877, indicating a small decrease in risk with higher education levels and a p-value of 0.512, suggesting the effect of education is not statistically significant.

The Cox model attempted to assess whether income affects survival time of prostate cancer patients. The analysis found that income is not statistically significant. The coefficient of -0.004654 indicates there is a very small negative effect, suggesting a small decrease in risk with higher income.

The final Cox model assess whether Race/Ethnicity affects the survival time of patients diagnosed with prostate cancer. The coefficient of 0.8706 for ‘Race_EthnicityWhite’, suggests an increase in risk for the White race/ethnicity group compared to other group. The hazard ratio of 2.3885, indicates that the risk for the White group is more than double compared to others. The results, however, are not statistically significant due to the p-value of 0.244.

cox_model <- coxph(Surv(time_to_event, status) ~ HEALTH_INSURANCE + Education + Income +Race_Ethnicity, data = extracted_condition_rows)

summary(cox_model)
## Call:
## coxph(formula = Surv(time_to_event, status) ~ HEALTH_INSURANCE + 
##     Education + Income + Race_Ethnicity, data = extracted_condition_rows)
## 
##   n= 51, number of events= 51 
## 
##                              coef exp(coef)  se(coef)      z Pr(>|z|)
## HEALTH_INSURANCEInsured -0.686424  0.503373  1.036151 -0.662    0.508
## Education1              -0.148883  0.861670  0.302342 -0.492    0.622
## Income                  -0.002739  0.997265  0.013819 -0.198    0.843
## Race_EthnicityWhite      0.805335  2.237446  0.764589  1.053    0.292
## 
##                         exp(coef) exp(-coef) lower .95 upper .95
## HEALTH_INSURANCEInsured    0.5034     1.9866   0.06606     3.836
## Education1                 0.8617     1.1605   0.47642     1.558
## Income                     0.9973     1.0027   0.97062     1.025
## Race_EthnicityWhite        2.2374     0.4469   0.49996    10.013
## 
## Concordance= 0.539  (se = 0.047 )
## Likelihood ratio test= 1.73  on 4 df,   p=0.8
## Wald test            = 2.03  on 4 df,   p=0.7
## Score (logrank) test = 2.14  on 4 df,   p=0.7
# Load necessary libraries
library(survival)
library(ggplot2)



# and 'mortstat' is the event indicator (1 if event occurred, 0 if censored)

# Fit separate Cox Proportional Hazards models for each variable
cox_model_education <- coxph(Surv(Age_or_Status_Numeric, mortstat) ~ Education, data = selected_data)
cox_model_income <- coxph(Surv(Age_or_Status_Numeric, mortstat) ~ Income, data = selected_data)
cox_model_race_ethnicity <- coxph(Surv(Age_or_Status_Numeric, mortstat) ~ Race_Ethnicity, data = selected_data)
cox_model_health_insurance <- coxph(Surv(Age_or_Status_Numeric, mortstat) ~ HEALTH_INSURANCE, data = selected_data)

# Create function to plot cumulative hazard
plot_cum_haz <- function(cox_model, title) {
  base_surv <- basehaz(cox_model, centered = FALSE)
  base_surv_df <- as.data.frame(base_surv)
  ggplot(base_surv_df, aes(x = time, y = hazard)) +
    geom_line() +
    labs(title = title,
         x = "Time",
         y = "Cumulative Hazard")
}

# Plotting cumulative hazards for each variable
plot_cum_haz(cox_model_education, "Cumulative Hazard by Education")

plot_cum_haz(cox_model_income, "Cumulative Hazard by Income")

plot_cum_haz(cox_model_race_ethnicity, "Cumulative Hazard by Race/Ethnicity")

plot_cum_haz(cox_model_health_insurance, "Cumulative Hazard by Health Insurance")