[rsplus] czlsfinal

Viewer

copydownloadembedprintName: czlsfinal
  1. ### SETUP ###
  2.  
  3. library(qwraps2)
  4. library(ggridges)
  5. library(ggplot2)
  6. library(sp)
  7. library(tidyverse)
  8. library(ggplot2)
  9. library(readxl)
  10. getwd()
  11.  
  12. ### Reading data and filtering ###
  13.  
  14. china <- read_csv("00_Data/GFW-CHN.csv")
  15. fao_chn <- read_csv("00_Data/FAO-fspfsc_CHN.csv")
  16. fao_chn <-  fao_chn %>% filter(Code == "CHN")
  17.  
  18. china$date <- as.Date(china$date)
  19.  
  20. china_2012 <- filter(china, date <= as.Date('2012-12-31'))
  21. china_2013 <-
  22.   filter(china, (date < as.Date('2013-12-31') &
  23.                    date >= as.Date('2013-01-01')))
  24. china_2014 <-
  25.   filter(china, (date < as.Date('2014-12-31') &
  26.                    date >= as.Date('2014-01-01')))
  27. china_2015 <-
  28.   filter(china, (date < as.Date('2015-12-31') &
  29.                    date >= as.Date('2015-01-01')))
  30. china_2016 <-
  31.   filter(china, (date < as.Date('2016-12-31') &
  32.                    date >= as.Date('2016-01-01')))
  33. china_2017 <-
  34.   filter(china, (date < as.Date('2017-12-31') &
  35.                    date >= as.Date('2017-01-01')))
  36. china_2018 <-
  37.   filter(china, (date < as.Date('2018-12-31') &
  38.                    date >= as.Date('2018-01-01')))
  39. china_2019 <-
  40.   filter(china, (date < as.Date('2019-12-31') &
  41.                    date >= as.Date('2019-01-01')))
  42. china_2020 <-
  43.   filter(china, (date < as.Date('2020-12-31') &
  44.                    date >= as.Date('2020-01-01')))
  45.  
  46.  
  47. # Get random samples from everything.
  48. china_2012$random <- runif(nrow(china_2012)min = 0max = 1)
  49. samples_2012 <- china_2012 %>% filter(random <= .1)
  50. china_2013$random <- runif(nrow(china_2013)min = 0max = 1)
  51. samples_2013 <- china_2013 %>% filter(random <= .1)
  52. china_2014$random <- runif(nrow(china_2014)min = 0max = 1)
  53. samples_2014 <- china_2014 %>% filter(random <= .1)
  54. china_2015$random <- runif(nrow(china_2015)min = 0max = 1)
  55. samples_2015 <- china_2015 %>% filter(random <= .1)
  56. china_2016$random <- runif(nrow(china_2016)min = 0max = 1)
  57. samples_2016 <- china_2016 %>% filter(random <= .1)
  58. china_2017$random <- runif(nrow(china_2017)min = 0max = 1)
  59. samples_2017 <- china_2017 %>% filter(random <= .1)
  60. china_2018$random <- runif(nrow(china_2018)min = 0max = 1)
  61. samples_2018 <- china_2018 %>% filter(random <= .1)
  62. china_2019$random <- runif(nrow(china_2019)min = 0max = 1)
  63. samples_2019 <- china_2019 %>% filter(random <= .1)
  64. china_2020$random <- runif(nrow(china_2020)min = 0max = 1)
  65. samples_2020 <- china_2020 %>% filter(random <= .1)
  66.  
  67.  
  68. # Take each long lat and get the distance to chinacenter <- c(104.195, 35.86)
  69. chinacenter <- c(104.19535.86)
  70. samples_2012$center_lon <- 104.195
  71. samples_2012$center_lat <- 35.86
  72. samples_2012$distance <-
  73.   distHaversine(samples_2012[4:3], samples_2012[10:11])
  74. #... repeat for 2012 - 2013
  75.  
  76. samples_2013$center_lon <- 104.195
  77. samples_2013$center_lat <- 35.86
  78. #... repeat for 2012 - 2013
  79. samples_2012$distance <-
  80.   distHaversine(samples_2012[4:3], samples_2012[10:11])
  81. #... repeat for 2012 - 2013
  82. samples_2012$dist_mi <- samples_2012$distance / 1609.34
  83. #... repeat for 2012 - 2013
  84.  
  85.  
  86. # Mean distance in 2012 vs 2020
  87. dist_2012 <- mean(samples_2012$dist_mi)
  88. #... repeat for 2012 - 2013
  89.  
  90. # Mean hours
  91. hrs_2012 <- mean(samples_2012$hours)
  92. #... repeat for 2012 - 2013
  93.  
  94. # Creating the dataframe that we do our analysis with.
  95. chn_distances <- data.frame(
  96.   Year = c(201220132014201520162017201820192020),
  97.   m_dist = c(
  98.     dist_2012,
  99.     dist_2013,
  100.     dist_2014,
  101.     dist_2015,
  102.     dist_2016,
  103.     dist_2017,
  104.     dist_2018,
  105.     dist_2019,
  106.     dist_2020
  107.   ),
  108.   fsp = fao_chn$fspc[52:60],
  109.   fsq = fao_chn$fsqc[52:60],
  110.   pop = fao_chn$Population[52:60],
  111.   m_hrs = c(
  112.     hrs_2012,
  113.     hrs_2013,
  114.     hrs_2014,
  115.     hrs_2015,
  116.     hrs_2016,
  117.     hrs_2017,
  118.     hrs_2018,
  119.     hrs_2019,
  120.     hrs_2020
  121.   )
  122. )
  123.  
  124. # Collapsing all samples into one.
  125. all_samples <-
  126.   rbind(
  127.     samples_2012,
  128.     samples_2013,
  129.     samples_2014,
  130.     samples_2015,
  131.     samples_2016,
  132.     samples_2017,
  133.     samples_2018
  134.     ,
  135.     samples_2019,
  136.     samples_2020
  137.   )
  138. all_samples$year <-
  139.   format(as.Date(all_samples$date, format = "%d/%m/%Y")"%Y")
  140. # Converting to miles
  141. all_samples$dist_mi <- all_samples$distance / 1609.34
  142.  
  143. # Creating bins by distance.
  144. all_samples <- all_samples %>% mutate(bins = case_when(
  145.   (dist_mi < 1501) ~ 1,
  146.   (dist_mi < 3001) ~ 2,
  147.   (dist_mi < 4501) ~ 3,
  148.   (dist_mi < 6001) ~ 4,
  149.   (dist_mi > 6000) ~ 5,
  150. ))
  151.  
  152.  
  153. chn_distances$diffs <- chn_distances$fsp - chn_distances$fsq
  154. chn_distances$m_dist_mi <- chn_distances$m_dist / 1609.34
  155.  
  156. ### ANALYSIS BELOW ###
  157.  
  158.  
  159. # Get summary statistics
  160. summary(fao_chn$fsqc)
  161. summary(fao_chn$fspc)
  162. summary(fao_chn$Population)
  163. sd(fao_chn$fsqc)
  164. sd(fao_chn$fspc)
  165. sd(fao_chn$Population)
  166.  
  167.  
  168. # Plotting supply and product and population, first as histogram then as scatter.
  169. distbyyear <- ggplot(all_samples, aes(= date, y = dist_mi)) +
  170.   geom_point(color = "dodgerblue4", pch = ".") +
  171.   labs(= "Date", y = "Distance") +
  172.   facet_wrap( ~ year, nrow = 3, scales = "free")
  173. ggsave(
  174.   file = "distbyyear.svg",
  175.   plot = distbyyear,
  176.   width = 12,
  177.   height = 10
  178. )
  179.  
  180. # Histogram
  181. hist_byyear <- ggplot(all_samples, aes(= dist_mi)) +
  182.   geom_histogram(binwidth = 100, color = "dodgerblue4") +
  183.   labs(= "Distance (mi)", y = "Frequency") +
  184.   facet_wrap( ~ year, nrow = 3, scales = "free")
  185. hist_byyear
  186. ggsave(
  187.   file = "histbyyear.svg",
  188.   plot = hist_byyear,
  189.   width = 12,
  190.   height = 10
  191. )
  192.  
  193. hg_stack <-
  194.   ggplot(all_samples, aes(= fishing_hours, y = factor(bins), fill = bins)) +
  195.   geom_density_ridges(
  196.     stat = "binline",
  197.     bins = 25,
  198.     scale = .9,
  199.     draw_baseline = TRUE,
  200.     show.legend = TRUE
  201.   ) +
  202.   theme_minimal() +
  203.   labs(= "Fishing Hours", y = "by Distance"title = "Fishing Hours by Distance")
  204. ggsave(
  205.   file = "histstack.svg",
  206.   plot = hg_stack,
  207.   width = 12,
  208.   height = 10
  209. )
  210. hg_stack
  211.  
  212. # Plot box plot & histograms
  213. samples_over1250 <- all_samples %>% filter(dist_mi > 1250)
  214. hist(samples_over1250$dist_mi)
  215.  
  216. boxplot <- ggplot(all_samples, aes(= year, y = dist_mi)) +
  217.   geom_boxplot(outlier.shape = NA)
  218. boxplot_o <- ggplot(samples_over1250, aes(= year, y = dist_mi)) +
  219.   geom_boxplot()
  220.  
  221. # ANOVA + HSD
  222. chn.aov <- aov(dist_mi ~ year, data = all_samples)
  223. summary(chn.aov)
  224. TukeyHSD(chn.aov)
  225.  
  226. # Effect of % change in population on production and consumption per capita.
  227. lm.fao_prod <- lm(fspc ~ log(Population)data = fao_chn)
  228. summary(lm.fao_prod)
  229. lm.fao_cons <- lm(fsqc ~ log(Population)data = fao_chn)
  230. summary(lm.fao_cons)
  231. # Effect of consumption on how much is fished.
  232. lm.fao_cp <- lm(fspc ~ fsqc, data = fao_chn)
  233. summary(lm.fao_cp)
  234.  
  235.  
  236. # Regression between mean dist in miles and fsp.
  237. chn_dist_svg <-
  238.   chn_distances %>%  ggplot(aes(= fsp, y = m_dist_mi), color = "red") +   geom_point(size =
  239.                                                                                          1) +
  240.   geom_smooth(method = 'lm', color = 'blue') +
  241.   theme_minimal() +
  242.   labs(= 'Fish Production', y = 'Mean Distance (mi)'title = 'Mean Distance ~ Fish Production') +
  243.   theme(plot.title = element_text(hjust = 0.5))
  244. ggsave(
  245.   file = "chn_dist.svg",
  246.   plot = chn_dist_svg,
  247.   width = 10,
  248.   height = 10
  249. )
  250.  
  251. # Regression between mean dists and mean hrs.
  252. chn_dist_hrs <-
  253.   chn_distances %>%  ggplot(aes(= m_hrs, y = m_dist), color = "red") +   geom_point(size =
  254.                                                                                         1) +
  255.   geom_smooth(method = 'lm', color = 'blue') +
  256.   theme_minimal() +
  257.   labs(= 'Mean Hours Fishing', y = 'Mean Distance (mi)'title = 'Mean Distance ~ Mean Hours') +
  258.   theme(plot.title = element_text(hjust = 0.5))
  259. ggsave(
  260.   file = "chn_dist_hrs.svg",
  261.   plot = chn_dist_hrs,
  262.   width = 10,
  263.   height = 10
  264. )
  265.  
  266.  
  267.  
  268. lm.dists_fsp <- lm(m_dist_mi ~ fsp, data = chn_distances)
  269. summary(lm.dists_fsp)
  270.  
  271. lm.dists_hrs <- lm(m_dist ~ m_hrs, data = chn_distances)
  272. summary(lm.dists_hrs)
  273.  
  274.  
  275. # Regression between mean dist in miles and fsq.
  276. chn_fsq_svg <-
  277.   chn_distances %>%  ggplot(aes(= fsq, y = m_dist_mi), color = "red") +   geom_point(size =
  278.                                                                                          1) +
  279.   geom_smooth(method = 'lm', color = 'red') +
  280.   theme_minimal() +
  281.   labs(= 'Fish Stock', y = 'Mean Distance (mi)'title = 'Mean Distance ~ Fish Stock') +
  282.   theme(plot.title = element_text(hjust = 0.5))
  283. lm.dists_fsq <- lm(m_dist_mi ~ fsq, data = chn_distances)
  284. summary(lm.dists_fsq)
  285. ggsave(
  286.   file = "chn_fsq.svg",
  287.   plot = chn_fsq_svg,
  288.   width = 10,
  289.   height = 10
  290. )
  291.  
  292. # We observe that fsq has greater correlation than fsp.
  293.  
  294.  
  295. # Plotting supply and product and population, ln of samples
  296. distbyyearln <-
  297.   ggplot(all_samples, aes(= date, y = log(dist_mi))) +
  298.   geom_point(color = "dodgerblue4", pch = ".") +
  299.   labs(= "Date", y = "Distance") +
  300.   facet_wrap( ~ year, nrow = 3, scales = "free")
  301. distbyyearln
  302.  

Editor

You can edit this paste and save as new:


File Description
  • czlsfinal
  • Paste Code
  • 03 Dec-2022
  • 8.78 Kb
You can Share it: