Homework 3

Data Analysis

library(tidyverse)

# Load data
load ("/Users/wilmb/Downloads/naic_asset.rta")

# Data preparation
longdata <- MyData %>%
  select(COCODE, COMPANY_NAME, COMPANY_TYPE_DESC, CURRENT_YEAR, YEAR, LINE) %>%
  filter(LINE %in% c("Bonds", "CashHandDeposit", "Stocks_Com", "Total"))

# Pivoting to wide format
widedata <- longdata %>%
  select(COCODE, COMPANY_NAME, COMPANY_TYPE_DESC, YEAR, LINE, CURRENT_YEAR) %>%
  mutate(LINE = factor(LINE, levels = c("Bonds","CashHandDeposit","Stocks_Com","Total"))) %>%
  pivot_wider(
    names_from  = LINE,
    values_from = CURRENT_YEAR,
    values_fn   = ~ sum(.x, na.rm = TRUE)
  ) %>%
  select(COCODE, COMPANY_NAME, COMPANY_TYPE_DESC, YEAR,
         Bonds, CashHandDeposit, Stocks_Com, Total) %>%
  filter(!is.na(Total), Total > 0)

# Ratio variables
widedata <- widedata %>%
  mutate(
    bondRatio  = Bonds / Total,
    stockRatio = Stocks_Com / Total,
    YEAR = as.factor(YEAR),
    COMPANY_TYPE_DESC = as.factor(COMPANY_TYPE_DESC)
  )

# Pivoting back to long format
longdata <- widedata %>%
  pivot_longer(
    cols = -c(COCODE, YEAR, COMPANY_NAME, COMPANY_TYPE_DESC),
    names_to  = "name",
    values_to = "value"
  )

# Plot example
longdata %>%
  filter(name == "Total") %>%
  ggplot(aes(x = YEAR, y = value)) +
  geom_point() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  labs(title = "Total Asset Trends Over Time", x = "Year", y = "Total Assets")

Briefly describe what you found going through these steps to visualize the data.

When I looked at the graphs, I saw that total assets went up over the years, but there are big differences between companies. Some company types grew faster, while others stayed more flat. The boxplots show that most companies have small asset sizes, but a few very large ones push the numbers higher. For the ratios, I found that bonds are the biggest part of assets for most company types, while stocks are smaller and change more from year to year. When comparing both, bonds are always higher than stocks, but in some groups stocks are growing a little. The average charts also show that bonds are the main investment, and stocks are only a smaller part.