Divorce and FBI Crime Data Analysis

Author

Brendan Behunin

Overview

This project provides a Python package for exploring FBI crime data and divorce statistics by state.
It includes a Streamlit app for interactive visualization and tools for data wrangling.

Key features:

  • Load and clean FBI crime and divorce data
  • Visualize crime trends over time
  • Compare crime vs divorce rates
  • Interactive dashboard via Streamlit

Installation

You can install the package from PyPI:

pip install Stat386-FinalProject-Divorce-Crime==0.1.5

Run

You can access the streamlit by going to this Streamlit Link


Interacting with Data

On the left sidebar of the streamlit app, there are a couple drop downs. Select a state from the state dropdown to compare different states.

Select the crime you wish to see.

Select whether to look at actual data (i.e. number of homicides) vs rates of crime per capita

After making your selection, streamlit will automatically make the necessary adjustments to the graph.


Functions

  • load_data() Load data from a file

    Parameters
    file_path : str or Path

    Returns ———- pd.DataFrame

  • save_to_csv() Saves a pandas dataframe to a csv file

    Parameters
    df: pd.DataFrame
    location: str
    file_name: str

    Return ———- None

  • save_to_parquet() Saves a pandas dataframe to a parquet file

    Parameters
    df: pd.DataFrame
    location: str
    file_name: str

    Return ———- None

  • load_offense_mapping() Load crime offense mapping json

    Parameters
    json_path : str or Path

    Returns ———- dict

  • load_json() Load json

    Parameters
    json_path : str or Path

    Returns ———- dict

  • get_fbi_data() Calls the FBI Crime Data Explorer API and collects data into a parquet file, starting at start and ending at end This function requires that you have an API Key registered with the FBI and stored in a .env file

    Parameters
    key_name : str
    start : str
    end : str

    Returns ———- pd.DataFrame

  • get_census_data() Calls the US Census Data API and collects data into a csv file, starting at start and ending at end This function requires that you have an API Key registered with the US Census Bureau and stored in a .env file

    Parameters
    key_name : str
    start : int
    end : sinttr
    Returns
    pd.DataFrame

analysis

  • national_aggregate() Creates national-level trends:

    • Averages rate variables
    • Sums actual count variables
    Parameters
    df : pd.DataFrame

    Returns ———- pd.DataFrame

  • linear_regression_by_crime_rate() Creates a linear regression to predict crime using marriage rate, divorce rate, state, and year as predictors

    Parameters
    df : pd.DataFrame
    crime : str

    Returns ———- statsmodels.formula.api ols model

  • linear_regression_by_marriage_divorce() Creates a linear regression to predict marriage or divorce using larceny rates and rape rates, and burglary and arson rates respectively

    Parameters
    df : pd.DataFrame
    marriage_true : bool

    Returns ———- statsmodels.formula.api ols model

  • histogram_maker() Create a histogram for a given dataframe column

    Parameters
    df : pd.DataFrame
    column : str
    title : str
    x_label : str
    Returns
    matplotlib figure

clean_data

  • main() Loads in data, cleans it, and merges it

    Parameters
    census_data_file : str | Path
    crime_data_file : str | Path
    Return
    pd.DataFrame