-
Notifications
You must be signed in to change notification settings - Fork 1
/
03_Analysis_FADN.R
1579 lines (1385 loc) · 68.9 KB
/
03_Analysis_FADN.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ---------------------------------------------------------------------------- #
# #
# R code for the article: "A note on synthetic data for replication purposes #
# in agricultural economics" #
# #
# Authors: Stefan Wimmer and Robert Finger #
# #
# Corresponding author: Stefan Wimmer ([email protected]) #
# #
# Citation: Wimmer, S., Finger, R. (2023). "A note on synthetic data for #
# replication purposes in agricultural economics." Journal of Agricultural #
# Economics. #
# #
# The program stores all tables and figures based on the FADN data for German #
# crop farms presented in the appendix in the subfolders 'Tables/FADN' #
# and 'Figures/FADN'. The data are not publicly available but can be #
# requested from DG-Agri. The script was successfully run with R version 4.2.0.#
# #
# ---------------------------------------------------------------------------- #
# Content:
# 0) Preparation (working directory, packages, data, colours)
# 1) Generation of synthetic data
# 2) Descriptive statistics
# 3) Estimation of production function
# 4) Estimation of production frontier
# 5) Data envelopment analysis
# -------------------- #
#### 0. Preparation ####
# -------------------- #
# Set working directory
my.wd <- getwd() # NOTE: getwd() can be replaced with any working directory
setwd(my.wd)
# Create subfolders for tables and figures
dir.create(file.path("Tables"))
dir.create(file.path("Tables", "FADN"))
dir.create(file.path("Figures"))
dir.create(file.path("Figures", "FADN"))
# Install missing packages
list.of.packages <- c("dplyr", "broom", "haven", "frontier", "Benchmarking",
"dotwhisker", "ggplot2", "ggpubr", "synthpop",
"stargazer")
new.packages <- list.of.packages[!(list.of.packages %in%
installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
# Load packages
library(dplyr) # for data management
library(broom) # data preparation
library(haven) # imports .dta files
library(frontier) # efficiency analysis
library(Benchmarking) # DEA analysis
library(dotwhisker) # coefficient plots
library(ggplot2) # plots
library(ggpubr) # to merge plots
library(synthpop) # creates synthetic data
library(stargazer) # for descriptive statistics
# Load FADN data
load("rOutput/FADN.Rds")
summary(lm(log(output)~log(land)+log(labor)+log(material)+log(capstock), data=dat))
summary(frontier::sfa(log(output)~log(land)+log(labor)+log(material)+log(capstock), data=dat))
# Colours
cbPalette <- c("#8c510a", "#d8b365", "#f6e8c3", "#c7eae5", "#5ab4ac", "#01665e")
# ------------------------------------- #
#### 1. Generation of synthetic data ####
# ------------------------------------- #
# Set seed for replication
my.seed <- 1234
# Define original data set
dat_orig <- as.data.frame(dat[c("output", "land", "labor", "material", "capstock")])
# ------------------------------------- #
##### 1.a Synthetic data using CART #####
# ------------------------------------- #
# Generate five synthetic data sets (cart)
dat_synt <- syn(dat_orig, method = "cart", m = 5, visit.sequence =
c("output", "land", "labor", "material", "capstock"),
seed = my.seed,
smoothing = list(output = "density",
land = "density",
labor = "density",
material = "density",
capstock = "density"))
# Store synthetic data in data frame
dat_syn1 <- as.data.frame(dat_synt$syn[[1]])
dat_syn2 <- as.data.frame(dat_synt$syn[[2]])
dat_syn3 <- as.data.frame(dat_synt$syn[[3]])
dat_syn4 <- as.data.frame(dat_synt$syn[[4]])
dat_syn5 <- as.data.frame(dat_synt$syn[[5]])
# ----------------------------------------- #
##### 1.b Synthetic data using NORMRANK #####
# ----------------------------------------- #
# Generate five synthetic data sets (normrank)
dat_synt <- syn(dat_orig, method = "parametric", m = 5, visit.sequence =
c("output", "land", "labor", "material", "capstock"),
seed = my.seed,
smoothing = list(output = "density",
land = "density",
labor = "density",
material = "density",
capstock = "density"))
# Store synthetic data in data frame
dat_syn6 <- as.data.frame(dat_synt$syn[[1]])
dat_syn7 <- as.data.frame(dat_synt$syn[[2]])
dat_syn8 <- as.data.frame(dat_synt$syn[[3]])
dat_syn9 <- as.data.frame(dat_synt$syn[[4]])
dat_syn10 <- as.data.frame(dat_synt$syn[[5]])
# ------------------------------- #
#### 2. Descriptive statistics ####
# ------------------------------- #
# Original data
stargazer::stargazer(dat_orig,
digits=2,
title="Summary statistics for FADN sample (2018), original",
nobs = FALSE,
omit.summary.stat = c("p25", "p75"),
covariate.labels=c("Output", "Land", "Labor",
"Material", "Capital"),
type = "text",
style = "ajps",
out = "Tables/FADN/TabA5a_descr_orig.txt")
# One synthetic data: CART
stargazer::stargazer(dat_syn1,
digits=2,
title="Summary statistics for FADN sample (2018), CART",
nobs = FALSE,
omit.summary.stat = c("p25", "p75"),
covariate.labels=c("Output", "Land", "Labor",
"Material", "Capital"),
type = "text",
style = "ajps",
out = "Tables/FADN/TabA5b_descr_cart.txt")
# One synthetic data: NORMRANK
stargazer::stargazer(dat_syn6,
digits=2,
title="Summary statistics for FADN sample (2018), NORMRANK",
nobs = FALSE,
omit.summary.stat = c("p25", "p75"),
covariate.labels=c("Output", "Land", "Labor",
"Material", "Capital"),
type = "text",
style = "ajps",
out = "Tables/FADN/TabA5c_descr_normrank.txt")
# -------------------------------------------- #
#### 3. Estimation of production function ####
# -------------------------------------------- #
fn.CD <- log(output) ~ log(land) + log(labor) +
log(material) + log(capstock)
fn.CD_logs <- loutput ~ lland + llabor +
lmaterial + lcapstock
fn.TL <- log(output) ~ log(land/mean(land)) + log(labor/mean(labor)) + log(material/mean(material)) + log(capstock/mean(capstock)) +
I(0.5*log(land/mean(land))*log(land/mean(land))) + I(log(land/mean(land))*log(labor/mean(labor))) + I(log(land/mean(land))*log(material/mean(material))) +
+ I(log(land/mean(land))*log(capstock/mean(capstock))) +
I(0.5*log(labor/mean(labor))*log(labor/mean(labor))) + I(log(labor/mean(labor))*log(material/mean(material))) +
+ I(log(labor/mean(labor))*log(capstock/mean(capstock))) +
I(0.5*log(material/mean(material))*log(material/mean(material))) + I(log(material/mean(material))*log(capstock/mean(capstock))) +
I(0.5*log(capstock/mean(capstock))*log(capstock/mean(capstock)))
# --------------------------------------------#
##### 3a Cobb Douglas Production Function #####
# --------------------------------------------#
# Original data
prod.orig <- tidy(lm(fn.CD, data = dat_orig))
prod.orig <- prod.orig %>% mutate(model = "Original data")
# CART:
# Synthetic data 1
prod.syn1 <- tidy(lm(fn.CD, data = dat_syn1))
prod.syn1 <- prod.syn1 %>% mutate(model = "Synthetic data 1")
# Synthetic data 2
prod.syn2 <- tidy(lm(fn.CD, data = dat_syn2))
prod.syn2 <- prod.syn2 %>% mutate(model = "Synthetic data 2")
# Synthetic data 3
prod.syn3 <- tidy(lm(fn.CD, data = dat_syn3))
prod.syn3 <- prod.syn3 %>% mutate(model = "Synthetic data 3")
# Synthetic data 4
prod.syn4 <- tidy(lm(fn.CD, data = dat_syn4))
prod.syn4 <- prod.syn4 %>% mutate(model = "Synthetic data 4")
# Synthetic data 5
prod.syn5 <- tidy(lm(fn.CD, data = dat_syn5))
prod.syn5 <- prod.syn5 %>% mutate(model = "Synthetic data 5")
# NORMRANK:
# Synthetic data 6
prod.syn6 <- tidy(lm(fn.CD, data = dat_syn6))
prod.syn6 <- prod.syn6 %>% mutate(model = "Synthetic data 6")
# Synthetic data 7
prod.syn7 <- tidy(lm(fn.CD, data = dat_syn7))
prod.syn7 <- prod.syn7 %>% mutate(model = "Synthetic data 7")
# Synthetic data 8
prod.syn8 <- tidy(lm(fn.CD, data = dat_syn8))
prod.syn8 <- prod.syn8 %>% mutate(model = "Synthetic data 8")
# Synthetic data 9
prod.syn9 <- tidy(lm(fn.CD, data = dat_syn9))
prod.syn9 <- prod.syn9 %>% mutate(model = "Synthetic data 9")
# Synthetic data 10
prod.syn10 <- tidy(lm(fn.CD, data = dat_syn10))
prod.syn10 <- prod.syn10 %>% mutate(model = "Synthetic data 10")
# ----------------------------------------------------------------------- #
# Figure A.8. Parameter estimates of the Cobb-Douglas production function #
# with 95% confidence intervals (95% CIs) #
# ----------------------------------------------------------------------- #
# Combine all models
models_cart <- bind_rows(prod.orig, prod.syn1, prod.syn2, prod.syn3,
prod.syn4, prod.syn5)
models_normrank <- bind_rows(prod.orig, prod.syn6, prod.syn7, prod.syn8,
prod.syn9, prod.syn10)
# Make plot for CART
elast_CD_prod_cart <- dwplot(models_cart,
show_intercept = FALSE,
ci = .95,
vars_order = c("log(land)", "log(labor)", "log(material)", "log(capstock)"),
model_order = c("Original data",
"Synthetic data 1",
"Synthetic data 2",
"Synthetic data 3",
"Synthetic data 4",
"Synthetic data 5")
) %>%
relabel_predictors(
c("log(land)" = "Land",
"log(labor)" = "Labor",
"log(material)" = "Material",
"log(capstock)" = "Capital")
) +
theme_light(base_family="Times") +
xlab("Estimated elasticities with 95% CIs") + ylab("") +
ggtitle("CART method") +
theme(
plot.title = element_text(size=10, hjust = 0.5),
legend.position = "bottom",
legend.background = element_rect(colour = "white"),
legend.title = element_blank()
) +
scale_x_continuous(breaks = seq(0, 1, by = 0.25), limits=c(-0.10,1.00)) +
scale_colour_manual(values=cbPalette)
# Make plot for NORMRANK
elast_CD_prod_normrank <- dwplot(models_normrank,
show_intercept = FALSE,
ci = .95,
vars_order = c("log(land)", "log(labor)", "log(material)", "log(capstock)"),
model_order = c("Original data",
"Synthetic data 6",
"Synthetic data 7",
"Synthetic data 8",
"Synthetic data 9",
"Synthetic data 10")) %>%
relabel_predictors(
c("log(land)" = "Land",
"log(labor)" = "Labor",
"log(material)" = "Material",
"log(capstock)" = "Capital")
) +
theme_light(base_family="Times") +
xlab("Estimated elasticities with 95% CIs") + ylab("") +
ggtitle("NORMRANK method") +
theme(
plot.title = element_text(size=10, hjust = 0.5),
legend.position = "bottom",
legend.background = element_rect(colour = "white"),
legend.title = element_blank()
) +
scale_x_continuous(breaks = seq(0, 1, by = 0.25), limits=c(-0.10,1.00)) +
scale_colour_manual(values=cbPalette)
# Save plots
elast_CD_prod <- ggarrange(elast_CD_prod_cart, elast_CD_prod_normrank, ncol = 2, nrow = 1)
ggsave("Figures/FADN/FigA8_elast_CD_prod.png", elast_CD_prod, width = 23, height = 11, units = "cm")
# ----------------------------------------#
##### 3b Translog Production Function #####
# ----------------------------------------#
# Create functions to estimate production function and calculate elasticities
prod_tl <- function(data) {
prod <- lm(fn.TL, data = data)
coef <- prod$coefficients
el_land <- coef["log(land/mean(land))"] +
coef["I(0.5 * log(land/mean(land)) * log(land/mean(land)))"]*log(data$land/mean(data$land)) +
coef["I(log(land/mean(land)) * log(labor/mean(labor)))"]*log(data$labor/mean(data$labor)) +
coef["I(log(land/mean(land)) * log(material/mean(material)))"]*log(data$material/mean(data$material)) +
coef["I(log(land/mean(land)) * log(capstock/mean(capstock)))"]*log(data$capstock/mean(data$capstock))
el_labor <- coef["log(labor/mean(labor))"] +
coef["I(log(land/mean(land)) * log(labor/mean(labor)))"]*log(data$land/mean(data$land)) +
coef["I(0.5 * log(labor/mean(labor)) * log(labor/mean(labor)))"]*log(data$labor/mean(data$labor)) +
coef["I(log(labor/mean(labor)) * log(material/mean(material)))"]*log(data$material/mean(data$material)) +
coef["I(log(labor/mean(labor)) * log(capstock/mean(capstock)))"]*log(data$capstock/mean(data$capstock))
el_material <- coef["log(material/mean(material))"] +
coef["I(log(land/mean(land)) * log(material/mean(material)))"]*log(data$land/mean(data$land)) +
coef["I(log(labor/mean(labor)) * log(material/mean(material)))"]*log(data$labor/mean(data$labor)) +
coef["I(0.5 * log(material/mean(material)) * log(material/mean(material)))"]*log(data$material/mean(data$material)) +
coef["I(log(material/mean(material)) * log(capstock/mean(capstock)))"]*log(data$capstock/mean(data$capstock))
el_capital <- coef["log(capstock/mean(capstock))"] +
coef["I(log(land/mean(land)) * log(capstock/mean(capstock)))"]*log(data$land/mean(data$land)) +
coef["I(log(labor/mean(labor)) * log(capstock/mean(capstock)))"]*log(data$labor/mean(data$labor)) +
coef["I(log(material/mean(material)) * log(capstock/mean(capstock)))"]*log(data$material/mean(data$material)) +
coef["I(0.5 * log(capstock/mean(capstock)) * log(capstock/mean(capstock)))"]*log(data$capstock/mean(data$capstock))
result <- list()
result$elast <- cbind(el_land, el_labor, el_material, el_capital)
result$model <- tidy(prod)
return(result)
}
# Estimate TL prod and calculate elasticities
# Original data
prod <- prod_tl(dat_orig)
prod.orig <- prod$model[c(1:5),] %>%
mutate(model = "Original data") # adds model label
dat_orig <- cbind(dat_orig,prod$elast)
# Synthetic data 1
prod <- prod_tl(dat_syn1)
prod.syn1 <- prod$model[c(1:5),] %>%
mutate(model = "Synthetic data 1")
dat_syn1 <- cbind(dat_syn1,prod$elast)
# Synthetic data 2
prod <- prod_tl(dat_syn2)
prod.syn2 <- prod$model[c(1:5),] %>%
mutate(model = "Synthetic data 2")
dat_syn2 <- cbind(dat_syn2,prod$elast)
# Synthetic data 3
prod <- prod_tl(dat_syn3)
prod.syn3 <- prod$model[c(1:5),] %>%
mutate(model = "Synthetic data 3")
dat_syn3 <- cbind(dat_syn3,prod$elast)
# Synthetic data 4
prod <- prod_tl(dat_syn4)
prod.syn4 <- prod$model[c(1:5),] %>%
mutate(model = "Synthetic data 4")
dat_syn4 <- cbind(dat_syn4,prod$elast)
# Synthetic data 5
prod <- prod_tl(dat_syn5)
prod.syn5 <- prod$model[c(1:5),] %>%
mutate(model = "Synthetic data 5")
dat_syn5 <- cbind(dat_syn5,prod$elast)
# Synthetic data 6
prod <- prod_tl(dat_syn6)
prod.syn6 <- prod$model[c(1:5),] %>%
mutate(model = "Synthetic data 6")
dat_syn6 <- cbind(dat_syn6,prod$elast)
# Synthetic data 7
prod <- prod_tl(dat_syn7)
prod.syn7 <- prod$model[c(1:5),] %>%
mutate(model = "Synthetic data 7")
dat_syn7 <- cbind(dat_syn7,prod$elast)
# Synthetic data 8
prod <- prod_tl(dat_syn8)
prod.syn8 <- prod$model[c(1:5),] %>%
mutate(model = "Synthetic data 8")
dat_syn8 <- cbind(dat_syn8,prod$elast)
# Synthetic data 9
prod <- prod_tl(dat_syn9)
prod.syn9 <- prod$model[c(1:5),] %>%
mutate(model = "Synthetic data 9")
dat_syn9 <- cbind(dat_syn9,prod$elast)
# Synthetic data 10
prod <- prod_tl(dat_syn10)
prod.syn10 <- prod$model[c(1:5),] %>%
mutate(model = "Synthetic data 10")
dat_syn10 <- cbind(dat_syn10,prod$elast)
#--------------#
# Figure A.11. #
#--------------#
# Combine all models
models_cart <- bind_rows(prod.orig, prod.syn1, prod.syn2, prod.syn3,
prod.syn4, prod.syn5)
models_normrank <- bind_rows(prod.orig, prod.syn6, prod.syn7, prod.syn8,
prod.syn9, prod.syn10)
# Make plot for CART
elast_TL_prod_cart <- dwplot(models_cart,
show_intercept = FALSE,
ci = .95,
vars_order = c("log(land/mean(land))", "log(labor/mean(labor))", "log(material/mean(material))", "log(capstock/mean(capstock))"),
model_order = c("Original data",
"Synthetic data 1",
"Synthetic data 2",
"Synthetic data 3",
"Synthetic data 4",
"Synthetic data 5")) %>%
relabel_predictors(
c("log(land/mean(land))" = "Land",
"log(labor/mean(labor))" = "Labor",
"log(material/mean(material))" = "Material",
"log(capstock/mean(capstock))" = "Capital")
) +
theme_light(base_family="Times") +
xlab("Estimated elasticities with 95% CIs") + ylab("") +
ggtitle("CART method") +
theme(
plot.title = element_text(size=10, hjust = 0.5),
legend.position = "bottom",
legend.background = element_rect(colour = "white"),
legend.title = element_blank()
) +
scale_x_continuous(breaks = seq(0, 0.75, by = 0.25), limits=c(-0.06,0.95)) +
scale_colour_manual(values=cbPalette)
# Make plot for NORMRANK
elast_TL_prod_normrank <- dwplot(models_normrank,
show_intercept = FALSE,
ci = .95,
vars_order = c("log(land/mean(land))", "log(labor/mean(labor))", "log(material/mean(material))", "log(capstock/mean(capstock))"),
model_order = c("Original data",
"Synthetic data 6",
"Synthetic data 7",
"Synthetic data 8",
"Synthetic data 9",
"Synthetic data 10")) %>%
relabel_predictors(
c("log(land/mean(land))" = "Land",
"log(labor/mean(labor))" = "Labor",
"log(material/mean(material))" = "Material",
"log(capstock/mean(capstock))" = "Capital")
) +
theme_light(base_family="Times") +
xlab("Estimated elasticities with 95% CIs") + ylab("") +
ggtitle("NORMRANK method") +
theme(
plot.title = element_text(size=10, hjust = 0.5),
legend.position = "bottom",
legend.background = element_rect(colour = "white"),
legend.title = element_blank()
) +
scale_x_continuous(breaks = seq(0, 0.75, by = 0.25), limits=c(-0.06,0.95)) +
scale_colour_manual(values=cbPalette)
# Save plots
elast_TL_prod <- ggarrange(elast_TL_prod_cart, elast_TL_prod_normrank, ncol = 2, nrow = 1)
ggsave("Figures/FADN/FigA11_elast_TL_prod.png", elast_TL_prod, width = 23, height = 11, units = "cm")
#--------------#
# Figure A.12. #
#--------------#
# Make data frame with elasticities from all datasets (original and synthetic)
# Land
nr_obs <- length(dat_orig$el_land)
el_land <- data.frame(matrix(ncol = 2, nrow = 11*nr_obs))
colnames(el_land) <- c("el", "data")
el_land$data <- c(rep("Original data", nr_obs),rep("Synthetic data 1", nr_obs),
rep("Synthetic data 2", nr_obs),rep("Synthetic data 3", nr_obs),
rep("Synthetic data 4", nr_obs),rep("Synthetic data 5", nr_obs),
rep("Synthetic data 6", nr_obs),rep("Synthetic data 7", nr_obs),
rep("Synthetic data 8", nr_obs),rep("Synthetic data 9", nr_obs),
rep("Synthetic data 10", nr_obs))
el_land$el <- c(dat_orig$el_land,dat_syn1$el_land,dat_syn2$el_land,dat_syn3$el_land,dat_syn4$el_land,dat_syn5$el_land,
dat_syn6$el_land,dat_syn7$el_land,dat_syn8$el_land,dat_syn9$el_land,dat_syn10$el_land)
el_land$inp <- "Land"
# Labor
el_labor <- data.frame(matrix(ncol = 2, nrow = 11*nr_obs))
colnames(el_labor) <- c("el", "data")
el_labor$data <- c(rep("Original data", nr_obs),rep("Synthetic data 1", nr_obs),
rep("Synthetic data 2", nr_obs),rep("Synthetic data 3", nr_obs),
rep("Synthetic data 4", nr_obs),rep("Synthetic data 5", nr_obs),
rep("Synthetic data 6", nr_obs),rep("Synthetic data 7", nr_obs),
rep("Synthetic data 8", nr_obs),rep("Synthetic data 9", nr_obs),
rep("Synthetic data 10", nr_obs))
el_labor$el <- c(dat_orig$el_labor,dat_syn1$el_labor,dat_syn2$el_labor,dat_syn3$el_labor,dat_syn4$el_labor,dat_syn5$el_labor,
dat_syn6$el_labor,dat_syn7$el_labor,dat_syn8$el_labor,dat_syn9$el_labor,dat_syn10$el_labor)
el_labor$inp <- "Labor"
# Material
el_material <- data.frame(matrix(ncol = 2, nrow = 11*nr_obs))
colnames(el_material) <- c("el", "data")
el_material$data <- c(rep("Original data", nr_obs),rep("Synthetic data 1", nr_obs),
rep("Synthetic data 2", nr_obs),rep("Synthetic data 3", nr_obs),
rep("Synthetic data 4", nr_obs),rep("Synthetic data 5", nr_obs),
rep("Synthetic data 6", nr_obs),rep("Synthetic data 7", nr_obs),
rep("Synthetic data 8", nr_obs),rep("Synthetic data 9", nr_obs),
rep("Synthetic data 10", nr_obs))
el_material$el <- c(dat_orig$el_material,dat_syn1$el_material,dat_syn2$el_material,dat_syn3$el_material,dat_syn4$el_material,dat_syn5$el_material,
dat_syn6$el_material,dat_syn7$el_material,dat_syn8$el_material,dat_syn9$el_material,dat_syn10$el_material)
el_material$inp <- "Material"
# Capital
el_capital <- data.frame(matrix(ncol = 2, nrow = 11*nr_obs))
colnames(el_capital) <- c("el", "data")
el_capital$data <- c(rep("Original data", nr_obs),rep("Synthetic data 1", nr_obs),
rep("Synthetic data 2", nr_obs),rep("Synthetic data 3", nr_obs),
rep("Synthetic data 4", nr_obs),rep("Synthetic data 5", nr_obs),
rep("Synthetic data 6", nr_obs),rep("Synthetic data 7", nr_obs),
rep("Synthetic data 8", nr_obs),rep("Synthetic data 9", nr_obs),
rep("Synthetic data 10", nr_obs))
el_capital$el <- c(dat_orig$el_capital,dat_syn1$el_capital,dat_syn2$el_capital,dat_syn3$el_capital,dat_syn4$el_capital,dat_syn5$el_capital,
dat_syn6$el_capital,dat_syn7$el_capital,dat_syn8$el_capital,dat_syn9$el_capital,dat_syn10$el_capital)
el_capital$inp <- "Capital"
# All
el_prod <- rbind(el_land,el_labor,el_material,el_capital)
el_prod_cart <- el_prod %>%
filter(data == "Original data" | data == "Synthetic data 1" |
data == "Synthetic data 2" | data == "Synthetic data 3" |
data == "Synthetic data 4" | data == "Synthetic data 5")
el_prod_normrank <- el_prod %>%
filter(data == "Original data" | data == "Synthetic data 6" |
data == "Synthetic data 7" | data == "Synthetic data 8" |
data == "Synthetic data 9" | data == "Synthetic data 10")
# Make sure data are plotted in right order
el_prod_cart$inp <- factor(el_prod_cart$inp,
levels=c("Land","Labor","Material","Capital"))
el_prod_cart$data <- factor(el_prod_cart$data,
levels=c("Synthetic data 10",
"Synthetic data 9",
"Synthetic data 8",
"Synthetic data 7",
"Synthetic data 6",
"Synthetic data 5",
"Synthetic data 4",
"Synthetic data 3",
"Synthetic data 2",
"Synthetic data 1",
"Original data"))
el_prod_normrank$inp <- factor(el_prod_normrank$inp,
levels=c("Land","Labor","Material","Capital"))
el_prod_normrank$data <- factor(el_prod_normrank$data,
levels=c("Synthetic data 10",
"Synthetic data 9",
"Synthetic data 8",
"Synthetic data 7",
"Synthetic data 6",
"Synthetic data 5",
"Synthetic data 4",
"Synthetic data 3",
"Synthetic data 2",
"Synthetic data 1",
"Original data"))
# Make plot for CART
elast_TL_fl_prod_cart <- ggplot(el_prod_cart, aes(x=data, y=el, fill=data)) +
geom_violin(size=0.2) +
theme_light() +
theme(
plot.title = element_text(size=10, hjust = 0.5),
legend.position="bottom",
legend.background = element_rect(colour = "white"),
legend.title = element_blank(),
axis.text.y=element_blank()
) +
coord_flip() + # To switch X and Y axes
xlab("") +
ylab("Elasticity") +
ggtitle("CART method") +
facet_wrap(~ inp) +
scale_y_continuous(breaks = seq(-0.3, 1.2, by = 0.3), limits=c(-0.4,1.3)) +
scale_fill_manual(values=cbPalette)
# Make plot for NORMRANK
elast_TL_fl_prod_normrank <- ggplot(el_prod_normrank, aes(x=data, y=el, fill=data)) +
geom_violin(size=0.2) +
theme_light() +
theme(
plot.title = element_text(size=10, hjust = 0.5),
legend.position="bottom",
legend.background = element_rect(colour = "white"),
legend.title = element_blank(),
axis.text.y=element_blank()
) +
coord_flip() + # To switch X and Y axes
xlab("") +
ylab("Elasticity") +
ggtitle("NORMRANK method") +
facet_wrap(~ inp) +
scale_y_continuous(breaks = seq(-0.3, 1.2, by = 0.3), limits=c(-0.4,1.3)) +
scale_fill_manual(values=cbPalette)
# Save plots
elast_TL_fl_prod <- ggarrange(elast_TL_fl_prod_cart, elast_TL_fl_prod_normrank, ncol = 2, nrow = 1)
ggsave("Figures/FADN/FigA12_elast_TL_fl_prod.png", elast_TL_fl_prod, width = 23, height = 11, units = "cm")
# -------------------------------------------- #
#### 4. Estimation of production frontier ####
# -------------------------------------------- #
# --------------------------------------------- #
##### 4.a Cobb-Douglas Production Frontier #####
# --------------------------------------------- #
# Create functions to estimate production frontier
front_cd <- function(data) {
front <- frontier::sfa(fn.CD, data = data)
result <- list()
# Store TE scores
result$te <- frontier::efficiencies(front, asInData=TRUE)
# prepare for coefficient plot (dwplot)
front <- summary(front)
front <- as_tibble(front$mleParam)
front$term <- c("Intercept", "Land", "Labor", "Material", "Capital",
"Sigma2", "Gamma")
front <- front %>%
select(term, everything()) #re-order variables
names(front) <- names(prod.orig)[c(1:5)]
result$estimates <- front
return(result)
}
# Original data
front <- front_cd(dat_orig)
front.orig <- front$estimates
front.orig$model <- "Original data"
te_orig <- front$te
# Synthetic data 1
front <- front_cd(dat_syn1)
front.syn1 <- front$estimates
front.syn1$model <- "Synthetic data 1"
te_syn1 <- front$te
# Synthetic data 2
front <- front_cd(dat_syn2)
front.syn2 <- front$estimates
front.syn2$model <- "Synthetic data 2"
te_syn2 <- front$te
# Synthetic data 3
front <- front_cd(dat_syn3)
front.syn3 <- front$estimates
front.syn3$model <- "Synthetic data 3"
te_syn3 <- front$te
# Synthetic data 4
front <- front_cd(dat_syn4)
front.syn4 <- front$estimates
front.syn4$model <- "Synthetic data 4"
te_syn4 <- front$te
# Synthetic data 5
front <- front_cd(dat_syn5)
front.syn5 <- front$estimates
front.syn5$model <- "Synthetic data 5"
te_syn5 <- front$te
# Synthetic data 6
front <- front_cd(dat_syn6)
front.syn6 <- front$estimates
front.syn6$model <- "Synthetic data 6"
te_syn6 <- front$te
# Synthetic data 7
front <- front_cd(dat_syn7)
front.syn7 <- front$estimates
front.syn7$model <- "Synthetic data 7"
te_syn7 <- front$te
# Synthetic data 8
front <- front_cd(dat_syn8)
front.syn8 <- front$estimates
front.syn8$model <- "Synthetic data 8"
te_syn8 <- front$te
# Synthetic data 9
front <- front_cd(dat_syn9)
front.syn9 <- front$estimates
front.syn9$model <- "Synthetic data 9"
te_syn9 <- front$te
# Synthetic data 10
front <- front_cd(dat_syn10)
front.syn10 <- front$estimates
front.syn10$model <- "Synthetic data 10"
te_syn10 <- front$te
#--------------#
# Figure A.13. #
#--------------#
# Combine all models
models_cart <- bind_rows(front.orig, front.syn1, front.syn2, front.syn3,
front.syn4, front.syn5)
models_normrank <- bind_rows(front.orig, front.syn6, front.syn7, front.syn8,
front.syn9, front.syn10)
# Make plot for CART
elast_CD_front_cart <- dwplot(models_cart,
show_intercept = FALSE,
ci = .95,
vars_order = c("Land", "Labor", "Material", "Capital"),
model_order = c("Original data",
"Synthetic data 1",
"Synthetic data 2",
"Synthetic data 3",
"Synthetic data 4",
"Synthetic data 5")) +
theme_light(base_family="Times") +
xlab("Estimated elasticities with 95% CIs") +
ylab("") +
ggtitle("CART method") +
theme(
plot.title = element_text(size=10, hjust = 0.5),
legend.position = "bottom",
legend.background = element_rect(colour = "white"),
legend.title = element_blank(),
) +
scale_x_continuous(breaks = seq(0, 1, by = 0.25), limits=c(-0.12,1)) +
scale_colour_manual(values=cbPalette)
# Make plot for NORMRANK
elast_CD_front_normrank <- dwplot(models_normrank,
show_intercept = FALSE,
ci = .95,
vars_order = c("Land", "Labor", "Material", "Capital"),
model_order = c("Original data",
"Synthetic data 6",
"Synthetic data 7",
"Synthetic data 8",
"Synthetic data 9",
"Synthetic data 10")) +
theme_light(base_family="Times") +
xlab("Estimated elasticities with 95% CIs") +
ylab("") +
ggtitle("NORMRANK method") +
theme(
plot.title = element_text(size=10, hjust = 0.5),
legend.position = "bottom",
legend.background = element_rect(colour = "white"),
legend.title = element_blank(),
) +
scale_x_continuous(breaks = seq(0, 1, by = 0.25), limits=c(-0.12,1)) +
scale_colour_manual(values=cbPalette)
# Save plots
elast_CD_front <- ggarrange(elast_CD_front_cart, elast_CD_front_normrank, ncol = 2, nrow = 1)
ggsave("Figures/FADN/FigA13_elast_CD_front.png", elast_CD_front, width = 23, height = 11, units = "cm")
# ---------- #
# Table A.6. #
# ---------- #
# Combine all TE scores
descr_te_cd <- cbind(te_orig,te_syn1,te_syn2,te_syn3,te_syn4,te_syn5,
te_syn6,te_syn7,te_syn8,te_syn9,te_syn10)
# Create table with descriptive statistics
stargazer(as.data.frame(descr_te_cd),
digits=2,
summary = TRUE,
iqr = TRUE,
title="Summary statistics: Technical efficiency scores, Cobb-Douglas, half-normal distribution",
nobs = FALSE,
covariate.labels=c("Original data", "Synthetic data 1",
"Synthetic data 2", "Synthetic data 3",
"Synthetic data 4","Synthetic data 5",
"Synthetic data 6","Synthetic data 7",
"Synthetic data 8","Synthetic data 9",
"Synthetic data 10","Synthetic data 11",
"Synthetic data 12","Synthetic data 13",
"Synthetic data 14","Synthetic data 15"),
type = "text",
style = "ajps",
out = "Tables/FADN/TabA6_descr_te_cd.txt")
#-------------#
# Figure A.9. #
#-------------#
# Make data frame with technical efficiency scores from all datasets
# Original and CART data
nr_obs <- length(te_orig)
efficiencies_cart <- data.frame(matrix(ncol = 2, nrow = 6*nr_obs))
colnames(efficiencies_cart) <- c("TE", "data")
efficiencies_cart$data <- c(rep("Original data", nr_obs),rep("Synthetic data 1", nr_obs),
rep("Synthetic data 2", nr_obs),rep("Synthetic data 3", nr_obs),
rep("Synthetic data 4", nr_obs),rep("Synthetic data 5", nr_obs))
efficiencies_cart$TE <- c(te_orig,te_syn1,te_syn2,te_syn3,te_syn4,te_syn5)
# Original and NORMRANK data
efficiencies_normrank <- data.frame(matrix(ncol = 2, nrow = 6*nr_obs))
colnames(efficiencies_normrank) <- c("TE", "data")
efficiencies_normrank$data <- c(rep("Original data", nr_obs),rep("Synthetic data 6", nr_obs),
rep("Synthetic data 7", nr_obs),rep("Synthetic data 8", nr_obs),
rep("Synthetic data 9", nr_obs),rep("Synthetic data 10", nr_obs))
efficiencies_normrank$TE <- c(te_orig,te_syn6,te_syn7,te_syn8,te_syn9,te_syn10)
# Make sure data are plotted in right order
efficiencies_cart$data <- factor(efficiencies_cart$data,
levels=c("Synthetic data 5",
"Synthetic data 4",
"Synthetic data 3",
"Synthetic data 2",
"Synthetic data 1",
"Original data"))
efficiencies_normrank$data <- factor(efficiencies_normrank$data,
levels=c("Synthetic data 10",
"Synthetic data 9",
"Synthetic data 8",
"Synthetic data 7",
"Synthetic data 6",
"Original data"))
# Make plot for CART
te_cd_cart <-
efficiencies_cart %>%
ggplot(aes(x = TE , y = data, fill = data)) +
ggridges::geom_density_ridges(scale = 1,
stat = "binline",
binwidth = 0.01) +
theme_light(base_family="Times") +
xlab("Technical efficiency") + ylab("") +
ggtitle("CART method") +
theme(
legend.position = "bottom",
legend.background = element_rect(colour = "white"),
legend.title = element_blank(),
plot.title = element_text(size=10, hjust = 0.5),
axis.text.y=element_blank()
) +
scale_x_continuous(breaks = seq(0, 1, by = 0.25), limits=c(0,1)) +
scale_fill_manual(values=cbPalette,
limits = c("Original data",
"Synthetic data 1",
"Synthetic data 2",
"Synthetic data 3",
"Synthetic data 4",
"Synthetic data 5"))
# Make plot for NORMRANK
te_cd_normrank <-
efficiencies_normrank %>%
ggplot(aes(x = TE , y = data, fill = data)) +
ggridges::geom_density_ridges(scale = 1,
stat = "binline",
binwidth = 0.01) +
theme_light(base_family="Times") +
xlab("Technical efficiency") + ylab("") +
ggtitle("NORMRANK method") +
theme(
legend.position = "bottom",
legend.background = element_rect(colour = "white"),
legend.title = element_blank(),
plot.title = element_text(size=10, hjust = 0.5),
axis.text.y=element_blank()
) +
scale_x_continuous(breaks = seq(0, 1, by = 0.25), limits=c(0,1)) +
scale_fill_manual(values=cbPalette,
limits = c("Original data",
"Synthetic data 6",
"Synthetic data 7",
"Synthetic data 8",
"Synthetic data 9",
"Synthetic data 10"))
# Save plots
te_cd <- ggarrange(te_cd_cart, te_cd_normrank, ncol = 2, nrow = 1)
ggsave("Figures/FADN/FigA9_te_cd.png", te_cd, width = 23, height = 11, units = "cm")
# ----------------------------------------- #
##### 4.b Translog Production Frontier #####
# ----------------------------------------- #
# Create functions to estimate production frontiers and calculate elasticities
front_tl <- function(data) {
front <- frontier::sfa(fn.TL, data = data)
result <- list()
# Store TE scores
result$te <- frontier::efficiencies(front, asInData=TRUE)
#calculate elasticities
coef <- front$mleParam
el_land_front <- coef["log(land/mean(land))"] +
coef["I(0.5 * log(land/mean(land)) * log(land/mean(land)))"]*log(data$land/mean(data$land)) +
coef["I(log(land/mean(land)) * log(labor/mean(labor)))"]*log(data$labor/mean(data$labor)) +
coef["I(log(land/mean(land)) * log(material/mean(material)))"]*log(data$material/mean(data$material)) +
coef["I(log(land/mean(land)) * log(capstock/mean(capstock)))"]*log(data$capstock/mean(data$capstock))
el_labor_front <- coef["log(labor/mean(labor))"] +
coef["I(log(land/mean(land)) * log(labor/mean(labor)))"]*log(data$land/mean(data$land)) +
coef["I(0.5 * log(labor/mean(labor)) * log(labor/mean(labor)))"]*log(data$labor/mean(data$labor)) +
coef["I(log(labor/mean(labor)) * log(material/mean(material)))"]*log(data$material/mean(data$material)) +
coef["I(log(labor/mean(labor)) * log(capstock/mean(capstock)))"]*log(data$capstock/mean(data$capstock))
el_material_front <- coef["log(material/mean(material))"] +
coef["I(log(land/mean(land)) * log(material/mean(material)))"]*log(data$land/mean(data$land)) +
coef["I(log(labor/mean(labor)) * log(material/mean(material)))"]*log(data$labor/mean(data$labor)) +
coef["I(0.5 * log(material/mean(material)) * log(material/mean(material)))"]*log(data$material/mean(data$material)) +
coef["I(log(material/mean(material)) * log(capstock/mean(capstock)))"]*log(data$capstock/mean(data$capstock))
el_capital_front <- coef["log(capstock/mean(capstock))"] +
coef["I(log(land/mean(land)) * log(capstock/mean(capstock)))"]*log(data$land/mean(data$land)) +
coef["I(log(labor/mean(labor)) * log(capstock/mean(capstock)))"]*log(data$labor/mean(data$labor)) +
coef["I(log(material/mean(material)) * log(capstock/mean(capstock)))"]*log(data$material/mean(data$material)) +
coef["I(0.5 * log(capstock/mean(capstock)) * log(capstock/mean(capstock)))"]*log(data$capstock/mean(data$capstock))
# prepare for coefficient plot (dwplot)
front <- summary(front)
front <- as_tibble(front$mleParam)
front <- front[c(1:5),]
front$term <- c("Intercept", "Land", "Labor", "Material", "Capital")
front <- front %>%
select(term, everything()) #re-order variables
names(front) <- names(prod.orig)[c(1:5)]
result$estimates <- front
result$elast <- cbind(el_land_front, el_labor_front, el_material_front,
el_capital_front)
return(result)
}
# Estimate TL prod and calculate elasticities
# Original data
front <- front_tl(dat_orig)
front.orig <- front$estimates %>%
mutate(model = "Original data") # adds model label
te_orig <- front$te
dat_orig <- cbind(dat_orig,front$elast)
# Synthetic data 1
front <- front_tl(dat_syn1)
front.syn1 <- front$estimates %>%
mutate(model = "Synthetic data 1")
te_syn1 <- front$te
dat_syn1 <- cbind(dat_syn1,front$elast)
# Synthetic data 2
front <- front_tl(dat_syn2)
front.syn2 <- front$estimates %>%
mutate(model = "Synthetic data 2")
te_syn2 <- front$te
dat_syn2 <- cbind(dat_syn2,front$elast)
# Synthetic data 3
front <- front_tl(dat_syn3)
front.syn3 <- front$estimates %>%
mutate(model = "Synthetic data 3")
te_syn3 <- front$te
dat_syn3 <- cbind(dat_syn3,front$elast)
# Synthetic data 4
front <- front_tl(dat_syn4)
front.syn4 <- front$estimates %>%
mutate(model = "Synthetic data 4")
te_syn4 <- front$te
dat_syn4 <- cbind(dat_syn4,front$elast)
# Synthetic data 5
front <- front_tl(dat_syn5)
front.syn5 <- front$estimates %>%
mutate(model = "Synthetic data 5")
te_syn5 <- front$te