Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ojo_eviction_cases #173

Merged
merged 8 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Depends:
License: GPL (>= 3)
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
VignetteBuilder: knitr
Config/testthat/edition: 3
Config/testthat/parallel: true
Expand Down
9 changes: 9 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export(ojo_connect)
export(ojo_county_population)
export(ojo_crim_cases)
export(ojo_env)
export(ojo_eviction_cases)
export(ojo_fill)
export(ojo_fiscal_year)
export(ojo_list_schemas)
Expand All @@ -27,4 +28,12 @@ export(ojo_tbl)
export(ojo_theme)
export(ojo_version)
import(dplyr)
importFrom(dplyr,case_when)
importFrom(dplyr,filter)
importFrom(dplyr,left_join)
importFrom(dplyr,mutate)
importFrom(dplyr,select)
importFrom(ggplot2,"%+replace%")
importFrom(lubridate,as_date)
importFrom(lubridate,floor_date)
importFrom(stringr,str_detect)
138 changes: 138 additions & 0 deletions R/ojo_eviction_cases.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#' @title OJO Eviction Cases
#'
#' @description
#' Collects Oklahoma eviction data for the specified districts (or all of Oklahoma)
#' for the user specified time frame. If date_end is not specified, the
#' most up-to-date data will be collected.
#'
#' Function uses the latest methodology we use for identifying eviction cases
#' and outcomes in Oklahoma. As that methodology is updated, this function will
#' be updated to reflect those changes.
#'
#' @param district District code for which to collect data (default is "all", which collects data for all districts)
#' @param ... Placeholder for additional arguments
#' @param date_start Start date for the data collection period
#' @param date_end End date for the data collection period (default is NULL, which collects the most up-to-date data)
#' @param more_case_variables Additional variables from case table to include in the output
#' @param more_issue_variables Additional variables from issue table to include in the output
#' @param get_judgments Logical value indicating whether to include eviction judgment information in the output
#'
#' @importFrom dplyr filter select left_join mutate case_when
#' @importFrom stringr str_detect
#' @importFrom lubridate floor_date as_date
#'
#' @export ojo_eviction_cases
#' @return A dataframe containing eviction data
#'
#' @examples
#' \dontrun{
#' ojo_eviction_cases()
#' ojo_eviction_cases(districts = c("TULSA", "ADAIR"))
#' ojo_eviction_cases(
#' districts = c("TULSA", "ADAIR"),
#' date_start = "2020-01-01",
#' date_end = "2020-01-31",
#' more_case_variables = disposition_date
#' )
#' ojo_eviction_cases(
#' districts = c("TULSA", "ADAIR"),
#' get_judgments = TRUE
#' )
#' }
#'

ojo_eviction_cases <- function(districts = "all",
...,
date_start = NULL,
date_end = NULL,
more_case_variables = NULL,
more_issue_variables = NULL,
get_judgments = TRUE) {
#### Variable Handling
.district <- toupper(districts)

##### Data Wrangling / Cleaning
## Construct Data
data <- ojodb::ojo_tbl("case")

if (!any(.district == "ALL")) {
data <- data |>
dplyr::filter(district %in% .district)
}

if (!is.null(date_end)) {
data <- data |>
dplyr::filter(date_filed <= date_end)
}

if (!is.null(date_start)) {
data <- data |>
dplyr::filter(date_filed >= date_start)
}

case_vars <- unique(
c(
"id", "district", "date_filed", "date_closed", "status",
more_case_variables
)
)

issue_vars <- unique(
c(
"id", "case_id", "description", "disposition",
more_issue_variables
)
)

data <- data |>
dplyr::filter(case_type == "SC") |>
dplyr::select(dplyr::all_of(case_vars)) |>
dplyr::left_join(
ojodb::ojo_tbl("issue") |>
dplyr::select(dplyr::all_of(issue_vars)),
by = c("id" = "case_id"),
suffix = c(".case", ".issue")
)

## Keep Only Eviction Cases
# This our standard "strict" definition for research and analysis applications.
# For applications where we may want to allow for more errors such as for
# non-profit service providers, we may want to consider a more lenient definition.
data <- data |>
dplyr::filter(
stringr::str_detect(
description,
"RENT|FORCI|EVICT|DETAIN"
)
)

if (get_judgments == TRUE) {
data <- data |>
dplyr::mutate(clean_disposition = case_when(
stringr::str_detect(disposition, "DISMISS") ~ "DISMISSED",
stringr::str_detect(disposition, "JUDGMENT|JUDGEMENT") ~
case_when(
stringr::str_detect(disposition, "DEFAULT") ~ "DEFAULT JUDGMENT",
stringr::str_detect(disposition, "PLAINTIFF") ~ "JUDGMENT FOR PLAINTIFF",
stringr::str_detect(disposition, "DEFENDANT") ~ "JUDGMENT FOR DEFENDANT",
TRUE ~ "JUDGMENT ENTERED"
),
stringr::str_detect(disposition, "ADVISEMENT") ~ "UNDER ADVISEMENT"
))

data <- data |>
dplyr::mutate(
judgment = dplyr::case_when(
clean_disposition %in%
c("DEFAULT JUDGMENT", "JUDGMENT FOR PLAINTIFF") ~ "Eviction Granted",
clean_disposition == "JUDGMENT FOR DEFENDANT" ~ "Eviction Denied",
clean_disposition == "JUDGMENT ENTERED" ~ "Case Decided, Outcome Unknown",
clean_disposition == "DISMISSED" ~ "Case Dismissed (Settled Outside Court)",
clean_disposition == "UNDER ADVISEMENT" ~ "Case Under Advisement",
.default = "Case Undecided"
)
)
}

return(data)
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ reference:
contents:
- ojo_civ_cases
- ojo_crim_cases
- ojo_eviction_cases
- ojo_add_counts
- ojo_add_minutes
- ojo_add_issues
Expand Down
60 changes: 60 additions & 0 deletions man/ojo_eviction_cases.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions tests/testthat/test-ojo-eviction_cases.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
test_that("ojo_eviction_cases works", {
skip_on_runiverse()

testthat::expect_no_error({
ojo_eviction_cases()
})
})
2 changes: 1 addition & 1 deletion tests/testthat/test-ojo_civ_cases.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test_that("ojo_civ_cases works", {
testthat::test_that("ojo_civ_cases works", {
skip_on_runiverse()

testthat::expect_no_error({
Expand Down
Loading