IV回归与聚类标准误

huangapple go评论78阅读模式
英文:

IV Regression With Clustered Standard Errors

问题

在R中,我有一个包含个体、年份以及变量y、x和z观测数据的面板数据集。我试图估计一个IV回归模型,使得:

$$y = \beta x + e$$

$$x = \alpha z + u$$

有没有办法在估计IV回归时按个体对标准误差进行聚类?

到目前为止,我已经使用了ivreg函数,如下所示:

my_iv <- ivreg(y ~ x + as.factor(year) | z + as.factor(year),
  data = data)

但我无法弄清如何包括聚类标准误差。提前感谢您的帮助!

英文:

On R, I have panel dataset with observations for individual, year and variables y, x and z. I am trying to estimate an IV regression so that:

$$y = \beta x + e$$

$$x = \alpha z + u$$

Is there a way to estimate the IV regression while clustering standard errors by individual?

So far I have used ivreg as follows:

my_iv &lt;- ivreg(y ~ x + as.factor(year) |z + as.factor(year),
  data = data)

But can't figure out how to include clustered standard errors. Thanks in advance!

答案1

得分: 2

We can use lfe::felm.

The formula is specified as y ~ x1 + x2 | f1 + f2 | (Q|W ~ x3+x4) | clu1 + clu2, where the first term is the model, f are the fixed effects, the middle term is the instrument, and the third term represents the clusters, where we only have clu1 = id.

ivest <- lfe::felm(y ~ x1 + x2 | id + firm | (Q|W ~ x3 + factor(x4)) | id, data = d)

summary(ivest)

Call:

lfe::felm(formula = y ~ x1 + x2 | id + firm | (Q | W ~ x3 + factor(x4)) | id, data = d)

Residuals:

Min 1Q Median 3Q Max

-1.9436 -0.5140 0.0078 0.4335 2.0452

Coefficients:

Estimate Cluster s.e. t value Pr(>|t|)

x1 1.6392 0.5155 3.180 0.00519 **

x2 0.5039 0.1547 3.257 0.00438 **

Q(fit) 0.9348 0.5148 1.816 0.08608 .

W(fit) 1.2116 0.1048 11.561 9.18e-10 ***

---

Signif. codes: 0 ‘’ 0.001 ‘’ 0.01 ‘’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.051 on 40 degrees of freedom

Multiple R-squared(full model): 0.8295 Adjusted R-squared: 0.6846

Multiple R-squared(proj model): 0.7157 Adjusted R-squared: 0.474

F-statistic(full model, iid): 6.157 on 34 and 40 DF, p-value: 6.629e-08

F-statistic(proj model): 85.66 on 4 and 18 DF, p-value: 1.837e-11

F-statistic(endog. vars): 121.7 on 2 and 18 DF, p-value: 3.488e-11

Data:

d <- structure(list(x1 = c(1.37095844714667, -0.564698171396089, 0.363128411337339,
0.63286260496104, 0.404268323140999, -0.106124516091484, 1.51152199743894,
-0.0946590384130976, 2.01842371387704, -0.062714099052421, 1.30486965422349,
2.28664539270111, -1.38886070111234, -0.278788766817371, -0.133321336393658,
0.635950398070074, -0.284252921416072, -2.65645542090478, -2.44046692857552,
1.32011334573019, -0.306638594078475, -1.78130843398, -0.171917355759621,
1.2146746991726, 1.89519346126497, -0.4304691316062, -0.25726938276893,
-1.76316308519478, 0.460097354831271, -0.639994875960119, 0.455450123241219,
0.704837337228819, 1.03510352196992, -0.608926375407211, 0.50495512329797,
-1.71700867907334, -0.784459008379496, -0.850907594176518, -2.41420764994663,
0.0361226068922556, 0.205998600200254, -0.361057298548666, 0.758163235699517,
-0.726704827076575, -1.36828104441929, 0.432818025888717, -0.811393176186672,
1.44410126172125, -0.431446202613345, 0.655647883402207, 0.321925265203947,
-0.783838940880375, 1.57572751979198, 0.642899305717316, 0.0897606465996057,
0.276550747291463, 0.679288816055271, 0.0898328865790817, -2.99309008315293,
0.284882953530659, -0.367234642740975, 0.185230564865609, 0.581823727365507,
1.39973682729268, -0.727292059474465, 1.

英文:

We can use lfe::felm.

The formula is specified as y ~ x1 + x2 | f1 + f2 | (Q|W ~ x3+x4) | clu1 + clu2 , where first term is the model, f are the fixed effects, middle term is the instrument and third term the clusters, where we only have clu1 = id.

ivest &lt;- lfe::felm(y ~ x1 + x2 | id + firm | (Q|W ~ x3 + factor(x4)) | id, data = d)

summary(ivest)
# Call:
#   lfe::felm(formula = y ~ x1 + x2 | id + firm | (Q | W ~ x3 + factor(x4)) |      id, data = d) 
# 
# Residuals:
#     Min      1Q  Median      3Q     Max 
# -1.9436 -0.5140  0.0078  0.4335  2.0452 
# 
# Coefficients:
#   Estimate Cluster s.e. t value Pr(&gt;|t|)    
# x1         1.6392       0.5155   3.180  0.00519 ** 
# x2         0.5039       0.1547   3.257  0.00438 ** 
# `Q(fit)`   0.9348       0.5148   1.816  0.08608 .  
# `W(fit)`   1.2116       0.1048  11.561 9.18e-10 ***
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
# Residual standard error: 1.051 on 40 degrees of freedom
# Multiple R-squared(full model): 0.8295   Adjusted R-squared: 0.6846 
# Multiple R-squared(proj model): 0.7157   Adjusted R-squared: 0.474 
# F-statistic(full model, *iid*):6.157 on 34 and 40 DF, p-value: 6.629e-08 
# F-statistic(proj model): 85.66 on 4 and 18 DF, p-value: 1.837e-11 
# F-statistic(endog. vars):121.7 on 2 and 18 DF, p-value: 3.488e-11 

Data:

d &lt;- structure(list(x1 = c(1.37095844714667, -0.564698171396089, 0.363128411337339, 
0.63286260496104, 0.404268323140999, -0.106124516091484, 1.51152199743894, 
-0.0946590384130976, 2.01842371387704, -0.062714099052421, 1.30486965422349, 
2.28664539270111, -1.38886070111234, -0.278788766817371, -0.133321336393658, 
0.635950398070074, -0.284252921416072, -2.65645542090478, -2.44046692857552, 
1.32011334573019, -0.306638594078475, -1.78130843398, -0.171917355759621, 
1.2146746991726, 1.89519346126497, -0.4304691316062, -0.25726938276893, 
-1.76316308519478, 0.460097354831271, -0.639994875960119, 0.455450123241219, 
0.704837337228819, 1.03510352196992, -0.608926375407211, 0.50495512329797, 
-1.71700867907334, -0.784459008379496, -0.850907594176518, -2.41420764994663, 
0.0361226068922556, 0.205998600200254, -0.361057298548666, 0.758163235699517, 
-0.726704827076575, -1.36828104441929, 0.432818025888717, -0.811393176186672, 
1.44410126172125, -0.431446202613345, 0.655647883402207, 0.321925265203947, 
-0.783838940880375, 1.57572751979198, 0.642899305717316, 0.0897606465996057, 
0.276550747291463, 0.679288816055271, 0.0898328865790817, -2.99309008315293, 
0.284882953530659, -0.367234642740975, 0.185230564865609, 0.581823727365507, 
1.39973682729268, -0.727292059474465, 1.30254263204414, 0.335848119752074, 
1.03850609869762, 0.920728568290646, 0.720878162866862, -1.04311893856785, 
-0.0901863866107067, 0.623518161999544, -0.953523357772344, -0.542828814573857
), x2 = c(0.580996497681682, 0.768178737834591, 0.463767588540167, 
-0.885776297409679, -1.09978089864786, 1.51270700980493, 0.257921437532031, 
0.0884402291595864, -0.120896537539089, -1.19432889516053, 0.611996898040387, 
-0.217139845746521, -0.182756706331922, 0.93334632857116, 0.821773110508249, 
1.39211637593427, -0.476173923054674, 0.650348560726305, 1.39111045639, 
-1.1107888794479, -0.860792586877842, -1.13173868085377, -1.4592139995024, 
0.0799825532411612, 0.65320433964919, 1.20096537559849, 1.04475108716773, 
-1.00320864683985, 1.84848190167275, -0.666773408757817, 0.105513812456069, 
-0.422255881868856, -0.122350171954971, 0.188193034501498, 0.119160957997006, 
-0.0250925508674029, 0.108072727942033, -0.485435235846668, -0.504217130687904, 
-1.66109907991481, -0.382333726873818, -0.5126502578778, 2.7018910003448, 
-1.36211623118972, 0.137256218558607, -1.49362506731629, -1.4704357414368, 
0.124702386197007, -0.996639134884037, -0.0018226143047082, -0.428258881425815, 
-0.613671606449495, -2.02467784541911, -1.22474795035999, 0.179516441117938, 
0.567620594423535, -0.492877353553475, 6.28840653511241e-05, 
1.12288964337997, 1.43985574297619, -1.09711376840582, -0.117319560250177, 
1.2014984009197, -0.469729580566301, -0.0524694849389963, -0.0861072982370896, 
-0.887679017906432, -0.444684004884738, -0.0294448790882381, 
-0.413868849057924, 1.1133860233682, -0.480992841653982, -0.433169032600729, 
0.696862576552103, -1.05636841317091), id = structure(c(15L, 
1L, 6L, 10L, 8L, 19L, 12L, 5L, 10L, 14L, 2L, 6L, 1L, 13L, 15L, 
16L, 10L, 17L, 2L, 9L, 4L, 19L, 14L, 7L, 16L, 5L, 19L, 17L, 8L, 
10L, 6L, 9L, 8L, 18L, 8L, 10L, 18L, 4L, 9L, 16L, 11L, 6L, 14L, 
8L, 9L, 17L, 7L, 13L, 11L, 18L, 9L, 5L, 2L, 3L, 2L, 13L, 15L, 
4L, 6L, 19L, 19L, 10L, 1L, 13L, 1L, 4L, 12L, 6L, 11L, 7L, 11L, 
5L, 17L, 7L, 12L), levels = c(&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot;, &quot;6&quot;, &quot;7&quot;, 
&quot;9&quot;, &quot;10&quot;, &quot;11&quot;, &quot;12&quot;, &quot;13&quot;, &quot;14&quot;, &quot;15&quot;, &quot;16&quot;, &quot;17&quot;, &quot;18&quot;, &quot;19&quot;, 
&quot;20&quot;), class = &quot;factor&quot;), firm = structure(c(10L, 6L, 2L, 12L, 
3L, 1L, 13L, 4L, 11L, 9L, 6L, 10L, 7L, 3L, 12L, 2L, 9L, 12L, 
10L, 9L, 3L, 7L, 3L, 6L, 13L, 4L, 10L, 2L, 7L, 1L, 2L, 3L, 1L, 
10L, 5L, 13L, 2L, 3L, 3L, 10L, 2L, 10L, 4L, 9L, 10L, 13L, 7L, 
1L, 4L, 6L, 3L, 12L, 3L, 6L, 12L, 9L, 10L, 7L, 3L, 9L, 2L, 6L, 
9L, 8L, 11L, 6L, 5L, 10L, 1L, 9L, 10L, 1L, 12L, 6L, 1L), levels = c(&quot;1&quot;, 
&quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot;, &quot;6&quot;, &quot;7&quot;, &quot;8&quot;, &quot;9&quot;, &quot;10&quot;, &quot;11&quot;, &quot;12&quot;, &quot;13&quot;
), class = &quot;factor&quot;), u = c(1.50059880011796, -1.1406741536911, 
0.496847374722702, 1.4083694575847, -0.62337037345602, -0.741660664568964, 
0.376627729983173, -1.31017535156112, 0.0781199235564999, -0.795416769640129, 
1.76649494325627, -0.936421828043658, 0.703918293809932, -0.755838819389157, 
-0.468169290964327, -0.844487992517338, -1.09153959975659, -1.32335062531384, 
-0.591596405204697, 0.748314146325651, -0.821525190798946, -1.76459494767604, 
-0.388535656201395, -0.669263871638158, 0.704517511710363, -0.827627694647081, 
-3.3493240811071, 0.342540952025075, -1.93109788557074, 0.177752576915369, 
1.40188419407316, 0.373066264998072, -0.0486032519865556, 2.46346047322287, 
0.567606140037349, 0.968840299750039, 2.65141739994693, -0.0344926185009225, 
-0.40962669998027, -0.103651100985079, -0.574649379426294, -0.51335141274752, 
-0.0552973857861534, -0.978438064871714, 0.0587080378017696, 
0.0618109093908222, -0.380486867891915, 1.41806216315907, -0.818524354680359, 
-0.457037558315825, 0.217494497375878, 0.625046465412299, 0.517376640159983, 
-0.185381470022198, -0.064033677082036, -0.704399082564789, -0.699697505160012, 
0.560902750983909, 0.0478343678989824, 0.566967709139165, -1.19615960100349, 
0.959600821189023, 0.507252101121057, -0.374112242378446, 1.42044733361966, 
1.69437460038628, 0.516779140506365, -0.0481044125067062, -1.28024577281817, 
-0.48968148391622, 1.28769545705832, 0.930501447346418, 0.72614069638199, 
-0.193061140800734, -0.561229382374985), y = c(0.708066254628044, 
0.589847453563791, 3.27667516342197, -0.358336088116629, 1.93500981542015, 
1.2290187506713, 3.88766205090481, -0.853662127286324, -2.97033719755537, 
-2.13167402260678, -1.24053074997088, -0.66281266951442, 3.49598166168038, 
1.69270069297096, -2.09652282229081, 1.54306435896839, 0.732824572951072, 
2.19324620039636, 0.431719256134024, -1.98271609019762, -1.99702206469318, 
0.797786579028574, 2.22710786022503, 1.76093212830986, 1.31242354427332, 
-1.72283495631895, -1.37198900123695, 1.08479618619769, 0.0287449582719836, 
-1.50213538600816, 1.65240525388632, -1.4421369631952, -1.30345852538258, 
3.28724465137813, -0.211786236468322, 2.18957244539307, 1.56845473318596, 
1.53145225517394, 2.27959056711884, 0.64051870094227, 0.864194170850803, 
-1.64068991456381, 4.40925788562828, 0.841266425448286, 0.132140253784467, 
3.05682249679669, 0.150826763337795, 0.133032535285816, -2.05592783553907, 
0.203954571581413, -1.27312068778677, 1.9033153295143, -4.05715144236128, 
-1.34004031196686, -0.000958627238489718, -0.362160895354921, 
-0.0579078985442866, -0.0265915674252484, 4.71311872421459, 1.98292166068375, 
-2.23214380882587, 1.18921557572872, 3.47207577553789, 0.495203981641904, 
2.05682465802038, 1.89443159356544, -0.584175385578428, -0.679597405650453, 
0.749822321747065, 1.95112667124936, 3.36808860806582, -0.287388459122759, 
3.02594427585541, 3.65847302005212, 0.404317829797556), x3 = c(-0.0580369218956107, 
0.449420458030374, 1.07708171621578, 0.18648317970334, 1.56915807785103, 
-0.226757041173532, 0.766361213944578, -0.312113303827908, -1.35545758035236, 
-0.905646776474841, -0.651186922939884, -1.04474994594567, 1.59391872402891, 
0.542347904017506, -2.36633351109067, 0.471107993311305, -0.231608083899238, 
0.0936844299205661, -1.29508960780026, -0.848784608724723, -2.29005568761573, 
1.28285601248922, 1.66378018954505, 0.401436254867987, -1.5093425111217, 
-1.13836587194805, 0.337741204287881, -1.47262814385863, -0.291877329483668, 
-2.02964531250218, -0.594044533118403, -1.05455069953558, -1.07847961911409, 
1.28112323849091, 0.0810282612193334, -0.99307899011046, -0.75707153139077, 
0.00924351551023881, 1.40932694027844, 0.293275146448466, 0.297134825971617, 
-1.40253035970293, 0.104285140622482, 0.371419703354857, -1.67615738713669, 
1.78467147315505, -2.27698465336006, -1.58982110493222, -0.246374254853273, 
-0.35374988018924, 0.268372607619405, 0.454175327851366, -2.68824727377896, 
0.866650172377294, 0.168739701377876, -1.09082406185189, -0.38034811429265, 
-0.948091836164654, 0.721252264089489, -0.159142456737528, -1.78755661450632, 
1.49377847299869, 1.52389572182853, 0.458664055612706, -0.812496512482602, 
1.05538328565825, -0.907595054419802, -0.417152112834271, 1.12843784555488, 
0.933612853893915, 0.445603849144031, -0.0591298458723813, 0.188708474704579, 
0.691269579039664, 0.821856282128092), x4 = c(11L, 1L, 1L, 4L, 
10L, 5L, 12L, 11L, 9L, 3L, 5L, 5L, 2L, 9L, 3L, 8L, 12L, 2L, 11L, 
8L, 12L, 2L, 6L, 10L, 1L, 4L, 8L, 8L, 3L, 9L, 10L, 6L, 3L, 1L, 
4L, 12L, 9L, 1L, 11L, 6L, 4L, 9L, 12L, 8L, 6L, 12L, 6L, 1L, 11L, 
11L, 9L, 2L, 11L, 3L, 1L, 11L, 1L, 1L, 8L, 11L, 2L, 1L, 7L, 6L, 
2L, 5L, 1L, 9L, 7L, 2L, 12L, 4L, 7L, 10L, 3L), Q = c(0.646076516637449, 
0.774080826010094, 0.705491710522028, -0.329960376229488, 2.3937371586073, 
-0.175453512555076, 2.54744938000504, 0.227305524232654, 1.17480056418941, 
0.314456133339992, -0.0804355243921238, 2.9329566992971, -0.662472721938112, 
1.26523857842063, -0.623496493384185, 1.24297501180904, 0.338575486300627, 
-1.01897501642229, -1.4577819702458, -0.220428059190226, -1.09122765376906, 
-0.797414605643003, 1.79407962827076, 2.41577034670514, 0.692621619981134, 
-0.606319409257714, 0.956043025501215, -1.11775444992615, 1.89608324348157, 
-0.863502236328827, 0.767257776055209, -0.320789249747173, 1.08195881842052, 
-0.0440656150328142, 0.757553454326518, -1.61835680364557, -0.728173973541659, 
-0.939760619892448, -1.42632613622065, 0.484748356232311, 0.0228991794151839, 
0.487920320035935, 2.55563673422967, 0.626999269491833, -1.60281421987745, 
0.961527689359054, 0.910717534800114, 0.580434197322011, -0.958460907355067, 
1.07029842296127, 0.445149928725711, -0.986338470177715, -0.207729334549163, 
0.146619643496565, -1.07290918879056, 0.385381081617997, 0.534497721129405, 
-0.902058044918584, -0.994572369688443, 0.180823001094562, -0.866662979176913, 
-0.202275546504082, 1.73827586670431, 2.13966378394672, -0.165781203871104, 
0.533931178962832, 0.822623338094019, 1.74551382869949, 1.32794595998142, 
2.43573160454602, -0.559235835256031, -0.362748369122295, 0.962221699341186, 
1.60692805915648, 0.928717057173142), W = c(-2.83762382837145, 
-0.0881326865595118, -0.849333816502672, -3.47988767629463, -1.25757341429974, 
1.84520152770484, -3.05023294700363, 0.00359941535195923, -6.56098795097819, 
-3.49031810270443, -4.72002812925721, -5.8535812329552, 3.8408026234803, 
-0.185770570993576, -2.90334736382513, -1.36070438950823, 0.98928972872898, 
4.56099504732292, 4.64544468251834, -3.02066411165329, 0.616674402296625, 
5.91850052824554, -0.515720909998593, -3.38773746666513, -3.68833342688417, 
-0.734948617130689, 1.43850797310029, 2.38978672741508, -2.57084901665802, 
-0.145829650882282, -3.35360143478379, -1.47279229368909, -4.42324187193105, 
0.991719825790708, -2.50310563360398, 3.07779435805313, -1.32782534107766, 
3.56323944057156, 7.29719069966174, 1.19307470232548, 1.40086649613106, 
-2.01403461672415, -2.39775611572149, 0.771955483382669, 4.27959416950681, 
0.403938975289119, -0.849080251103613, -4.09776416085041, 1.21595604447267, 
-2.05700790259951, -1.528229857328, 1.94298831392009, -5.2991157119565, 
-0.671165412529572, -0.507466314736962, -2.04536515574161, -0.0631163240487921, 
0.506176392202083, 6.28698004765225, -0.136395621843103, 0.11629291113172, 
-0.266486705229334, -1.62824000948479, -2.65620379813359, 0.522554460895879, 
-1.4430959240814, -2.29002246193027, -4.20932914045798, 0.775344843756961, 
-3.10339695092203, 4.43830824189278, -0.386409948433949, -1.37520970091814, 
0.702201027077844, -0.0774679544406474)), row.names = c(NA, -75L
), class = &quot;data.frame&quot;)

答案2

得分: 1

我建议使用fixest包中的feols函数:

library(fixest)
feols(y ~ as.factor(year) | x ~ z, cluster = "id", data = data)
英文:

I would recommend the feols function from the fixest package:

library(fixest)
feols(y ~ as.factor(year) | x ~ z, cluster = &quot;id&quot;, data = data)

huangapple
  • 本文由 发表于 2023年2月19日 19:51:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499933.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定