##載入套件
library(haven)
library(dplyr)
library(knitr) # for kable function
library(kableExtra) # for table styling
##讀取 .sav 檔案
data <- read_sav("C:/Users/isisg/OneDrive/Desktop/社會變遷基本調查/2010Hofstade國家開車.sav")
##定義run_regression函數
# 定義線性回歸分析函數
run_regression <- function(data, response_var, predictor_vars) {
# 確認所需列存在
all_vars <- c(response_var, predictor_vars)
if (all(all_vars %in% names(data))) {
# 動態構建公式
formula <- as.formula(paste(response_var, "~", paste(predictor_vars, collapse = " + ")))
# 運行線性模型
model <- lm(formula, data = data)
# 獲取模型總結
model_summary <- summary(model)
# 提取係數表格
coef_table <- as.data.frame(model_summary$coefficients)
# 將數據四捨五入到小數點後兩位,不使用科學記號
coef_table <- coef_table %>%
mutate(across(where(is.numeric), ~ format(round(.x, 2), nsmall = 2, scientific = FALSE)))
# 自定义列名
colnames(coef_table) <- c("估計值", "標準誤差", "t 值", "P 值")
# 顯示結果表格
return(
kable(coef_table, format = "html", row.names = TRUE, align = "c", caption = "模型係數表") %>%
kable_styling(full_width = FALSE, position = "left", font_size = 14) %>%
row_spec(0, bold = TRUE) %>%
column_spec(1:4, border_left = TRUE, border_right = TRUE)
)
} else {
stop("所需的列名不存在於數據集中。")
}
}
##中介前段 ##環境五題制-直接低成本
run_regression(data, "Zdirect_low", c("Ze_att1", "Zgdp_per_cap"))
| 估計值 | 標準誤差 | t 值 | P 值 | |
|---|---|---|---|---|
| (Intercept) | 0.00 | 0.10 | 0.00 | 1.00 |
| Ze_att1 | 0.77 | 0.16 | 4.77 | 0.00 |
| Zgdp_per_cap | 0.04 | 0.16 | 0.25 | 0.81 |
##環境五題制-直接高成本
run_regression(data, "Zdirect_high", c("Ze_att1", "Zgdp_per_cap"))
| 估計值 | 標準誤差 | t 值 | P 值 | |
|---|---|---|---|---|
| (Intercept) | 0.00 | 0.13 | 0.00 | 1.00 |
| Ze_att1 | 0.90 | 0.20 | 4.47 | 0.00 |
| Zgdp_per_cap | -0.39 | 0.20 | -1.92 | 0.06 |
##環境五題制-間接低成本
run_regression(data, "Zindirect_low", c("Ze_att1", "Zgdp_per_cap"))
| 估計值 | 標準誤差 | t 值 | P 值 | |
|---|---|---|---|---|
| (Intercept) | 0.00 | 0.09 | 0.00 | 1.00 |
| Ze_att1 | 0.49 | 0.14 | 3.39 | 0.00 |
| Zgdp_per_cap | 0.41 | 0.14 | 2.89 | 0.01 |
##環境五題制-間接高成本
run_regression(data, "Zindirect_high", c("Ze_att1", "Zgdp_per_cap"))
| 估計值 | 標準誤差 | t 值 | P 值 | |
|---|---|---|---|---|
| (Intercept) | 0.00 | 0.17 | 0.00 | 1.00 |
| Ze_att1 | 0.03 | 0.26 | 0.12 | 0.91 |
| Zgdp_per_cap | -0.16 | 0.26 | -0.61 | 0.54 |
##中介後段 ##直接低成本-無條件人均
run_regression(data, "Zun_cap", c("Zdirect_low", "Zgdp_per_cap"))
| 估計值 | 標準誤差 | t 值 | P 值 | |
|---|---|---|---|---|
| (Intercept) | 0.00 | 0.09 | 0.00 | 1.00 |
| Zdirect_low | -0.39 | 0.12 | -3.23 | 0.00 |
| Zgdp_per_cap | -0.55 | 0.12 | -4.53 | 0.00 |
##直接低成本-有條件人均
run_regression(data, "Zcon_cap", c("Zdirect_low", "Zgdp_per_cap"))
| 估計值 | 標準誤差 | t 值 | P 值 | |
|---|---|---|---|---|
| (Intercept) | 0.00 | 0.11 | 0.00 | 1.00 |
| Zdirect_low | -0.38 | 0.14 | -2.66 | 0.01 |
| Zgdp_per_cap | -0.47 | 0.14 | -3.27 | 0.00 |
##直接低成本-無條件碳強度
run_regression(data, "Zun_gdp", c("Zdirect_low", "Zgdp_per_cap"))
| 估計值 | 標準誤差 | t 值 | P 值 | |
|---|---|---|---|---|
| (Intercept) | 0.00 | 0.14 | 0.00 | 1.00 |
| Zdirect_low | -0.31 | 0.18 | -1.70 | 0.10 |
| Zgdp_per_cap | -0.32 | 0.18 | -1.71 | 0.10 |
##直接低成本-有條件碳強度
run_regression(data, "Zcon_gdp", c("Zdirect_low", "Zgdp_per_cap"))
| 估計值 | 標準誤差 | t 值 | P 值 | |
|---|---|---|---|---|
| (Intercept) | 0.00 | 0.16 | 0.00 | 1.00 |
| Zdirect_low | -0.25 | 0.20 | -1.26 | 0.22 |
| Zgdp_per_cap | -0.24 | 0.20 | -1.19 | 0.24 |
##直接高成本-無條件人均
run_regression(data, "Zun_cap", c("Zdirect_high", "Zgdp_per_cap"))
| 估計值 | 標準誤差 | t 值 | P 值 | |
|---|---|---|---|---|
| (Intercept) | 0.00 | 0.11 | 0.00 | 1.00 |
| Zdirect_high | -0.08 | 0.11 | -0.69 | 0.49 |
| Zgdp_per_cap | -0.76 | 0.11 | -6.74 | 0.00 |
##直接高成本-有條件人均
run_regression(data, "Zcon_cap", c("Zdirect_high", "Zgdp_per_cap"))
| 估計值 | 標準誤差 | t 值 | P 值 | |
|---|---|---|---|---|
| (Intercept) | 0.00 | 0.12 | 0.00 | 1.00 |
| Zdirect_high | -0.19 | 0.13 | -1.53 | 0.14 |
| Zgdp_per_cap | -0.65 | 0.13 | -5.16 | 0.00 |
##直接高成本-無條件碳強度
run_regression(data, "Zun_gdp", c("Zdirect_high", "Zgdp_per_cap"))
| 估計值 | 標準誤差 | t 值 | P 值 | |
|---|---|---|---|---|
| (Intercept) | 0.00 | 0.15 | 0.00 | 1.00 |
| Zdirect_high | 0.00 | 0.16 | -0.01 | 0.99 |
| Zgdp_per_cap | -0.51 | 0.16 | -3.21 | 0.00 |
##直接高成本-有條件碳強度
run_regression(data, "Zcon_gdp", c("Zdirect_high", "Zgdp_per_cap"))
| 估計值 | 標準誤差 | t 值 | P 值 | |
|---|---|---|---|---|
| (Intercept) | 0.00 | 0.16 | 0.00 | 1.00 |
| Zdirect_high | -0.05 | 0.17 | -0.27 | 0.79 |
| Zgdp_per_cap | -0.38 | 0.17 | -2.26 | 0.03 |
##間接低成本-無條件人均
run_regression(data, "Zun_cap", c("Zindirect_low", "Zgdp_per_cap"))
| 估計值 | 標準誤差 | t 值 | P 值 | |
|---|---|---|---|---|
| (Intercept) | 0.00 | 0.10 | 0.00 | 1.00 |
| Zindirect_low | -0.35 | 0.16 | -2.16 | 0.04 |
| Zgdp_per_cap | -0.51 | 0.16 | -3.13 | 0.00 |
##間接低成本-有條件人均
run_regression(data, "Zcon_cap", c("Zindirect_low", "Zgdp_per_cap"))
| 估計值 | 標準誤差 | t 值 | P 值 | |
|---|---|---|---|---|
| (Intercept) | 0.00 | 0.11 | 0.00 | 1.00 |
| Zindirect_low | -0.43 | 0.18 | -2.32 | 0.03 |
| Zgdp_per_cap | -0.37 | 0.18 | -2.01 | 0.05 |
##間接低成本-無條件碳強度
run_regression(data, "Zun_gdp", c("Zindirect_low", "Zgdp_per_cap"))
| 估計值 | 標準誤差 | t 值 | P 值 | |
|---|---|---|---|---|
| (Intercept) | 0.00 | 0.15 | 0.00 | 1.00 |
| Zindirect_low | -0.12 | 0.24 | -0.48 | 0.63 |
| Zgdp_per_cap | -0.42 | 0.24 | -1.73 | 0.09 |
##間接低成本-有條件碳強度
run_regression(data, "Zcon_gdp", c("Zindirect_low", "Zgdp_per_cap"))
| 估計值 | 標準誤差 | t 值 | P 值 | |
|---|---|---|---|---|
| (Intercept) | 0.00 | 0.16 | 0.00 | 1.00 |
| Zindirect_low | -0.15 | 0.26 | -0.59 | 0.56 |
| Zgdp_per_cap | -0.28 | 0.26 | -1.08 | 0.29 |
##間接高成本-無條件人均
run_regression(data, "Zun_cap", c("Zindirect_high", "Zgdp_per_cap"))
| 估計值 | 標準誤差 | t 值 | P 值 | |
|---|---|---|---|---|
| (Intercept) | 0.00 | 0.11 | 0.00 | 1.00 |
| Zindirect_high | -0.04 | 0.11 | -0.34 | 0.74 |
| Zgdp_per_cap | -0.79 | 0.11 | -7.17 | 0.00 |
##間接高成本-有條件人均
run_regression(data, "Zcon_cap", c("Zindirect_high", "Zgdp_per_cap"))
| 估計值 | 標準誤差 | t 值 | P 值 | |
|---|---|---|---|---|
| (Intercept) | 0.00 | 0.12 | 0.00 | 1.00 |
| Zindirect_high | -0.07 | 0.13 | -0.58 | 0.57 |
| Zgdp_per_cap | -0.72 | 0.13 | -5.69 | 0.00 |
##間接高成本-無條件碳強度
run_regression(data, "Zun_gdp", c("Zindirect_high", "Zgdp_per_cap"))
| 估計值 | 標準誤差 | t 值 | P 值 | |
|---|---|---|---|---|
| (Intercept) | 0.00 | 0.15 | 0.00 | 1.00 |
| Zindirect_high | -0.03 | 0.15 | -0.20 | 0.84 |
| Zgdp_per_cap | -0.51 | 0.15 | -3.35 | 0.00 |
##間接高成本-有條件碳強度
run_regression(data, "Zcon_gdp", c("Zindirect_high", "Zgdp_per_cap"))
| 估計值 | 標準誤差 | t 值 | P 值 | |
|---|---|---|---|---|
| (Intercept) | 0.00 | 0.16 | 0.00 | 1.00 |
| Zindirect_high | -0.07 | 0.16 | -0.41 | 0.68 |
| Zgdp_per_cap | -0.41 | 0.16 | -2.48 | 0.02 |