使用 Tidyverse 清洗 CFPS 数据(四)

R
数据处理
一些尝试
tidyverse
plm
Author

Rui

Published

September 27, 2022

导入数据

Code
## 导入数据
library(tidyverse)

file_path <- "data/operated_data/clean_data.sav"
clean_data <- haven::read_sav(file_path) %>% as_tibble()
clean_data %>% head(10) %>% knitr::kable() 
id fid.x fid.y provcd16 urban16 fml16 expense16 fincome16 family_debts16 family_asset16 age16 age2_16 gender16 marriage16 health16 provcd18 urban18 fml18 fincome18 expense18 family_debts18 family_asset18 age18 age2_18 gender18 marriage18 health18
100160_120009102 100160 100160 2 2 1 50691.0 85000 0 75000 25 625 1 1 6 2 2 2 173528.959 212283.76 421454.66 2187977.41 27 729 1 2 6
100286_130005103 100286 100286 3 2 1 31900.0 70700 120000 158750 38 1444 2 1 7 3 2 1 28921.493 45175.37 44835.60 538027.23 40 1600 2 1 7
100569_130299106 100569 100569 3 2 2 4560.0 1800 0 1300 71 5041 1 2 6 3 2 2 2622.215 12127.75 0.00 26004.65 73 5329 1 2 4
100724_130492103 100724 100724 3 2 2 8290.0 20000 0 66000 28 784 1 2 5 3 2 4 28921.493 19897.99 17934.24 68150.12 30 900 1 2 7
100810_100810551 100810 100810 3 2 3 34844.0 53000 12000 175500 29 841 1 2 5 3 2 4 35187.817 41970.87 0.00 336267.02 31 961 1 2 5
101023_130815105 101023 101023 3 2 4 41741.4 32005 115000 310000 35 1225 1 2 6 3 2 4 55914.887 58537.10 26901.36 538027.23 37 1369 1 2 6
101129_130896107 101129 101129 3 1 5 59558.0 80000 38000 464375 26 676 2 2 7 3 1 5 40008.066 25778.69 0.00 125455.62 28 784 2 2 7
101130_130897103 101130 101130 3 1 1 17880.0 20000 0 9800 22 484 2 1 7 3 1 1 57842.986 44543.92 0.00 747857.85 24 576 2 1 7
101797_140668105 101797 101797 1 2 1 53420.0 50000 14500 19200 31 961 2 2 6 1 2 5 68640.344 175669.15 71736.96 185339.17 33 1089 2 2 6
102448_210223103 102448 102448 5 2 1 53430.3 107500 230000 378000 24 576 2 1 5 6 2 1 96404.977 128576.28 403520.42 399036.86 25 625 2 1 5

数据转换

将变量转换为论文需要的变量,其中 +1 是为了防止变量为 0 的情况从而导致无法转换。

Code
trans_data <- clean_data %>% 
  mutate(lnexp16 = log(expense16 + 1),
         lninc16 = log(fincome16 + 1),
         lnasset16 = log(family_asset16 + 1),
         lev16 = family_debts16/(family_asset16 + 1),
         lnexp18 = log(expense18 + 1),
         lninc18 = log(fincome18 + 1),
         lnasset18 = log(family_asset18 + 1),
         lev18 = family_debts18/(family_asset18 + 1),
         year16 = 2016,
         year18 = 2018) %>%
  select(-c(expense16, fincome16, family_asset16, family_debts16,
            expense18, fincome18, family_asset18, family_debts18))

剔除 lev 变量的极端值

Code
library(magrittr)

trans_data %<>% filter(lev16 < 50 & lev18 < 50)

拆分并对列重命名

上一节中将两个年份的数据合并是为了方便同时对两年的数据进行处理,在这里需要再将数据按照年份拆分开。

Code
temp16 <- trans_data %>% 
  select(ends_with("16")) %>% 
  cbind(trans_data$id, .) %>% 
  as_tibble() %>%
  rename(year = year16,
         id = "trans_data$id",
         provcd = provcd16,
         urban = urban16,
         gender = gender16,
         marriage = marriage16,
         age = age16,
         age2 = age2_16,
         health = health16,
         fml = fml16,
         lnexp = lnexp16,
         lev = lev16,
         lninc = lninc16,
         lnasset = lnasset16)

temp18 <- trans_data %>% 
  select(ends_with("18")) %>% 
  cbind(trans_data$id, .) %>% 
  as_tibble() %>%
  rename(year = year18,
         id = "trans_data$id",
         provcd = provcd18,
         urban = urban18,
         gender = gender18,
         marriage = marriage18,
         age = age18,
         age2 = age2_18,
         health = health18,
         fml = fml18,
         lnexp = lnexp18,
         lev = lev18,
         lninc = lninc18,
         lnasset = lnasset18)

查看数据:

Code
temp16 %>% head(10) %>% knitr::kable()
id provcd urban fml age age2 gender marriage health lnexp lninc lnasset lev year
100160_120009102 2 2 1 25 625 1 1 6 10.833523 11.350418 11.225257 0.0000000 2016
100286_130005103 3 2 1 38 1444 2 1 7 10.370393 11.166215 11.975092 0.7559008 2016
100569_130299106 3 2 2 71 5041 1 2 6 8.425297 7.496097 7.170889 0.0000000 2016
100724_130492103 3 2 2 28 784 1 2 5 9.022926 9.903538 11.097425 0.0000000 2016
100810_100810551 3 2 3 29 841 1 2 5 10.458665 10.878066 12.075400 0.0683757 2016
101023_130815105 3 2 4 35 1225 1 2 6 10.639273 10.373679 12.644331 0.3709665 2016
101129_130896107 3 1 5 26 676 2 2 7 10.994723 11.289794 13.048450 0.0818302 2016
101130_130897103 3 1 1 22 484 2 1 7 9.791494 9.903538 9.190240 0.0000000 2016
101797_140668105 1 2 1 31 961 2 2 6 10.885959 10.819798 9.862718 0.7551690 2016
102448_210223103 5 2 1 24 576 2 1 5 10.886152 11.585255 12.842652 0.6084640 2016
Code
temp18 %>% head(10) %>% knitr::kable()
id provcd urban fml age age2 gender marriage health lnexp lninc lnasset lev year
100160_120009102 2 2 2 27 729 1 2 6 12.265684 12.064106 14.59849 0.1926229 2018
100286_130005103 3 2 1 40 1600 2 1 7 10.718329 10.272375 13.19567 0.0833332 2018
100569_130299106 3 2 2 73 5329 1 2 4 9.403334 7.872156 10.16607 0.0000000 2018
100724_130492103 3 2 4 30 900 1 2 7 9.898424 10.272375 11.12948 0.2631540 2018
100810_100810551 3 2 4 31 961 1 2 5 10.644755 10.468484 12.72566 0.0000000 2018
101023_130815105 3 2 4 37 1369 1 2 6 10.977433 10.931604 13.19567 0.0499999 2018
101129_130896107 3 1 5 28 784 2 2 7 10.157342 10.596861 11.73972 0.0000000 2018
101130_130897103 3 1 1 24 576 2 1 7 10.704253 10.965505 13.52497 0.0000000 2018
101797_140668105 1 2 5 33 1089 2 2 6 12.076363 11.136650 12.12995 0.3870557 2018
102448_210223103 6 2 1 25 625 2 1 5 11.764285 11.476323 12.89681 1.0112334 2018

论文中的数据

Code
## 论文中的数据
delta_lnexp <- temp18$lnexp - temp16$lnexp
delta_lninc <- temp18$lninc - temp16$lninc
delta_lnasset <- temp18$lnasset - temp16$lnasset
delta_fml <- temp18$fml - temp16$fml

data <- data.frame(delta_lnexp = delta_lnexp, 
                   delta_lninc = delta_lninc,
                   delta_lnasset = delta_lnasset,
                   delta_fml) %>% 
  cbind(temp16[c(13, 2:3, 5:12)]) %>% 
  as_tibble()

数据类型规整

Code
data$provcd %<>% as.factor()
data$urban %<>% as.factor()
data$gender %<>% as.factor()
data$marriage %<>% as.factor()
data$health %<>% as.factor()
data$delta_fml %<>% as.integer()
data$age %<>% as.integer()
data$age2 %<>% as.integer()

建立模型

Code
## 多元线性回归
model1 <- lm(delta_lnexp ~ 
               lev + delta_lninc + lnasset +
               provcd + urban + age + age2 + 
               gender + marriage + health +  delta_fml, 
             data = data)

library(plm)
library(lmtest)
coeftest(model1, vcov = vcovHC, type = "HC1")
## 
## t test of coefficients:
## 
##                Estimate  Std. Error t value  Pr(>|t|)    
## (Intercept)  7.7948e-01  3.1406e-01  2.4820 0.0130957 *  
## lev         -2.8680e-02  7.8787e-03 -3.6402 0.0002749 ***
## delta_lninc  1.3955e-01  1.5312e-02  9.1139 < 2.2e-16 ***
## lnasset     -3.3174e-02  7.9782e-03 -4.1580 3.259e-05 ***
## provcd2      2.2348e-01  1.6743e-01  1.3348 0.1820002    
## provcd3      4.2549e-02  1.1932e-01  0.3566 0.7214202    
## provcd4      1.0751e-01  1.2391e-01  0.8677 0.3856054    
## provcd5      6.5762e-02  1.1533e-01  0.5702 0.5685713    
## provcd6      8.0810e-02  1.3915e-01  0.5807 0.5614386    
## provcd7      2.5229e-02  1.2363e-01  0.2041 0.8383062    
## provcd8      2.0558e-01  1.1998e-01  1.7134 0.0866958 .  
## provcd9      9.8817e-02  1.4465e-01  0.6831 0.4945452    
## provcd10     2.7220e-01  1.4992e-01  1.8156 0.0694898 .  
## provcd11     1.3628e-02  1.3665e-01  0.0997 0.9205630    
## provcd12     1.6516e-01  1.5323e-01  1.0779 0.2811314    
## provcd13    -3.3222e-02  1.3593e-01 -0.2444 0.8069281    
## provcd14     5.2986e-02  1.2205e-01  0.4341 0.6642077    
## provcd15     1.1069e-01  1.1691e-01  0.9468 0.3438036    
## provcd16     2.0147e-01  1.3153e-01  1.5318 0.1256222    
## provcd17     4.2191e-02  1.1962e-01  0.3527 0.7243180    
## provcd18     9.0580e-02  1.1941e-01  0.7586 0.4481497    
## provcd19    -4.9425e-03  1.2938e-01 -0.0382 0.9695284    
## provcd21    -1.3433e-02  1.5602e-01 -0.0861 0.9313914    
## provcd22     1.7073e-02  1.2307e-01  0.1387 0.8896750    
## provcd23    -5.8248e-02  1.3580e-01 -0.4289 0.6679879    
## provcd24     2.1956e-01  1.3138e-01  1.6711 0.0947516 .  
## provcd26     3.8291e-03  1.2915e-01  0.0296 0.9763481    
## provcd27     9.5697e-02  1.1941e-01  0.8014 0.4229166    
## urban2       2.8041e-02  2.3780e-02  1.1792 0.2383718    
## age         -1.5815e-02  5.4848e-03 -2.8834 0.0039490 ** 
## age2         1.0600e-04  5.2766e-05  2.0089 0.0446014 *  
## gender2      1.9647e-02  2.2419e-02  0.8763 0.3808818    
## marriage2    4.6270e-02  3.2045e-02  1.4439 0.1488221    
## health2     -1.5417e-01  2.5021e-01 -0.6162 0.5377930    
## health3     -9.8047e-02  2.4547e-01 -0.3994 0.6895991    
## health4     -2.6107e-02  2.4000e-01 -0.1088 0.9133830    
## health5     -3.1457e-02  2.3877e-01 -0.1317 0.8951908    
## health6     -3.0115e-02  2.3832e-01 -0.1264 0.8994501    
## health7     -5.0323e-02  2.3840e-01 -0.2111 0.8328289    
## delta_fml    9.3443e-02  1.1461e-02  8.1533 4.341e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

问题

可能存在的问题:

  • 数据中 lev 变量大量为 0,原数据中用于计算 lev 的 fameliy_debts 也大量为 0。可能是漏了计算家庭负债变量的相关变量。

  • 论文中使用的是 2010 和 2012 年的 CFPS 数据。

  • 论文中控制了其他与家庭相关的协变量,如家庭中未婚男性个数等。而本文没有纳入模型中加以控制。

  • 论文中将 health 变量粗分为 3 级,本文未作处理。

构建面板数据

Code
## 面板数据
panel_data <- rbind(temp16, temp18)


## 数据类型规整
panel_data$provcd %<>% as.factor()
panel_data$urban %<>% as.factor()
panel_data$gender %<>% as.factor()
panel_data$marriage %<>% as.factor()
panel_data$health %<>% as.factor()

## 完成
panel_data %<>% select(id, year, everything()) %>% arrange(id, year)
# 先按id排序,再按year排序

panel_data %>% head(10) %>% knitr::kable()
id year provcd urban fml age age2 gender marriage health lnexp lninc lnasset lev
100160_120009102 2016 2 2 1 25 625 1 1 6 10.833523 11.350418 11.225257 0.0000000
100160_120009102 2018 2 2 2 27 729 1 2 6 12.265684 12.064106 14.598489 0.1926229
100286_130005103 2016 3 2 1 38 1444 2 1 7 10.370393 11.166215 11.975092 0.7559008
100286_130005103 2018 3 2 1 40 1600 2 1 7 10.718329 10.272375 13.195666 0.0833332
100569_130299106 2016 3 2 2 71 5041 1 2 6 8.425297 7.496097 7.170889 0.0000000
100569_130299106 2018 3 2 2 73 5329 1 2 4 9.403334 7.872156 10.166069 0.0000000
100724_130492103 2016 3 2 2 28 784 1 2 5 9.022926 9.903538 11.097425 0.0000000
100724_130492103 2018 3 2 4 30 900 1 2 7 9.898424 10.272375 11.129483 0.2631540
100810_100810551 2016 3 2 3 29 841 1 2 5 10.458665 10.878066 12.075400 0.0683757
100810_100810551 2018 3 2 4 31 961 1 2 5 10.644755 10.468484 12.725664 0.0000000

保存面板数据

Code
haven::write_sav(panel_data, "data/operated_data/两年面板数据.sav")
write_excel_csv(panel_data, "data/operated_data/两年面板数据.csv")

其他尝试

论文中使用的模型相当于一个截面数据模型(具体原因参见论文)。而我们现在拿到了面板数据,可以尝试使用面板数据建立双向固定效应模型。

Code
coplot(lnexp ~ year|provcd, type="l", data=panel_data) # Lines

Code
library(car)
scatterplot(lnexp ~ year|provcd, boxplots=FALSE, smooth=TRUE, reg.line=FALSE, panel_data)

Code
library(gplots)
plotmeans(lnexp ~ provcd, main="Heterogeineity across provinces", data=panel_data)

Code
plotmeans(lnexp ~ year, main="Heterogeineity across years", data=panel_data)

普通最小二乘回归

不考虑个体的异质性,仅使用普通最小二乘回归

Code
ols <-lm(lnexp ~ lev + lninc + lnasset, data=panel_data)
summary(ols)
## 
## Call:
## lm(formula = lnexp ~ lev + lninc + lnasset, data = panel_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.5240 -0.4229 -0.0068  0.4172  5.9409 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 4.212613   0.067693  62.231   <2e-16 ***
## lev         0.065123   0.007137   9.125   <2e-16 ***
## lninc       0.398298   0.007172  55.534   <2e-16 ***
## lnasset     0.176172   0.005518  31.927   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.7107 on 11148 degrees of freedom
## Multiple R-squared:  0.4482, Adjusted R-squared:  0.4481 
## F-statistic:  3019 on 3 and 11148 DF,  p-value: < 2.2e-16
Code
yhat <- ols$fitted # 获取拟合值
plot(panel_data$lev, panel_data$lnexp, pch=19, xlab="lev", ylab="lnexp")
abline(lm(panel_data$lnexp ~ panel_data$lev), lwd=3, col="red")

LSDV

先不考虑个人,将省份作为个体。

用 LSDV 模型控制省份固定效应(其实就是将因子型变量 provcd 转换为多个 0-1 虚拟变量):

Code
fixed.dum <-lm(lnexp ~ lev + lninc + lnasset + factor(provcd) - 1, data=panel_data)
summary(fixed.dum)
## 
## Call:
## lm(formula = lnexp ~ lev + lninc + lnasset + factor(provcd) - 
##     1, data = panel_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.5177 -0.4183 -0.0115  0.4101  5.8689 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## lev              0.062886   0.007075   8.888  < 2e-16 ***
## lninc            0.388932   0.007158  54.336  < 2e-16 ***
## lnasset          0.179798   0.005657  31.780  < 2e-16 ***
## factor(provcd)1  4.590386   0.132091  34.752  < 2e-16 ***
## factor(provcd)2  4.481364   0.115672  38.742  < 2e-16 ***
## factor(provcd)3  4.065250   0.073240  55.506  < 2e-16 ***
## factor(provcd)4  4.253351   0.075345  56.452  < 2e-16 ***
## factor(provcd)5  4.437868   0.074657  59.443  < 2e-16 ***
## factor(provcd)6  4.316971   0.074680  57.806  < 2e-16 ***
## factor(provcd)7  4.470729   0.080470  55.558  < 2e-16 ***
## factor(provcd)8  4.318285   0.081991  52.668  < 2e-16 ***
## factor(provcd)9  4.226367   0.086947  48.608  < 2e-16 ***
## factor(provcd)10 4.276200   0.090909  47.038  < 2e-16 ***
## factor(provcd)11 4.426879   0.088514  50.013  < 2e-16 ***
## factor(provcd)12 4.225096   0.091924  45.963  < 2e-16 ***
## factor(provcd)13 4.205789   0.089246  47.126  < 2e-16 ***
## factor(provcd)14 4.115438   0.077060  53.406  < 2e-16 ***
## factor(provcd)15 4.184240   0.073260  57.115  < 2e-16 ***
## factor(provcd)16 4.193424   0.075634  55.444  < 2e-16 ***
## factor(provcd)17 4.468321   0.084535  52.858  < 2e-16 ***
## factor(provcd)18 4.370859   0.077490  56.406  < 2e-16 ***
## factor(provcd)19 4.201983   0.078090  53.809  < 2e-16 ***
## factor(provcd)20 4.075776   0.093643  43.525  < 2e-16 ***
## factor(provcd)21 4.299480   0.113519  37.874  < 2e-16 ***
## factor(provcd)22 4.444469   0.077916  57.042  < 2e-16 ***
## factor(provcd)23 4.364189   0.076947  56.716  < 2e-16 ***
## factor(provcd)24 4.245489   0.080305  52.867  < 2e-16 ***
## factor(provcd)25 4.281619   0.089791  47.684  < 2e-16 ***
## factor(provcd)26 4.375738   0.091624  47.758  < 2e-16 ***
## factor(provcd)27 4.256262   0.073472  57.931  < 2e-16 ***
## factor(provcd)28 4.191828   0.074599  56.191  < 2e-16 ***
## factor(provcd)30 4.087577   0.501916   8.144 4.23e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.7026 on 11120 degrees of freedom
## Multiple R-squared:  0.9957, Adjusted R-squared:  0.9957 
## F-statistic: 7.99e+04 on 32 and 11120 DF,  p-value: < 2.2e-16
Code
yhat <- fixed.dum$fitted # 获取拟合值
scatterplot(yhat ~ panel_data$lev|panel_data$provcd, boxplots=FALSE, xlab="lev", ylab="yhat",smooth=FALSE)
abline(lm(panel_data$lnexp ~ panel_data$lev),lwd=3, col="red") # 普通最小二乘回归作为对照

使用 FE

首先先看单向的个体固定效应,即考虑个体差异忽视时间差异。将每个人视为一个个体,控制个体固定效应。个体非常多,无法像 LSDV 模型那样设置 0-1 虚拟变量。

Code
library(plm)
fixed1 <- plm(lnexp ~ lev + lninc + lnasset, data=panel_data, index=c("id", "year"), effect = "individual", model="within")
summary(fixed1)
## Oneway (individual) effect Within Model
## 
## Call:
## plm(formula = lnexp ~ lev + lninc + lnasset, data = panel_data, 
##     effect = "individual", model = "within", index = c("id", 
##         "year"))
## 
## Balanced Panel: n = 5576, T = 2, N = 11152
## 
## Residuals:
##        Min.     1st Qu.      Median     3rd Qu.        Max. 
## -2.3925e+00 -2.2947e-01 -8.8818e-16  2.2947e-01  2.3925e+00 
## 
## Coefficients:
##          Estimate Std. Error t-value  Pr(>|t|)    
## lev     0.0283948  0.0085755  3.3111 0.0009351 ***
## lninc   0.1509303  0.0110861 13.6144 < 2.2e-16 ***
## lnasset 0.0494915  0.0082660  5.9874 2.266e-09 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    1919.6
## Residual Sum of Squares: 1832.4
## R-Squared:      0.045397
## Adj. R-Squared: -0.91006
## F-statistic: 88.3423 on 3 and 5573 DF, p-value: < 2.22e-16

使用双向固定效应来同时控制个体与时间:

Code
fixed2 <- plm(lnexp ~ lev + lninc + lnasset, data=panel_data, index=c("id", "year"), effect = "twoways", model="within")
summary(fixed2)
## Twoways effects Within Model
## 
## Call:
## plm(formula = lnexp ~ lev + lninc + lnasset, data = panel_data, 
##     effect = "twoways", model = "within", index = c("id", "year"))
## 
## Balanced Panel: n = 5576, T = 2, N = 11152
## 
## Residuals:
##        Min.     1st Qu.      Median     3rd Qu.        Max. 
## -2.3556e+00 -2.3132e-01 -3.5527e-15  2.3132e-01  2.3556e+00 
## 
## Coefficients:
##          Estimate Std. Error t-value  Pr(>|t|)    
## lev     0.0272714  0.0085453  3.1914  0.001424 ** 
## lninc   0.1582024  0.0111004 14.2519 < 2.2e-16 ***
## lnasset 0.0579328  0.0083355  6.9501 4.067e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    1915.2
## Residual Sum of Squares: 1818.4
## R-Squared:      0.050526
## Adj. R-Squared: -0.90014
## F-statistic: 98.8379 on 3 and 5572 DF, p-value: < 2.22e-16

加入控制变量:

Code
fixed3 <- plm(
  lnexp ~ lev + lninc + lnasset + provcd + urban + fml + age + age2 + gender + marriage + health, 
  data = panel_data, 
  index=c("id", "year"), 
  effect = "twoways", 
  model="within"
)
summary(fixed3)
## Twoways effects Within Model
## 
## Call:
## plm(formula = lnexp ~ lev + lninc + lnasset + provcd + urban + 
##     fml + age + age2 + gender + marriage + health, data = panel_data, 
##     effect = "twoways", model = "within", index = c("id", "year"))
## 
## Balanced Panel: n = 5576, T = 2, N = 11152
## 
## Residuals:
##        Min.     1st Qu.      Median     3rd Qu.        Max. 
## -2.3664e+00 -2.2620e-01 -2.8258e-15  2.2620e-01  2.3664e+00 
## 
## Coefficients: (1 dropped because of singularities)
##              Estimate  Std. Error t-value  Pr(>|t|)    
## lev        0.02646635  0.00851454  3.1084  0.001891 ** 
## lninc      0.13324212  0.01130221 11.7890 < 2.2e-16 ***
## lnasset    0.05266464  0.00833228  6.3206 2.810e-10 ***
## provcd3   -0.52105551  0.80099919 -0.6505  0.515392    
## provcd5   -0.75661737  0.65535148 -1.1545  0.248336    
## provcd6   -0.77196472  0.64564967 -1.1956  0.231888    
## provcd7   -0.78321335  0.63428725 -1.2348  0.216960    
## provcd8   -0.82365040  0.62534399 -1.3171  0.187854    
## provcd9   -0.76401882  0.61800700 -1.2363  0.216413    
## provcd10  -0.75227959  0.60986616 -1.2335  0.217436    
## provcd11  -0.56077519  0.60198026 -0.9316  0.351609    
## provcd12  -0.62208193  0.59467806 -1.0461  0.295569    
## provcd13  -0.52698587  0.58255687 -0.9046  0.365712    
## provcd14  -0.64677478  0.57972361 -1.1157  0.264616    
## provcd15  -0.67361397  0.57730121 -1.1668  0.243328    
## provcd16  -0.64418542  0.57568435 -1.1190  0.263193    
## provcd17  -0.56610482  0.57498665 -0.9846  0.324887    
## provcd18  -0.61254133  0.57499560 -1.0653  0.286788    
## provcd19  -0.61506632  0.57617856 -1.0675  0.285796    
## provcd20  -0.66661875  0.58314453 -1.1431  0.253028    
## provcd21   0.10240125  0.17701772  0.5785  0.562963    
## provcd22   0.02047910  0.12768734  0.1604  0.872584    
## provcd23  -0.02829353  0.10355344 -0.2732  0.784689    
## provcd24  -0.14825597  0.06758155 -2.1937  0.028296 *  
## provcd26   0.38610533  0.80285008  0.4809  0.630594    
## provcd27   0.30718816  0.80536992  0.3814  0.702903    
## provcd28   0.32753645  0.80632010  0.4062  0.684603    
## provcd30  -0.50855595  0.98475721 -0.5164  0.605576    
## urban2     0.62625742  0.80215193  0.7807  0.435000    
## fml        0.09027908  0.01027611  8.7853 < 2.2e-16 ***
## age        0.07740285  0.06380871  1.2130  0.225164    
## age2      -0.00101173  0.00021525 -4.7002 2.662e-06 ***
## marriage2  0.06698994  0.05949835  1.1259  0.260251    
## health2    0.09464327  0.13247993  0.7144  0.475012    
## health3    0.03872889  0.12284075  0.3153  0.752563    
## health4    0.01473722  0.12008651  0.1227  0.902332    
## health5    0.02839236  0.11953730  0.2375  0.812263    
## health6    0.01221696  0.12003333  0.1018  0.918935    
## health7   -0.00212853  0.12008174 -0.0177  0.985858    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    1915.2
## Residual Sum of Squares: 1772.6
## R-Squared:      0.074442
## Adj. R-Squared: -0.86432
## F-statistic: 11.4168 on 39 and 5536 DF, p-value: < 2.22e-16
Code
library(lmtest)
coeftest(fixed3, vcov = vcovHC, type = "HC1")
## 
## t test of coefficients:
## 
##             Estimate Std. Error  t value  Pr(>|t|)    
## lev        0.0264663  0.0081696   3.2396 0.0012039 ** 
## lninc      0.1332421  0.0152232   8.7526 < 2.2e-16 ***
## lnasset    0.0526646  0.0094359   5.5813 2.500e-08 ***
## provcd3   -0.5210555  0.0356238 -14.6266 < 2.2e-16 ***
## provcd5   -0.7566174  0.5476679  -1.3815 0.1671731    
## provcd6   -0.7719647  0.5381598  -1.4345 0.1514996    
## provcd7   -0.7832134  0.5276709  -1.4843 0.1377906    
## provcd8   -0.8236504  0.5201409  -1.5835 0.1133615    
## provcd9   -0.7640188  0.5131024  -1.4890 0.1365395    
## provcd10  -0.7522796  0.5059520  -1.4869 0.1371088    
## provcd11  -0.5607752  0.4969361  -1.1285 0.2591723    
## provcd12  -0.6220819  0.4891203  -1.2718 0.2034840    
## provcd13  -0.5269859  0.4780638  -1.1023 0.2703645    
## provcd14  -0.6467748  0.4751424  -1.3612 0.1734986    
## provcd15  -0.6736140  0.4719213  -1.4274 0.1535250    
## provcd16  -0.6441854  0.4697387  -1.3714 0.1703154    
## provcd17  -0.5661048  0.4686776  -1.2079 0.2271463    
## provcd18  -0.6125413  0.4690513  -1.3059 0.1916356    
## provcd19  -0.6150663  0.4704373  -1.3074 0.1911192    
## provcd20  -0.6666188  0.4775125  -1.3960 0.1627633    
## provcd21   0.1024012  0.1838060   0.5571 0.5774708    
## provcd22   0.0204791  0.1341802   0.1526 0.8787005    
## provcd23  -0.0282935  0.1126223  -0.2512 0.8016495    
## provcd24  -0.1482560  0.0709725  -2.0889 0.0367603 *  
## provcd26   0.3861053  0.0645112   5.9851 2.298e-09 ***
## provcd27   0.3071882  0.0851143   3.6091 0.0003099 ***
## provcd28   0.3275364  0.0956496   3.4243 0.0006208 ***
## provcd30  -0.5085559  0.1393052  -3.6507 0.0002640 ***
## urban2     0.6262574  0.0516390  12.1276 < 2.2e-16 ***
## fml        0.0902791  0.0114165   7.9078 3.140e-15 ***
## age        0.0774028  0.0634910   1.2191 0.2228527    
## age2      -0.0010117  0.0002118  -4.7768 1.827e-06 ***
## marriage2  0.0669899  0.0625358   1.0712 0.2841150    
## health2    0.0946433  0.1735059   0.5455 0.5854483    
## health3    0.0387289  0.1695445   0.2284 0.8193211    
## health4    0.0147372  0.1678711   0.0878 0.9300476    
## health5    0.0283924  0.1682673   0.1687 0.8660123    
## health6    0.0122170  0.1673425   0.0730 0.9418042    
## health7   -0.0021285  0.1676447  -0.0127 0.9898702    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

使用 stargazer 来输出回归结果

Code
library(stargazer)
output <- stargazer(fixed2, fixed3, type="latex", title = "双向固定效应")
## 
## % Table created by stargazer v.5.2.3 by Marek Hlavac, Social Policy Institute. E-mail: marek.hlavac at gmail.com
## % Date and time: 周日, 4月 23, 2023 - 16:13:41
## \begin{table}[!htbp] \centering 
##   \caption{双向固定效应} 
##   \label{} 
## \begin{tabular}{@{\extracolsep{5pt}}lcc} 
## \\[-1.8ex]\hline 
## \hline \\[-1.8ex] 
##  & \multicolumn{2}{c}{\textit{Dependent variable:}} \\ 
## \cline{2-3} 
## \\[-1.8ex] & \multicolumn{2}{c}{lnexp} \\ 
## \\[-1.8ex] & (1) & (2)\\ 
## \hline \\[-1.8ex] 
##  lev & 0.027$^{***}$ & 0.026$^{***}$ \\ 
##   & (0.009) & (0.009) \\ 
##   & & \\ 
##  lninc & 0.158$^{***}$ & 0.133$^{***}$ \\ 
##   & (0.011) & (0.011) \\ 
##   & & \\ 
##  lnasset & 0.058$^{***}$ & 0.053$^{***}$ \\ 
##   & (0.008) & (0.008) \\ 
##   & & \\ 
##  provcd3 &  & $-$0.521 \\ 
##   &  & (0.801) \\ 
##   & & \\ 
##  provcd5 &  & $-$0.757 \\ 
##   &  & (0.655) \\ 
##   & & \\ 
##  provcd6 &  & $-$0.772 \\ 
##   &  & (0.646) \\ 
##   & & \\ 
##  provcd7 &  & $-$0.783 \\ 
##   &  & (0.634) \\ 
##   & & \\ 
##  provcd8 &  & $-$0.824 \\ 
##   &  & (0.625) \\ 
##   & & \\ 
##  provcd9 &  & $-$0.764 \\ 
##   &  & (0.618) \\ 
##   & & \\ 
##  provcd10 &  & $-$0.752 \\ 
##   &  & (0.610) \\ 
##   & & \\ 
##  provcd11 &  & $-$0.561 \\ 
##   &  & (0.602) \\ 
##   & & \\ 
##  provcd12 &  & $-$0.622 \\ 
##   &  & (0.595) \\ 
##   & & \\ 
##  provcd13 &  & $-$0.527 \\ 
##   &  & (0.583) \\ 
##   & & \\ 
##  provcd14 &  & $-$0.647 \\ 
##   &  & (0.580) \\ 
##   & & \\ 
##  provcd15 &  & $-$0.674 \\ 
##   &  & (0.577) \\ 
##   & & \\ 
##  provcd16 &  & $-$0.644 \\ 
##   &  & (0.576) \\ 
##   & & \\ 
##  provcd17 &  & $-$0.566 \\ 
##   &  & (0.575) \\ 
##   & & \\ 
##  provcd18 &  & $-$0.613 \\ 
##   &  & (0.575) \\ 
##   & & \\ 
##  provcd19 &  & $-$0.615 \\ 
##   &  & (0.576) \\ 
##   & & \\ 
##  provcd20 &  & $-$0.667 \\ 
##   &  & (0.583) \\ 
##   & & \\ 
##  provcd21 &  & 0.102 \\ 
##   &  & (0.177) \\ 
##   & & \\ 
##  provcd22 &  & 0.020 \\ 
##   &  & (0.128) \\ 
##   & & \\ 
##  provcd23 &  & $-$0.028 \\ 
##   &  & (0.104) \\ 
##   & & \\ 
##  provcd24 &  & $-$0.148$^{**}$ \\ 
##   &  & (0.068) \\ 
##   & & \\ 
##  provcd26 &  & 0.386 \\ 
##   &  & (0.803) \\ 
##   & & \\ 
##  provcd27 &  & 0.307 \\ 
##   &  & (0.805) \\ 
##   & & \\ 
##  provcd28 &  & 0.328 \\ 
##   &  & (0.806) \\ 
##   & & \\ 
##  provcd30 &  & $-$0.509 \\ 
##   &  & (0.985) \\ 
##   & & \\ 
##  urban2 &  & 0.626 \\ 
##   &  & (0.802) \\ 
##   & & \\ 
##  fml &  & 0.090$^{***}$ \\ 
##   &  & (0.010) \\ 
##   & & \\ 
##  age &  & 0.077 \\ 
##   &  & (0.064) \\ 
##   & & \\ 
##  age2 &  & $-$0.001$^{***}$ \\ 
##   &  & (0.0002) \\ 
##   & & \\ 
##  marriage2 &  & 0.067 \\ 
##   &  & (0.059) \\ 
##   & & \\ 
##  health2 &  & 0.095 \\ 
##   &  & (0.132) \\ 
##   & & \\ 
##  health3 &  & 0.039 \\ 
##   &  & (0.123) \\ 
##   & & \\ 
##  health4 &  & 0.015 \\ 
##   &  & (0.120) \\ 
##   & & \\ 
##  health5 &  & 0.028 \\ 
##   &  & (0.120) \\ 
##   & & \\ 
##  health6 &  & 0.012 \\ 
##   &  & (0.120) \\ 
##   & & \\ 
##  health7 &  & $-$0.002 \\ 
##   &  & (0.120) \\ 
##   & & \\ 
## \hline \\[-1.8ex] 
## Observations & 11,152 & 11,152 \\ 
## R$^{2}$ & 0.051 & 0.074 \\ 
## Adjusted R$^{2}$ & $-$0.900 & $-$0.864 \\ 
## F Statistic & 98.838$^{***}$ (df = 3; 5572) & 11.417$^{***}$ (df = 39; 5536) \\ 
## \hline 
## \hline \\[-1.8ex] 
## \textit{Note:}  & \multicolumn{2}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\ 
## \end{tabular} 
## \end{table}

更多关于 R 语言创建面板模型的教程参考:https://blog.csdn.net/weixin_43718786/article/details/112854803?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-1-112854803-blog-109094100.235%5Ev31%5Epc_relevant_default_base3&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-1-112854803-blog-109094100.235%5Ev31%5Epc_relevant_default_base3&utm_relevant_index=2