#### Cleaning data for new and clean data set along with calculating age and erasing patient info after
el <- EL_DATA %>%
  rename(
    DOS = DOS,
    Name = NAME,
    DOB = DOB,
    Procedure = PROCEDURE,
    Eye = EYE,
    Target = `TARGET (DISTANCE or NEAR)`,
    Pre_Sphere = `SPHERE (pre-op)`,
    Pre_Cyl = `CYL (pre-op)`,
    Pre_Axis = `AXIS (pre-op)`,
    Lasik_Type = `TYPE (Contoura vs WFO if LASIK)`,
    POD1_VA = `POD1 VA`,
    POW1_VA = `POW1 VA`,
    POM1_VA = `POM1 or 2 VA`,
    Post_Sphere = `SPHERE (post-op)`,
    Post_Cyl = `CYL (post-op)`,
    Post_Axis = `AXIS (post-op)`,
    Enhancement = `ENH? (YES or NO)`,
    Enh_Sphere = `ENH SPH`,
    Enh_Cyl = `ENH CYL`,
    Enh_Axis = `ENH AXIS`,
    ICL_Exchange_Reason = `Reason for Exchange (ICL)`
  )

el <- el %>%
  mutate(
    DOS = mdy(DOS),
    DOB = mdy(DOB),
    Age = as.numeric(DOS - DOB) / 365.25
  ) %>%
  select(-Name, -DOB, -DOS)
#### further cleaning more focused on prescription section will also be showing where NA values or miss inputs may be hidden 
el <- el %>%
  mutate(
    Pre_Cyl = ifelse(Pre_Cyl %in% c("Sph", "SPH", "sph", "Plano", "plano"), 0, Pre_Cyl),
    Post_Cyl = ifelse(Post_Cyl %in% c("Sph", "SPH", "sph", "Plano", "plano"), 0, Post_Cyl),
    
    Pre_Sphere = as.numeric(Pre_Sphere),
    Pre_Cyl = as.numeric(Pre_Cyl),
    Pre_Axis = as.numeric(Pre_Axis),
    
    Post_Sphere = as.numeric(Post_Sphere),
    Post_Cyl = as.numeric(Post_Cyl),
    Post_Axis = as.numeric(Post_Axis)
  )

EL_DATA %>%
  filter(is.na(as.numeric(`SPHERE (pre-op)`))) %>%
  select(`SPHERE (pre-op)`) %>%
  distinct()
## # A tibble: 3 × 1
##   `SPHERE (pre-op)`
##   <chr>            
## 1 <NA>             
## 2 -3/75            
## 3 --9.00
### stats for calculations residual
el <- el %>%
  mutate(
    Pre_SE = Pre_Sphere + (Pre_Cyl / 2),
    Post_SE = Post_Sphere + (Post_Cyl / 2),
    Residual_SE = abs(Post_SE),
    Change_SE = Post_SE - Pre_SE,
    Abs_Cyl_Post = abs(Post_Cyl)
  )
#### categorical variables 

el <- el %>%
  mutate(
    Procedure = str_trim(Procedure),
    Procedure = case_when(
      str_detect(Procedure, regex("EVO", ignore_case = TRUE)) ~ "EVO",
      str_detect(Procedure, regex("LASIK", ignore_case = TRUE)) ~ "LASIK",
      TRUE ~ Procedure
    ),
    
    Lasik_Type = str_trim(Lasik_Type),
    Lasik_Type = case_when(
      str_detect(Lasik_Type, regex("contoura", ignore_case = TRUE)) ~ "Contoura",
      str_detect(Lasik_Type, regex("WFO", ignore_case = TRUE)) ~ "WFO",
      TRUE ~ Lasik_Type
    ),
    
    Enhancement = str_trim(Enhancement),
    Enhancement = case_when(
      str_detect(Enhancement, regex("^yes$|^y$", ignore_case = TRUE)) ~ "Yes",
      str_detect(Enhancement, regex("^no$|^n$", ignore_case = TRUE)) ~ "No",
      TRUE ~ NA_character_
    ),
    
    Procedure = factor(Procedure),
    Lasik_Type = factor(Lasik_Type),
    Enhancement = factor(Enhancement, levels = c("No", "Yes"))
  )
### scope of NA values as suspected 
####Abs_Cyl_Post = amount of residual astigmatism  Change_SE = how much the patient's refractive error changed because of surgery 
####Residual_SE = absolute residual error Spherical Equivalent (SE) Pre_SE = (Pre-operative Spherical Equivalent) Post_SE = Post-operative Spherical Equivalent

table(el$Procedure, useNA = "ifany")
## 
##   EVO LASIK 
##   282   286
table(el$Eye, useNA = "ifany")
## 
##   OD   OS <NA> 
##  288  277    3
table(el$Procedure, el$Eye, useNA = "ifany")
##        
##          OD  OS <NA>
##   EVO   145 136    1
##   LASIK 143 141    2
table(el$Lasik_Type, useNA = "ifany")
## 
## Contoura      WFO     <NA> 
##       68      216      284
table(el$Enhancement, useNA = "ifany")
## 
##   No  Yes <NA> 
##  525    6   37
summary_by_procedure <- el %>%
  group_by(Procedure) %>%
  summarise(
    n = n(),
    Mean_Age = mean(Age, na.rm = TRUE),
    SD_Age = sd(Age, na.rm = TRUE),
    Mean_Pre_SE = mean(Pre_SE, na.rm = TRUE),
    SD_Pre_SE = sd(Pre_SE, na.rm = TRUE),
    Mean_Post_SE = mean(Post_SE, na.rm = TRUE),
    SD_Post_SE = sd(Post_SE, na.rm = TRUE),
    Mean_Residual_SE = mean(Residual_SE, na.rm = TRUE),
    SD_Residual_SE = sd(Residual_SE, na.rm = TRUE),
    Mean_Post_Cyl = mean(Abs_Cyl_Post, na.rm = TRUE),
    SD_Post_Cyl = sd(Abs_Cyl_Post, na.rm = TRUE)
  )

summary_by_procedure
## # A tibble: 2 × 12
##   Procedure     n Mean_Age SD_Age Mean_Pre_SE SD_Pre_SE Mean_Post_SE SD_Post_SE
##   <fct>     <int>    <dbl>  <dbl>       <dbl>     <dbl>        <dbl>      <dbl>
## 1 EVO         282     34.1  11.0        -6.69      2.92       -0.216      0.512
## 2 LASIK       286     33.8   9.17       -2.70      1.85       -0.167      0.490
## # ℹ 4 more variables: Mean_Residual_SE <dbl>, SD_Residual_SE <dbl>,
## #   Mean_Post_Cyl <dbl>, SD_Post_Cyl <dbl>
#### Compare residual refractive error t test
t.test(Residual_SE ~ Procedure, data = el)
## 
##  Welch Two Sample t-test
## 
## data:  Residual_SE by Procedure
## t = 0.91321, df = 427.7, p-value = 0.3616
## alternative hypothesis: true difference in means between group EVO and group LASIK is not equal to 0
## 95 percent confidence interval:
##  -0.04003627  0.10952378
## sample estimates:
##   mean in group EVO mean in group LASIK 
##           0.3796512           0.3449074
wilcox.test(Residual_SE ~ Procedure, data = el)
## 
##  Wilcoxon rank sum test with continuity correction
## 
## data:  Residual_SE by Procedure
## W = 24346, p-value = 0.3772
## alternative hypothesis: true location shift is not equal to 0

Wilcoxon rank-sum test is a nonparametric alternative that compares the distributions without assuming normality.

#### LASIK comparison 
lasik_only <- el %>%
  filter(Procedure == "LASIK",
         Lasik_Type %in% c("Contoura", "WFO"))

table(lasik_only$Lasik_Type, useNA = "ifany")
## 
## Contoura      WFO 
##       68      216
lasik_summary <- lasik_only %>%
  group_by(Lasik_Type) %>%
  summarise(
    n = n(),
    Mean_Pre_SE = mean(Pre_SE, na.rm = TRUE),
    SD_Pre_SE = sd(Pre_SE, na.rm = TRUE),
    Mean_Post_SE = mean(Post_SE, na.rm = TRUE),
    SD_Post_SE = sd(Post_SE, na.rm = TRUE),
    Mean_Residual_SE = mean(Residual_SE, na.rm = TRUE),
    SD_Residual_SE = sd(Residual_SE, na.rm = TRUE),
    Mean_Post_Cyl = mean(Abs_Cyl_Post, na.rm = TRUE),
    SD_Post_Cyl = sd(Abs_Cyl_Post, na.rm = TRUE)
  )

lasik_summary
## # A tibble: 2 × 10
##   Lasik_Type     n Mean_Pre_SE SD_Pre_SE Mean_Post_SE SD_Post_SE
##   <fct>      <int>       <dbl>     <dbl>        <dbl>      <dbl>
## 1 Contoura      68       -3.44      1.54       -0.152      0.418
## 2 WFO          216       -2.47      1.89       -0.171      0.510
## # ℹ 4 more variables: Mean_Residual_SE <dbl>, SD_Residual_SE <dbl>,
## #   Mean_Post_Cyl <dbl>, SD_Post_Cyl <dbl>
t.test(Residual_SE ~ Lasik_Type, data = lasik_only)
## 
##  Welch Two Sample t-test
## 
## data:  Residual_SE by Lasik_Type
## t = -0.42911, df = 109.25, p-value = 0.6687
## alternative hypothesis: true difference in means between group Contoura and group WFO is not equal to 0
## 95 percent confidence interval:
##  -0.12726679  0.08196559
## sample estimates:
## mean in group Contoura      mean in group WFO 
##              0.3275000              0.3501506
wilcox.test(Residual_SE ~ Lasik_Type, data = lasik_only)
## 
##  Wilcoxon rank sum test with continuity correction
## 
## data:  Residual_SE by Lasik_Type
## W = 4354, p-value = 0.592
## alternative hypothesis: true location shift is not equal to 0
#### Plots 
ggplot(el, aes(x = Procedure, y = Pre_SE, fill = Procedure)) +
  geom_boxplot(alpha = 0.7) +
  geom_jitter(width = 0.15, alpha = 0.4) +
  labs(
    title = "Pre-operative Spherical Equivalent by Procedure",
    x = "Procedure",
    y = "Pre-op Spherical Equivalent (D)"
  ) +
  theme_minimal()

ggplot(el, aes(x = Procedure, y = Residual_SE, fill = Procedure)) +
  geom_boxplot(alpha = 0.7) +
  geom_jitter(width = 0.15, alpha = 0.4) +
  labs(
    title = "Residual Refractive Error by Procedure",
    x = "Procedure",
    y = "Absolute Post-op SE (D)"
  ) +
  theme_minimal()

ggplot(lasik_only, aes(x = Lasik_Type, y = Residual_SE, fill = Lasik_Type)) +
  geom_boxplot(alpha = 0.7) +
  geom_jitter(width = 0.15, alpha = 0.4) +
  labs(
    title = "Residual SE: Contoura vs WFO LASIK",
    x = "LASIK Type",
    y = "Absolute Post-op SE (D)"
  ) +
  theme_minimal()

ggplot(el, aes(x = Pre_SE, y = Post_SE, color = Procedure)) +
  geom_point(alpha = 0.6) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  labs(
    title = "Pre-op SE vs Post-op SE",
    x = "Pre-op Spherical Equivalent (D)",
    y = "Post-op Spherical Equivalent (D)"
  ) +
  theme_minimal()