-
Notifications
You must be signed in to change notification settings - Fork 4
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 info from outlier reason 2 and clean up graphs #69
Merged
dfsnow
merged 4 commits into
2025-assessment-year
from
add-sv-outlier-reason-2-to-outlier-performance-report
Dec 17, 2024
+28
−23
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,27 +18,29 @@ exclusively on these price-based factors that determine outlier status. | |
### Count by Type | ||
|
||
```{r _outliers_type_breakdown} | ||
# Make new column for a few cells of the outliers section | ||
# Subset important outlier information | ||
training_data <- training_data %>% | ||
mutate(price_outlier_reason = case_when( | ||
# Also grabs outlier reason Non-livable computed in 00-ingest.R | ||
str_detect(sv_outlier_reason1, regex("price|Non-livable", ignore_case = TRUE)) ~ sv_outlier_reason1, | ||
str_detect(sv_outlier_reason2, regex("price", ignore_case = TRUE)) ~ sv_outlier_reason2, | ||
str_detect(sv_outlier_reason3, regex("price", ignore_case = TRUE)) ~ sv_outlier_reason3, | ||
TRUE ~ NA_character_ | ||
)) | ||
mutate( | ||
outlier_reasons_to_graph = if_else( | ||
sv_is_outlier, | ||
paste0(sv_outlier_reason1, " (", sv_outlier_reason2, ")"), | ||
NA_character_ | ||
) | ||
) %>% | ||
# Yank NA string values to make output cleaner | ||
mutate(outlier_reasons_to_graph = str_remove_all(outlier_reasons_to_graph, " \\(NA\\)")) | ||
|
||
# Determine the axis limit | ||
y_lim_axis_outlier_breakdown <- training_data %>% | ||
filter(sv_is_outlier) %>% | ||
count(price_outlier_reason) %>% | ||
count(outlier_reasons_to_graph) %>% | ||
summarise(max_value = max(n)) %>% | ||
pull(max_value) | ||
|
||
training_data %>% | ||
filter(sv_is_outlier) %>% | ||
count(price_outlier_reason) %>% | ||
ggplot(aes(x = reorder(price_outlier_reason, -n), y = n)) + | ||
count(outlier_reasons_to_graph) %>% | ||
ggplot(aes(x = reorder(outlier_reasons_to_graph, -n), y = n)) + | ||
geom_bar(stat = "identity") + | ||
geom_text(aes(label = comma(n)), vjust = -0.5) + | ||
ylim(0, 1.03 * y_lim_axis_outlier_breakdown) + | ||
|
@@ -57,19 +59,19 @@ training_data %>% | |
```{r _outliers_type_breakdown_table} | ||
training_data %>% | ||
filter(sv_is_outlier) %>% | ||
group_by(meta_year, price_outlier_reason) %>% | ||
group_by(meta_year, outlier_reasons_to_graph) %>% | ||
summarise(n = n()) %>% | ||
rename(Year = meta_year) %>% | ||
pivot_wider(id_cols = Year, names_from = price_outlier_reason, values_from = n) %>% | ||
pivot_wider(id_cols = Year, names_from = outlier_reasons_to_graph, values_from = n) %>% | ||
kable() %>% | ||
kable_styling("striped") | ||
|
||
training_data %>% | ||
filter(sv_is_outlier) %>% | ||
group_by(meta_year, price_outlier_reason) %>% | ||
group_by(meta_year, outlier_reasons_to_graph) %>% | ||
summarise(n = n(), .groups = "drop") %>% | ||
rename(Year = meta_year) %>% | ||
pivot_wider(id_cols = Year, names_from = price_outlier_reason, values_from = n) %>% | ||
pivot_wider(id_cols = Year, names_from = outlier_reasons_to_graph, values_from = n) %>% | ||
kable() %>% | ||
kable_styling("striped") | ||
``` | ||
|
@@ -262,6 +264,11 @@ outliers_ratio_comparison <- training_data %>% | |
) %>% | ||
distinct(meta_township_name, percent, above_below, triad) | ||
|
||
axis_limit_outlier_ratio_comparison <- outliers_ratio_comparison %>% | ||
arrange(desc(percent)) %>% | ||
Matrix::head(1) %>% | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (blocking): Replace this with |
||
pull(percent) | ||
|
||
outliers_ratio_comparison %>% | ||
ggplot(aes(x = reorder(meta_township_name, percent), y = percent)) + | ||
labs( | ||
|
@@ -270,7 +277,7 @@ outliers_ratio_comparison %>% | |
geom_bar(stat = "identity", aes(fill = above_below)) + | ||
coord_flip() + | ||
geom_text(aes(label = round(percent, 2)), size = 3.2, hjust = -0.2) + | ||
scale_y_continuous(limits = c(0, 1.5)) + | ||
scale_y_continuous(limits = c(0, 1.1 * axis_limit_outlier_ratio_comparison)) + | ||
theme_minimal() + | ||
theme( | ||
axis.title.y = element_blank(), | ||
|
@@ -301,7 +308,7 @@ outliers_table_township_summary <- outliers_table_township_summary %>% | |
`Med. Sale Price` = median(meta_sale_price, na.rm = TRUE), | ||
`Max. Sale Price` = max(meta_sale_price, na.rm = TRUE), | ||
Count = n(), | ||
.by = c(price_outlier_reason, meta_township_name) | ||
.by = c(outlier_reasons_to_graph, meta_township_name) | ||
) %>% | ||
left_join( | ||
outliers_table_township_summary %>% | ||
|
@@ -315,7 +322,7 @@ outliers_table_township_summary <- outliers_table_township_summary %>% | |
mutate(across(contains("Sale"), dollar)) %>% | ||
relocate(meta_township_name) %>% | ||
dplyr::rename( | ||
"Outlier Type" = price_outlier_reason, | ||
"Outlier Type" = outlier_reasons_to_graph, | ||
"Township Name" = meta_township_name | ||
) %>% | ||
arrange(`Township Name`, desc(Count)) | ||
|
@@ -346,7 +353,7 @@ outliers_table_class_summary <- outliers_table_class_summary %>% | |
`Med. Sale Price` = median(meta_sale_price, na.rm = TRUE), | ||
`Max. Sale Price` = max(meta_sale_price, na.rm = TRUE), | ||
Count = n(), | ||
.by = c(price_outlier_reason, meta_class) | ||
.by = c(outlier_reasons_to_graph, meta_class) | ||
) %>% | ||
left_join( | ||
outliers_table_class_summary %>% | ||
|
@@ -360,7 +367,7 @@ outliers_table_class_summary <- outliers_table_class_summary %>% | |
mutate(across(contains("Sale"), dollar)) %>% | ||
relocate(meta_class) %>% | ||
dplyr::rename( | ||
"Outlier Type" = price_outlier_reason, | ||
"Outlier Type" = outlier_reasons_to_graph, | ||
"Class" = meta_class | ||
) %>% | ||
arrange(`Class`, desc(Count)) | ||
|
@@ -431,7 +438,7 @@ outlier_decile_breakout <- function(data, dec) { | |
arrange(meta_sale_price) %>% | ||
mutate(decile = ntile(meta_sale_price, 10)) %>% | ||
filter(decile == dec & sv_is_outlier) %>% | ||
group_by(price_outlier_reason) %>% | ||
group_by(outlier_reasons_to_graph) %>% | ||
summarise(count = n()) %>% | ||
ungroup() %>% | ||
slice_max(count, n = 1) %>% | ||
|
@@ -441,8 +448,8 @@ outlier_decile_breakout <- function(data, dec) { | |
arrange(meta_sale_price) %>% | ||
mutate(decile = ntile(meta_sale_price, 10)) %>% | ||
filter(decile == dec & sv_is_outlier) %>% | ||
summarise(count = n(), .by = price_outlier_reason) %>% | ||
ggplot(aes(x = reorder(price_outlier_reason, -count), y = count)) + | ||
summarise(count = n(), .by = outlier_reasons_to_graph) %>% | ||
ggplot(aes(x = reorder(outlier_reasons_to_graph, -count), y = count)) + | ||
labs( | ||
y = "Number of Sales", | ||
x = "Outlier Types" | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (blocking): Use a
case_when()
here. Creating a string and then manipulating it is fragile and error-prone.