---
title: "00c_ConfounderCompilationBufferETC"
output: html_document
date: "2024-11-08"
---

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


```{r Buffer 3km}

###Precip Compilation

mada_2000_precip_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_3km/ZonalSt_3km_Buffer_Pre_2000_TableToExcel.xlsx")
mada_2001_precip_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_3km/ZonalSt_3km_Buffer_Pre_2001_TableToExcel.xlsx")
mada_2002_precip_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_3km/ZonalSt_3km_Buffer_Pre_2002_TableToExcel.xlsx")
mada_2003_precip_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_3km/ZonalSt_3km_Buffer_Pre_2003_TableToExcel.xlsx")
mada_2004_precip_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_3km/ZonalSt_3km_Buffer_Pre_2004_TableToExcel.xlsx")
mada_2005_precip_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_3km/ZonalSt_3km_Buffer_Pre_2005_TableToExcel.xlsx")
mada_2006_precip_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_3km/ZonalSt_3km_Buffer_Pre_2006_TableToExcel.xlsx")
mada_2007_precip_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_3km/ZonalSt_3km_Buffer_Pre_2007_TableToExcel.xlsx")
mada_2008_precip_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_3km/ZonalSt_3km_Buffer_Pre_2008_TableToExcel.xlsx")
mada_2009_precip_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_3km/ZonalSt_3km_Buffer_Pre_2009_TableToExcel.xlsx")
mada_2010_precip_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_3km/ZonalSt_3km_Buffer_Pre_2010_TableToExcel.xlsx")
mada_2011_precip_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_3km/ZonalSt_3km_Buffer_Pre_2011_TableToExcel.xlsx")
mada_2012_precip_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_3km/ZonalSt_3km_Buffer_Pre_2012_TableToExcel.xlsx")
mada_2013_precip_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_3km/ZonalSt_3km_Buffer_Pre_2013_TableToExcel.xlsx")
mada_2014_precip_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_3km/ZonalSt_3km_Buffer_Pre_2014_TableToExcel.xlsx")
mada_2015_precip_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_3km/ZonalSt_3km_Buffer_Pre_2015_TableToExcel.xlsx")
mada_2016_precip_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_3km/ZonalSt_3km_Buffer_Pre_2016_TableToExcel.xlsx")
mada_2017_precip_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_3km/ZonalSt_3km_Buffer_Pre_2017_TableToExcel.xlsx")
mada_2018_precip_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_3km/ZonalSt_3km_Buffer_Pre_2018_TableToExcel.xlsx")
mada_2019_precip_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_3km/ZonalSt_3km_Buffer_Pre_2019_TableToExcel.xlsx")
mada_2020_precip_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_3km/ZonalSt_3km_Buffer_Pre_2020_TableToExcel.xlsx")
mada_2021_precip_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_3km/ZonalSt_3km_Buffer_Pre_2021_TableToExcel.xlsx")

mada_2000_precip_buffer_3km$Year <- 2000
mada_2001_precip_buffer_3km$Year <- 2001
mada_2002_precip_buffer_3km$Year <- 2002
mada_2003_precip_buffer_3km$Year <- 2003
mada_2004_precip_buffer_3km$Year <- 2004
mada_2005_precip_buffer_3km$Year <- 2005
mada_2006_precip_buffer_3km$Year <- 2006
mada_2007_precip_buffer_3km$Year <- 2007
mada_2008_precip_buffer_3km$Year <- 2008
mada_2009_precip_buffer_3km$Year <- 2009
mada_2010_precip_buffer_3km$Year <- 2010
mada_2011_precip_buffer_3km$Year <- 2011
mada_2012_precip_buffer_3km$Year <- 2012
mada_2013_precip_buffer_3km$Year <- 2013
mada_2014_precip_buffer_3km$Year <- 2014
mada_2015_precip_buffer_3km$Year <- 2015
mada_2016_precip_buffer_3km$Year <- 2016
mada_2017_precip_buffer_3km$Year <- 2017
mada_2018_precip_buffer_3km$Year <- 2018
mada_2019_precip_buffer_3km$Year <- 2019
mada_2020_precip_buffer_3km$Year <- 2020
mada_2021_precip_buffer_3km$Year <- 2021

mada_precip_buffer_3km <- rbind(mada_2000_precip_buffer_3km,
                            mada_2001_precip_buffer_3km, 
                            mada_2002_precip_buffer_3km, 
                            mada_2003_precip_buffer_3km, 
                            mada_2004_precip_buffer_3km, 
                            mada_2005_precip_buffer_3km, 
                            mada_2006_precip_buffer_3km, 
                            mada_2007_precip_buffer_3km, 
                            mada_2008_precip_buffer_3km, 
                            mada_2009_precip_buffer_3km, 
                            mada_2010_precip_buffer_3km, 
                            mada_2011_precip_buffer_3km, 
                            mada_2012_precip_buffer_3km, 
                            mada_2013_precip_buffer_3km, 
                            mada_2014_precip_buffer_3km, 
                            mada_2015_precip_buffer_3km, 
                            mada_2016_precip_buffer_3km, 
                            mada_2017_precip_buffer_3km,
                            mada_2018_precip_buffer_3km,
                            mada_2019_precip_buffer_3km,
                            mada_2020_precip_buffer_3km,
                            mada_2021_precip_buffer_3km)


mada_precip_buffer_3km$Field[mada_precip_buffer_3km$Field=="Bemaraha"]<- "Bemaraha (combined)"
mada_precip_buffer_3km$Field[mada_precip_buffer_3km$Field=="Baie de Baly/Tsingy de Namoroka"]<- "Namoroka/Baie de Baly"
mada_precip_buffer_3km$Field[mada_precip_buffer_3km$Field=="Mantadia"]<- "Andasibe/Analamazaotra"


mada_precip_buffer_3km <- mada_precip_buffer_3km %>% rename("Name"="Field")
mada_precip_buffer_3km2 <- mada_precip_buffer_3km %>% dplyr::select(Name,MEAN,Year)
mada_precip_buffer_3km2 <- mada_precip_buffer_3km2 %>% rename("Precipitation" = "MEAN")


write.csv(mada_precip_buffer_3km, "~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_buffer_3km/Precip_buffer_3km.csv")


###COMMUNE PRECIPITATION
mada_2000_precip_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Pre_2000_TableToExcel.xlsx")
mada_2001_precip_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Pre_2001_TableToExcel.xlsx")
mada_2002_precip_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Pre_2002_TableToExcel.xlsx")
mada_2003_precip_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Pre_2003_TableToExcel.xlsx")
mada_2004_precip_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Pre_2004_TableToExcel.xlsx")
mada_2005_precip_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Pre_2005_TableToExcel.xlsx")
mada_2006_precip_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Pre_2006_TableToExcel.xlsx")
mada_2007_precip_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Pre_2007_TableToExcel.xlsx")
mada_2008_precip_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Pre_2008_TableToExcel.xlsx")
mada_2009_precip_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Pre_2009_TableToExcel.xlsx")
mada_2010_precip_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Pre_2010_TableToExcel.xlsx")
mada_2011_precip_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Pre_2011_TableToExcel.xlsx")
mada_2012_precip_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Pre_2012_TableToExcel.xlsx")
mada_2013_precip_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Pre_2013_TableToExcel.xlsx")
mada_2014_precip_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Pre_2014_TableToExcel.xlsx")
mada_2015_precip_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Pre_2015_TableToExcel.xlsx")
mada_2016_precip_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Pre_2016_TableToExcel.xlsx")
mada_2017_precip_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Pre_2017_TableToExcel.xlsx")
mada_2018_precip_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Pre_2018_TableToExcel.xlsx")
mada_2019_precip_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Pre_2019_TableToExcel.xlsx")
mada_2020_precip_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Pre_2020_TableToExcel.xlsx")
mada_2021_precip_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Pre_2021_TableToExcel.xlsx")

mada_2000_precip_buffer_commune_3km$Year <- 2000
mada_2001_precip_buffer_commune_3km$Year <- 2001
mada_2002_precip_buffer_commune_3km$Year <- 2002
mada_2003_precip_buffer_commune_3km$Year <- 2003
mada_2004_precip_buffer_commune_3km$Year <- 2004
mada_2005_precip_buffer_commune_3km$Year <- 2005
mada_2006_precip_buffer_commune_3km$Year <- 2006
mada_2007_precip_buffer_commune_3km$Year <- 2007
mada_2008_precip_buffer_commune_3km$Year <- 2008
mada_2009_precip_buffer_commune_3km$Year <- 2009
mada_2010_precip_buffer_commune_3km$Year <- 2010
mada_2011_precip_buffer_commune_3km$Year <- 2011
mada_2012_precip_buffer_commune_3km$Year <- 2012
mada_2013_precip_buffer_commune_3km$Year <- 2013
mada_2014_precip_buffer_commune_3km$Year <- 2014
mada_2015_precip_buffer_commune_3km$Year <- 2015
mada_2016_precip_buffer_commune_3km$Year <- 2016
mada_2017_precip_buffer_commune_3km$Year <- 2017
mada_2018_precip_buffer_commune_3km$Year <- 2018
mada_2019_precip_buffer_commune_3km$Year <- 2019
mada_2020_precip_buffer_commune_3km$Year <- 2020
mada_2021_precip_buffer_commune_3km$Year <- 2021

mada_precip_buffer_commune_3km <- rbind(mada_2000_precip_buffer_commune_3km,
                                    mada_2001_precip_buffer_commune_3km, 
                                    mada_2002_precip_buffer_commune_3km, 
                                    mada_2003_precip_buffer_commune_3km, 
                                    mada_2004_precip_buffer_commune_3km, 
                                    mada_2005_precip_buffer_commune_3km, 
                                    mada_2006_precip_buffer_commune_3km, 
                                    mada_2007_precip_buffer_commune_3km, 
                                    mada_2008_precip_buffer_commune_3km, 
                                    mada_2009_precip_buffer_commune_3km, 
                                    mada_2010_precip_buffer_commune_3km, 
                                    mada_2011_precip_buffer_commune_3km, 
                                    mada_2012_precip_buffer_commune_3km, 
                                    mada_2013_precip_buffer_commune_3km, 
                                    mada_2014_precip_buffer_commune_3km, 
                                    mada_2015_precip_buffer_commune_3km, 
                                    mada_2016_precip_buffer_commune_3km, 
                                    mada_2017_precip_buffer_commune_3km,
                                    mada_2018_precip_buffer_commune_3km,
                                    mada_2019_precip_buffer_commune_3km,
                                    mada_2020_precip_buffer_commune_3km,
                                    mada_2021_precip_buffer_commune_3km)

mada_precip_buffer_commune_3km$Field[mada_precip_buffer_commune_3km$Field=="Bemaraha"]<- "Bemaraha (combined)"
mada_precip_buffer_commune_3km$Field[mada_precip_buffer_commune_3km$Field=="Baie de Baly/Tsingy de Namoroka"]<- "Namoroka/Baie de Baly"
mada_precip_buffer_commune_3km$Field[mada_precip_buffer_commune_3km$Field=="Mantadia"]<- "Andasibe/Analamazaotra"


mada_precip_buffer_commune_3km <- mada_precip_buffer_commune_3km %>% rename("Name"="Field")
mada_precip_buffer_commune_3km2 <- mada_precip_buffer_commune_3km %>% dplyr::select(Name,MEAN,Year)
mada_precip_buffer_commune_3km2 <- mada_precip_buffer_commune_3km2 %>% rename("Precipitation" = "MEAN")

write.csv(mada_precip_buffer_commune_3km, "~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_3km/Precip_Buffer_Commune_3km.csv")


####BUFFER DEFORESTATION

deforestation_dat1990_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_3km/ZonalSt_Buffer_3km_forest_Laborde_1990_TableToExcel.xlsx")
deforestation_dat2000_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_3km/ZonalSt_3km_Buffer_Forest_Laborde_2000_TableToExcel.xlsx")
deforestation_dat2001_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_3km/ZonalSt_3km_Buffer_Forest_Laborde_2001_TableToExcel.xlsx")
deforestation_dat2002_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_3km/ZonalSt_3km_Buffer_Forest_Laborde_2002_TableToExcel.xlsx")
deforestation_dat2003_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_3km/ZonalSt_3km_Buffer_Forest_Laborde_2003_TableToExcel.xlsx")
deforestation_dat2004_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_3km/ZonalSt_3km_Buffer_Forest_Laborde_2004_TableToExcel.xlsx")
deforestation_dat2005_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_3km/ZonalSt_3km_Buffer_Forest_Laborde_2005_TableToExcel.xlsx")
deforestation_dat2006_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_3km/ZonalSt_3km_Buffer_Forest_Laborde_2006_TableToExcel.xlsx")
deforestation_dat2007_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_3km/ZonalSt_3km_Buffer_Forest_Laborde_2007_TableToExcel.xlsx")
deforestation_dat2008_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_3km/ZonalSt_3km_Buffer_Forest_Laborde_2008_TableToExcel.xlsx")
deforestation_dat2009_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_3km/ZonalSt_3km_Buffer_Forest_Laborde_2009_TableToExcel.xlsx")
deforestation_dat2010_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_3km/ZonalSt_3km_Buffer_Forest_Laborde_2010_TableToExcel.xlsx")
deforestation_dat2011_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_3km/ZonalSt_3km_Buffer_Forest_Laborde_2011_TableToExcel.xlsx")
deforestation_dat2012_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_3km/ZonalSt_3km_Buffer_Forest_Laborde_2012_TableToExcel.xlsx")
deforestation_dat2013_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_3km/ZonalSt_3km_Buffer_Forest_Laborde_2013_TableToExcel.xlsx")
deforestation_dat2014_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_3km/ZonalSt_3km_Buffer_Forest_Laborde_2014_TableToExcel.xlsx")
deforestation_dat2015_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_3km/ZonalSt_3km_Buffer_Forest_Laborde_2015_TableToExcel.xlsx")
deforestation_dat2016_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_3km/ZonalSt_3km_Buffer_Forest_Laborde_2016_TableToExcel.xlsx")
deforestation_dat2017_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_3km/ZonalSt_3km_Buffer_Forest_Laborde_2017_TableToExcel.xlsx")
deforestation_dat2018_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_3km/ZonalSt_3km_Buffer_Forest_Laborde_2018_TableToExcel.xlsx")
deforestation_dat2019_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_3km/ZonalSt_3km_Buffer_Forest_Laborde_2019_TableToExcel.xlsx")
deforestation_dat2020_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_3km/ZonalSt_3km_Buffer_Forest_Laborde_2020_TableToExcel.xlsx")
deforestation_dat2021_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_3km/ZonalSt_3km_Buffer_Forest_Laborde_2021_TableToExcel.xlsx")

deforestation_dat1990_buffer_3km$Year <- "fc1990"
deforestation_dat2000_buffer_3km$Year <- "fc2000"
deforestation_dat2001_buffer_3km$Year <- "fc2001"
deforestation_dat2002_buffer_3km$Year <- "fc2002"
deforestation_dat2003_buffer_3km$Year <- "fc2003"
deforestation_dat2004_buffer_3km$Year <- "fc2004"
deforestation_dat2005_buffer_3km$Year <- "fc2005"
deforestation_dat2006_buffer_3km$Year <- "fc2006"
deforestation_dat2007_buffer_3km$Year <- "fc2007"
deforestation_dat2008_buffer_3km$Year <- "fc2008"
deforestation_dat2009_buffer_3km$Year <- "fc2009"
deforestation_dat2010_buffer_3km$Year <- "fc2010"
deforestation_dat2011_buffer_3km$Year <- "fc2011"
deforestation_dat2012_buffer_3km$Year <- "fc2012"
deforestation_dat2013_buffer_3km$Year <- "fc2013"
deforestation_dat2014_buffer_3km$Year <- "fc2014"
deforestation_dat2015_buffer_3km$Year <- "fc2015"
deforestation_dat2016_buffer_3km$Year <- "fc2016"
deforestation_dat2017_buffer_3km$Year <- "fc2017"
deforestation_dat2018_buffer_3km$Year <- "fc2018"
deforestation_dat2019_buffer_3km$Year <- "fc2019"
deforestation_dat2020_buffer_3km$Year <- "fc2020"
deforestation_dat2021_buffer_3km$Year <- "fc2021"

deforestation_dat2000_buffer_3km <- deforestation_dat2000_buffer_3km %>% dplyr::select(OBJECTID, Field, ZONE_CODE, COUNT, AREA, MIN, MAX, RANGE, MEAN, STD, SUM, Year)
deforestation_dat1990_buffer_3km <- deforestation_dat1990_buffer_3km %>% dplyr::select(OBJECTID, Field, ZONE_CODE, COUNT, AREA, MIN, MAX, RANGE, MEAN, STD, SUM, Year)


deforestation_dat_buffer_3km <- rbind(deforestation_dat1990_buffer_3km, deforestation_dat2000_buffer_3km,
                                  deforestation_dat2001_buffer_3km,
                                  deforestation_dat2002_buffer_3km,
                                  deforestation_dat2003_buffer_3km,
                                  deforestation_dat2004_buffer_3km,
                                  deforestation_dat2005_buffer_3km,
                                  deforestation_dat2006_buffer_3km,
                                  deforestation_dat2007_buffer_3km,
                                  deforestation_dat2008_buffer_3km,
                                  deforestation_dat2009_buffer_3km,
                                  deforestation_dat2010_buffer_3km,
                                  deforestation_dat2011_buffer_3km,
                                  deforestation_dat2012_buffer_3km,
                                  deforestation_dat2013_buffer_3km,
                                  deforestation_dat2014_buffer_3km,
                                  deforestation_dat2015_buffer_3km,
                                  deforestation_dat2016_buffer_3km,
                                  deforestation_dat2017_buffer_3km,
                                  deforestation_dat2018_buffer_3km,
                                  deforestation_dat2019_buffer_3km,
                                  deforestation_dat2020_buffer_3km,
                                  deforestation_dat2021_buffer_3km)

deforestation_dat2_buffer_3km <- deforestation_dat_buffer_3km%>%  dplyr::select(Field,SUM, Year) %>% pivot_wider(names_from = "Year", values_from= "SUM")

deforestation_dat2_buffer_3km$flp2001 <- 100*(deforestation_dat2_buffer_3km$fc2000 - deforestation_dat2_buffer_3km$fc2001)/ deforestation_dat2_buffer_3km$fc2000
deforestation_dat2_buffer_3km$flp2002 <- 100*(deforestation_dat2_buffer_3km$fc2001 - deforestation_dat2_buffer_3km$fc2002)/ deforestation_dat2_buffer_3km$fc2000
deforestation_dat2_buffer_3km$flp2003 <- 100*(deforestation_dat2_buffer_3km$fc2002 - deforestation_dat2_buffer_3km$fc2003)/ deforestation_dat2_buffer_3km$fc2000
deforestation_dat2_buffer_3km$flp2004 <- 100*(deforestation_dat2_buffer_3km$fc2003 - deforestation_dat2_buffer_3km$fc2004)/ deforestation_dat2_buffer_3km$fc2000
deforestation_dat2_buffer_3km$flp2005 <- 100*(deforestation_dat2_buffer_3km$fc2004 - deforestation_dat2_buffer_3km$fc2005)/ deforestation_dat2_buffer_3km$fc2000
deforestation_dat2_buffer_3km$flp2006 <- 100*(deforestation_dat2_buffer_3km$fc2005 - deforestation_dat2_buffer_3km$fc2006)/ deforestation_dat2_buffer_3km$fc2000
deforestation_dat2_buffer_3km$flp2007 <- 100*(deforestation_dat2_buffer_3km$fc2006 - deforestation_dat2_buffer_3km$fc2007)/ deforestation_dat2_buffer_3km$fc2000
deforestation_dat2_buffer_3km$flp2008 <- 100*(deforestation_dat2_buffer_3km$fc2007 - deforestation_dat2_buffer_3km$fc2008)/ deforestation_dat2_buffer_3km$fc2000
deforestation_dat2_buffer_3km$flp2009 <- 100*(deforestation_dat2_buffer_3km$fc2008 - deforestation_dat2_buffer_3km$fc2009)/ deforestation_dat2_buffer_3km$fc2000
deforestation_dat2_buffer_3km$flp2010 <- 100*(deforestation_dat2_buffer_3km$fc2009 - deforestation_dat2_buffer_3km$fc2010)/ deforestation_dat2_buffer_3km$fc2000
deforestation_dat2_buffer_3km$flp2011 <- 100*(deforestation_dat2_buffer_3km$fc2010 - deforestation_dat2_buffer_3km$fc2011)/ deforestation_dat2_buffer_3km$fc2000
deforestation_dat2_buffer_3km$flp2012 <- 100*(deforestation_dat2_buffer_3km$fc2011 - deforestation_dat2_buffer_3km$fc2012)/ deforestation_dat2_buffer_3km$fc2000
deforestation_dat2_buffer_3km$flp2013 <- 100*(deforestation_dat2_buffer_3km$fc2012 - deforestation_dat2_buffer_3km$fc2013)/ deforestation_dat2_buffer_3km$fc2000
deforestation_dat2_buffer_3km$flp2014 <- 100*(deforestation_dat2_buffer_3km$fc2013 - deforestation_dat2_buffer_3km$fc2014)/ deforestation_dat2_buffer_3km$fc2000
deforestation_dat2_buffer_3km$flp2015 <- 100*(deforestation_dat2_buffer_3km$fc2014 - deforestation_dat2_buffer_3km$fc2015)/ deforestation_dat2_buffer_3km$fc2000
deforestation_dat2_buffer_3km$flp2016 <- 100*(deforestation_dat2_buffer_3km$fc2015 - deforestation_dat2_buffer_3km$fc2016)/ deforestation_dat2_buffer_3km$fc2000
deforestation_dat2_buffer_3km$flp2017 <- 100*(deforestation_dat2_buffer_3km$fc2016 - deforestation_dat2_buffer_3km$fc2017)/ deforestation_dat2_buffer_3km$fc2000
deforestation_dat2_buffer_3km$flp2018 <- 100*(deforestation_dat2_buffer_3km$fc2017 - deforestation_dat2_buffer_3km$fc2018)/ deforestation_dat2_buffer_3km$fc2000
deforestation_dat2_buffer_3km$flp2019 <- 100*(deforestation_dat2_buffer_3km$fc2018 - deforestation_dat2_buffer_3km$fc2019)/ deforestation_dat2_buffer_3km$fc2000
deforestation_dat2_buffer_3km$flp2020 <- 100*(deforestation_dat2_buffer_3km$fc2019 - deforestation_dat2_buffer_3km$fc2020)/ deforestation_dat2_buffer_3km$fc2000
deforestation_dat2_buffer_3km$flp2021 <- 100*(deforestation_dat2_buffer_3km$fc2020 - deforestation_dat2_buffer_3km$fc2021)/ deforestation_dat2_buffer_3km$fc2000

write.csv(deforestation_dat2_buffer_3km, "~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_buffer_3km/deforestation_dat2_buffer_3km.csv")


deforestation_dat3_buffer_3km <- deforestation_dat2_buffer_3km %>% dplyr::select(Field, flp2001:flp2021) %>% pivot_longer(cols=flp2001:flp2021, names_to="Year", values_to="PercentDeforest") 
deforestation_dat3_buffer_3km$Year <- gsub( "flp", "", deforestation_dat3_buffer_3km$Year)

deforestation_dat3_buffer_3km$Field[deforestation_dat3_buffer_3km$Field=="Bemaraha"]<- "Bemaraha (combined)"
deforestation_dat3_buffer_3km$Field[deforestation_dat3_buffer_3km$Field=="Baie de Baly/Tsingy de Namoroka"]<- "Namoroka/Baie de Baly"
deforestation_dat3_buffer_3km$Field[deforestation_dat3_buffer_3km$Field=="Mantadia"]<- "Andasibe/Analamazaotra"
deforestation_dat3_buffer_3km <- deforestation_dat3_buffer_3km %>% rename("Name"="Field")


write.csv(deforestation_dat3_buffer_3km, "~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_buffer_3km/Forest_buffer_3km_Deforestation.csv")


######COMMUNE DEFORESTATION
deforestation_dat2000_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Forest_Laborde_2000_TableToExcel.xlsx")
deforestation_dat2001_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Forest_Laborde_2001_TableToExcel.xlsx")
deforestation_dat2002_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Forest_Laborde_2002_TableToExcel.xlsx")
deforestation_dat2003_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Forest_Laborde_2003_TableToExcel.xlsx")
deforestation_dat2004_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Forest_Laborde_2004_TableToExcel.xlsx")
deforestation_dat2005_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Forest_Laborde_2005_TableToExcel.xlsx")
deforestation_dat2006_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Forest_Laborde_2006_TableToExcel.xlsx")
deforestation_dat2007_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Forest_Laborde_2007_TableToExcel.xlsx")
deforestation_dat2008_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Forest_Laborde_2008_TableToExcel.xlsx")
deforestation_dat2009_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Forest_Laborde_2009_TableToExcel.xlsx")
deforestation_dat2010_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Forest_Laborde_2010_TableToExcel.xlsx")
deforestation_dat2011_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Forest_Laborde_2011_TableToExcel.xlsx")
deforestation_dat2012_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Forest_Laborde_2012_TableToExcel.xlsx")
deforestation_dat2013_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Forest_Laborde_2013_TableToExcel.xlsx")
deforestation_dat2014_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Forest_Laborde_2014_TableToExcel.xlsx")
deforestation_dat2015_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Forest_Laborde_2015_TableToExcel.xlsx")
deforestation_dat2016_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Forest_Laborde_2016_TableToExcel.xlsx")
deforestation_dat2017_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Forest_Laborde_2017_TableToExcel.xlsx")
deforestation_dat2018_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Forest_Laborde_2018_TableToExcel.xlsx")
deforestation_dat2019_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Forest_Laborde_2019_TableToExcel.xlsx")
deforestation_dat2020_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Forest_Laborde_2020_TableToExcel.xlsx")
deforestation_dat2021_buffer_commune_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_3km/ZonalSt_3km_Buffer_Commune_Forest_Laborde_2021_TableToExcel.xlsx")

deforestation_dat2000_buffer_commune_3km$Year <- "fc2000"
deforestation_dat2001_buffer_commune_3km$Year <- "fc2001"
deforestation_dat2002_buffer_commune_3km$Year <- "fc2002"
deforestation_dat2003_buffer_commune_3km$Year <- "fc2003"
deforestation_dat2004_buffer_commune_3km$Year <- "fc2004"
deforestation_dat2005_buffer_commune_3km$Year <- "fc2005"
deforestation_dat2006_buffer_commune_3km$Year <- "fc2006"
deforestation_dat2007_buffer_commune_3km$Year <- "fc2007"
deforestation_dat2008_buffer_commune_3km$Year <- "fc2008"
deforestation_dat2009_buffer_commune_3km$Year <- "fc2009"
deforestation_dat2010_buffer_commune_3km$Year <- "fc2010"
deforestation_dat2011_buffer_commune_3km$Year <- "fc2011"
deforestation_dat2012_buffer_commune_3km$Year <- "fc2012"
deforestation_dat2013_buffer_commune_3km$Year <- "fc2013"
deforestation_dat2014_buffer_commune_3km$Year <- "fc2014"
deforestation_dat2015_buffer_commune_3km$Year <- "fc2015"
deforestation_dat2016_buffer_commune_3km$Year <- "fc2016"
deforestation_dat2017_buffer_commune_3km$Year <- "fc2017"
deforestation_dat2018_buffer_commune_3km$Year <- "fc2018"
deforestation_dat2019_buffer_commune_3km$Year <- "fc2019"
deforestation_dat2020_buffer_commune_3km$Year <- "fc2020"
deforestation_dat2021_buffer_commune_3km$Year <- "fc2021"

deforestation_dat2000_buffer_commune_3km <- deforestation_dat2000_buffer_commune_3km %>% dplyr::select(OBJECTID, Field, ZONE_CODE, COUNT, AREA, MIN, MAX, RANGE, MEAN, STD, SUM, Year)

deforestation_dat_buffer_commune_3km <- rbind(deforestation_dat2000_buffer_commune_3km,
                                          deforestation_dat2001_buffer_commune_3km,
                                          deforestation_dat2002_buffer_commune_3km,
                                          deforestation_dat2003_buffer_commune_3km,
                                          deforestation_dat2004_buffer_commune_3km,
                                          deforestation_dat2005_buffer_commune_3km,
                                          deforestation_dat2006_buffer_commune_3km,
                                          deforestation_dat2007_buffer_commune_3km,
                                          deforestation_dat2008_buffer_commune_3km,
                                          deforestation_dat2009_buffer_commune_3km,
                                          deforestation_dat2010_buffer_commune_3km,
                                          deforestation_dat2011_buffer_commune_3km,
                                          deforestation_dat2012_buffer_commune_3km,
                                          deforestation_dat2013_buffer_commune_3km,
                                          deforestation_dat2014_buffer_commune_3km,
                                          deforestation_dat2015_buffer_commune_3km,
                                          deforestation_dat2016_buffer_commune_3km,
                                          deforestation_dat2017_buffer_commune_3km,
                                          deforestation_dat2018_buffer_commune_3km,
                                          deforestation_dat2019_buffer_commune_3km,
                                          deforestation_dat2020_buffer_commune_3km,
                                          deforestation_dat2021_buffer_commune_3km)

deforestation_dat2_buffer_commune_3km <- deforestation_dat_buffer_commune_3km%>%  dplyr::select(Field,SUM, Year) %>% pivot_wider(names_from = "Year", values_from= "SUM")

deforestation_dat2_buffer_commune_3km$flp2001 <- 100*(deforestation_dat2_buffer_commune_3km$fc2000 - deforestation_dat2_buffer_commune_3km$fc2001)/ deforestation_dat2_buffer_commune_3km$fc2000
deforestation_dat2_buffer_commune_3km$flp2002 <- 100*(deforestation_dat2_buffer_commune_3km$fc2001 - deforestation_dat2_buffer_commune_3km$fc2002)/ deforestation_dat2_buffer_commune_3km$fc2000
deforestation_dat2_buffer_commune_3km$flp2003 <- 100*(deforestation_dat2_buffer_commune_3km$fc2002 - deforestation_dat2_buffer_commune_3km$fc2003)/ deforestation_dat2_buffer_commune_3km$fc2000
deforestation_dat2_buffer_commune_3km$flp2004 <- 100*(deforestation_dat2_buffer_commune_3km$fc2003 - deforestation_dat2_buffer_commune_3km$fc2004)/ deforestation_dat2_buffer_commune_3km$fc2000
deforestation_dat2_buffer_commune_3km$flp2005 <- 100*(deforestation_dat2_buffer_commune_3km$fc2004 - deforestation_dat2_buffer_commune_3km$fc2005)/ deforestation_dat2_buffer_commune_3km$fc2000
deforestation_dat2_buffer_commune_3km$flp2006 <- 100*(deforestation_dat2_buffer_commune_3km$fc2005 - deforestation_dat2_buffer_commune_3km$fc2006)/ deforestation_dat2_buffer_commune_3km$fc2000
deforestation_dat2_buffer_commune_3km$flp2007 <- 100*(deforestation_dat2_buffer_commune_3km$fc2006 - deforestation_dat2_buffer_commune_3km$fc2007)/ deforestation_dat2_buffer_commune_3km$fc2000
deforestation_dat2_buffer_commune_3km$flp2008 <- 100*(deforestation_dat2_buffer_commune_3km$fc2007 - deforestation_dat2_buffer_commune_3km$fc2008)/ deforestation_dat2_buffer_commune_3km$fc2000
deforestation_dat2_buffer_commune_3km$flp2009 <- 100*(deforestation_dat2_buffer_commune_3km$fc2008 - deforestation_dat2_buffer_commune_3km$fc2009)/ deforestation_dat2_buffer_commune_3km$fc2000
deforestation_dat2_buffer_commune_3km$flp2010 <- 100*(deforestation_dat2_buffer_commune_3km$fc2009 - deforestation_dat2_buffer_commune_3km$fc2010)/ deforestation_dat2_buffer_commune_3km$fc2000
deforestation_dat2_buffer_commune_3km$flp2011 <- 100*(deforestation_dat2_buffer_commune_3km$fc2010 - deforestation_dat2_buffer_commune_3km$fc2011)/ deforestation_dat2_buffer_commune_3km$fc2000
deforestation_dat2_buffer_commune_3km$flp2012 <- 100*(deforestation_dat2_buffer_commune_3km$fc2011 - deforestation_dat2_buffer_commune_3km$fc2012)/ deforestation_dat2_buffer_commune_3km$fc2000
deforestation_dat2_buffer_commune_3km$flp2013 <- 100*(deforestation_dat2_buffer_commune_3km$fc2012 - deforestation_dat2_buffer_commune_3km$fc2013)/ deforestation_dat2_buffer_commune_3km$fc2000
deforestation_dat2_buffer_commune_3km$flp2014 <- 100*(deforestation_dat2_buffer_commune_3km$fc2013 - deforestation_dat2_buffer_commune_3km$fc2014)/ deforestation_dat2_buffer_commune_3km$fc2000
deforestation_dat2_buffer_commune_3km$flp2015 <- 100*(deforestation_dat2_buffer_commune_3km$fc2014 - deforestation_dat2_buffer_commune_3km$fc2015)/ deforestation_dat2_buffer_commune_3km$fc2000
deforestation_dat2_buffer_commune_3km$flp2016 <- 100*(deforestation_dat2_buffer_commune_3km$fc2015 - deforestation_dat2_buffer_commune_3km$fc2016)/ deforestation_dat2_buffer_commune_3km$fc2000
deforestation_dat2_buffer_commune_3km$flp2017 <- 100*(deforestation_dat2_buffer_commune_3km$fc2016 - deforestation_dat2_buffer_commune_3km$fc2017)/ deforestation_dat2_buffer_commune_3km$fc2000
deforestation_dat2_buffer_commune_3km$flp2018 <- 100*(deforestation_dat2_buffer_commune_3km$fc2017 - deforestation_dat2_buffer_commune_3km$fc2018)/ deforestation_dat2_buffer_commune_3km$fc2000
deforestation_dat2_buffer_commune_3km$flp2019 <- 100*(deforestation_dat2_buffer_commune_3km$fc2018 - deforestation_dat2_buffer_commune_3km$fc2019)/ deforestation_dat2_buffer_commune_3km$fc2000
deforestation_dat2_buffer_commune_3km$flp2020 <- 100*(deforestation_dat2_buffer_commune_3km$fc2019 - deforestation_dat2_buffer_commune_3km$fc2020)/ deforestation_dat2_buffer_commune_3km$fc2000
deforestation_dat2_buffer_commune_3km$flp2021 <- 100*(deforestation_dat2_buffer_commune_3km$fc2020 - deforestation_dat2_buffer_commune_3km$fc2021)/ deforestation_dat2_buffer_commune_3km$fc2000


write.csv(deforestation_dat2_buffer_commune_3km, "~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_3km/deforestation_commune_dat2_buffer_3km.csv")

deforestation_dat3_buffer_commune_3km <- deforestation_dat2_buffer_commune_3km %>% dplyr::select(Field, flp2001:flp2021) %>% pivot_longer(cols=flp2001:flp2021, names_to="Year", values_to="PercentDeforest") 
deforestation_dat3_buffer_commune_3km$Year <- gsub( "flp", "", deforestation_dat3_buffer_commune_3km$Year)

deforestation_dat3_buffer_commune_3km$Field[deforestation_dat3_buffer_commune_3km$Field=="Bemaraha"]<- "Bemaraha (combined)"
deforestation_dat3_buffer_commune_3km$Field[deforestation_dat3_buffer_commune_3km$Field=="Baie de Baly/Tsingy de Namoroka"]<- "Namoroka/Baie de Baly"
deforestation_dat3_buffer_commune_3km$Field[deforestation_dat3_buffer_commune_3km$Field=="Mantadia"]<- "Andasibe/Analamazaotra"

deforestation_dat3_buffer_commune_3km <- deforestation_dat3_buffer_commune_3km %>% rename("Name"="Field")

write.csv(deforestation_dat3_buffer_commune_3km, "~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_3km/Forest_Buffer_Commune_Deforestation.csv")


#########MAIN ANALYSIS

master1_full <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/MasterSheet_full.csv")
master1_full <- master1_full %>% dplyr::select(Name, Tourism, Year, PopDensity, funding, funding_by_area, area, OilPrice, Regions, Deflation, OilPriceAdj)
master1_full$Year <- as.character(master1_full$Year)

mada_precip_buffer_3km2$Year <- as.character(mada_precip_buffer_3km2$Year)
master_buffer_3km <- full_join(deforestation_dat3_buffer_3km, master1_full, by=c("Name", "Year"))
master_buffer_3km <- full_join(master_buffer_3km,mada_precip_buffer_3km2, by=c("Name", "Year"))

master_buffer_analysis_3km <- master_buffer_3km
master_buffer_analysis_3km <- master_buffer_analysis_3km  %>% filter(Year >= 2001)
master_buffer_analysis_3km$Tourism <- master_buffer_analysis_3km$Tourism/1000

fem_full_formula_3km <- as.formula("PercentDeforest ~ Tourism + PopDensity + Precipitation + OilPriceAdj | Year + Name | 0 | Regions")
dd_reg_full_3km <- felm(fem_full_formula_3km, data = master_buffer_analysis_3km)
summary_dd_reg_full_3km <- summary(dd_reg_full_3km)

fem_full_formula_3km_precip <- as.formula("PercentDeforest ~ Tourism + PopDensity + OilPriceAdj | Year + Name | 0 | Regions")
dd_reg_full_3km_precip <- felm(fem_full_formula_3km_precip, data = master_buffer_analysis_3km)
summary_dd_reg_full_3km_precip <- summary(dd_reg_full_3km_precip)

write.csv(master_buffer_analysis_3km, "~/Documents/GitHub/TourismDeforestation/Data/master_buffer_analysis_3km.csv")

```

```{r Buffer 6km}


###Precip Compilation

mada_2000_precip_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_6km/ZonalSt_6km_Buffer_Pre_2000_TableToExcel.xlsx")
mada_2001_precip_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_6km/ZonalSt_6km_Buffer_Pre_2001_TableToExcel.xlsx")
mada_2002_precip_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_6km/ZonalSt_6km_Buffer_Pre_2002_TableToExcel.xlsx")
mada_2003_precip_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_6km/ZonalSt_6km_Buffer_Pre_2003_TableToExcel.xlsx")
mada_2004_precip_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_6km/ZonalSt_6km_Buffer_Pre_2004_TableToExcel.xlsx")
mada_2005_precip_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_6km/ZonalSt_6km_Buffer_Pre_2005_TableToExcel.xlsx")
mada_2006_precip_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_6km/ZonalSt_6km_Buffer_Pre_2006_TableToExcel.xlsx")
mada_2007_precip_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_6km/ZonalSt_6km_Buffer_Pre_2007_TableToExcel.xlsx")
mada_2008_precip_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_6km/ZonalSt_6km_Buffer_Pre_2008_TableToExcel.xlsx")
mada_2009_precip_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_6km/ZonalSt_6km_Buffer_Pre_2009_TableToExcel.xlsx")
mada_2010_precip_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_6km/ZonalSt_6km_Buffer_Pre_2010_TableToExcel.xlsx")
mada_2011_precip_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_6km/ZonalSt_6km_Buffer_Pre_2011_TableToExcel.xlsx")
mada_2012_precip_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_6km/ZonalSt_6km_Buffer_Pre_2012_TableToExcel.xlsx")
mada_2013_precip_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_6km/ZonalSt_6km_Buffer_Pre_2013_TableToExcel.xlsx")
mada_2014_precip_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_6km/ZonalSt_6km_Buffer_Pre_2014_TableToExcel.xlsx")
mada_2015_precip_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_6km/ZonalSt_6km_Buffer_Pre_2015_TableToExcel.xlsx")
mada_2016_precip_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_6km/ZonalSt_6km_Buffer_Pre_2016_TableToExcel.xlsx")
mada_2017_precip_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_6km/ZonalSt_6km_Buffer_Pre_2017_TableToExcel.xlsx")
mada_2018_precip_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_6km/ZonalSt_6km_Buffer_Pre_2018_TableToExcel.xlsx")
mada_2019_precip_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_6km/ZonalSt_6km_Buffer_Pre_2019_TableToExcel.xlsx")
mada_2020_precip_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_6km/ZonalSt_6km_Buffer_Pre_2020_TableToExcel.xlsx")
mada_2021_precip_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_6km/ZonalSt_6km_Buffer_Pre_2021_TableToExcel.xlsx")

mada_2000_precip_buffer_6km$Year <- 2000
mada_2001_precip_buffer_6km$Year <- 2001
mada_2002_precip_buffer_6km$Year <- 2002
mada_2003_precip_buffer_6km$Year <- 2003
mada_2004_precip_buffer_6km$Year <- 2004
mada_2005_precip_buffer_6km$Year <- 2005
mada_2006_precip_buffer_6km$Year <- 2006
mada_2007_precip_buffer_6km$Year <- 2007
mada_2008_precip_buffer_6km$Year <- 2008
mada_2009_precip_buffer_6km$Year <- 2009
mada_2010_precip_buffer_6km$Year <- 2010
mada_2011_precip_buffer_6km$Year <- 2011
mada_2012_precip_buffer_6km$Year <- 2012
mada_2013_precip_buffer_6km$Year <- 2013
mada_2014_precip_buffer_6km$Year <- 2014
mada_2015_precip_buffer_6km$Year <- 2015
mada_2016_precip_buffer_6km$Year <- 2016
mada_2017_precip_buffer_6km$Year <- 2017
mada_2018_precip_buffer_6km$Year <- 2018
mada_2019_precip_buffer_6km$Year <- 2019
mada_2020_precip_buffer_6km$Year <- 2020
mada_2021_precip_buffer_6km$Year <- 2021

mada_precip_buffer_6km <- rbind(mada_2000_precip_buffer_6km,
                                mada_2001_precip_buffer_6km, 
                                mada_2002_precip_buffer_6km, 
                                mada_2003_precip_buffer_6km, 
                                mada_2004_precip_buffer_6km, 
                                mada_2005_precip_buffer_6km, 
                                mada_2006_precip_buffer_6km, 
                                mada_2007_precip_buffer_6km, 
                                mada_2008_precip_buffer_6km, 
                                mada_2009_precip_buffer_6km, 
                                mada_2010_precip_buffer_6km, 
                                mada_2011_precip_buffer_6km, 
                                mada_2012_precip_buffer_6km, 
                                mada_2013_precip_buffer_6km, 
                                mada_2014_precip_buffer_6km, 
                                mada_2015_precip_buffer_6km, 
                                mada_2016_precip_buffer_6km, 
                                mada_2017_precip_buffer_6km,
                                mada_2018_precip_buffer_6km,
                                mada_2019_precip_buffer_6km,
                                mada_2020_precip_buffer_6km,
                                mada_2021_precip_buffer_6km)


mada_precip_buffer_6km$Field[mada_precip_buffer_6km$Field=="Bemaraha"]<- "Bemaraha (combined)"
mada_precip_buffer_6km$Field[mada_precip_buffer_6km$Field=="Baie de Baly/Tsingy de Namoroka"]<- "Namoroka/Baie de Baly"
mada_precip_buffer_6km$Field[mada_precip_buffer_6km$Field=="Mantadia"]<- "Andasibe/Analamazaotra"


mada_precip_buffer_6km <- mada_precip_buffer_6km %>% rename("Name"="Field")
mada_precip_buffer_6km2 <- mada_precip_buffer_6km %>% dplyr::select(Name,MEAN,Year)
mada_precip_buffer_6km2 <- mada_precip_buffer_6km2 %>% rename("Precipitation" = "MEAN")


write.csv(mada_precip_buffer_6km, "~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_buffer_6km/Precip_buffer_6km.csv")


###COMMUNE PRECIPITATION
mada_2000_precip_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Pre_2000_TableToExcel.xlsx")
mada_2001_precip_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Pre_2001_TableToExcel.xlsx")
mada_2002_precip_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Pre_2002_TableToExcel.xlsx")
mada_2003_precip_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Pre_2003_TableToExcel.xlsx")
mada_2004_precip_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Pre_2004_TableToExcel.xlsx")
mada_2005_precip_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Pre_2005_TableToExcel.xlsx")
mada_2006_precip_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Pre_2006_TableToExcel.xlsx")
mada_2007_precip_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Pre_2007_TableToExcel.xlsx")
mada_2008_precip_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Pre_2008_TableToExcel.xlsx")
mada_2009_precip_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Pre_2009_TableToExcel.xlsx")
mada_2010_precip_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Pre_2010_TableToExcel.xlsx")
mada_2011_precip_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Pre_2011_TableToExcel.xlsx")
mada_2012_precip_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Pre_2012_TableToExcel.xlsx")
mada_2013_precip_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Pre_2013_TableToExcel.xlsx")
mada_2014_precip_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Pre_2014_TableToExcel.xlsx")
mada_2015_precip_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Pre_2015_TableToExcel.xlsx")
mada_2016_precip_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Pre_2016_TableToExcel.xlsx")
mada_2017_precip_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Pre_2017_TableToExcel.xlsx")
mada_2018_precip_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Pre_2018_TableToExcel.xlsx")
mada_2019_precip_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Pre_2019_TableToExcel.xlsx")
mada_2020_precip_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Pre_2020_TableToExcel.xlsx")
mada_2021_precip_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Pre_2021_TableToExcel.xlsx")

mada_2000_precip_buffer_commune_6km$Year <- 2000
mada_2001_precip_buffer_commune_6km$Year <- 2001
mada_2002_precip_buffer_commune_6km$Year <- 2002
mada_2003_precip_buffer_commune_6km$Year <- 2003
mada_2004_precip_buffer_commune_6km$Year <- 2004
mada_2005_precip_buffer_commune_6km$Year <- 2005
mada_2006_precip_buffer_commune_6km$Year <- 2006
mada_2007_precip_buffer_commune_6km$Year <- 2007
mada_2008_precip_buffer_commune_6km$Year <- 2008
mada_2009_precip_buffer_commune_6km$Year <- 2009
mada_2010_precip_buffer_commune_6km$Year <- 2010
mada_2011_precip_buffer_commune_6km$Year <- 2011
mada_2012_precip_buffer_commune_6km$Year <- 2012
mada_2013_precip_buffer_commune_6km$Year <- 2013
mada_2014_precip_buffer_commune_6km$Year <- 2014
mada_2015_precip_buffer_commune_6km$Year <- 2015
mada_2016_precip_buffer_commune_6km$Year <- 2016
mada_2017_precip_buffer_commune_6km$Year <- 2017
mada_2018_precip_buffer_commune_6km$Year <- 2018
mada_2019_precip_buffer_commune_6km$Year <- 2019
mada_2020_precip_buffer_commune_6km$Year <- 2020
mada_2021_precip_buffer_commune_6km$Year <- 2021

mada_precip_buffer_commune_6km <- rbind(mada_2000_precip_buffer_commune_6km,
                                        mada_2001_precip_buffer_commune_6km, 
                                        mada_2002_precip_buffer_commune_6km, 
                                        mada_2003_precip_buffer_commune_6km, 
                                        mada_2004_precip_buffer_commune_6km, 
                                        mada_2005_precip_buffer_commune_6km, 
                                        mada_2006_precip_buffer_commune_6km, 
                                        mada_2007_precip_buffer_commune_6km, 
                                        mada_2008_precip_buffer_commune_6km, 
                                        mada_2009_precip_buffer_commune_6km, 
                                        mada_2010_precip_buffer_commune_6km, 
                                        mada_2011_precip_buffer_commune_6km, 
                                        mada_2012_precip_buffer_commune_6km, 
                                        mada_2013_precip_buffer_commune_6km, 
                                        mada_2014_precip_buffer_commune_6km, 
                                        mada_2015_precip_buffer_commune_6km, 
                                        mada_2016_precip_buffer_commune_6km, 
                                        mada_2017_precip_buffer_commune_6km,
                                        mada_2018_precip_buffer_commune_6km,
                                        mada_2019_precip_buffer_commune_6km,
                                        mada_2020_precip_buffer_commune_6km,
                                        mada_2021_precip_buffer_commune_6km)

mada_precip_buffer_commune_6km$Field[mada_precip_buffer_commune_6km$Field=="Bemaraha"]<- "Bemaraha (combined)"
mada_precip_buffer_commune_6km$Field[mada_precip_buffer_commune_6km$Field=="Baie de Baly/Tsingy de Namoroka"]<- "Namoroka/Baie de Baly"
mada_precip_buffer_commune_6km$Field[mada_precip_buffer_commune_6km$Field=="Mantadia"]<- "Andasibe/Analamazaotra"


mada_precip_buffer_commune_6km <- mada_precip_buffer_commune_6km %>% rename("Name"="Field")
mada_precip_buffer_commune_6km2 <- mada_precip_buffer_commune_6km %>% dplyr::select(Name,MEAN,Year)
mada_precip_buffer_commune_6km2 <- mada_precip_buffer_commune_6km2 %>% rename("Precipitation" = "MEAN")

write.csv(mada_precip_buffer_commune_6km, "~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_6km/Precip_Buffer_Commune.csv")


####BUFFER DEFORESTATION
deforestation_dat2000_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_6km/ZonalSt_6km_Buffer_Forest_Laborde_2000_TableToExcel.xlsx")
deforestation_dat2001_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_6km/ZonalSt_6km_Buffer_Forest_Laborde_2001_TableToExcel.xlsx")
deforestation_dat2002_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_6km/ZonalSt_6km_Buffer_Forest_Laborde_2002_TableToExcel.xlsx")
deforestation_dat2003_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_6km/ZonalSt_6km_Buffer_Forest_Laborde_2003_TableToExcel.xlsx")
deforestation_dat2004_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_6km/ZonalSt_6km_Buffer_Forest_Laborde_2004_TableToExcel.xlsx")
deforestation_dat2005_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_6km/ZonalSt_6km_Buffer_Forest_Laborde_2005_TableToExcel.xlsx")
deforestation_dat2006_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_6km/ZonalSt_6km_Buffer_Forest_Laborde_2006_TableToExcel.xlsx")
deforestation_dat2007_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_6km/ZonalSt_6km_Buffer_Forest_Laborde_2007_TableToExcel.xlsx")
deforestation_dat2008_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_6km/ZonalSt_6km_Buffer_Forest_Laborde_2008_TableToExcel.xlsx")
deforestation_dat2009_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_6km/ZonalSt_6km_Buffer_Forest_Laborde_2009_TableToExcel.xlsx")
deforestation_dat2010_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_6km/ZonalSt_6km_Buffer_Forest_Laborde_2010_TableToExcel.xlsx")
deforestation_dat2011_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_6km/ZonalSt_6km_Buffer_Forest_Laborde_2011_TableToExcel.xlsx")
deforestation_dat2012_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_6km/ZonalSt_6km_Buffer_Forest_Laborde_2012_TableToExcel.xlsx")
deforestation_dat2013_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_6km/ZonalSt_6km_Buffer_Forest_Laborde_2013_TableToExcel.xlsx")
deforestation_dat2014_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_6km/ZonalSt_6km_Buffer_Forest_Laborde_2014_TableToExcel.xlsx")
deforestation_dat2015_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_6km/ZonalSt_6km_Buffer_Forest_Laborde_2015_TableToExcel.xlsx")
deforestation_dat2016_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_6km/ZonalSt_6km_Buffer_Forest_Laborde_2016_TableToExcel.xlsx")
deforestation_dat2017_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_6km/ZonalSt_6km_Buffer_Forest_Laborde_2017_TableToExcel.xlsx")
deforestation_dat2018_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_6km/ZonalSt_6km_Buffer_Forest_Laborde_2018_TableToExcel.xlsx")
deforestation_dat2019_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_6km/ZonalSt_6km_Buffer_Forest_Laborde_2019_TableToExcel.xlsx")
deforestation_dat2020_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_6km/ZonalSt_6km_Buffer_Forest_Laborde_2020_TableToExcel.xlsx")
deforestation_dat2021_buffer_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_6km/ZonalSt_6km_Buffer_Forest_Laborde_2021_TableToExcel.xlsx")


deforestation_dat2000_buffer_6km$Year <- "fc2000"
deforestation_dat2001_buffer_6km$Year <- "fc2001"
deforestation_dat2002_buffer_6km$Year <- "fc2002"
deforestation_dat2003_buffer_6km$Year <- "fc2003"
deforestation_dat2004_buffer_6km$Year <- "fc2004"
deforestation_dat2005_buffer_6km$Year <- "fc2005"
deforestation_dat2006_buffer_6km$Year <- "fc2006"
deforestation_dat2007_buffer_6km$Year <- "fc2007"
deforestation_dat2008_buffer_6km$Year <- "fc2008"
deforestation_dat2009_buffer_6km$Year <- "fc2009"
deforestation_dat2010_buffer_6km$Year <- "fc2010"
deforestation_dat2011_buffer_6km$Year <- "fc2011"
deforestation_dat2012_buffer_6km$Year <- "fc2012"
deforestation_dat2013_buffer_6km$Year <- "fc2013"
deforestation_dat2014_buffer_6km$Year <- "fc2014"
deforestation_dat2015_buffer_6km$Year <- "fc2015"
deforestation_dat2016_buffer_6km$Year <- "fc2016"
deforestation_dat2017_buffer_6km$Year <- "fc2017"
deforestation_dat2018_buffer_6km$Year <- "fc2018"
deforestation_dat2019_buffer_6km$Year <- "fc2019"
deforestation_dat2020_buffer_6km$Year <- "fc2020"
deforestation_dat2021_buffer_6km$Year <- "fc2021"

deforestation_dat2000_buffer_6km <- deforestation_dat2000_buffer_6km %>% dplyr::select(OBJECTID, Field, ZONE_CODE, COUNT, AREA, MIN, MAX, RANGE, MEAN, STD, SUM, Year)

deforestation_dat_buffer_6km <- rbind(deforestation_dat2000_buffer_6km,
                                      deforestation_dat2001_buffer_6km,
                                      deforestation_dat2002_buffer_6km,
                                      deforestation_dat2003_buffer_6km,
                                      deforestation_dat2004_buffer_6km,
                                      deforestation_dat2005_buffer_6km,
                                      deforestation_dat2006_buffer_6km,
                                      deforestation_dat2007_buffer_6km,
                                      deforestation_dat2008_buffer_6km,
                                      deforestation_dat2009_buffer_6km,
                                      deforestation_dat2010_buffer_6km,
                                      deforestation_dat2011_buffer_6km,
                                      deforestation_dat2012_buffer_6km,
                                      deforestation_dat2013_buffer_6km,
                                      deforestation_dat2014_buffer_6km,
                                      deforestation_dat2015_buffer_6km,
                                      deforestation_dat2016_buffer_6km,
                                      deforestation_dat2017_buffer_6km,
                                      deforestation_dat2018_buffer_6km,
                                      deforestation_dat2019_buffer_6km,
                                      deforestation_dat2020_buffer_6km,
                                      deforestation_dat2021_buffer_6km)

deforestation_dat2_buffer_6km <- deforestation_dat_buffer_6km%>%  dplyr::select(Field,SUM, Year) %>% pivot_wider(names_from = "Year", values_from= "SUM")

deforestation_dat2_buffer_6km$flp2001 <- 100*(deforestation_dat2_buffer_6km$fc2000 - deforestation_dat2_buffer_6km$fc2001)/ deforestation_dat2_buffer_6km$fc2000
deforestation_dat2_buffer_6km$flp2002 <- 100*(deforestation_dat2_buffer_6km$fc2001 - deforestation_dat2_buffer_6km$fc2002)/ deforestation_dat2_buffer_6km$fc2000
deforestation_dat2_buffer_6km$flp2003 <- 100*(deforestation_dat2_buffer_6km$fc2002 - deforestation_dat2_buffer_6km$fc2003)/ deforestation_dat2_buffer_6km$fc2000
deforestation_dat2_buffer_6km$flp2004 <- 100*(deforestation_dat2_buffer_6km$fc2003 - deforestation_dat2_buffer_6km$fc2004)/ deforestation_dat2_buffer_6km$fc2000
deforestation_dat2_buffer_6km$flp2005 <- 100*(deforestation_dat2_buffer_6km$fc2004 - deforestation_dat2_buffer_6km$fc2005)/ deforestation_dat2_buffer_6km$fc2000
deforestation_dat2_buffer_6km$flp2006 <- 100*(deforestation_dat2_buffer_6km$fc2005 - deforestation_dat2_buffer_6km$fc2006)/ deforestation_dat2_buffer_6km$fc2000
deforestation_dat2_buffer_6km$flp2007 <- 100*(deforestation_dat2_buffer_6km$fc2006 - deforestation_dat2_buffer_6km$fc2007)/ deforestation_dat2_buffer_6km$fc2000
deforestation_dat2_buffer_6km$flp2008 <- 100*(deforestation_dat2_buffer_6km$fc2007 - deforestation_dat2_buffer_6km$fc2008)/ deforestation_dat2_buffer_6km$fc2000
deforestation_dat2_buffer_6km$flp2009 <- 100*(deforestation_dat2_buffer_6km$fc2008 - deforestation_dat2_buffer_6km$fc2009)/ deforestation_dat2_buffer_6km$fc2000
deforestation_dat2_buffer_6km$flp2010 <- 100*(deforestation_dat2_buffer_6km$fc2009 - deforestation_dat2_buffer_6km$fc2010)/ deforestation_dat2_buffer_6km$fc2000
deforestation_dat2_buffer_6km$flp2011 <- 100*(deforestation_dat2_buffer_6km$fc2010 - deforestation_dat2_buffer_6km$fc2011)/ deforestation_dat2_buffer_6km$fc2000
deforestation_dat2_buffer_6km$flp2012 <- 100*(deforestation_dat2_buffer_6km$fc2011 - deforestation_dat2_buffer_6km$fc2012)/ deforestation_dat2_buffer_6km$fc2000
deforestation_dat2_buffer_6km$flp2013 <- 100*(deforestation_dat2_buffer_6km$fc2012 - deforestation_dat2_buffer_6km$fc2013)/ deforestation_dat2_buffer_6km$fc2000
deforestation_dat2_buffer_6km$flp2014 <- 100*(deforestation_dat2_buffer_6km$fc2013 - deforestation_dat2_buffer_6km$fc2014)/ deforestation_dat2_buffer_6km$fc2000
deforestation_dat2_buffer_6km$flp2015 <- 100*(deforestation_dat2_buffer_6km$fc2014 - deforestation_dat2_buffer_6km$fc2015)/ deforestation_dat2_buffer_6km$fc2000
deforestation_dat2_buffer_6km$flp2016 <- 100*(deforestation_dat2_buffer_6km$fc2015 - deforestation_dat2_buffer_6km$fc2016)/ deforestation_dat2_buffer_6km$fc2000
deforestation_dat2_buffer_6km$flp2017 <- 100*(deforestation_dat2_buffer_6km$fc2016 - deforestation_dat2_buffer_6km$fc2017)/ deforestation_dat2_buffer_6km$fc2000
deforestation_dat2_buffer_6km$flp2018 <- 100*(deforestation_dat2_buffer_6km$fc2017 - deforestation_dat2_buffer_6km$fc2018)/ deforestation_dat2_buffer_6km$fc2000
deforestation_dat2_buffer_6km$flp2019 <- 100*(deforestation_dat2_buffer_6km$fc2018 - deforestation_dat2_buffer_6km$fc2019)/ deforestation_dat2_buffer_6km$fc2000
deforestation_dat2_buffer_6km$flp2020 <- 100*(deforestation_dat2_buffer_6km$fc2019 - deforestation_dat2_buffer_6km$fc2020)/ deforestation_dat2_buffer_6km$fc2000
deforestation_dat2_buffer_6km$flp2021 <- 100*(deforestation_dat2_buffer_6km$fc2020 - deforestation_dat2_buffer_6km$fc2021)/ deforestation_dat2_buffer_6km$fc2000

deforestation_dat3_buffer_6km <- deforestation_dat2_buffer_6km %>% dplyr::select(Field, flp2001:flp2021) %>% pivot_longer(cols=flp2001:flp2021, names_to="Year", values_to="PercentDeforest") 
deforestation_dat3_buffer_6km$Year <- gsub( "flp", "", deforestation_dat3_buffer_6km$Year)

deforestation_dat3_buffer_6km$Field[deforestation_dat3_buffer_6km$Field=="Bemaraha"]<- "Bemaraha (combined)"
deforestation_dat3_buffer_6km$Field[deforestation_dat3_buffer_6km$Field=="Baie de Baly/Tsingy de Namoroka"]<- "Namoroka/Baie de Baly"
deforestation_dat3_buffer_6km$Field[deforestation_dat3_buffer_6km$Field=="Mantadia"]<- "Andasibe/Analamazaotra"
deforestation_dat3_buffer_6km <- deforestation_dat3_buffer_6km %>% rename("Name"="Field")


write.csv(deforestation_dat3_buffer_6km, "~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_buffer_6km/Forest_buffer_6km_Deforestation.csv")


######COMMUNE DEFORESTATION
deforestation_dat2000_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Forest_Laborde_2000_TableToExcel.xlsx")
deforestation_dat2001_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Forest_Laborde_2001_TableToExcel.xlsx")
deforestation_dat2002_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Forest_Laborde_2002_TableToExcel.xlsx")
deforestation_dat2003_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Forest_Laborde_2003_TableToExcel.xlsx")
deforestation_dat2004_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Forest_Laborde_2004_TableToExcel.xlsx")
deforestation_dat2005_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Forest_Laborde_2005_TableToExcel.xlsx")
deforestation_dat2006_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Forest_Laborde_2006_TableToExcel.xlsx")
deforestation_dat2007_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Forest_Laborde_2007_TableToExcel.xlsx")
deforestation_dat2008_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Forest_Laborde_2008_TableToExcel.xlsx")
deforestation_dat2009_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Forest_Laborde_2009_TableToExcel.xlsx")
deforestation_dat2010_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Forest_Laborde_2010_TableToExcel.xlsx")
deforestation_dat2011_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Forest_Laborde_2011_TableToExcel.xlsx")
deforestation_dat2012_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Forest_Laborde_2012_TableToExcel.xlsx")
deforestation_dat2013_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Forest_Laborde_2013_TableToExcel.xlsx")
deforestation_dat2014_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Forest_Laborde_2014_TableToExcel.xlsx")
deforestation_dat2015_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Forest_Laborde_2015_TableToExcel.xlsx")
deforestation_dat2016_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Forest_Laborde_2016_TableToExcel.xlsx")
deforestation_dat2017_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Forest_Laborde_2017_TableToExcel.xlsx")
deforestation_dat2018_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Forest_Laborde_2018_TableToExcel.xlsx")
deforestation_dat2019_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Forest_Laborde_2019_TableToExcel.xlsx")
deforestation_dat2020_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Forest_Laborde_2020_TableToExcel.xlsx")
deforestation_dat2021_buffer_commune_6km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_6km/ZonalSt_6km_Buffer_Commune_Forest_Laborde_2021_TableToExcel.xlsx")

deforestation_dat2000_buffer_commune_6km$Year <- "fc2000"
deforestation_dat2001_buffer_commune_6km$Year <- "fc2001"
deforestation_dat2002_buffer_commune_6km$Year <- "fc2002"
deforestation_dat2003_buffer_commune_6km$Year <- "fc2003"
deforestation_dat2004_buffer_commune_6km$Year <- "fc2004"
deforestation_dat2005_buffer_commune_6km$Year <- "fc2005"
deforestation_dat2006_buffer_commune_6km$Year <- "fc2006"
deforestation_dat2007_buffer_commune_6km$Year <- "fc2007"
deforestation_dat2008_buffer_commune_6km$Year <- "fc2008"
deforestation_dat2009_buffer_commune_6km$Year <- "fc2009"
deforestation_dat2010_buffer_commune_6km$Year <- "fc2010"
deforestation_dat2011_buffer_commune_6km$Year <- "fc2011"
deforestation_dat2012_buffer_commune_6km$Year <- "fc2012"
deforestation_dat2013_buffer_commune_6km$Year <- "fc2013"
deforestation_dat2014_buffer_commune_6km$Year <- "fc2014"
deforestation_dat2015_buffer_commune_6km$Year <- "fc2015"
deforestation_dat2016_buffer_commune_6km$Year <- "fc2016"
deforestation_dat2017_buffer_commune_6km$Year <- "fc2017"
deforestation_dat2018_buffer_commune_6km$Year <- "fc2018"
deforestation_dat2019_buffer_commune_6km$Year <- "fc2019"
deforestation_dat2020_buffer_commune_6km$Year <- "fc2020"
deforestation_dat2021_buffer_commune_6km$Year <- "fc2021"

deforestation_dat2000_buffer_commune_6km <- deforestation_dat2000_buffer_commune_6km %>% dplyr::select(OBJECTID, Field, ZONE_CODE, COUNT, AREA, MIN, MAX, RANGE, MEAN, STD, SUM, Year)

deforestation_dat_buffer_commune_6km <- rbind(deforestation_dat2000_buffer_commune_6km,
                                              deforestation_dat2001_buffer_commune_6km,
                                              deforestation_dat2002_buffer_commune_6km,
                                              deforestation_dat2003_buffer_commune_6km,
                                              deforestation_dat2004_buffer_commune_6km,
                                              deforestation_dat2005_buffer_commune_6km,
                                              deforestation_dat2006_buffer_commune_6km,
                                              deforestation_dat2007_buffer_commune_6km,
                                              deforestation_dat2008_buffer_commune_6km,
                                              deforestation_dat2009_buffer_commune_6km,
                                              deforestation_dat2010_buffer_commune_6km,
                                              deforestation_dat2011_buffer_commune_6km,
                                              deforestation_dat2012_buffer_commune_6km,
                                              deforestation_dat2013_buffer_commune_6km,
                                              deforestation_dat2014_buffer_commune_6km,
                                              deforestation_dat2015_buffer_commune_6km,
                                              deforestation_dat2016_buffer_commune_6km,
                                              deforestation_dat2017_buffer_commune_6km,
                                              deforestation_dat2018_buffer_commune_6km,
                                              deforestation_dat2019_buffer_commune_6km,
                                              deforestation_dat2020_buffer_commune_6km,
                                              deforestation_dat2021_buffer_commune_6km)

deforestation_dat2_buffer_commune_6km <- deforestation_dat_buffer_commune_6km%>%  dplyr::select(Field,SUM, Year) %>% pivot_wider(names_from = "Year", values_from= "SUM")

deforestation_dat2_buffer_commune_6km$flp2001 <- 100*(deforestation_dat2_buffer_commune_6km$fc2000 - deforestation_dat2_buffer_commune_6km$fc2001)/ deforestation_dat2_buffer_commune_6km$fc2000
deforestation_dat2_buffer_commune_6km$flp2002 <- 100*(deforestation_dat2_buffer_commune_6km$fc2001 - deforestation_dat2_buffer_commune_6km$fc2002)/ deforestation_dat2_buffer_commune_6km$fc2000
deforestation_dat2_buffer_commune_6km$flp2003 <- 100*(deforestation_dat2_buffer_commune_6km$fc2002 - deforestation_dat2_buffer_commune_6km$fc2003)/ deforestation_dat2_buffer_commune_6km$fc2000
deforestation_dat2_buffer_commune_6km$flp2004 <- 100*(deforestation_dat2_buffer_commune_6km$fc2003 - deforestation_dat2_buffer_commune_6km$fc2004)/ deforestation_dat2_buffer_commune_6km$fc2000
deforestation_dat2_buffer_commune_6km$flp2005 <- 100*(deforestation_dat2_buffer_commune_6km$fc2004 - deforestation_dat2_buffer_commune_6km$fc2005)/ deforestation_dat2_buffer_commune_6km$fc2000
deforestation_dat2_buffer_commune_6km$flp2006 <- 100*(deforestation_dat2_buffer_commune_6km$fc2005 - deforestation_dat2_buffer_commune_6km$fc2006)/ deforestation_dat2_buffer_commune_6km$fc2000
deforestation_dat2_buffer_commune_6km$flp2007 <- 100*(deforestation_dat2_buffer_commune_6km$fc2006 - deforestation_dat2_buffer_commune_6km$fc2007)/ deforestation_dat2_buffer_commune_6km$fc2000
deforestation_dat2_buffer_commune_6km$flp2008 <- 100*(deforestation_dat2_buffer_commune_6km$fc2007 - deforestation_dat2_buffer_commune_6km$fc2008)/ deforestation_dat2_buffer_commune_6km$fc2000
deforestation_dat2_buffer_commune_6km$flp2009 <- 100*(deforestation_dat2_buffer_commune_6km$fc2008 - deforestation_dat2_buffer_commune_6km$fc2009)/ deforestation_dat2_buffer_commune_6km$fc2000
deforestation_dat2_buffer_commune_6km$flp2010 <- 100*(deforestation_dat2_buffer_commune_6km$fc2009 - deforestation_dat2_buffer_commune_6km$fc2010)/ deforestation_dat2_buffer_commune_6km$fc2000
deforestation_dat2_buffer_commune_6km$flp2011 <- 100*(deforestation_dat2_buffer_commune_6km$fc2010 - deforestation_dat2_buffer_commune_6km$fc2011)/ deforestation_dat2_buffer_commune_6km$fc2000
deforestation_dat2_buffer_commune_6km$flp2012 <- 100*(deforestation_dat2_buffer_commune_6km$fc2011 - deforestation_dat2_buffer_commune_6km$fc2012)/ deforestation_dat2_buffer_commune_6km$fc2000
deforestation_dat2_buffer_commune_6km$flp2013 <- 100*(deforestation_dat2_buffer_commune_6km$fc2012 - deforestation_dat2_buffer_commune_6km$fc2013)/ deforestation_dat2_buffer_commune_6km$fc2000
deforestation_dat2_buffer_commune_6km$flp2014 <- 100*(deforestation_dat2_buffer_commune_6km$fc2013 - deforestation_dat2_buffer_commune_6km$fc2014)/ deforestation_dat2_buffer_commune_6km$fc2000
deforestation_dat2_buffer_commune_6km$flp2015 <- 100*(deforestation_dat2_buffer_commune_6km$fc2014 - deforestation_dat2_buffer_commune_6km$fc2015)/ deforestation_dat2_buffer_commune_6km$fc2000
deforestation_dat2_buffer_commune_6km$flp2016 <- 100*(deforestation_dat2_buffer_commune_6km$fc2015 - deforestation_dat2_buffer_commune_6km$fc2016)/ deforestation_dat2_buffer_commune_6km$fc2000
deforestation_dat2_buffer_commune_6km$flp2017 <- 100*(deforestation_dat2_buffer_commune_6km$fc2016 - deforestation_dat2_buffer_commune_6km$fc2017)/ deforestation_dat2_buffer_commune_6km$fc2000
deforestation_dat2_buffer_commune_6km$flp2018 <- 100*(deforestation_dat2_buffer_commune_6km$fc2017 - deforestation_dat2_buffer_commune_6km$fc2018)/ deforestation_dat2_buffer_commune_6km$fc2000
deforestation_dat2_buffer_commune_6km$flp2019 <- 100*(deforestation_dat2_buffer_commune_6km$fc2018 - deforestation_dat2_buffer_commune_6km$fc2019)/ deforestation_dat2_buffer_commune_6km$fc2000
deforestation_dat2_buffer_commune_6km$flp2020 <- 100*(deforestation_dat2_buffer_commune_6km$fc2019 - deforestation_dat2_buffer_commune_6km$fc2020)/ deforestation_dat2_buffer_commune_6km$fc2000
deforestation_dat2_buffer_commune_6km$flp2021 <- 100*(deforestation_dat2_buffer_commune_6km$fc2020 - deforestation_dat2_buffer_commune_6km$fc2021)/ deforestation_dat2_buffer_commune_6km$fc2000

deforestation_dat3_buffer_commune_6km <- deforestation_dat2_buffer_commune_6km %>% dplyr::select(Field, flp2001:flp2021) %>% pivot_longer(cols=flp2001:flp2021, names_to="Year", values_to="PercentDeforest") 
deforestation_dat3_buffer_commune_6km$Year <- gsub( "flp", "", deforestation_dat3_buffer_commune_6km$Year)

deforestation_dat3_buffer_commune_6km$Field[deforestation_dat3_buffer_commune_6km$Field=="Bemaraha"]<- "Bemaraha (combined)"
deforestation_dat3_buffer_commune_6km$Field[deforestation_dat3_buffer_commune_6km$Field=="Baie de Baly/Tsingy de Namoroka"]<- "Namoroka/Baie de Baly"
deforestation_dat3_buffer_commune_6km$Field[deforestation_dat3_buffer_commune_6km$Field=="Mantadia"]<- "Andasibe/Analamazaotra"

deforestation_dat3_buffer_commune_6km <- deforestation_dat3_buffer_commune_6km %>% rename("Name"="Field")

write.csv(deforestation_dat3_buffer_commune_6km, "~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_6km/Forest_Buffer_Commune_Deforestation.csv")


```

```{r Buffer 9km}

library(tidyverse); library(readxl)


###Precip Compilation

mada_2000_precip_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_9km/ZonalSt_9km_Buffer_Pre_2000_TableToExcel.xlsx")
mada_2001_precip_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_9km/ZonalSt_9km_Buffer_Pre_2001_TableToExcel.xlsx")
mada_2002_precip_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_9km/ZonalSt_9km_Buffer_Pre_2002_TableToExcel.xlsx")
mada_2003_precip_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_9km/ZonalSt_9km_Buffer_Pre_2003_TableToExcel.xlsx")
mada_2004_precip_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_9km/ZonalSt_9km_Buffer_Pre_2004_TableToExcel.xlsx")
mada_2005_precip_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_9km/ZonalSt_9km_Buffer_Pre_2005_TableToExcel.xlsx")
mada_2006_precip_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_9km/ZonalSt_9km_Buffer_Pre_2006_TableToExcel.xlsx")
mada_2007_precip_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_9km/ZonalSt_9km_Buffer_Pre_2007_TableToExcel.xlsx")
mada_2008_precip_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_9km/ZonalSt_9km_Buffer_Pre_2008_TableToExcel.xlsx")
mada_2009_precip_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_9km/ZonalSt_9km_Buffer_Pre_2009_TableToExcel.xlsx")
mada_2010_precip_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_9km/ZonalSt_9km_Buffer_Pre_2010_TableToExcel.xlsx")
mada_2011_precip_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_9km/ZonalSt_9km_Buffer_Pre_2011_TableToExcel.xlsx")
mada_2012_precip_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_9km/ZonalSt_9km_Buffer_Pre_2012_TableToExcel.xlsx")
mada_2013_precip_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_9km/ZonalSt_9km_Buffer_Pre_2013_TableToExcel.xlsx")
mada_2014_precip_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_9km/ZonalSt_9km_Buffer_Pre_2014_TableToExcel.xlsx")
mada_2015_precip_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_9km/ZonalSt_9km_Buffer_Pre_2015_TableToExcel.xlsx")
mada_2016_precip_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_9km/ZonalSt_9km_Buffer_Pre_2016_TableToExcel.xlsx")
mada_2017_precip_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_9km/ZonalSt_9km_Buffer_Pre_2017_TableToExcel.xlsx")
mada_2018_precip_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_9km/ZonalSt_9km_Buffer_Pre_2018_TableToExcel.xlsx")
mada_2019_precip_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_9km/ZonalSt_9km_Buffer_Pre_2019_TableToExcel.xlsx")
mada_2020_precip_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_9km/ZonalSt_9km_Buffer_Pre_2020_TableToExcel.xlsx")
mada_2021_precip_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_9km/ZonalSt_9km_Buffer_Pre_2021_TableToExcel.xlsx")

mada_2000_precip_buffer_9km$Year <- 2000
mada_2001_precip_buffer_9km$Year <- 2001
mada_2002_precip_buffer_9km$Year <- 2002
mada_2003_precip_buffer_9km$Year <- 2003
mada_2004_precip_buffer_9km$Year <- 2004
mada_2005_precip_buffer_9km$Year <- 2005
mada_2006_precip_buffer_9km$Year <- 2006
mada_2007_precip_buffer_9km$Year <- 2007
mada_2008_precip_buffer_9km$Year <- 2008
mada_2009_precip_buffer_9km$Year <- 2009
mada_2010_precip_buffer_9km$Year <- 2010
mada_2011_precip_buffer_9km$Year <- 2011
mada_2012_precip_buffer_9km$Year <- 2012
mada_2013_precip_buffer_9km$Year <- 2013
mada_2014_precip_buffer_9km$Year <- 2014
mada_2015_precip_buffer_9km$Year <- 2015
mada_2016_precip_buffer_9km$Year <- 2016
mada_2017_precip_buffer_9km$Year <- 2017
mada_2018_precip_buffer_9km$Year <- 2018
mada_2019_precip_buffer_9km$Year <- 2019
mada_2020_precip_buffer_9km$Year <- 2020
mada_2021_precip_buffer_9km$Year <- 2021

mada_precip_buffer_9km <- rbind(mada_2000_precip_buffer_9km,
                                mada_2001_precip_buffer_9km, 
                                mada_2002_precip_buffer_9km, 
                                mada_2003_precip_buffer_9km, 
                                mada_2004_precip_buffer_9km, 
                                mada_2005_precip_buffer_9km, 
                                mada_2006_precip_buffer_9km, 
                                mada_2007_precip_buffer_9km, 
                                mada_2008_precip_buffer_9km, 
                                mada_2009_precip_buffer_9km, 
                                mada_2010_precip_buffer_9km, 
                                mada_2011_precip_buffer_9km, 
                                mada_2012_precip_buffer_9km, 
                                mada_2013_precip_buffer_9km, 
                                mada_2014_precip_buffer_9km, 
                                mada_2015_precip_buffer_9km, 
                                mada_2016_precip_buffer_9km, 
                                mada_2017_precip_buffer_9km,
                                mada_2018_precip_buffer_9km,
                                mada_2019_precip_buffer_9km,
                                mada_2020_precip_buffer_9km,
                                mada_2021_precip_buffer_9km)


mada_precip_buffer_9km$Field[mada_precip_buffer_9km$Field=="Bemaraha"]<- "Bemaraha (combined)"
mada_precip_buffer_9km$Field[mada_precip_buffer_9km$Field=="Baie de Baly/Tsingy de Namoroka"]<- "Namoroka/Baie de Baly"
mada_precip_buffer_9km$Field[mada_precip_buffer_9km$Field=="Mantadia"]<- "Andasibe/Analamazaotra"


mada_precip_buffer_9km <- mada_precip_buffer_9km %>% rename("Name"="Field")
mada_precip_buffer_9km2 <- mada_precip_buffer_9km %>% dplyr::select(Name,MEAN,Year)
mada_precip_buffer_9km2 <- mada_precip_buffer_9km2 %>% rename("Precipitation" = "MEAN")


write.csv(mada_precip_buffer_9km, "~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_buffer_9km/Precip_buffer_9km.csv")


###COMMUNE PRECIPITATION
mada_2000_precip_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Pre_2000_TableToExcel.xlsx")
mada_2001_precip_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Pre_2001_TableToExcel.xlsx")
mada_2002_precip_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Pre_2002_TableToExcel.xlsx")
mada_2003_precip_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Pre_2003_TableToExcel.xlsx")
mada_2004_precip_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Pre_2004_TableToExcel.xlsx")
mada_2005_precip_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Pre_2005_TableToExcel.xlsx")
mada_2006_precip_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Pre_2006_TableToExcel.xlsx")
mada_2007_precip_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Pre_2007_TableToExcel.xlsx")
mada_2008_precip_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Pre_2008_TableToExcel.xlsx")
mada_2009_precip_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Pre_2009_TableToExcel.xlsx")
mada_2010_precip_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Pre_2010_TableToExcel.xlsx")
mada_2011_precip_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Pre_2011_TableToExcel.xlsx")
mada_2012_precip_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Pre_2012_TableToExcel.xlsx")
mada_2013_precip_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Pre_2013_TableToExcel.xlsx")
mada_2014_precip_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Pre_2014_TableToExcel.xlsx")
mada_2015_precip_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Pre_2015_TableToExcel.xlsx")
mada_2016_precip_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Pre_2016_TableToExcel.xlsx")
mada_2017_precip_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Pre_2017_TableToExcel.xlsx")
mada_2018_precip_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Pre_2018_TableToExcel.xlsx")
mada_2019_precip_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Pre_2019_TableToExcel.xlsx")
mada_2020_precip_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Pre_2020_TableToExcel.xlsx")
mada_2021_precip_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Pre_2021_TableToExcel.xlsx")

mada_2000_precip_buffer_commune_9km$Year <- 2000
mada_2001_precip_buffer_commune_9km$Year <- 2001
mada_2002_precip_buffer_commune_9km$Year <- 2002
mada_2003_precip_buffer_commune_9km$Year <- 2003
mada_2004_precip_buffer_commune_9km$Year <- 2004
mada_2005_precip_buffer_commune_9km$Year <- 2005
mada_2006_precip_buffer_commune_9km$Year <- 2006
mada_2007_precip_buffer_commune_9km$Year <- 2007
mada_2008_precip_buffer_commune_9km$Year <- 2008
mada_2009_precip_buffer_commune_9km$Year <- 2009
mada_2010_precip_buffer_commune_9km$Year <- 2010
mada_2011_precip_buffer_commune_9km$Year <- 2011
mada_2012_precip_buffer_commune_9km$Year <- 2012
mada_2013_precip_buffer_commune_9km$Year <- 2013
mada_2014_precip_buffer_commune_9km$Year <- 2014
mada_2015_precip_buffer_commune_9km$Year <- 2015
mada_2016_precip_buffer_commune_9km$Year <- 2016
mada_2017_precip_buffer_commune_9km$Year <- 2017
mada_2018_precip_buffer_commune_9km$Year <- 2018
mada_2019_precip_buffer_commune_9km$Year <- 2019
mada_2020_precip_buffer_commune_9km$Year <- 2020
mada_2021_precip_buffer_commune_9km$Year <- 2021

mada_precip_buffer_commune_9km <- rbind(mada_2000_precip_buffer_commune_9km,
                                        mada_2001_precip_buffer_commune_9km, 
                                        mada_2002_precip_buffer_commune_9km, 
                                        mada_2003_precip_buffer_commune_9km, 
                                        mada_2004_precip_buffer_commune_9km, 
                                        mada_2005_precip_buffer_commune_9km, 
                                        mada_2006_precip_buffer_commune_9km, 
                                        mada_2007_precip_buffer_commune_9km, 
                                        mada_2008_precip_buffer_commune_9km, 
                                        mada_2009_precip_buffer_commune_9km, 
                                        mada_2010_precip_buffer_commune_9km, 
                                        mada_2011_precip_buffer_commune_9km, 
                                        mada_2012_precip_buffer_commune_9km, 
                                        mada_2013_precip_buffer_commune_9km, 
                                        mada_2014_precip_buffer_commune_9km, 
                                        mada_2015_precip_buffer_commune_9km, 
                                        mada_2016_precip_buffer_commune_9km, 
                                        mada_2017_precip_buffer_commune_9km,
                                        mada_2018_precip_buffer_commune_9km,
                                        mada_2019_precip_buffer_commune_9km,
                                        mada_2020_precip_buffer_commune_9km,
                                        mada_2021_precip_buffer_commune_9km)

mada_precip_buffer_commune_9km$Field[mada_precip_buffer_commune_9km$Field=="Bemaraha"]<- "Bemaraha (combined)"
mada_precip_buffer_commune_9km$Field[mada_precip_buffer_commune_9km$Field=="Baie de Baly/Tsingy de Namoroka"]<- "Namoroka/Baie de Baly"
mada_precip_buffer_commune_9km$Field[mada_precip_buffer_commune_9km$Field=="Mantadia"]<- "Andasibe/Analamazaotra"


mada_precip_buffer_commune_9km <- mada_precip_buffer_commune_9km %>% rename("Name"="Field")
mada_precip_buffer_commune_9km2 <- mada_precip_buffer_commune_9km %>% dplyr::select(Name,MEAN,Year)
mada_precip_buffer_commune_9km2 <- mada_precip_buffer_commune_9km2 %>% rename("Precipitation" = "MEAN")

write.csv(mada_precip_buffer_commune_9km, "~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_Commune_9km/Precip_Buffer_Commune.csv")


####BUFFER DEFORESTATION
deforestation_dat2000_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_9km/ZonalSt_9km_Buffer_Forest_Laborde_2000_TableToExcel.xlsx")
deforestation_dat2001_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_9km/ZonalSt_9km_Buffer_Forest_Laborde_2001_TableToExcel.xlsx")
deforestation_dat2002_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_9km/ZonalSt_9km_Buffer_Forest_Laborde_2002_TableToExcel.xlsx")
deforestation_dat2003_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_9km/ZonalSt_9km_Buffer_Forest_Laborde_2003_TableToExcel.xlsx")
deforestation_dat2004_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_9km/ZonalSt_9km_Buffer_Forest_Laborde_2004_TableToExcel.xlsx")
deforestation_dat2005_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_9km/ZonalSt_9km_Buffer_Forest_Laborde_2005_TableToExcel.xlsx")
deforestation_dat2006_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_9km/ZonalSt_9km_Buffer_Forest_Laborde_2006_TableToExcel.xlsx")
deforestation_dat2007_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_9km/ZonalSt_9km_Buffer_Forest_Laborde_2007_TableToExcel.xlsx")
deforestation_dat2008_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_9km/ZonalSt_9km_Buffer_Forest_Laborde_2008_TableToExcel.xlsx")
deforestation_dat2009_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_9km/ZonalSt_9km_Buffer_Forest_Laborde_2009_TableToExcel.xlsx")
deforestation_dat2010_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_9km/ZonalSt_9km_Buffer_Forest_Laborde_2010_TableToExcel.xlsx")
deforestation_dat2011_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_9km/ZonalSt_9km_Buffer_Forest_Laborde_2011_TableToExcel.xlsx")
deforestation_dat2012_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_9km/ZonalSt_9km_Buffer_Forest_Laborde_2012_TableToExcel.xlsx")
deforestation_dat2013_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_9km/ZonalSt_9km_Buffer_Forest_Laborde_2013_TableToExcel.xlsx")
deforestation_dat2014_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_9km/ZonalSt_9km_Buffer_Forest_Laborde_2014_TableToExcel.xlsx")
deforestation_dat2015_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_9km/ZonalSt_9km_Buffer_Forest_Laborde_2015_TableToExcel.xlsx")
deforestation_dat2016_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_9km/ZonalSt_9km_Buffer_Forest_Laborde_2016_TableToExcel.xlsx")
deforestation_dat2017_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_9km/ZonalSt_9km_Buffer_Forest_Laborde_2017_TableToExcel.xlsx")
deforestation_dat2018_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_9km/ZonalSt_9km_Buffer_Forest_Laborde_2018_TableToExcel.xlsx")
deforestation_dat2019_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_9km/ZonalSt_9km_Buffer_Forest_Laborde_2019_TableToExcel.xlsx")
deforestation_dat2020_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_9km/ZonalSt_9km_Buffer_Forest_Laborde_2020_TableToExcel.xlsx")
deforestation_dat2021_buffer_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_9km/ZonalSt_9km_Buffer_Forest_Laborde_2021_TableToExcel.xlsx")


deforestation_dat2000_buffer_9km$Year <- "fc2000"
deforestation_dat2001_buffer_9km$Year <- "fc2001"
deforestation_dat2002_buffer_9km$Year <- "fc2002"
deforestation_dat2003_buffer_9km$Year <- "fc2003"
deforestation_dat2004_buffer_9km$Year <- "fc2004"
deforestation_dat2005_buffer_9km$Year <- "fc2005"
deforestation_dat2006_buffer_9km$Year <- "fc2006"
deforestation_dat2007_buffer_9km$Year <- "fc2007"
deforestation_dat2008_buffer_9km$Year <- "fc2008"
deforestation_dat2009_buffer_9km$Year <- "fc2009"
deforestation_dat2010_buffer_9km$Year <- "fc2010"
deforestation_dat2011_buffer_9km$Year <- "fc2011"
deforestation_dat2012_buffer_9km$Year <- "fc2012"
deforestation_dat2013_buffer_9km$Year <- "fc2013"
deforestation_dat2014_buffer_9km$Year <- "fc2014"
deforestation_dat2015_buffer_9km$Year <- "fc2015"
deforestation_dat2016_buffer_9km$Year <- "fc2016"
deforestation_dat2017_buffer_9km$Year <- "fc2017"
deforestation_dat2018_buffer_9km$Year <- "fc2018"
deforestation_dat2019_buffer_9km$Year <- "fc2019"
deforestation_dat2020_buffer_9km$Year <- "fc2020"
deforestation_dat2021_buffer_9km$Year <- "fc2021"

deforestation_dat2000_buffer_9km <- deforestation_dat2000_buffer_9km %>% dplyr::select(OBJECTID, Field, ZONE_CODE, COUNT, AREA, MIN, MAX, RANGE, MEAN, STD, SUM, Year)

deforestation_dat_buffer_9km <- rbind(deforestation_dat2000_buffer_9km,
                                      deforestation_dat2001_buffer_9km,
                                      deforestation_dat2002_buffer_9km,
                                      deforestation_dat2003_buffer_9km,
                                      deforestation_dat2004_buffer_9km,
                                      deforestation_dat2005_buffer_9km,
                                      deforestation_dat2006_buffer_9km,
                                      deforestation_dat2007_buffer_9km,
                                      deforestation_dat2008_buffer_9km,
                                      deforestation_dat2009_buffer_9km,
                                      deforestation_dat2010_buffer_9km,
                                      deforestation_dat2011_buffer_9km,
                                      deforestation_dat2012_buffer_9km,
                                      deforestation_dat2013_buffer_9km,
                                      deforestation_dat2014_buffer_9km,
                                      deforestation_dat2015_buffer_9km,
                                      deforestation_dat2016_buffer_9km,
                                      deforestation_dat2017_buffer_9km,
                                      deforestation_dat2018_buffer_9km,
                                      deforestation_dat2019_buffer_9km,
                                      deforestation_dat2020_buffer_9km,
                                      deforestation_dat2021_buffer_9km)

deforestation_dat2_buffer_9km <- deforestation_dat_buffer_9km%>%  dplyr::select(Field,SUM, Year) %>% pivot_wider(names_from = "Year", values_from= "SUM")

deforestation_dat2_buffer_9km$flp2001 <- 100*(deforestation_dat2_buffer_9km$fc2000 - deforestation_dat2_buffer_9km$fc2001)/ deforestation_dat2_buffer_9km$fc2000
deforestation_dat2_buffer_9km$flp2002 <- 100*(deforestation_dat2_buffer_9km$fc2001 - deforestation_dat2_buffer_9km$fc2002)/ deforestation_dat2_buffer_9km$fc2000
deforestation_dat2_buffer_9km$flp2003 <- 100*(deforestation_dat2_buffer_9km$fc2002 - deforestation_dat2_buffer_9km$fc2003)/ deforestation_dat2_buffer_9km$fc2000
deforestation_dat2_buffer_9km$flp2004 <- 100*(deforestation_dat2_buffer_9km$fc2003 - deforestation_dat2_buffer_9km$fc2004)/ deforestation_dat2_buffer_9km$fc2000
deforestation_dat2_buffer_9km$flp2005 <- 100*(deforestation_dat2_buffer_9km$fc2004 - deforestation_dat2_buffer_9km$fc2005)/ deforestation_dat2_buffer_9km$fc2000
deforestation_dat2_buffer_9km$flp2006 <- 100*(deforestation_dat2_buffer_9km$fc2005 - deforestation_dat2_buffer_9km$fc2006)/ deforestation_dat2_buffer_9km$fc2000
deforestation_dat2_buffer_9km$flp2007 <- 100*(deforestation_dat2_buffer_9km$fc2006 - deforestation_dat2_buffer_9km$fc2007)/ deforestation_dat2_buffer_9km$fc2000
deforestation_dat2_buffer_9km$flp2008 <- 100*(deforestation_dat2_buffer_9km$fc2007 - deforestation_dat2_buffer_9km$fc2008)/ deforestation_dat2_buffer_9km$fc2000
deforestation_dat2_buffer_9km$flp2009 <- 100*(deforestation_dat2_buffer_9km$fc2008 - deforestation_dat2_buffer_9km$fc2009)/ deforestation_dat2_buffer_9km$fc2000
deforestation_dat2_buffer_9km$flp2010 <- 100*(deforestation_dat2_buffer_9km$fc2009 - deforestation_dat2_buffer_9km$fc2010)/ deforestation_dat2_buffer_9km$fc2000
deforestation_dat2_buffer_9km$flp2011 <- 100*(deforestation_dat2_buffer_9km$fc2010 - deforestation_dat2_buffer_9km$fc2011)/ deforestation_dat2_buffer_9km$fc2000
deforestation_dat2_buffer_9km$flp2012 <- 100*(deforestation_dat2_buffer_9km$fc2011 - deforestation_dat2_buffer_9km$fc2012)/ deforestation_dat2_buffer_9km$fc2000
deforestation_dat2_buffer_9km$flp2013 <- 100*(deforestation_dat2_buffer_9km$fc2012 - deforestation_dat2_buffer_9km$fc2013)/ deforestation_dat2_buffer_9km$fc2000
deforestation_dat2_buffer_9km$flp2014 <- 100*(deforestation_dat2_buffer_9km$fc2013 - deforestation_dat2_buffer_9km$fc2014)/ deforestation_dat2_buffer_9km$fc2000
deforestation_dat2_buffer_9km$flp2015 <- 100*(deforestation_dat2_buffer_9km$fc2014 - deforestation_dat2_buffer_9km$fc2015)/ deforestation_dat2_buffer_9km$fc2000
deforestation_dat2_buffer_9km$flp2016 <- 100*(deforestation_dat2_buffer_9km$fc2015 - deforestation_dat2_buffer_9km$fc2016)/ deforestation_dat2_buffer_9km$fc2000
deforestation_dat2_buffer_9km$flp2017 <- 100*(deforestation_dat2_buffer_9km$fc2016 - deforestation_dat2_buffer_9km$fc2017)/ deforestation_dat2_buffer_9km$fc2000
deforestation_dat2_buffer_9km$flp2018 <- 100*(deforestation_dat2_buffer_9km$fc2017 - deforestation_dat2_buffer_9km$fc2018)/ deforestation_dat2_buffer_9km$fc2000
deforestation_dat2_buffer_9km$flp2019 <- 100*(deforestation_dat2_buffer_9km$fc2018 - deforestation_dat2_buffer_9km$fc2019)/ deforestation_dat2_buffer_9km$fc2000
deforestation_dat2_buffer_9km$flp2020 <- 100*(deforestation_dat2_buffer_9km$fc2019 - deforestation_dat2_buffer_9km$fc2020)/ deforestation_dat2_buffer_9km$fc2000
deforestation_dat2_buffer_9km$flp2021 <- 100*(deforestation_dat2_buffer_9km$fc2020 - deforestation_dat2_buffer_9km$fc2021)/ deforestation_dat2_buffer_9km$fc2000

deforestation_dat3_buffer_9km <- deforestation_dat2_buffer_9km %>% dplyr::select(Field, flp2001:flp2021) %>% pivot_longer(cols=flp2001:flp2021, names_to="Year", values_to="PercentDeforest") 
deforestation_dat3_buffer_9km$Year <- gsub( "flp", "", deforestation_dat3_buffer_9km$Year)

deforestation_dat3_buffer_9km$Field[deforestation_dat3_buffer_9km$Field=="Bemaraha"]<- "Bemaraha (combined)"
deforestation_dat3_buffer_9km$Field[deforestation_dat3_buffer_9km$Field=="Baie de Baly/Tsingy de Namoroka"]<- "Namoroka/Baie de Baly"
deforestation_dat3_buffer_9km$Field[deforestation_dat3_buffer_9km$Field=="Mantadia"]<- "Andasibe/Analamazaotra"
deforestation_dat3_buffer_9km <- deforestation_dat3_buffer_9km %>% rename("Name"="Field")


write.csv(deforestation_dat3_buffer_9km, "~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_buffer_9km/Forest_buffer_9km_Deforestation.csv")


######COMMUNE DEFORESTATION
deforestation_dat2000_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Forest_Laborde_2000_TableToExcel.xlsx")
deforestation_dat2001_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Forest_Laborde_2001_TableToExcel.xlsx")
deforestation_dat2002_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Forest_Laborde_2002_TableToExcel.xlsx")
deforestation_dat2003_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Forest_Laborde_2003_TableToExcel.xlsx")
deforestation_dat2004_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Forest_Laborde_2004_TableToExcel.xlsx")
deforestation_dat2005_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Forest_Laborde_2005_TableToExcel.xlsx")
deforestation_dat2006_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Forest_Laborde_2006_TableToExcel.xlsx")
deforestation_dat2007_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Forest_Laborde_2007_TableToExcel.xlsx")
deforestation_dat2008_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Forest_Laborde_2008_TableToExcel.xlsx")
deforestation_dat2009_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Forest_Laborde_2009_TableToExcel.xlsx")
deforestation_dat2010_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Forest_Laborde_2010_TableToExcel.xlsx")
deforestation_dat2011_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Forest_Laborde_2011_TableToExcel.xlsx")
deforestation_dat2012_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Forest_Laborde_2012_TableToExcel.xlsx")
deforestation_dat2013_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Forest_Laborde_2013_TableToExcel.xlsx")
deforestation_dat2014_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Forest_Laborde_2014_TableToExcel.xlsx")
deforestation_dat2015_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Forest_Laborde_2015_TableToExcel.xlsx")
deforestation_dat2016_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Forest_Laborde_2016_TableToExcel.xlsx")
deforestation_dat2017_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Forest_Laborde_2017_TableToExcel.xlsx")
deforestation_dat2018_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Forest_Laborde_2018_TableToExcel.xlsx")
deforestation_dat2019_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Forest_Laborde_2019_TableToExcel.xlsx")
deforestation_dat2020_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Forest_Laborde_2020_TableToExcel.xlsx")
deforestation_dat2021_buffer_commune_9km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_9km/ZonalSt_9km_Buffer_Commune_Forest_Laborde_2021_TableToExcel.xlsx")

deforestation_dat2000_buffer_commune_9km$Year <- "fc2000"
deforestation_dat2001_buffer_commune_9km$Year <- "fc2001"
deforestation_dat2002_buffer_commune_9km$Year <- "fc2002"
deforestation_dat2003_buffer_commune_9km$Year <- "fc2003"
deforestation_dat2004_buffer_commune_9km$Year <- "fc2004"
deforestation_dat2005_buffer_commune_9km$Year <- "fc2005"
deforestation_dat2006_buffer_commune_9km$Year <- "fc2006"
deforestation_dat2007_buffer_commune_9km$Year <- "fc2007"
deforestation_dat2008_buffer_commune_9km$Year <- "fc2008"
deforestation_dat2009_buffer_commune_9km$Year <- "fc2009"
deforestation_dat2010_buffer_commune_9km$Year <- "fc2010"
deforestation_dat2011_buffer_commune_9km$Year <- "fc2011"
deforestation_dat2012_buffer_commune_9km$Year <- "fc2012"
deforestation_dat2013_buffer_commune_9km$Year <- "fc2013"
deforestation_dat2014_buffer_commune_9km$Year <- "fc2014"
deforestation_dat2015_buffer_commune_9km$Year <- "fc2015"
deforestation_dat2016_buffer_commune_9km$Year <- "fc2016"
deforestation_dat2017_buffer_commune_9km$Year <- "fc2017"
deforestation_dat2018_buffer_commune_9km$Year <- "fc2018"
deforestation_dat2019_buffer_commune_9km$Year <- "fc2019"
deforestation_dat2020_buffer_commune_9km$Year <- "fc2020"
deforestation_dat2021_buffer_commune_9km$Year <- "fc2021"

deforestation_dat2000_buffer_commune_9km <- deforestation_dat2000_buffer_commune_9km %>% dplyr::select(OBJECTID, Field, ZONE_CODE, COUNT, AREA, MIN, MAX, RANGE, MEAN, STD, SUM, Year)

deforestation_dat_buffer_commune_9km <- rbind(deforestation_dat2000_buffer_commune_9km,
                                              deforestation_dat2001_buffer_commune_9km,
                                              deforestation_dat2002_buffer_commune_9km,
                                              deforestation_dat2003_buffer_commune_9km,
                                              deforestation_dat2004_buffer_commune_9km,
                                              deforestation_dat2005_buffer_commune_9km,
                                              deforestation_dat2006_buffer_commune_9km,
                                              deforestation_dat2007_buffer_commune_9km,
                                              deforestation_dat2008_buffer_commune_9km,
                                              deforestation_dat2009_buffer_commune_9km,
                                              deforestation_dat2010_buffer_commune_9km,
                                              deforestation_dat2011_buffer_commune_9km,
                                              deforestation_dat2012_buffer_commune_9km,
                                              deforestation_dat2013_buffer_commune_9km,
                                              deforestation_dat2014_buffer_commune_9km,
                                              deforestation_dat2015_buffer_commune_9km,
                                              deforestation_dat2016_buffer_commune_9km,
                                              deforestation_dat2017_buffer_commune_9km,
                                              deforestation_dat2018_buffer_commune_9km,
                                              deforestation_dat2019_buffer_commune_9km,
                                              deforestation_dat2020_buffer_commune_9km,
                                              deforestation_dat2021_buffer_commune_9km)

deforestation_dat2_buffer_commune_9km <- deforestation_dat_buffer_commune_9km%>%  dplyr::select(Field,SUM, Year) %>% pivot_wider(names_from = "Year", values_from= "SUM")

deforestation_dat2_buffer_commune_9km$flp2001 <- 100*(deforestation_dat2_buffer_commune_9km$fc2000 - deforestation_dat2_buffer_commune_9km$fc2001)/ deforestation_dat2_buffer_commune_9km$fc2000
deforestation_dat2_buffer_commune_9km$flp2002 <- 100*(deforestation_dat2_buffer_commune_9km$fc2001 - deforestation_dat2_buffer_commune_9km$fc2002)/ deforestation_dat2_buffer_commune_9km$fc2000
deforestation_dat2_buffer_commune_9km$flp2003 <- 100*(deforestation_dat2_buffer_commune_9km$fc2002 - deforestation_dat2_buffer_commune_9km$fc2003)/ deforestation_dat2_buffer_commune_9km$fc2000
deforestation_dat2_buffer_commune_9km$flp2004 <- 100*(deforestation_dat2_buffer_commune_9km$fc2003 - deforestation_dat2_buffer_commune_9km$fc2004)/ deforestation_dat2_buffer_commune_9km$fc2000
deforestation_dat2_buffer_commune_9km$flp2005 <- 100*(deforestation_dat2_buffer_commune_9km$fc2004 - deforestation_dat2_buffer_commune_9km$fc2005)/ deforestation_dat2_buffer_commune_9km$fc2000
deforestation_dat2_buffer_commune_9km$flp2006 <- 100*(deforestation_dat2_buffer_commune_9km$fc2005 - deforestation_dat2_buffer_commune_9km$fc2006)/ deforestation_dat2_buffer_commune_9km$fc2000
deforestation_dat2_buffer_commune_9km$flp2007 <- 100*(deforestation_dat2_buffer_commune_9km$fc2006 - deforestation_dat2_buffer_commune_9km$fc2007)/ deforestation_dat2_buffer_commune_9km$fc2000
deforestation_dat2_buffer_commune_9km$flp2008 <- 100*(deforestation_dat2_buffer_commune_9km$fc2007 - deforestation_dat2_buffer_commune_9km$fc2008)/ deforestation_dat2_buffer_commune_9km$fc2000
deforestation_dat2_buffer_commune_9km$flp2009 <- 100*(deforestation_dat2_buffer_commune_9km$fc2008 - deforestation_dat2_buffer_commune_9km$fc2009)/ deforestation_dat2_buffer_commune_9km$fc2000
deforestation_dat2_buffer_commune_9km$flp2010 <- 100*(deforestation_dat2_buffer_commune_9km$fc2009 - deforestation_dat2_buffer_commune_9km$fc2010)/ deforestation_dat2_buffer_commune_9km$fc2000
deforestation_dat2_buffer_commune_9km$flp2011 <- 100*(deforestation_dat2_buffer_commune_9km$fc2010 - deforestation_dat2_buffer_commune_9km$fc2011)/ deforestation_dat2_buffer_commune_9km$fc2000
deforestation_dat2_buffer_commune_9km$flp2012 <- 100*(deforestation_dat2_buffer_commune_9km$fc2011 - deforestation_dat2_buffer_commune_9km$fc2012)/ deforestation_dat2_buffer_commune_9km$fc2000
deforestation_dat2_buffer_commune_9km$flp2013 <- 100*(deforestation_dat2_buffer_commune_9km$fc2012 - deforestation_dat2_buffer_commune_9km$fc2013)/ deforestation_dat2_buffer_commune_9km$fc2000
deforestation_dat2_buffer_commune_9km$flp2014 <- 100*(deforestation_dat2_buffer_commune_9km$fc2013 - deforestation_dat2_buffer_commune_9km$fc2014)/ deforestation_dat2_buffer_commune_9km$fc2000
deforestation_dat2_buffer_commune_9km$flp2015 <- 100*(deforestation_dat2_buffer_commune_9km$fc2014 - deforestation_dat2_buffer_commune_9km$fc2015)/ deforestation_dat2_buffer_commune_9km$fc2000
deforestation_dat2_buffer_commune_9km$flp2016 <- 100*(deforestation_dat2_buffer_commune_9km$fc2015 - deforestation_dat2_buffer_commune_9km$fc2016)/ deforestation_dat2_buffer_commune_9km$fc2000
deforestation_dat2_buffer_commune_9km$flp2017 <- 100*(deforestation_dat2_buffer_commune_9km$fc2016 - deforestation_dat2_buffer_commune_9km$fc2017)/ deforestation_dat2_buffer_commune_9km$fc2000
deforestation_dat2_buffer_commune_9km$flp2018 <- 100*(deforestation_dat2_buffer_commune_9km$fc2017 - deforestation_dat2_buffer_commune_9km$fc2018)/ deforestation_dat2_buffer_commune_9km$fc2000
deforestation_dat2_buffer_commune_9km$flp2019 <- 100*(deforestation_dat2_buffer_commune_9km$fc2018 - deforestation_dat2_buffer_commune_9km$fc2019)/ deforestation_dat2_buffer_commune_9km$fc2000
deforestation_dat2_buffer_commune_9km$flp2020 <- 100*(deforestation_dat2_buffer_commune_9km$fc2019 - deforestation_dat2_buffer_commune_9km$fc2020)/ deforestation_dat2_buffer_commune_9km$fc2000
deforestation_dat2_buffer_commune_9km$flp2021 <- 100*(deforestation_dat2_buffer_commune_9km$fc2020 - deforestation_dat2_buffer_commune_9km$fc2021)/ deforestation_dat2_buffer_commune_9km$fc2000

deforestation_dat3_buffer_commune_9km <- deforestation_dat2_buffer_commune_9km %>% dplyr::select(Field, flp2001:flp2021) %>% pivot_longer(cols=flp2001:flp2021, names_to="Year", values_to="PercentDeforest") 
deforestation_dat3_buffer_commune_9km$Year <- gsub( "flp", "", deforestation_dat3_buffer_commune_9km$Year)

deforestation_dat3_buffer_commune_9km$Field[deforestation_dat3_buffer_commune_9km$Field=="Bemaraha"]<- "Bemaraha (combined)"
deforestation_dat3_buffer_commune_9km$Field[deforestation_dat3_buffer_commune_9km$Field=="Baie de Baly/Tsingy de Namoroka"]<- "Namoroka/Baie de Baly"
deforestation_dat3_buffer_commune_9km$Field[deforestation_dat3_buffer_commune_9km$Field=="Mantadia"]<- "Andasibe/Analamazaotra"

deforestation_dat3_buffer_commune_9km <- deforestation_dat3_buffer_commune_9km %>% rename("Name"="Field")

write.csv(deforestation_dat3_buffer_commune_9km , "~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_Commune_9km/Forest_Buffer_Commune_Deforestation.csv")

```

```{r Combined}
deforestation_dat1990 <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/CommuneForestCover/MNP1990fc.csv")
deforestation_dat1990$Name <- deforestation_dat1990$NOM

deforestation_dat1990$Name[deforestation_dat1990$Name=="Analamazaotra"]<- "Andasibe/Analamazaotra"
deforestation_dat1990$Name[deforestation_dat1990$Name=="Mantadia"]<- "Andasibe/Analamazaotra"
deforestation_dat1990$Name[deforestation_dat1990$Name=="Bemaraha (partie Nord)"]<- "Bemaraha (combined)"
deforestation_dat1990$Name[deforestation_dat1990$Name=="Bemaraha partie Sud"]<- "Bemaraha (combined)"
deforestation_dat1990$Name[deforestation_dat1990$Name=="Tsingy de Namoroka"]<- "Namoroka/Baie de Baly"
deforestation_dat1990$Name[deforestation_dat1990$Name=="Baie de Baly"]<- "Namoroka/Baie de Baly"
deforestation_dat1990$Name[deforestation_dat1990$Name=="Zahamena RNI"]<- "Zahamena" 
deforestation_dat1990$Name[deforestation_dat1990$Name=="Nosy Mangabe"]<- "Masoala"
deforestation_dat1990$Name[deforestation_dat1990$Name=="Foret d'Ambre"]<-  "Montagne d'Ambre" 


deforestation_dat2000 <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/CommuneForestCover/cff2000.csv")
deforestation_dat2001 <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/CommuneForestCover/cff2001.csv")
deforestation_dat2002 <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/CommuneForestCover/cff2002.csv")
deforestation_dat2003 <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/CommuneForestCover/cff2003.csv")
deforestation_dat2004 <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/CommuneForestCover/cff2004.csv")
deforestation_dat2005 <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/CommuneForestCover/cff2005.csv")
deforestation_dat2006 <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/CommuneForestCover/cff2006.csv")
deforestation_dat2007 <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/CommuneForestCover/cff2007.csv")
deforestation_dat2008 <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/CommuneForestCover/cff2008.csv")
deforestation_dat2009 <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/CommuneForestCover/cff2009.csv")
deforestation_dat2010 <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/CommuneForestCover/cff2010.csv")
deforestation_dat2011 <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/CommuneForestCover/cff2011.csv")
deforestation_dat2012 <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/CommuneForestCover/cff2012.csv")
deforestation_dat2013 <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/CommuneForestCover/cff2013.csv")
deforestation_dat2014 <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/CommuneForestCover/cff2014.csv")
deforestation_dat2015 <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/CommuneForestCover/cff2015.csv")
deforestation_dat2016 <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/CommuneForestCover/cff2016.csv")
deforestation_dat2017 <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/CommuneForestCover/cff2017.csv")
deforestation_dat2018 <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/CommuneForestCover/cff2018.csv")
deforestation_dat2019 <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/CommuneForestCover/cff2019.csv")
deforestation_dat2020 <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/CommuneForestCover/cff2020.csv")
deforestation_dat2021 <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/CommuneForestCover/cff2021.csv")

deforestation_dat1990$Year <- "fc1990"
deforestation_dat2000$Year <- "fc2000"
deforestation_dat2001$Year <- "fc2001"
deforestation_dat2002$Year <- "fc2002"
deforestation_dat2003$Year <- "fc2003"
deforestation_dat2004$Year <- "fc2004"
deforestation_dat2005$Year <- "fc2005"
deforestation_dat2006$Year <- "fc2006"
deforestation_dat2007$Year <- "fc2007"
deforestation_dat2008$Year <- "fc2008"
deforestation_dat2009$Year <- "fc2009"
deforestation_dat2010$Year <- "fc2010"
deforestation_dat2011$Year <- "fc2011"
deforestation_dat2012$Year <- "fc2012"
deforestation_dat2013$Year <- "fc2013"
deforestation_dat2014$Year <- "fc2014"
deforestation_dat2015$Year <- "fc2015"
deforestation_dat2016$Year <- "fc2016"
deforestation_dat2017$Year <- "fc2017"
deforestation_dat2018$Year <- "fc2018"
deforestation_dat2019$Year <- "fc2019"
deforestation_dat2020$Year <- "fc2020"
deforestation_dat2021$Year <- "fc2021"
deforestation_dat2000$Year <- "fc2000"

deforestation_dat1990 <- deforestation_dat1990 %>% dplyr::select(NOM, SUM, Year)
deforestation_dat2000 <- deforestation_dat2000 %>% dplyr::select(NOM, SUM, Year)

deforestation_dat <- rbind(deforestation_dat2001, deforestation_dat2002, deforestation_dat2003, deforestation_dat2004, deforestation_dat2005, deforestation_dat2006, deforestation_dat2007, deforestation_dat2008, deforestation_dat2009, deforestation_dat2010, deforestation_dat2011, deforestation_dat2012, deforestation_dat2013, deforestation_dat2014, deforestation_dat2015, deforestation_dat2016, deforestation_dat2017, deforestation_dat2018, deforestation_dat2019, deforestation_dat2020, deforestation_dat2021)

deforestation_dat <- deforestation_dat %>% dplyr::select(NOM, SUM, Year)
deforestation_dat2 <- rbind(deforestation_dat, deforestation_dat1990)
deforestation_dat2 <- rbind(deforestation_dat2, deforestation_dat2000)

deforestation_dat2 <- deforestation_dat2%>% pivot_wider(names_from = "Year", values_from= "SUM")


deforestation_dat2$NOM[deforestation_dat2$NOM=="Analamazaotra"]<- "Andasibe/Analamazaotra"
deforestation_dat2$NOM[deforestation_dat2$NOM=="Mantadia"]<- "Andasibe/Analamazaotra"
deforestation_dat2$NOM[deforestation_dat2$NOM=="Bemaraha (partie Nord)"]<- "Bemaraha (combined)"
deforestation_dat2$NOM[deforestation_dat2$NOM=="Bemaraha partie Sud"]<- "Bemaraha (combined)"
deforestation_dat2$NOM[deforestation_dat2$NOM=="Tsingy de Namoroka"]<- "Namoroka/Baie de Baly"
deforestation_dat2$NOM[deforestation_dat2$NOM=="Baie de Baly"]<- "Namoroka/Baie de Baly"
deforestation_dat2$NOM[deforestation_dat2$NOM=="Zahamena RNI"]<- "Zahamena" 
deforestation_dat2$NOM[deforestation_dat2$NOM=="Nosy Mangabe"]<- "Masoala"
deforestation_dat2$NOM[deforestation_dat2$NOM=="Foret d'Ambre"]<-  "Montagne d'Ambre" 


deforestation123 <- aggregate(. ~ NOM , data = deforestation_dat2,FUN = sum)


deforestation_dat2_buffer_3km <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_buffer_3km/deforestation_dat2_buffer_3km.csv")

deforestation_dat2_buffer_3km <- deforestation_dat2_buffer_3km %>% rename(NOM=Field)

deforestation_dat2_buffer_3km$NOM[deforestation_dat2_buffer_3km$NOM=="Bemaraha"]<- "Bemaraha (combined)"
deforestation_dat2_buffer_3km$NOM[deforestation_dat2_buffer_3km$NOM=="Baie de Baly/Tsingy de Namoroka"]<- "Namoroka/Baie de Baly"
deforestation_dat2_buffer_3km$NOM[deforestation_dat2_buffer_3km$NOM=="Mantadia"]<- "Andasibe/Analamazaotra"

deforestation_combined_buffer_3km <- full_join(deforestation123, deforestation_dat2_buffer_3km, by= "NOM")



deforestation_combined_buffer_3km$totalflp2001 <- 100*((deforestation_combined_buffer_3km$fc2000.x + deforestation_combined_buffer_3km$fc2000.y) - (deforestation_combined_buffer_3km$fc2001.x + deforestation_combined_buffer_3km$fc2001.y))/ (deforestation_combined_buffer_3km$fc2000.x + deforestation_combined_buffer_3km$fc2000.y)
deforestation_combined_buffer_3km$totalflp2002 <- 100*((deforestation_combined_buffer_3km$fc2001.x + deforestation_combined_buffer_3km$fc2001.y) - (deforestation_combined_buffer_3km$fc2002.x + deforestation_combined_buffer_3km$fc2002.y))/ (deforestation_combined_buffer_3km$fc2000.x + deforestation_combined_buffer_3km$fc2000.y)
deforestation_combined_buffer_3km$totalflp2003 <- 100*((deforestation_combined_buffer_3km$fc2002.x + deforestation_combined_buffer_3km$fc2002.y) - (deforestation_combined_buffer_3km$fc2003.x + deforestation_combined_buffer_3km$fc2003.y))/ (deforestation_combined_buffer_3km$fc2000.x + deforestation_combined_buffer_3km$fc2000.y)
deforestation_combined_buffer_3km$totalflp2004 <- 100*((deforestation_combined_buffer_3km$fc2003.x + deforestation_combined_buffer_3km$fc2003.y) - (deforestation_combined_buffer_3km$fc2004.x + deforestation_combined_buffer_3km$fc2004.y))/ (deforestation_combined_buffer_3km$fc2000.x + deforestation_combined_buffer_3km$fc2000.y)
deforestation_combined_buffer_3km$totalflp2005 <- 100*((deforestation_combined_buffer_3km$fc2004.x + deforestation_combined_buffer_3km$fc2004.y) - (deforestation_combined_buffer_3km$fc2005.x + deforestation_combined_buffer_3km$fc2005.y))/ (deforestation_combined_buffer_3km$fc2000.x + deforestation_combined_buffer_3km$fc2000.y)
deforestation_combined_buffer_3km$totalflp2006 <- 100*((deforestation_combined_buffer_3km$fc2005.x + deforestation_combined_buffer_3km$fc2005.y) - (deforestation_combined_buffer_3km$fc2006.x + deforestation_combined_buffer_3km$fc2006.y))/ (deforestation_combined_buffer_3km$fc2000.x + deforestation_combined_buffer_3km$fc2000.y)
deforestation_combined_buffer_3km$totalflp2007 <- 100*((deforestation_combined_buffer_3km$fc2006.x + deforestation_combined_buffer_3km$fc2006.y) - (deforestation_combined_buffer_3km$fc2007.x + deforestation_combined_buffer_3km$fc2007.y))/ (deforestation_combined_buffer_3km$fc2000.x + deforestation_combined_buffer_3km$fc2000.y)
deforestation_combined_buffer_3km$totalflp2008 <- 100*((deforestation_combined_buffer_3km$fc2007.x + deforestation_combined_buffer_3km$fc2007.y) - (deforestation_combined_buffer_3km$fc2008.x + deforestation_combined_buffer_3km$fc2008.y))/ (deforestation_combined_buffer_3km$fc2000.x + deforestation_combined_buffer_3km$fc2000.y)
deforestation_combined_buffer_3km$totalflp2009 <- 100*((deforestation_combined_buffer_3km$fc2008.x + deforestation_combined_buffer_3km$fc2008.y) - (deforestation_combined_buffer_3km$fc2009.x + deforestation_combined_buffer_3km$fc2009.y))/ (deforestation_combined_buffer_3km$fc2000.x + deforestation_combined_buffer_3km$fc2000.y)
deforestation_combined_buffer_3km$totalflp2010 <- 100*((deforestation_combined_buffer_3km$fc2009.x + deforestation_combined_buffer_3km$fc2009.y) - (deforestation_combined_buffer_3km$fc2010.x + deforestation_combined_buffer_3km$fc2010.y))/ (deforestation_combined_buffer_3km$fc2000.x + deforestation_combined_buffer_3km$fc2000.y)
deforestation_combined_buffer_3km$totalflp2011 <- 100*((deforestation_combined_buffer_3km$fc2010.x + deforestation_combined_buffer_3km$fc2010.y) - (deforestation_combined_buffer_3km$fc2011.x + deforestation_combined_buffer_3km$fc2011.y))/ (deforestation_combined_buffer_3km$fc2000.x + deforestation_combined_buffer_3km$fc2000.y)
deforestation_combined_buffer_3km$totalflp2012 <- 100*((deforestation_combined_buffer_3km$fc2011.x + deforestation_combined_buffer_3km$fc2011.y) - (deforestation_combined_buffer_3km$fc2012.x + deforestation_combined_buffer_3km$fc2012.y))/ (deforestation_combined_buffer_3km$fc2000.x + deforestation_combined_buffer_3km$fc2000.y)
deforestation_combined_buffer_3km$totalflp2013 <- 100*((deforestation_combined_buffer_3km$fc2012.x + deforestation_combined_buffer_3km$fc2012.y) - (deforestation_combined_buffer_3km$fc2013.x + deforestation_combined_buffer_3km$fc2013.y))/ (deforestation_combined_buffer_3km$fc2000.x + deforestation_combined_buffer_3km$fc2000.y)
deforestation_combined_buffer_3km$totalflp2014 <- 100*((deforestation_combined_buffer_3km$fc2013.x + deforestation_combined_buffer_3km$fc2013.y) - (deforestation_combined_buffer_3km$fc2014.x + deforestation_combined_buffer_3km$fc2014.y))/ (deforestation_combined_buffer_3km$fc2000.x + deforestation_combined_buffer_3km$fc2000.y)
deforestation_combined_buffer_3km$totalflp2015 <- 100*((deforestation_combined_buffer_3km$fc2014.x + deforestation_combined_buffer_3km$fc2014.y) - (deforestation_combined_buffer_3km$fc2015.x + deforestation_combined_buffer_3km$fc2015.y))/ (deforestation_combined_buffer_3km$fc2000.x + deforestation_combined_buffer_3km$fc2000.y)
deforestation_combined_buffer_3km$totalflp2016 <- 100*((deforestation_combined_buffer_3km$fc2015.x + deforestation_combined_buffer_3km$fc2015.y) - (deforestation_combined_buffer_3km$fc2016.x + deforestation_combined_buffer_3km$fc2016.y))/ (deforestation_combined_buffer_3km$fc2000.x + deforestation_combined_buffer_3km$fc2000.y)
deforestation_combined_buffer_3km$totalflp2017 <- 100*((deforestation_combined_buffer_3km$fc2016.x + deforestation_combined_buffer_3km$fc2016.y) - (deforestation_combined_buffer_3km$fc2017.x + deforestation_combined_buffer_3km$fc2017.y))/ (deforestation_combined_buffer_3km$fc2000.x + deforestation_combined_buffer_3km$fc2000.y)
deforestation_combined_buffer_3km$totalflp2018 <- 100*((deforestation_combined_buffer_3km$fc2017.x + deforestation_combined_buffer_3km$fc2017.y) - (deforestation_combined_buffer_3km$fc2018.x + deforestation_combined_buffer_3km$fc2018.y))/ (deforestation_combined_buffer_3km$fc2000.x + deforestation_combined_buffer_3km$fc2000.y)
deforestation_combined_buffer_3km$totalflp2019 <- 100*((deforestation_combined_buffer_3km$fc2018.x + deforestation_combined_buffer_3km$fc2018.y) - (deforestation_combined_buffer_3km$fc2019.x + deforestation_combined_buffer_3km$fc2019.y))/ (deforestation_combined_buffer_3km$fc2000.x + deforestation_combined_buffer_3km$fc2000.y)
deforestation_combined_buffer_3km$totalflp2020 <- 100*((deforestation_combined_buffer_3km$fc2019.x + deforestation_combined_buffer_3km$fc2019.y) - (deforestation_combined_buffer_3km$fc2020.x + deforestation_combined_buffer_3km$fc2020.y))/ (deforestation_combined_buffer_3km$fc2000.x + deforestation_combined_buffer_3km$fc2000.y)
deforestation_combined_buffer_3km$totalflp2021 <- 100*((deforestation_combined_buffer_3km$fc2020.x + deforestation_combined_buffer_3km$fc2020.y) - (deforestation_combined_buffer_3km$fc2021.x + deforestation_combined_buffer_3km$fc2021.y))/ (deforestation_combined_buffer_3km$fc2000.x + deforestation_combined_buffer_3km$fc2000.y)

deforestation_combined2_buffer_3km <- deforestation_combined_buffer_3km %>% dplyr::select(NOM, totalflp2001:totalflp2021)


deforestation_combined3_buffer_3km <- deforestation_combined2_buffer_3km  %>% pivot_longer(cols=totalflp2001:totalflp2021, names_to="Year", values_to="PercentDeforest") 

deforestation_combined3_buffer_3km$Year <- gsub( "totalflp", "", deforestation_combined3_buffer_3km$Year)
deforestation_combined3_buffer_3km <- deforestation_combined3_buffer_3km %>% rename(Name=NOM)

##PRECIP DATA
PA_area <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/MNP_PA_Dissolve_TableToExcel.xlsx")
Buffer_3km_area <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_3km_TableToExcel.xlsx")

master1_full <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/MasterSheet_full.csv")
Buffer_3km_precip <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_buffer_3km/Precip_buffer_3km.csv")
Buffer_3km_precip <- Buffer_3km_precip %>% dplyr::select(Name,MEAN,Year)
Buffer_3km_precip <- Buffer_3km_precip %>% rename("Precipitation" = "MEAN")

master1_precip <- master1_full %>% dplyr::select(Name,precipitation,Year)

PA_area <- PA_area %>% rename(Name=Field)
PA_area$Name[PA_area$Name=="Bemaraha"]<- "Bemaraha (combined)"
PA_area$Name[PA_area$Name=="Baie de Baly/Tsingy de Namoroka"]<- "Namoroka/Baie de Baly"
PA_area$Name[PA_area$Name=="Mantadia"]<- "Andasibe/Analamazaotra"

PA_within_precip <- full_join(PA_area, master1_precip , by=c("Name"))
PA_within_precip <- PA_within_precip %>% rename("precip_within" = "precipitation")
PA_within_precip <- PA_within_precip %>% rename("PA_area" = "Shape_Area_km2")



Buffer_3km_area <- Buffer_3km_area %>% rename(Name=Field)
Buffer_3km_area$Name[Buffer_3km_area$Name=="Bemaraha"]<- "Bemaraha (combined)"
Buffer_3km_area$Name[Buffer_3km_area$Name=="Baie de Baly/Tsingy de Namoroka"]<- "Namoroka/Baie de Baly"
Buffer_3km_area$Name[Buffer_3km_area$Name=="Mantadia"]<- "Andasibe/Analamazaotra"

PA_outside_precip <- full_join(Buffer_3km_area, Buffer_3km_precip , by=c("Name"))
PA_outside_precip <- PA_outside_precip %>% rename("precip_outside" = "Precipitation")
PA_outside_precip <- PA_outside_precip %>% rename("Buffer_area" = "Shape_Area")


Combined_precip <- left_join(PA_outside_precip, PA_within_precip , by=c("Name","Year"))
Combined_precip$precip_within <- Combined_precip$precip_within*12
Combined_precip$combined_precip <- ((Combined_precip$Buffer_area * Combined_precip$precip_outside) + (Combined_precip$PA_area * Combined_precip$precip_within))/(Combined_precip$PA_area + Combined_precip$Buffer_area)


##ANALYSIS - FULL

master1_full_combined <- master1_full %>% dplyr::select(Name, Tourism, Year, PopDensity, funding, funding_by_area, area, OilPrice, Regions, Deflation, OilPriceAdj)
master1_full_combined$Year <- as.character(master1_full_combined$Year)

master_buffer_combined <- full_join(deforestation_combined3_buffer_3km, master1_full_combined , by=c("Name", "Year"))
Combined_precip$Year <- as.character(Combined_precip$Year)
master_buffer_combined <- full_join(Combined_precip, master_buffer_combined , by=c("Name", "Year"))

write.csv(master_buffer_combined, "~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_buffer_3km/master_buffer_combined.csv")

```

```{r Aggregate}

mada_1990_precip_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_3km/ZonalSt_Buffer_3km_Pre_1990_TableToExcel.xlsx")
mada_1991_precip_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_3km/ZonalSt_Buffer_3km_Pre_1991_TableToExcel.xlsx")
mada_1992_precip_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_3km/ZonalSt_Buffer_3km_Pre_1992_TableToExcel.xlsx")
mada_1993_precip_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_3km/ZonalSt_Buffer_3km_Pre_1993_TableToExcel.xlsx")
mada_1994_precip_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_3km/ZonalSt_Buffer_3km_Pre_1994_TableToExcel.xlsx")
mada_1995_precip_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_3km/ZonalSt_Buffer_3km_Pre_1995_TableToExcel.xlsx")
mada_1996_precip_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_3km/ZonalSt_Buffer_3km_Pre_1996_TableToExcel.xlsx")
mada_1997_precip_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_3km/ZonalSt_Buffer_3km_Pre_1997_TableToExcel.xlsx")
mada_1998_precip_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_3km/ZonalSt_Buffer_3km_Pre_1998_TableToExcel.xlsx")
mada_1999_precip_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_Buffer_3km/ZonalSt_Buffer_3km_Pre_1999_TableToExcel.xlsx")

mada_1990_precip_buffer_3km$Year <- 1990
mada_1991_precip_buffer_3km$Year <- 1991
mada_1992_precip_buffer_3km$Year <- 1992
mada_1993_precip_buffer_3km$Year <- 1993
mada_1994_precip_buffer_3km$Year <- 1994
mada_1995_precip_buffer_3km$Year <- 1995
mada_1996_precip_buffer_3km$Year <- 1996
mada_1997_precip_buffer_3km$Year <- 1997
mada_1998_precip_buffer_3km$Year <- 1998
mada_1999_precip_buffer_3km$Year <- 1999


precip_buffer_3km_90s <- rbind(mada_1991_precip_buffer_3km,mada_1992_precip_buffer_3km,mada_1993_precip_buffer_3km,mada_1994_precip_buffer_3km,mada_1995_precip_buffer_3km,mada_1996_precip_buffer_3km,mada_1997_precip_buffer_3km,mada_1998_precip_buffer_3km,mada_1999_precip_buffer_3km)
precip_buffer_3km_90s  <- precip_buffer_3km_90s %>% dplyr::select(Year, Field, MEAN)%>% rename(Name=Field, precipitation =MEAN)

precip_buffer_3km_90s$Name[precip_buffer_3km_90s$Name=="Bemaraha"]<- "Bemaraha (combined)"
precip_buffer_3km_90s$Name[precip_buffer_3km_90s$Name=="Baie de Baly/Tsingy de Namoroka"]<- "Namoroka/Baie de Baly"
precip_buffer_3km_90s$Name[precip_buffer_3km_90s$Name=="Mantadia"]<- "Andasibe/Analamazaotra"



mada_precip_buffer_3km1 <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_buffer_3km/Precip_buffer_3km.csv")
mada_precip_buffer_3km1  <- mada_precip_buffer_3km1 %>% dplyr::select(Year, Name, MEAN)%>% rename( precipitation =MEAN)



mada_precip_buffer_3km_full <- rbind(precip_buffer_3km_90s, mada_precip_buffer_3km1)
write.csv(mada_precip_buffer_3km_full, "~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Precip_buffer_3km/mada_precip_buffer_3km_full.csv")

precip_buffer_agg2000 <- mada_precip_buffer_3km_full %>% filter (Year<=2000) %>% group_by(Name)%>% summarise(precipitation2000 =mean(precipitation))
precip_buffer_agg2010 <- mada_precip_buffer_3km_full %>% filter (Year<=2010,Year >2000) %>% group_by(Name)%>% summarise(precipitation2010 =mean(precipitation))
precip_buffer_agg2020 <- mada_precip_buffer_3km_full %>% filter (Year<=2020,Year>2010) %>% group_by(Name)%>% summarise(precipitation2020 =mean(precipitation))

precip_agg_buffer <- left_join(precip_buffer_agg2000, precip_buffer_agg2010, by="Name")
precip_agg_buffer <- left_join(precip_agg_buffer, precip_buffer_agg2020, by="Name")

precip_agg_buffer <- precip_agg_buffer %>% pivot_longer(cols = precipitation2000:precipitation2020, names_to="Year", values_to="precipitation") 
precip_agg_buffer$Year <- gsub("precipitation", "" ,precip_agg_buffer$Year)




##DEFORESTATION

deforestation_dat2_buffer_3km <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_buffer_3km/deforestation_dat2_buffer_3km.csv")

deforestation_agg_buffer <- deforestation_dat2_buffer_3km

deforestation_agg_buffer$agg2000 <- (deforestation_agg_buffer$fc1990 - deforestation_agg_buffer$fc2000)/ deforestation_agg_buffer$fc1990
deforestation_agg_buffer$agg2010 <- (deforestation_agg_buffer$fc2000 - deforestation_agg_buffer$fc2010)/ deforestation_agg_buffer$fc1990
deforestation_agg_buffer$agg2020 <- (deforestation_agg_buffer$fc2010 - deforestation_agg_buffer$fc2020)/ deforestation_agg_buffer$fc1990

deforestation_agg2_buffer <- deforestation_agg_buffer %>% dplyr::select(Field, agg2000, agg2010, agg2020) %>% pivot_longer(cols=agg2000:agg2020, names_to="Year", values_to="PercentDeforest") %>% rename(Name=Field)

deforestation_agg2_buffer$Year <- gsub( "agg", "", deforestation_agg2_buffer$Year)
deforestation_agg2_buffer$Year <- as.numeric(deforestation_agg2_buffer$Year)

deforestation_agg2_buffer$Name[deforestation_agg2_buffer$Name=="Bemaraha"]<- "Bemaraha (combined)"
deforestation_agg2_buffer$Name[deforestation_agg2_buffer$Name=="Baie de Baly/Tsingy de Namoroka"]<- "Namoroka/Baie de Baly"
deforestation_agg2_buffer$Name[deforestation_agg2_buffer$Name=="Mantadia"]<- "Andasibe/Analamazaotra"


#Compilation
master_agg1 <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/aggregate_master.csv")
master_agg1$Tourism <- master_agg1$Tourism/1000

master_agg1 <- master_agg1 %>% dplyr::select(-PercentDeforest, -PercentDeforest_old, -precipitation, -area)

master_agg1 <- left_join( master_agg1, deforestation_agg2_buffer,by=c("Name", "Year"))
precip_agg_buffer$Year <- as.numeric(precip_agg_buffer$Year)
master_agg1 <- left_join( master_agg1, precip_agg_buffer,by=c("Name", "Year"))

master_agg_analysis <- master_agg1
master_agg_analysis$PercentDeforest <- master_agg_analysis$PercentDeforest*100
master_agg_analysis$precipitation <- master_agg_analysis$precipitation/12

write.csv(master_agg_analysis, "~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_buffer_3km/master_agg_analysis_3km")



```

```{r Aggregate Combined}

master_agg <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/aggregate_master.csv")
master_agg$Tourism <- master_agg$Tourism/1000
master_agg$PercentDeforest <- master_agg$PercentDeforest*100

PA_area <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/MNP_PA_Dissolve_TableToExcel.xlsx")
Buffer_3km_area <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_3km_TableToExcel.xlsx")

PA_area <- PA_area %>% rename(Name=Field)
PA_area$Name[PA_area$Name=="Bemaraha"]<- "Bemaraha (combined)"
PA_area$Name[PA_area$Name=="Baie de Baly/Tsingy de Namoroka"]<- "Namoroka/Baie de Baly"
PA_area$Name[PA_area$Name=="Mantadia"]<- "Andasibe/Analamazaotra"

Buffer_3km_area <- Buffer_3km_area %>% rename(Name=Field)
Buffer_3km_area$Name[Buffer_3km_area$Name=="Bemaraha"]<- "Bemaraha (combined)"
Buffer_3km_area$Name[Buffer_3km_area$Name=="Baie de Baly/Tsingy de Namoroka"]<- "Namoroka/Baie de Baly"
Buffer_3km_area$Name[Buffer_3km_area$Name=="Mantadia"]<- "Andasibe/Analamazaotra"

master_agg  <- left_join(master_agg, PA_area, by=c("Name"))

deforestation_dat1990 <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/CommuneForestCover/MNP1990fc.csv")
deforestation_dat1990$Name <- deforestation_dat1990$NOM

deforestation_dat1990$Name[deforestation_dat1990$Name=="Analamazaotra"]<- "Andasibe/Analamazaotra"
deforestation_dat1990$Name[deforestation_dat1990$Name=="Mantadia"]<- "Andasibe/Analamazaotra"
deforestation_dat1990$Name[deforestation_dat1990$Name=="Bemaraha (partie Nord)"]<- "Bemaraha (combined)"
deforestation_dat1990$Name[deforestation_dat1990$Name=="Bemaraha partie Sud"]<- "Bemaraha (combined)"
deforestation_dat1990$Name[deforestation_dat1990$Name=="Tsingy de Namoroka"]<- "Namoroka/Baie de Baly"
deforestation_dat1990$Name[deforestation_dat1990$Name=="Baie de Baly"]<- "Namoroka/Baie de Baly"
deforestation_dat1990$Name[deforestation_dat1990$Name=="Zahamena RNI"]<- "Zahamena" 
deforestation_dat1990$Name[deforestation_dat1990$Name=="Nosy Mangabe"]<- "Masoala"
deforestation_dat1990$Name[deforestation_dat1990$Name=="Foret d'Ambre"]<-  "Montagne d'Ambre" 

deforestation_dat1990 <- deforestation_dat1990 %>% dplyr::select(Name, SUM)
deforestation_dat1990 <- deforestation_dat1990 %>% rename(PA_1990fc=SUM)


deforestation1_dat1990 <- aggregate(. ~ Name , data = deforestation_dat1990,FUN = sum)

master_agg  <- left_join(master_agg, deforestation1_dat1990, by=c("Name"))
master_agg <- master_agg %>% rename(precip_PA=precipitation)
master_agg <- master_agg %>% rename(PercentDeforest_PA=PercentDeforest)
master_agg <- master_agg %>% rename(Area_PA=Shape_Area_km2)




master_agg_3km <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_buffer_3km/master_agg_analysis_3km")
master_agg_3km  <- left_join(master_agg_3km, Buffer_3km_area, by=c("Name"))

deforestation_dat1990_buffer_3km <- read_xlsx("~/Documents/GitHub/TourismDeforestation/Data/Buffer_Outputs/Forest_Buffer_3km/ZonalSt_Buffer_3km_forest_Laborde_1990_TableToExcel.xlsx")
deforestation_dat1990_buffer_3km <- deforestation_dat1990_buffer_3km %>% rename(Name=Field)

deforestation_dat1990_buffer_3km$Name[deforestation_dat1990_buffer_3km$Name=="Bemaraha"]<- "Bemaraha (combined)"
deforestation_dat1990_buffer_3km$Name[deforestation_dat1990_buffer_3km$Name=="Baie de Baly/Tsingy de Namoroka"]<- "Namoroka/Baie de Baly"
deforestation_dat1990_buffer_3km$Name[deforestation_dat1990_buffer_3km$Name=="Mantadia"]<- "Andasibe/Analamazaotra"

deforestation_dat1990_buffer_3km <- deforestation_dat1990_buffer_3km %>% dplyr::select(Name, SUM)
deforestation_dat1990_buffer_3km <- deforestation_dat1990_buffer_3km %>% rename(Buffer_1990fc=SUM)

master_agg_3km  <- left_join(master_agg_3km, deforestation_dat1990_buffer_3km, by=c("Name"))
master_agg_3km <- master_agg_3km %>% rename(precip_buffer=precipitation)
master_agg_3km <- master_agg_3km %>% rename(PercentDeforest_buffer=PercentDeforest)
master_agg_3km <- master_agg_3km %>% rename(Area_Buffer=Shape_Area)

master1_agg_3km <- master_agg_3km %>% dplyr::select(Name, Year, PercentDeforest_buffer, Area_Buffer, precip_buffer, Buffer_1990fc)

master_agg_combined <- left_join(master_agg, master1_agg_3km, by=c("Name","Year"))

master_agg_combined$PercentDeforest <- 100*((master_agg_combined$PA_1990fc * master_agg_combined$PercentDeforest_PA/100)+ (master_agg_combined$Buffer_1990fc * master_agg_combined$PercentDeforest_buffer/100))/(master_agg_combined$Buffer_1990fc+master_agg_combined$PA_1990fc)
master_agg_combined$Precipitation <- ((master_agg_combined$precip_PA*master_agg_combined$Area_PA)+(master_agg_combined$precip_buffer*master_agg_combined$Area_Buffer))/(master_agg_combined$Area_Buffer + master_agg_combined$Area_PA)


fem_full_formula <- as.formula("PercentDeforest ~ Tourism + PopDensity + Precipitation + OilPriceAdj | Year + Name | 0 | Regions")
dd_reg_full_agg_combined <- felm(fem_full_formula, data = master_agg_combined)

fem_full_formula_precip <- as.formula("PercentDeforest ~ Tourism + PopDensity +OilPriceAdj | Year + Name | 0 | Regions")
dd_reg_full_agg_combined_precip <- felm(fem_full_formula_precip, data = master_agg_combined)

#Summary tables
summary_dd_reg_full_agg_buffer <- summary(dd_reg_full_agg_combined)
agg_combined <- as.data.frame(summary_dd_reg_full_agg_buffer$coefficients)
agg_combined$Analysis <- "PA"
agg_combined$Variable <- rownames(agg_combined)
agg_combined$Location <- "Inside PA + Buffer 3km"
agg_combined$`Pr(>|t|)` <- round(agg_combined$`Pr(>|t|)`, digits = 3)
agg_combined$Estimate <- round(agg_combined$Estimate, digits = 3)
agg_combined$`Cluster s.e.` <- round(agg_combined$`Cluster s.e.`, digits = 3)
agg_combined$`t value` <- round(agg_combined$`t value`, digits = 3)
agg_combined$Variable[agg_combined$Variable=="PopDensity"] <- "Pop. Density"
agg_combined$Variable[agg_combined$Variable=="OilPriceAdj"] <- "Oil Price"
agg_combined$Variable[agg_combined$Variable=="Tourism1"] <- "Tourism"
agg_combined$Variable[agg_combined$Variable=="PopDensityCommune"] <- "Pop. Density"

100*agg_combined[1,1]/mean(master_agg_combined$PercentDeforest)

write.csv(agg_combined, "~/Documents/GitHub/TourismDeforestation/Data/regression_table_combined_agg.csv")

```
