library(readxl)
library(tidyr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(purrr)
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(plm)
## 
## Attaching package: 'plm'
## The following objects are masked from 'package:dplyr':
## 
##     between, lag, lead
library(ggplot2)
library(vars)
## Loading required package: MASS
## 
## Attaching package: 'MASS'
## The following object is masked from 'package:dplyr':
## 
##     select
## Loading required package: strucchange
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: sandwich
## Loading required package: urca
## Loading required package: lmtest
library (lpirfs)
crisis <- read_excel("20160923_global_crisis_data.xlsx", na = "n/a")
## Warning: Expecting numeric in L4530 / R4530C12: got 'n.a.'
## Warning: Expecting numeric in L4531 / R4531C12: got 'n.a.'
## Warning: Expecting numeric in J4559 / R4559C10: got 'NA'
## Warning: Coercing text to numeric in I5961 / R5961C9: ''
## Warning: Coercing text to numeric in I10993 / R10993C9: ''
## Warning: Coercing text to numeric in I10994 / R10994C9: ''
## Warning: Coercing text to numeric in I10995 / R10995C9: ''
## Warning: Coercing text to numeric in I10996 / R10996C9: ''
## Warning: Coercing text to numeric in I10997 / R10997C9: ''
## Warning: Coercing text to numeric in I10998 / R10998C9: ''
civilwar <- read_excel("Intra-StateWarData_v4.1.xlsx")
 growthrates <-read_excel("growthrates.xlsx")
 View(growthrates)
options(scipen=999)
civilwar[sapply(civilwar, is.numeric)] <- lapply(civilwar[sapply(civilwar, is.numeric)], function(x) replace(x, x == -8, NA))
civilwar <- data.frame(lapply(civilwar, function(x) replace(x, x == -8, NA)))
civilwar=filter(civilwar,StartYear1>1950)
# get civil war information information I want 
civilwar <- civilwar %>%
  dplyr::select(WarName:EndYear1)%>%filter((Intnl==0)&(SideA!="United States of America"&SideA!="france"))
# get rid of  "First Philippine-Moro" war the Philippines were in two wars at the same time I got rid of the smaller one 
civilwar = civilwar %>%
  mutate(WarName = ifelse(is.na(WarName), 0, WarName))


civilwar <-filter(civilwar, WarName != "First Philippine-Moro")
civilwar = civilwar %>%
  mutate(WarName = ifelse(WarName == "Second Nepal Maoists", "First Nepal Maoist Insurgency", WarName))
#get ready to merge 
colnames(growthrates)[1]="Country"
growthrates=growthrates%>%group_by(Country)%>%pivot_longer(cols = 5:67,names_to = "year",values_to = "growth")%>%drop_na()
colnames(growthrates)[1]="Country"
colnames(civilwar)[4]="Country"
#colnames(Inter_StateWarData_v4_0)[26]="year"
# Convert start and end dates to Date objects
civilwar<- civilwar %>%
  mutate(StartDate = as.Date(paste(StartYear1, StartMonth1, StartDay1, sep = "-"), format = "%Y-%m-%d"),
         EndDate = as.Date(paste(EndYear1, EndMonth1, EndDay1, sep = "-"), format = "%Y-%m-%d"))
#filter wars without known start and end dates 
civilwar <- civilwar %>%
  filter(!is.na(StartDate) &!is.na(EndDate))
#treat the two civil wars as the same 
# Split data
nepal_war <- civilwar %>% 
  filter(WarName == "First Nepal Maoist Insurgency")

other_wars <- civilwar %>% 
  filter(WarName != "First Nepal Maoist Insurgency")

# Process the Nepal war data
nepal_war_summary <- nepal_war %>%
  summarise(
    WarName = first(WarName),
    WarType = first(WarType),
    CcodeA = first(CcodeA),
    Country = first(Country),
    CcodeB = first(CcodeB),
    SideB = first(SideB),
    Intnl = first(Intnl),
    StartDate = min(StartDate),
    EndDate = max(EndDate),
    StartMonth1 = month(min(StartDate)),
    StartDay1 = day(min(StartDate)),
    StartYear1 = year(min(StartDate)),
    EndMonth1 = month(max(EndDate)),
    EndDay1 = day(max(EndDate)),
    EndYear1 = year(max(EndDate))
  )
# Merge back into the original data
civilwar <- bind_rows(other_wars, nepal_war_summary)
# Extract the start and end years
civilwar <- civilwar %>%
  mutate(StartYear = year(StartDate),
         EndYear = year(EndDate))

# Create a new row for each year of the war for each country
civilwar <- civilwar %>%
  mutate(year = map2(StartYear, EndYear, seq)) %>%
  unnest(year)
# merge two DFs
growthrates$year=growthrates$year%>%as.integer()

 mergeddata <- growthrates %>%
    left_join(civilwar, by = c("Country", "year"))
# Nepal had two civil war one ended months before the second I decided to treat them as the same war 
mergeddata = mergeddata %>%
  mutate(WarName = ifelse(WarName == "Second Nepal Maoists", "First Nepal Maoist Insurgency", WarName))
# Assuming 'mergeddata' is your data frame
mergeddata <- mergeddata %>%
  group_by(Country) %>%
  filter(!all(is.na(WarName)))
#get needed rows
mergeddata <- mergeddata %>%
  dplyr::select(year,Country, growth, WarName, StartDate, EndDate, StartYear, EndYear)
# First, create a new column that indicates the year of the civil war
#mergeddata <- mergeddata %>%
 # arrange(Country, year) %>%
  #group_by(Country, WarName) %>%
  #mutate(civil_war_year = ifelse(!is.na(WarName), row_number(), NA))

# Then, spread this out into separate columns for each year of the civil war
#mergeddata <- mergeddata %>%
 # pivot_wider(names_from = civil_war_year, values_from = civil_war_year,
  #            names_prefix = "civil_war_year_", values_fill = 0)

# Replace all non-zero values with 1
#mergeddata[,-(1:8)] <- ifelse(mergeddata[,-(1:8)] > 0, 1, 0)
mergeddata$decgrowth=mergeddata$growth/100
# Identify earliest year for each country
earliest_years <- mergeddata %>%
  group_by(Country) %>%
  summarise(earliest_year = min(year), .groups = "drop")

# Identify countries where WarName is not NA in the earliest year
countries_to_remove <- mergeddata %>%
  semi_join(earliest_years, by = c("Country" = "Country", "year" = "earliest_year")) %>%
  filter(!is.na(WarName)) %>%
  pull(Country)

# Filter out these countries from the original dataset
mergeddata <- mergeddata %>%
  filter(!(Country %in% countries_to_remove))

mergeddata<- mergeddata %>%
     arrange(Country, year) %>%
     group_by(Country) %>%
    mutate(startwar = ifelse(is.na(dplyr::lag(WarName,1)) & !is.na(WarName), 1, 0))
# Initialize an empty vector to store the cumulative growth
mergeddata$cumulative_growth <- NA

# Loop over each country
for (country in unique(mergeddata$Country)) {
  # Get the rows for this country
  rows <- which(mergeddata$Country == country)
  
  # Initialize the cumulative growth at 1
  cumulative_growth <- 1
  
  # Loop over each row for this country
  for (i in rows) {
    # Update the cumulative growth, regardless of whether a war starts or not
    cumulative_growth <- cumulative_growth * (1 + mergeddata$decgrowth[i])
    
    # Store the cumulative growth
    mergeddata$cumulative_growth[i] <- cumulative_growth
  }  
}
#make a dummy for war
mergeddata <- mergeddata %>%
   mutate(war = ifelse(!is.na(WarName), 1, 0))

#make a recovery dummy that is 1 until the year output is 110% of prewar output 

# Make a recovery dummy that is 1 until the year output is 125% of prewar output 
mergeddata <- mergeddata %>%
  group_by(Country) %>%
  mutate(pre_war_cumulative_growth = ifelse(war == 0 & dplyr::lag(war, default = 0) == 1, dplyr::lag(cumulative_growth), NA)) %>%
  fill(pre_war_cumulative_growth, .direction = "down") %>%
  mutate(temp = ifelse(is.na(pre_war_cumulative_growth) | cumulative_growth >= pre_war_cumulative_growth * 1.25, 0, 1),
         above_one_after_war = ifelse(war == 0 & cumulative_growth < pre_war_cumulative_growth * 1.25 & temp == 1, 1, 0)) %>%
  dplyr::select(-temp)
mergeddata$prewwar=ifelse(dplyr::lead(mergeddata$startwar,1)==1& mergeddata$startwar==0,1,0)
#remove unnecessary data only war 
mergeddata=mergeddata%>%filter((prewwar==1)|(war==1)|(above_one_after_war==1))
mergeddata <- mergeddata %>%
  group_by(Country) %>%
  mutate(year_since_start = ifelse(war == 1, 0, cumsum(war == 0)))
#plot the recovery one plot 
ggplot(mergeddata, aes(x = year_since_start, y = decgrowth, color = Country)) +
  geom_line() +
  labs(x = "Years Since Start of War", y = "Decadal Growth") +
  theme_minimal() 

#plot the recovery one plot by country 
ggplot(mergeddata, aes(x = year_since_start, y = decgrowth, color = Country)) +
  geom_line() +
  labs(x = "Years Since Start of War", y = "Decadal Growth") +
  theme_minimal() +
  facet_wrap(~Country)

# Create lagged variables
mergeddata <- mergeddata %>%
  group_by(Country) %>%
  mutate(across(c(growth, war, above_one_after_war, prewwar, year_since_start), 
                list(lag1 = ~lag(., 1),
                     lag2 = ~lag(., 2),
                     lag3 = ~lag(., 3),
                     lag4 = ~lag(., 4))))
# Drop the first 4 rows for each country to avoid NA values due to lags
mergeddata <- mergeddata %>%
  group_by(Country) %>%
  slice(-(1:4))
pdf=pdata.frame(mergeddata,index = c("Country","year"))
pdf <- pdf[order(pdf$Country, pdf$year), ]
# Finish the formula
formula <- as.formula("growth ~ growth_lag1 + growth_lag2 + growth_lag3 + growth_lag4 +war_lag1 + war_lag2 + war_lag3 + war_lag4 + above_one_after_war_lag1 + above_one_after_war_lag2 + above_one_after_war_lag3 + above_one_after_war_lag4 +prewwar_lag1 + prewwar_lag2 + prewwar_lag3 + prewwar_lag4 + year_since_start_lag1 + year_since_start_lag2 + year_since_start_lag3 + year_since_start_lag4")

# Estimate the model
model <- plm(formula, data = pdf, model = "within",index = c("country","year"))
## Warning in summary.lm(object, ...): essentially perfect fit: summary may be
## unreliable
irf_result <- lp_lin_panel(data_set = pdf,data_sample ="Full",shock = "startwar",endog_data = "growth" ,confint = 1.96 ,hor = 7)
plot(irf_result)

irf_result2 <- lp_lin_panel(data_set = pdf,data_sample ="Full",shock = "war",endog_data = "cumulative_growth",confint = 1.96 ,hor = 7)
plot(irf_result2)

irf_result3 <- lp_lin_panel(data_set = pdf,data_sample ="Full",shock = "above_one_after_war",endog_data = "cumulative_growth" ,confint = 1.96 ,hor = 7)
plot(irf_result3)