TourismDeforestationSubmission / 00b_DataframeCompilation.Rmd
00b_DataframeCompilation.Rmd
Raw
---
title: "02_DataframeCompilation"
author: "Camille"
date: "3/1/2023"
output: html_document
---

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

```{r Load Packages and Data}
library(tidyverse)
library(lubridate)

#read confounder data
precipitation <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/CleanedConfounders/precipitation_points.csv")
PopDensity <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/CleanedConfounders/PopDensity_points.csv")
funding <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/CleanedConfounders/Funding.csv")
forest <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/CommuneForestCover/deforestation_full_points.csv")
tourism <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/tourism.csv")
forest_funding <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/CommuneForestCover/deforestation_full_funding_points.csv")

#read confounder data for the buffer analysis 
precipitation_buffer <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/CleanedConfounders/precipitation_points_buffer.csv")
PopDensity_buffer <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/CleanedConfounders/PopDensity_points_buffer.csv")
forest_buffer <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/CommuneForestCover/deforestation_full_points_buffer.csv")
tourism_buffer <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/tourism.csv")

```

```{r Compile all variables}
#FULL ANALYSIS
precipitation <- precipitation %>% filter(Year!=2000) %>% select(-X)
PopDensity <- PopDensity %>% select(-X, -Name)
confounders_full <- full_join(precipitation, PopDensity, by=c("Year", "OBJECTID"))
confounders_full <- left_join(confounders_full, funding, by=c("Year", "Name"))
forest <- forest %>% select(-Name) %>% filter(PercentDeforest < 101, PercentDeforest>-0.00001)
deforestation_full <- full_join(confounders_full, forest, by=c("Year", "OBJECTID"))
deforestation_full2 <- full_join(deforestation_full, tourism , by=c("Year", "Name"))
deforestation_full3 <- deforestation_full2 %>% select(Name, PercentDeforest, Tourism, Year, precipitation, PopDensity, OBJECTID)
deforestation_full4 <- deforestation_full3 %>% filter(Name !="Sahamalaza", Name!="Mikea")


#BUFFER ANALYSIS
precipitation_buffer <- precipitation_buffer %>% filter(Year!=2000) %>% select(-X)
PopDensity_buffer <- PopDensity_buffer %>% select(-X, -Name)
confounders_full_buffer <- full_join(precipitation_buffer, PopDensity_buffer, by=c("Year", "OBJECTID"))
forest_buffer <- forest_buffer %>% select(-Name) %>% filter(PercentDeforest < 101, PercentDeforest>-0.00001)
deforestation_full_buffer <- full_join(confounders_full_buffer, forest, by=c("Year", "OBJECTID"))
deforestation_full2_buffer <- full_join(deforestation_full_buffer, tourism , by=c("Year", "Name"))
deforestation_full3_buffer <- deforestation_full2_buffer %>% select(Name, PercentDeforest, Tourism, Year, precipitation, PopDensity, OBJECTID)
deforestation_full4_buffer <- deforestation_full3_buffer %>% filter(Name !="Sahamalaza", Name!="Mikea")

```

```{r Add oil prices}
oil_dat <- read.csv("~/Documents/GitHub/TourismDeforestation/Data/oil_prices2.csv")
oil_dat$DATE <- as.Date(oil_dat$DATE)
oil_dat$DCOILBRENTEU <- as.numeric(oil_dat$DCOILBRENTEU)
oil_dat2 <- oil_dat %>% rename(Date=DATE, OilPrice = DCOILBRENTEU) %>% mutate(Year = year(Date)) %>%drop_na(OilPrice)%>% group_by(Year) %>% summarise(OilPrice =mean(OilPrice)) %>% filter(Year >1991)

deforestation_full4 <- left_join(deforestation_full4, oil_dat2, by="Year")
deforestation_full4_buffer <- left_join(deforestation_full4_buffer, oil_dat2, by="Year")


```

```{r Add in region information for each park}
#full dataset
deforestation_full4$Regions <- deforestation_full4$Name
deforestation_full4$Regions[deforestation_full4$Regions=="Ambatovaky"] <- "Analanjorofo"
deforestation_full4$Regions[deforestation_full4$Regions=="Ambohijanahary"] <- "Alaotra Mangoro"
deforestation_full4$Regions[deforestation_full4$Regions=="Ambohitantely"] <- "Analamanga"
deforestation_full4$Regions[deforestation_full4$Regions=="Analamerana"] <- "Diana"
deforestation_full4$Regions[deforestation_full4$Regions=="Andasibe/Analamazaotra"] <- "Alaotra Mangoro"
deforestation_full4$Regions[deforestation_full4$Regions=="Andohahela"] <- "Anosy"
deforestation_full4$Regions[deforestation_full4$Regions=="Andranomena"] <- "Sava"
deforestation_full4$Regions[deforestation_full4$Regions=="Andringitra"] <- "Haute Matsiatra"
deforestation_full4$Regions[deforestation_full4$Regions=="Anjanaharibe-Sud"] <- 
deforestation_full4$Regions[deforestation_full4$Regions=="Ankarafantsika"] <- "Boeny"
deforestation_full4$Regions[deforestation_full4$Regions=="Ankarana"] <- "Sava"
deforestation_full4$Regions[deforestation_full4$Regions=="Bemaraha (combined)"] <- "Melaky" 
deforestation_full4$Regions[deforestation_full4$Regions=="Bemarivo"] <- "Melaky" 
deforestation_full4$Regions[deforestation_full4$Regions=="Betampona"] <- "Atsinanana"
deforestation_full4$Regions[deforestation_full4$Regions=="Bezaha Mahafaly"] <- "Atsimo Andrefana"
deforestation_full4$Regions[deforestation_full4$Regions=="Bora"] <- "Sofia"
deforestation_full4$Regions[deforestation_full4$Regions=="Cap Sainte Marie"] <- "Androy"
deforestation_full4$Regions[deforestation_full4$Regions=="Foret d'Ambre"] <- "Diana"
deforestation_full4$Regions[deforestation_full4$Regions=="Isalo"] <- "Ihorombe"
deforestation_full4$Regions[deforestation_full4$Regions=="Kalambatritra"] <- "Anosy"
deforestation_full4$Regions[deforestation_full4$Regions=="Kasijy"] <- "Betsiboka"
deforestation_full4$Regions[deforestation_full4$Regions=="Kirindy Mitea"] <- "Menabe"
deforestation_full4$Regions[deforestation_full4$Regions=="Lokobe"] <- "Diana"
deforestation_full4$Regions[deforestation_full4$Regions=="Mananara-Nord"] <- "Analanjorofo"
deforestation_full4$Regions[deforestation_full4$Regions=="Mangerivola"] <- "Atsinanana"
deforestation_full4$Regions[deforestation_full4$Regions=="Maningoza"] <- "Melaky"
deforestation_full4$Regions[deforestation_full4$Regions=="Manombo"] <- "Atsimo Atsinanana"
deforestation_full4$Regions[deforestation_full4$Regions=="Manongarivo"] <- "Diana"
deforestation_full4$Regions[deforestation_full4$Regions=="Marojejy"] <- "Sava"
deforestation_full4$Regions[deforestation_full4$Regions=="Marotandrano"] <- "Sofia"
deforestation_full4$Regions[deforestation_full4$Regions=="Masoala"] <- "Sava"
deforestation_full4$Regions[deforestation_full4$Regions=="Midongy du Sud"] <- "Atsimo Atsinanana"
deforestation_full4$Regions[deforestation_full4$Regions=="Pic d'Ivohibe"] <- "Ihorombe"
deforestation_full4$Regions[deforestation_full4$Regions=="Ranomafana"] <- "Haute Matsiatra" #in two regions.. went with the one that has another park prepresented
deforestation_full4$Regions[deforestation_full4$Regions=="Tampoketsa Analamaitso"] <- "Sofia"
deforestation_full4$Regions[deforestation_full4$Regions=="Tsaratanana"] <- "Betsiboka"
deforestation_full4$Regions[deforestation_full4$Regions=="Tsimanampesotse"] <- "Atsimo Andrefana"
deforestation_full4$Regions[deforestation_full4$Regions=="Zahamena"] <- "Alaotra Mangoro"
deforestation_full4$Regions[deforestation_full4$Regions=="Zombitse/Vohibasia"] <- "Atsimo Andrefana"
deforestation_full4$Regions[deforestation_full4$Regions=="Namoroka/Baie de Baly"] <- "Boeny"
deforestation_full4$Regions[deforestation_full4$Regions=="Montagne d'Ambre"] <- "Diana"



#deflation data
deforestation_full4$Deflation <- deforestation_full4$Regions
deforestation_full4$Deflation[deforestation_full4$Deflation=="Analanjorofo"] <- 0.81834
deforestation_full4$Deflation[deforestation_full4$Deflation=="Alaotra Mangoro"] <- 0.844633
deforestation_full4$Deflation[deforestation_full4$Deflation=="Analamanga"] <- 1
deforestation_full4$Deflation[deforestation_full4$Deflation=="Diana"] <- 0.706547
deforestation_full4$Deflation[deforestation_full4$Deflation=="Anosy"] <- 0.856932
deforestation_full4$Deflation[deforestation_full4$Deflation=="Sava"] <- 0.814796
deforestation_full4$Deflation[deforestation_full4$Deflation=="Haute Matsiatra"] <-0.947125
deforestation_full4$Deflation[deforestation_full4$Deflation=="Boeny"] <- 1.007638
deforestation_full4$Deflation[deforestation_full4$Deflation=="Melaky"] <- 0.884184
deforestation_full4$Deflation[deforestation_full4$Deflation=="Atsinanana"] <- 0.973824
deforestation_full4$Deflation[deforestation_full4$Deflation=="Atsimo Andrefana"] <- 0.789781
deforestation_full4$Deflation[deforestation_full4$Deflation=="Sofia"] <- 0.722568
deforestation_full4$Deflation[deforestation_full4$Deflation=="Androy"] <- 0.805615
deforestation_full4$Deflation[deforestation_full4$Deflation=="Ihorombe"] <-0.711432 
deforestation_full4$Deflation[deforestation_full4$Deflation=="Betsiboka"] <-0.747884
deforestation_full4$Deflation[deforestation_full4$Deflation=="Menabe"] <- 0.626845
deforestation_full4$Deflation[deforestation_full4$Deflation=="Atsimo Atsinanana"] <- 0.700499
deforestation_full4$Deflation[deforestation_full4$Deflation=="Boeny"] <- 1.007638

deforestation_full4$Deflation <- as.numeric(deforestation_full4$Deflation)
deforestation_full4$OilPriceAdj <- deforestation_full4$OilPrice* deforestation_full4$Deflation



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






#3km buffer 
deforestation_full4_buffer$Regions <- deforestation_full4_buffer$Name
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Ambatovaky"] <- "Analanjorofo"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Ambohijanahary"] <- "Alaotra Mangoro"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Ambohitantely"] <- "Analamanga"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Analamerana"] <- "Diana"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Andasibe/Analamazaotra"] <- "Alaotra Mangoro"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Andohahela"] <- "Anosy"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Andranomena"] <- "Sava"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Andringitra"] <- "Haute Matsiatra"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Anjanaharibe-Sud"] <- 
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Ankarafantsika"] <- "Boeny"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Ankarana"] <- "Sava"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Bemaraha (combined)"] <- "Melaky" 
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Bemarivo"] <- "Melaky" 
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Betampona"] <- "Atsinanana"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Bezaha Mahafaly"] <- "Atsimo Andrefana"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Bora"] <- "Sofia"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Cap Sainte Marie"] <- "Androy"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Foret d'Ambre"] <- "Diana"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Isalo"] <- "Ihorombe"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Kalambatritra"] <- "Anosy"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Kasijy"] <- "Betsiboka"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Kirindy Mitea"] <- "Menabe"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Lokobe"] <- "Diana"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Mananara-Nord"] <- "Analanjorofo"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Mangerivola"] <- "Atsinanana"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Maningoza"] <- "Melaky"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Manombo"] <- "Atsimo Atsinanana"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Manongarivo"] <- "Diana"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Marojejy"] <- "Sava"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Marotandrano"] <- "Sofia"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Masoala"] <- "Sava"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Midongy du Sud"] <- "Atsimo Atsinanana"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Pic d'Ivohibe"] <- "Ihorombe"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Ranomafana"] <- "Haute Matsiatra" #in two regions.. went with the one that has another park prepresented
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Tampoketsa Analamaitso"] <- "Sofia"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Tsaratanana"] <- "Betsiboka"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Tsimanampesotse"] <- "Atsimo Andrefana"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Zahamena"] <- "Alaotra Mangoro"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Zombitse/Vohibasia"] <- "Atsimo Andrefana"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Namoroka/Baie de Baly"] <- "Boeny"
deforestation_full4_buffer$Regions[deforestation_full4_buffer$Regions=="Montagne d'Ambre"] <- "Diana"



#deflation data
deforestation_full4_buffer$Deflation <- deforestation_full4_buffer$Regions
deforestation_full4_buffer$Deflation[deforestation_full4_buffer$Deflation=="Analanjorofo"] <- 0.81834
deforestation_full4_buffer$Deflation[deforestation_full4_buffer$Deflation=="Alaotra Mangoro"] <- 0.844633
deforestation_full4_buffer$Deflation[deforestation_full4_buffer$Deflation=="Analamanga"] <- 1
deforestation_full4_buffer$Deflation[deforestation_full4_buffer$Deflation=="Diana"] <- 0.706547
deforestation_full4_buffer$Deflation[deforestation_full4_buffer$Deflation=="Anosy"] <- 0.856932
deforestation_full4_buffer$Deflation[deforestation_full4_buffer$Deflation=="Sava"] <- 0.814796
deforestation_full4_buffer$Deflation[deforestation_full4_buffer$Deflation=="Haute Matsiatra"] <-0.947125
deforestation_full4_buffer$Deflation[deforestation_full4_buffer$Deflation=="Boeny"] <- 1.007638
deforestation_full4_buffer$Deflation[deforestation_full4_buffer$Deflation=="Melaky"] <- 0.884184
deforestation_full4_buffer$Deflation[deforestation_full4_buffer$Deflation=="Atsinanana"] <- 0.973824
deforestation_full4_buffer$Deflation[deforestation_full4_buffer$Deflation=="Atsimo Andrefana"] <- 0.789781
deforestation_full4_buffer$Deflation[deforestation_full4_buffer$Deflation=="Sofia"] <- 0.722568
deforestation_full4_buffer$Deflation[deforestation_full4_buffer$Deflation=="Androy"] <- 0.805615
deforestation_full4_buffer$Deflation[deforestation_full4_buffer$Deflation=="Ihorombe"] <-0.711432 
deforestation_full4_buffer$Deflation[deforestation_full4_buffer$Deflation=="Betsiboka"] <-0.747884
deforestation_full4_buffer$Deflation[deforestation_full4_buffer$Deflation=="Menabe"] <- 0.626845
deforestation_full4_buffer$Deflation[deforestation_full4_buffer$Deflation=="Atsimo Atsinanana"] <- 0.700499
deforestation_full4_buffer$Deflation[deforestation_full4_buffer$Deflation=="Boeny"] <- 1.007638

deforestation_full4_buffer$Deflation <- as.numeric(deforestation_full4_buffer$Deflation)
deforestation_full4_buffer$OilPriceAdj <- deforestation_full4_buffer$OilPrice* deforestation_full4_buffer$Deflation



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

```