```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)

suppressWarnings(suppressPackageStartupMessages({
  library(dplyr)
  library(ggplot2)
  library(scales)
  library(tidyr)
  library(ggthemes)
  library(kableExtra)
  library(DT)
  library(randomForest)
  library(caret)
}))
```

## Enough Falling – It’s Time to Rise

Do you remember Blackberry, Metro-Goldwyn-Mayer (MGM), Toys ‘R’ Us, Atari, and Karmelkorn?

Some will recognize these names—companies that were once very famous and lasted for years.

What happened to them? Why did they stop? Why couldn’t they keep going?

If you want to avoid the same fate, this report is for you.

It analyzes financial performance, stock trends, and employee data across many companies to identify patterns of survival and closure.

By understanding the traits of both successful and failed companies, I provide actionable insights and strategies to help you avoid failure and thrive in a competitive market.



```{r}
knitr::opts_chunk$set(echo = FALSE)
library(tidyverse)
library(ggplot2)
library(dplyr)
library(readr)
library(knitr)
library(gt)
library(lubridate)
library(corrplot)
library(tidyr)
library(forecast)
```


```{r load-data}
companiesSurvival <- read.csv("D:/Data Analysis/Company_Analysis_Project/Data/companiesDataCleaned.csv")
```

# Data Overview

```{r data-glimpse}
str(companiesSurvival)
summary(companiesSurvival)
```

```{r show-table, results='asis'}
head(companiesSurvival) %>%
  kable() %>%
  kable_styling(bootstrap_options = c("striped", "hover"))
```

## 📊 Company Status: Who Survived and Who Didn't

**Insight:**  
This visualizations shows various distributions of companies by their status—whether they are still operational or have closed.

# Company Lifespan by Status
The lifespan of companies shows a significant difference between those that survived and those that didn’t. Defunct companies tend to have shorter lifespans.
Lifespan is an important measure of company health.
Let’s compare the lifespans of active and defunct companies using the Status dataset.

```{r status-distribution}
ggplot(companiesSurvival, aes(x = Current_Status, fill = Current_Status)) +
  geom_bar() +
  labs(title = "Number of Companies by Status", x = "Current_Status", y = "Count") +
  theme_minimal()
```

## Company Lifespan by Industry
## Warnings:
1. **Large Outliers:**
   - Pay attention to companies that fall at the extremes of the chart, as these may represent exceptional cases of very long or short lifespans. These outliers could indicate extraordinary circumstances or significant fluctuations in performance.

2. **Variability Across Industries:**
   - Some industries show significant variation in company lifespans. This variation may be due to factors like market changes or a sudden drop in demand for products or services, which calls for companies in these industries to review their strategies to ensure sustainability.

3. **Data Limitations:**
   - The analysis does not account for external factors that may influence company lifespans, such as economic fluctuations or technological changes. These factors should be considered when developing growth strategies.

4. **Geographical and Cultural Differences:**
   - The lifespan of companies within the same industry can vary based on location or cultural practices. Business owners should consider the local context when evaluating lifespan expectations for their companies.

## Insights:
1. **Industry Lifespan Trends:**
   - Some industries consistently show shorter or longer lifespans. For example, [Industry X] tends to have shorter lifespans compared to [Industry Y], which could indicate rapid market changes or higher failure rates in that industry.

2. **High Variability in Some Industries:**
   - Industries with a wider range of lifespans may require a more flexible strategy to cope with the rapid changes in the market. Companies in these industries could benefit from adopting more agile business models that can adapt to challenges quickly.

3. **Comparison of Lifespan Across Industries:**
   - The chart allows for a comparison of company lifespans across industries, making it easier to understand stability in each sector. Industries with more tightly grouped values suggest greater stability.

4. **Opportunities for Industry-Specific Strategies:**
   - Based on this analysis, business owners in industries with shorter lifespans may focus on increasing flexibility and improving adaptability. On the other hand, industries with longer lifespans can concentrate on sustainable growth and innovation to ensure long-term success.
```{r}
duration_plot <- ggplot(companiesSurvival, aes(x = reorder(Industries, Dynamic_Duration, FUN = median), y = Dynamic_Duration)) +
  geom_boxplot(fill = "skyblue") +
  coord_flip() +
  labs(title = "Company Lifespan by Industry", x = "Industry", y = "Lifespan Duration (Years)") +
  theme_minimal()

print(duration_plot)
```

**Actionable Takeaway:**  
If your company is in the closure group, it is essential to analyze the contributing factors and take immediate action to turn the trend around.

**Insight:**  
This boxplot compares the lifespans of companies based on their status. 

**Actionable Takeaway:**  
A company’s ability to extend its lifespan depends on factors like financial management, adaptability, and market positioning. If your company is near the lower end of the lifespan spectrum, it is crucial to reassess its strategies, focusing on long-term sustainability rather than short-term gains.

## Employee Count: Employee Distribution by Company Status
Employee count is another factor that can help determine company success.
A significant drop in employee numbers is often seen in companies that are in decline.
Companies that are growing or stable tend to have a steady increase in their employee base.
Let’s look at how the number of employees varies based on company status.

# Data Summary for Employee Count
```{r emp-count-summary}
dim(companiesSurvival$Employee_Number)          # Dimensions
str(companiesSurvival$Employee_Number)          # Structure
summary(companiesSurvival$Employee_Number)      # Summary statistics
sum(is.na(companiesSurvival$Employee_Number))   # Count missing values
```

# Employee Count Distribution by Company Status

```{r emp-histogram}
ggplot(companiesSurvival, aes(x = Employee_Number, fill = Current_Status)) +
  geom_histogram(bins = 30) +
  theme_minimal() +
  labs(title = "Employee Count Distribution by Company Current Status",
       x = "Number of Employees", y = "Count")
```

## 📈 Revenue Over Time  
Revenue is a key indicator of a company’s financial performance.
Companies that have closed often show erratic or decreasing revenue trends, while active companies tend to have more stable or growing revenues.
Let’s explore how revenue varies across companies based on their status (active vs. defunct).

**Insight:**  
This line chart and histogram visualize the revenue trends of companies over time, categorized by their status. Healthy companies show consistent or growing revenue over the years, while companies that close often experience sharp declines or erratic fluctuations in revenue.

**Actionable Takeaway:**  
If your revenue is showing a decline or significant fluctuation, it is a red flag. To avoid closure, consider analyzing the reasons behind these revenue drops—such as market shifts, cost inefficiencies, or competition—and make adjustments like cost-cutting, diversifying product lines, or improving marketing efforts.

```{r revenue-trend}
ggplot(companiesSurvival, aes(x = Years, y = Revenue, group = Company_Name, color = Current_Status)) +
  geom_line(alpha = 0.6) +
  labs(title = "Revenue Trends Over Time", x = "Year", y = "Revenue") +
  theme_minimal()
```

```{r}
model <- lm(Revenue ~ Years, data = companiesSurvival)
summary(model)
```

### Predict future revenue
```{r}
future_years <- data.frame(Years = 2025:2028)
predicted_revenue <- predict(model, newdata = future_years)
data.frame(future_years, predicted_revenue)
```

### Insights
- Most companies show a positive revenue trend from 2018 to 2023.
- Linear models suggest continued growth through 2028 for leading companies.
- Some companies show volatility, which may indicate unstable markets or changing strategies.

## Average Revenue and Net Income by Industry
```{r}
industry_summary <- companiesSurvival %>%
  group_by(Industries) %>%
  summarise(
    Avg_Revenue = mean(Revenue, na.rm = TRUE),
    Avg_Income = mean(Net_Income, na.rm = TRUE),
    Avg_Duration = mean(Dynamic_Duration, na.rm = TRUE),
    Company_Count = n()
  ) %>%
  arrange(desc(Avg_Revenue))

industry_finance_long <- industry_summary %>%
  pivot_longer(cols = c(Avg_Revenue, Avg_Income), names_to = "Metric", values_to = "Value")

industry_finance_plot <- ggplot(industry_finance_long, aes(x = reorder(Industries, Value), y = Value, fill = Metric)) +
  geom_bar(stat = "identity", position = "dodge") +
  coord_flip() +
  labs(title = "Average Revenue and Net Income by Industry", x = "Industry", y = "Amount") +
  scale_fill_manual(values = c("Avg_Revenue" = "blue", "Avg_Income" = "green")) +
  theme_minimal()

print(industry_finance_plot)

```

# Net Income vs. Revenue

```{r income-vs-revenue}
ggplot(companiesSurvival, aes(x = Revenue, y = Net_Income, color = Current_Status)) +
  geom_point(alpha = 0.7) +
  labs(title = "Net Income vs Revenue", x = "Revenue", y = "Net Income") +
  theme_minimal()
```

## Prediction Model: Forecasting Future Company Status
Let’s build a predictive model using the data to identify which companies are more likely to continue, and which ones might be closed.

```{r}
# Prepare data
model_data <- companiesSurvival %>%
  filter(Current_Status %in% c("Active", "Defunct", "Re-Opened")) %>%
  mutate(Status_Class = as.factor(Current_Status)) %>%
  select(Status_Class, Foundation_Year, Closing_Year, Dynamic_Duration, Employee_Number,
         Revenue, Net_Income, Assets, long_Term_Debt, Total_Liabilities,
         Holders_Equity, Avg_Stock_Price, Avg_TTM_Net_EPS, Avg_PE_Ratio) %>%
  drop_na()


# Split into train/test sets
set.seed(42)
trainIndex <- createDataPartition(model_data$Status_Class, p = 0.8, list = FALSE)
train <- model_data[trainIndex, ]
test <- model_data[-trainIndex, ]

# Train random forest model
rf_model <- randomForest(Status_Class ~ ., data = train, ntree = 300, importance = TRUE)

# Predictions on test set
test$Prediction <- predict(rf_model, newdata = test)

# Confusion matrix
confusionMatrix(test$Prediction, test$Status_Class)

```

## Feature Importance
```{r}
# Plot feature importance
varImpPlot(rf_model)
```

## Industry-Specific Insights
### Company Status by Industry
```{r}
industry_status_summary <- companiesSurvival %>%
  group_by(Industries, Current_Status) %>%
  summarise(Count = n(), .groups = 'drop')

industry_status_plot <- ggplot(industry_status_summary, aes(x = reorder(Industries, -Count), y = Count, fill = Current_Status)) +
  geom_bar(stat = "identity", position = "dodge") +
  coord_flip() + 
  labs(title = "Company Status by Industry", x = "Industries", y = "Count") +
  scale_fill_manual(values = c("Active" = "green", "Defunct" = "red", "Re-Opened" = "blue")) +
  theme_minimal()

print(industry_status_plot)
```


## ⚠️ Critical Warning for At-Risk Companies

- If your company has been flagged by the prediction model as “Defunct” or at risk, this is not just a data point—it's a wake-up call.

- Your financial indicators, operational trends, and employee patterns mirror those of companies that have already failed.

- Unless immediate and strategic actions are taken, your company may face irreversible closure.

- This report has highlighted the exact warning signs—declining revenue, shrinking employee numbers, rising liabilities, and reduced investor confidence.

**You are not out of time yet—but you're close:**

- Take this prediction seriously:

- Reassess your business model.

- Cut non-essential costs.

- Innovate your offerings.

- Rebuild trust with investors and employees.

The next move you make could decide your company’s fate. Delay is the most dangerous option.


## 💰 Stock Price and PE Ratio
**Stock Trends:**
Companies with increasing stock prices tend to have better financial performance and longevity. On the other hand, companies with fluctuating or decreasing stock prices may face challenges.
We will analyze the stock data to visualize how different companies' stock prices have evolved over time.

**Insight:**  
This scatter plot shows the relationship between the Price-to-Earnings (PE) ratio and the average stock price. Companies that are financially healthy often have a higher PE ratio, indicating that investors have more confidence in their future earnings potential.

**Actionable Takeaway:**  
A low PE ratio can indicate that your company is undervalued or struggling. To boost the PE ratio, consider improving profitability by increasing revenue, reducing costs, or communicating your company's growth potential more effectively to investors.

**Important Note:**  
As the chart shows, the overall average revenue among companies is 5 million, reflecting the good performance of the companies that survived compared to those that closed.

# Average PE Ratio by Status

```{r pe-ratio-by-status}
companiesSurvival %>%
  group_by(Status) %>%
  summarise(avg_pe = mean(Avg_PE_Ratio, na.rm = TRUE)) %>%
  ggplot(aes(x = Status, y = avg_pe, fill = Status)) +
  geom_col() +
  labs(title = "Average PE Ratio by Company Status", x = "Status", y = "Avg PE Ratio") +
  theme_minimal()
```

# Average Stock Price Distribution

```{r stock-price-distribution}
ggplot(companiesSurvival, aes(x = Avg_Stock_Price, fill = Status)) +
  geom_histogram(bins = 30, alpha = 0.7, position = "identity") +
  labs(title = "Average Stock Price Distribution", x = "Stock Price", y = "Frequency") +
  theme_minimal()
```

# Company-Level Summaries

```{r company-summary-table}
summary_table <- companiesSurvival %>%
  group_by(Company_Name, Status) %>%
  summarise(
    Avg_Revenue = mean(Revenue, na.rm = TRUE),
    Avg_Net_Income = mean(Net_Income, na.rm = TRUE),
    Avg_Stock_Price = mean(Avg_Stock_Price, na.rm = TRUE),
    Avg_PE = mean(Avg_PE_Ratio, na.rm = TRUE),
    .groups = 'drop')

datatable(summary_table)
```

# 8.1 Key Takeaways and Survival Strategies

## 1. Financial Health:
   - Regularly monitor key financial metrics (e.g., revenue, profit margins, debt) to identify signs of distress early. Financial health is crucial for making proactive adjustments before issues escalate into crises.
   - **Tip:** Set up automated financial alerts to track deviations from key performance indicators (KPIs).

## 2. Customer Focus:
   - Reassess your customer needs and the value you’re offering. Adapt to shifting market demands and invest in customer loyalty. Focus on enhancing the customer experience and responding to feedback.
   - **Tip:** Leverage customer surveys and feedback loops to stay aligned with market demands.

## 3. Case Study:
   - **Apple** successfully navigated financial and market crises by embracing new technologies such as touchscreen interfaces and app ecosystems. These innovations drastically improved product appeal, operational efficiency, and customer engagement.
   - In contrast, **BlackBerry** failed to adapt to changing consumer demands and technological trends, holding on to outdated features like physical keyboards and enterprise-focused software. As a result, Apple thrived, while BlackBerry’s market share collapsed.
   
   - **Insight:** Technology and customer preferences evolve quickly; companies must stay agile to meet these changes.

## 4. Lesson Learned:
   - Companies that proactively adopt relevant technologies and innovate based on market needs are more likely to survive and grow. Those that resist change or fail to anticipate shifts in consumer behavior risk becoming obsolete.
   - **Action Point:** Regularly assess emerging technologies and industry trends to remain competitive.

## 5. Investor Confidence:
   - If your PE ratio is low, focus on improving profitability and clearly communicating your vision for growth to investors. Transparency about future strategies and clear milestones can help rebuild investor confidence.
   - **Tip:** Develop a clear roadmap of your company’s growth and communicate this regularly to your investors to build trust.

## 6. Operational Efficiency:
   - Short lifespans and closures often result from poor management or operational inefficiencies. Regular audits of operations, cost-control measures, and continuous innovation can prevent stagnation and ensure long-term success.
   - **Action Point:** Implement Lean or Six Sigma practices to streamline processes and eliminate waste.

## 7. Adaptability:
   - The ability to pivot or adapt to market conditions is essential for long-term survival. Companies that failed typically couldn’t adapt quickly enough to external changes like technology shifts, regulatory changes, or market demands.
   - **Tip:** Build a culture of flexibility within your organization, ensuring your team is prepared to react swiftly to market changes.

---

# 8.2 Industry-Specific Insights: Most Affected by Closures

## 1. Industries Most Affected by Company Closures:
   - Some industries are more vulnerable to closures due to rapid technological changes, shifts in consumer behavior, and economic disruptions. These industries often experience a higher number of business failures due to their inability to adapt to external changes.
   
   - **Retail Industry:**
     - Retail has seen a significant number of closures in recent years, especially brick-and-mortar stores, as consumer preferences shift towards online shopping. Companies that fail to invest in e-commerce or adopt omni-channel strategies risk closure.
   
   - **Technology Industry:**
     - The fast pace of technological innovation poses both opportunities and threats. Startups and even established companies can be wiped out if they fail to innovate or keep up with new technologies, like the rise of mobile apps, AI, or cloud computing.
   
   - **Hospitality and Travel:**
     - The hospitality and travel sectors are highly vulnerable to external factors such as economic downturns, pandemics, and geopolitical instability. The COVID-19 pandemic, for example, led to massive closures in this industry.
   
   - **Manufacturing Industry:**
     - In some regions, the manufacturing industry has seen closures due to automation, shifting labor costs, and competition from low-cost countries. Companies that fail to innovate and adopt efficient technologies face an increased risk of shutting down.

## 2. Industries with the Highest Number of Closures:
   - Some industries show higher rates of company closures due to inherent challenges such as market saturation, regulatory pressure, and limited differentiation among companies. For example:
   
   - **Construction and Real Estate:**
     - The construction and real estate sectors often see high turnover due to fluctuating demand and market conditions. Economic recessions, shifting housing prices, and the difficulty of securing financing contribute to the closure of companies within this industry.

   - **Restaurants and Food Services:**
     - The foodservice industry is notoriously unstable, with many restaurants and cafes closing due to high competition, changing consumer tastes, and economic pressures. The pandemic has further exacerbated these closures, with many small businesses unable to recover.

   - **Transportation and Logistics:**
     - Companies in the transportation and logistics sector often face challenges from rising fuel costs, regulatory changes, and market volatility. The demand for transportation services can fluctuate dramatically, leading to company closures during economic downturns.

## 3. Lessons for Business Owners in Affected Industries:
   - **Adapting to Market Shifts:**
     - Industries prone to closures need to stay agile and adapt quickly to market shifts, whether they’re technological advancements, changing customer preferences, or external crises. The key is to continuously innovate and stay ahead of the competition.
   
   - **Diversification:**
     - For industries with high closure rates, diversifying revenue streams and exploring new markets can be critical. Companies should consider expanding their offerings or targeting new customer segments to reduce reliance on a single market.
   
   - **Technology Adoption:**
     - Industries like retail, technology, and manufacturing can significantly benefit from adopting the latest technologies. Automation, e-commerce platforms, and AI can help streamline operations and enhance customer experience, reducing the risk of closure.

---

### Conclusion:
In conclusion, analyzing financial performance, customer needs, technological trends, and operational efficiency provides valuable insights into a company’s potential for survival or closure. Proactive monitoring of these factors, combined with a willingness to adapt and innovate, can significantly increase a company's chances of thriving in a competitive and ever-changing marketplace. By making strategic decisions based on these insights, business owners can avoid closure and secure long-term success.
