library(dplyr)
library(tidyr)
library(ggplot2)
library(purrr)
library(broom)
library(gganimate)
library(cowplot)
library(multiverse)

Introduction

In this document, I implement the multimodal analysis performed by Young and Holsteen:

read data

M = multiverse()

```{multiverse default-m-1, inside = M} df <- read.csv('../data/mortgage.csv') %>% mutate( accept_scaled = accept * 100 ) %>% # here we drop all NAs for simplicity, but we will drop up to 7 more data # points in some models, which may cause discrepancy with Young et al. drop_na()

# linear regression

```{multiverse default-m-2, inside = M}
model <- lm(
  accept_scaled ~ 
    female +
    branch(black, "included" ~ black, "excluded" ~ NULL) +
    branch(housing_expense_ratio, "included" ~ housing_expense_ratio, "excluded" ~ NULL) +
    branch(self_employed, "included" ~ self_employed, "excluded" ~ NULL) +
    branch(married, "included" ~ married, "excluded" ~ NULL) +
    branch(bad_history, "included" ~ bad_history, "excluded" ~ NULL) +
    branch(loan_to_value, "included" ~ loan_to_value, "excluded" ~ NULL) +
    branch(denied_PMI, "included" ~ denied_PMI, "excluded" ~ NULL), 
  data = df)

fit.df = broom::tidy(model, conf.int = TRUE)

predictions = predict(model, df)
execute_multiverse(M)
results_df = extract_variables(M, fit.df, predictions)
results_df %>%
  group_by(.universe) %>%
  select(-fit.df) %>%
  filter(!(.universe %in% c(107, 108, 111, 112, 123, 124, 128))) %>%
  unnest(c(predictions)) %>%
  ggplot() +
  geom_line(aes(x = predictions, group = .universe), stat="density", alpha = 0.1)
results_df %>%
  group_by(.universe) %>%
  unnest(c(fit.df)) %>%
  filter(term == "female") %>%
  mutate(
    race_marriage_controlled = ifelse(black == "included" & married == "included", "yes", "no")
  ) %>%
  ggplot() +
  geom_line(aes(x = estimate, colour = race_marriage_controlled), stat="density")
results_df %>%
  group_by(.universe) %>%
  unnest(c(fit.df)) %>%
  filter(term == "female") %>%
  mutate(
    race_marriage_controlled = ifelse(black == "included" & married == "included", "yes", "no")
  ) %>%
  ggplot() +
  geom_line(aes(x = estimate), stat="density")
results_df %>%
  group_by(.universe) %>%
  filter(.universe %in% c(107, 108, 111, 112, 123, 124, 128))


MUCollective/multidy documentation built on Jan. 27, 2024, 9:52 a.m.