R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

Install Packages Needed

install.packages(“cancensus”) library(cancensus) library(dplyr) library(tidyr) library(readr) library(sf) library(ggplot2) install.packages(“vtable”) library(vtable) install.packages(“units”) library(units) install.packages(“survival”) library(survival) library(haven)

Survival Analysis

Create data

data <- read.csv(“/Users/abimeza/Documents/Documents/Canadian Urban Institute/Datasets/eviction_data.csv”) names(data)

Create the rental_units_gta variable

data <- data %>% mutate(rental_units_gta = case_when( gta == “City of Toronto” ~ 159367, gta == “City of Mississauga” ~ 30149, gta == “York Region” ~ 36842,
gta == “City of Brampton” ~ 12007, gta %in% c(“Halton Region”, “Town of Milton”) ~ 1049,
gta %in% c(“City of Pickering”, “Town of Ajax”, “Township of Uxbridge”) ~ 2962, gta == “City of Markham” ~ 1631, # markham gta %in% c(“Town of Aurora”, “Town of Whitchurch-Stouffville”) ~ 2312,
gta %in% c(“City of Richmond Hill”, “City of Vaughan”, “Township of King”) ~ 2438, gta == “Town of Caledon” ~ 224, gta == “Town of Oakville” ~ 5698,
TRUE ~ NA_real_ #for any unmatched values ))

Checking if it worked

table(data$rental_units_gta, useNA = “always”)

table(data$evic_type)

checking empty evic_type values are

table(data\(evic_type == "") table(data\)case_type[data$evic_type == “”], useNA = “always”)

Create a cleaner version by setting L1 as reference and handling missing values

data <- data %>% mutate( # Scale rental units for easier interpretation rental_units_gta_scaled = rental_units_gta / 1000,

# Clean up evic_type: set L1 as reference, handle missing as separate category

evic_type_clean = case_when( evic_type == “” ~ “Other/Unknown”, evic_type %in% c(“L1”, “L2”, “L3”, “L4”, “L5”, “L9”) ~ evic_type, TRUE ~ “Other” ), evic_type_factor = factor(evic_type_clean, levels = c(“L1”, “L2”, “L3”, “L4”, “L5”, “L9”, “Other/Unknown”, “Other”)) )

Check the new distribution

table(data$evic_type_factor)

Running cox model with L1 as reference

cox_model_clean <- coxph(Surv(tenure_days, event) ~ evic_type_factor + rental_units_gta_scaled, data = data)

summary(cox_model_clean)

Create categorical area variable based on rental_units_gta

data <- data %>% mutate(area_name = case_when( rental_units_gta == 159357 ~ “Toronto”, rental_units_gta == 30149 ~ “Mississauga”, rental_units_gta == 36842 ~ “York Region”, rental_units_gta == 12007 ~ “Brampton”, rental_units_gta == 1049 ~ “Halton/Milton”, rental_units_gta == 2962 ~ “Pickering/Ajax/Uxbridge”, rental_units_gta == 1631 ~ “Markham”, rental_units_gta == 2312 ~ “Aurora/Whitchurch-Stouffville”, rental_units_gta == 2438 ~ “Richmond Hill/Vaughan/King”, rental_units_gta == 224 ~ “Caledon”, rental_units_gta == 5698 ~ “Oakville”, TRUE ~ “Other” ))

Set Toronto as reference (largest market)

data\(area_factor <- factor(data\)area_name, levels = c(“Toronto”, “Mississauga”, “York Region”, “Brampton”, “Halton/Milton”, “Pickering/Ajax/Uxbridge”, “Markham”, “Aurora/Whitchurch-Stouffville”, “Richmond Hill/Vaughan/King”, “Caledon”, “Oakville”, “Other”)) # Model with both eviction type and specific areas cox_model_full <- coxph(Surv(tenure_days, event) ~ evic_type_factor + area_factor, data = data)

summary(cox_model_full)

library(broom) library(knitr) install.packages(“gt”) library(gt)

tidy(cox_model_full, exponentiate = TRUE, conf.int = TRUE) %>% gt() %>% tab_header(title = “Cox Model: Time to Eviction Resolution”) %>% fmt_number(columns = c(estimate, conf.low, conf.high), decimals = 2) %>% fmt_scientific(columns = p.value, decimals = 3)