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
| library(deaR) library(magrittr) library(openxlsx) library(ggplot2) library(showtext) library(readxl) library(flextable) library(officer) library(AER) library(GGally) library(dplyr)
showtext_auto(enable = TRUE, record = TRUE) font_add(family = "苹方",regular = "/System/Library/Fonts/PingFang.ttc")
data <- read_excel("Data/数据模板.xlsx")
years <- unique(data$Year)
efficiency_results <- list(crs = data.frame(), vrs = data.frame(), scale = data.frame(), sbm = data.frame(), super = data.frame())
for (year in years) { data_year <- subset(data, Year == year) inputs <- c("Input1", "Input2", "Input3") outputs <- c("Output1", "Output2") dea_data <- make_deadata(datadea = data_year, inputs = inputs, outputs = outputs) models <- list( crs = model_basic(dea_data, rts = "crs", orientation = "io"), vrs = model_basic(dea_data, rts = "vrs", orientation = "io"), sbm = model_sbmeff(dea_data, orientation = "io"), super = model_supereff(dea_data, rts = "crs", orientation = "io") ) scale_eff <- efficiencies(models$crs) / efficiencies(models$vrs) efficiency_results$crs <- rbind(efficiency_results$crs, data.frame(Year = year, DMU = data_year$DMU, Efficiency = round(efficiencies(models$crs), 3))) efficiency_results$vrs <- rbind(efficiency_results$vrs, data.frame(Year = year, DMU = data_year$DMU, Efficiency = round(efficiencies(models$vrs), 3))) efficiency_results$scale <- rbind(efficiency_results$scale, data.frame(Year = year, DMU = data_year$DMU, Efficiency = round(scale_eff, 3))) efficiency_results$sbm <- rbind(efficiency_results$sbm, data.frame(Year = year, DMU = data_year$DMU, Efficiency = round(efficiencies(models$sbm), 3))) efficiency_results$super <- rbind(efficiency_results$super, data.frame(Year = year, DMU = data_year$DMU, Efficiency = round(efficiencies(models$super), 3))) }
all_efficiencies <- rbind( efficiency_results$crs %>% mutate(Model = "CRS"), efficiency_results$vrs %>% mutate(Model = "VRS"), efficiency_results$scale %>% mutate(Model = "Scale"), efficiency_results$sbm %>% mutate(Model = "SBM"), efficiency_results$super %>% mutate(Model = "Super") )
merged_data <- merge(data, all_efficiencies, by = c("Year", "DMU"))
ggpairs(data, columns = c("解释变量1", "Input2", "Input3","Output1","解释变量2"))
tobit_results <- list() models <- c("CRS", "VRS", "Scale", "SBM", "Super") for (model in models) { model_data <- subset(merged_data, Model == model) tobit_model <- tobit(Efficiency ~ 解释变量1 + 解释变量2 + Input3, data = model_data, left = 0, right = 1) summary_tobit <- summary(tobit_model) tobit_results[[model]] <- data.frame( Model = model, Variable = rownames(summary_tobit$coefficients), Coefficient = summary_tobit$coefficients[, "Estimate"], StdError = summary_tobit$coefficients[, "Std. Error"], ZValue = summary_tobit$coefficients[, "z value"], PValue = summary_tobit$coefficients[, "Pr(>|z|)"] ) }
print(tobit_results)
wb <- createWorkbook() for (model in models) { addWorksheet(wb, model) writeData(wb, sheet = model, tobit_results[[model]]) }
saveWorkbook(wb, "Table/DEA_Tobit_Results.xlsx", overwrite = TRUE)
print(tobit_results)
vrs_model_data <- subset(merged_data, Model == "VRS") vrs_tobit_model <- tobit(Efficiency ~ 解释变量1 + 解释变量2, data = vrs_model_data, left = 0, right = 1)
tobit_residuals <- residuals(vrs_tobit_model) fitted_values <- fitted(vrs_tobit_model)
residuals_data <- data.frame( Fitted = fitted_values, Residuals = tobit_residuals )
ggplot(residuals_data, aes(x = Fitted, y = Residuals)) + geom_point() + geom_smooth(method = "loess", col = "red") + labs(title = "DEA规模效率-Tobit 模型的残差图", x = "拟合值", y = "残差") + theme_minimal(base_family = "苹方")
calculate_marginal_effects <- function(model, variable_name, data) { coefs <- summary(model)$coefficients marginal_effects <- data.frame( Variable = variable_name, Effect = coefs[variable_name, "Estimate"] ) return(marginal_effects) }
marginal_effects_list <- list() for (model in models) { model_data <- subset(merged_data, Model == model) tobit_model <- tobit(Efficiency ~ 解释变量1 + 解释变量2 + Input3, data = model_data, left = 0, right = 1) effect1 <- calculate_marginal_effects(tobit_model, "解释变量1", model_data) effect2 <- calculate_marginal_effects(tobit_model, "解释变量2", model_data) effect3 <- calculate_marginal_effects(tobit_model, "Input3", model_data) marginal_effects_list[[model]] <- rbind(effect1, effect2, effect3) }
all_marginal_effects <- do.call(rbind, marginal_effects_list) all_marginal_effects$Model <- rep(models, each = nrow(all_marginal_effects) / length(models))
ggplot(all_marginal_effects, aes(x = Variable, y = Effect, fill = Model)) + geom_bar(stat = "identity", position = "dodge") + theme_minimal() + labs(title = "边际效应图", x = "解释变量", y = "边际效应") + scale_fill_brewer(palette = "Set1") + theme(axis.text.x = element_text(angle = 45, hjust = 1))
|