不允许因子变量时的交互变量(Stata)
Interaction Variables when Factor Variables are not allowed (Stata)
我是 运行 Stata 中的回归,我想对其使用 cluster2
(http://www.kellogg.northwestern.edu/faculty/petersen/htm/papers/se/se_programming.htm)。
我遇到了以下问题。 Stata 报告 factor variables and time-series operators not allowed
。我正在使用大量控件,广泛应用 Stata 提供的交互方法。
例如:state##c.wind_speed##L.c.relative_humidity
。 cluster2
以及其他 Stata 包不允许包含此类表达式作为自变量。有没有一种有效的方法可以自己创建这么长的交互变量向量?
您没有提供可验证的示例。有关关键建议,请参阅 https://whosebug.com/help/mcve。
然而,乍一看,问题在于 cluster2
是一个编写于 2006/2007 年的旧程序,其 syntax
语句不允许因子变量。
您可以尝试破解该程序的克隆来修复该问题;我不知道这是否足够。
无法对您暗示有相同问题的 "other Stata packages" 进行具体评论,只是它很可能出于相同原因而出现。因子变量于 2009 年在 Stata 11 (see here for documentation) 中引入,旧程序不允许它们未经修改。
一般来说,我会在Statalist上问这样的问题。该程序很可能已被其他程序取代。
如果您在 Internet 上找到没有帮助文件的 Stata 程序,就像这里的情况一样,通常表明该程序是 ad hoc 编写的,并且没有被维护。在这种情况下,也很明显该程序自 Stata 11 以来 6 年没有更新。
您也可以像您暗示的那样,自己创建交互变量。我认为没有人编写过真正通用的工具来实现自动化:(自 2009 年以来)没有必要(自 2009 年起)使用因子变量表示法的复杂替代方法。
我相信人们可以通过 Baum-Shaffer-Stillman 将 ivreg2
骗到 运行 OLS 中,从而实现双向聚类和交互:
. webuse nlswork
(National Longitudinal Survey. Young Women 14-26 years of age in 1968)
. ivreg2 ln_w grade c.age##c.ttl_exp tenure, cluster(idcode year)
OLS estimation
--------------
Estimates efficient for homoskedasticity only
Statistics robust to heteroskedasticity and clustering on idcode and year
Number of clusters (idcode) = 4697 Number of obs = 28099
Number of clusters (year) = 15 F( 5, 14) = 674.29
Prob > F = 0.0000
Total (centered) SS = 6414.823933 Centered R2 = 0.3206
Total (uncentered) SS = 85448.21266 Uncentered R2 = 0.9490
Residual SS = 4357.997339 Root MSE = .3938
---------------------------------------------------------------------------------
| Robust
ln_wage | Coef. Std. Err. z P>|z| [95% Conf. Interval]
----------------+----------------------------------------------------------------
grade | .0734785 .002644 27.79 0.000 .0682964 .0786606
age | -.0005405 .002259 -0.24 0.811 -.0049681 .0038871
ttl_exp | .0656393 .0068499 9.58 0.000 .0522138 .0790648
|
c.age#c.ttl_exp | -.0010539 .0002217 -4.75 0.000 -.0014885 -.0006194
|
tenure | .0197137 .0029555 6.67 0.000 .013921 .0255064
_cons | .5165052 .0529343 9.76 0.000 .4127559 .6202544
---------------------------------------------------------------------------------
Included instruments: grade age ttl_exp c.age#c.ttl_exp tenure
------------------------------------------------------------------------------
为了确保将其与 OLS 系数进行比较:
. reg ln_w grade c.age##c.ttl_exp tenure
Source | SS df MS Number of obs = 28,099
-------------+---------------------------------- F(5, 28093) = 2651.79
Model | 2056.82659 5 411.365319 Prob > F = 0.0000
Residual | 4357.99734 28,093 .155127517 R-squared = 0.3206
-------------+---------------------------------- Adj R-squared = 0.3205
Total | 6414.82393 28,098 .228301798 Root MSE = .39386
---------------------------------------------------------------------------------
ln_wage | Coef. Std. Err. t P>|t| [95% Conf. Interval]
----------------+----------------------------------------------------------------
grade | .0734785 .0010414 70.55 0.000 .0714373 .0755198
age | -.0005405 .000663 -0.82 0.415 -.0018401 .0007591
ttl_exp | .0656393 .0030809 21.31 0.000 .0596007 .0716779
|
c.age#c.ttl_exp | -.0010539 .0000856 -12.32 0.000 -.0012216 -.0008862
|
tenure | .0197137 .0008568 23.01 0.000 .0180344 .021393
_cons | .5165052 .0206744 24.98 0.000 .4759823 .557028
---------------------------------------------------------------------------------
我是 运行 Stata 中的回归,我想对其使用 cluster2
(http://www.kellogg.northwestern.edu/faculty/petersen/htm/papers/se/se_programming.htm)。
我遇到了以下问题。 Stata 报告 factor variables and time-series operators not allowed
。我正在使用大量控件,广泛应用 Stata 提供的交互方法。
例如:state##c.wind_speed##L.c.relative_humidity
。 cluster2
以及其他 Stata 包不允许包含此类表达式作为自变量。有没有一种有效的方法可以自己创建这么长的交互变量向量?
您没有提供可验证的示例。有关关键建议,请参阅 https://whosebug.com/help/mcve。
然而,乍一看,问题在于 cluster2
是一个编写于 2006/2007 年的旧程序,其 syntax
语句不允许因子变量。
您可以尝试破解该程序的克隆来修复该问题;我不知道这是否足够。
无法对您暗示有相同问题的 "other Stata packages" 进行具体评论,只是它很可能出于相同原因而出现。因子变量于 2009 年在 Stata 11 (see here for documentation) 中引入,旧程序不允许它们未经修改。
一般来说,我会在Statalist上问这样的问题。该程序很可能已被其他程序取代。
如果您在 Internet 上找到没有帮助文件的 Stata 程序,就像这里的情况一样,通常表明该程序是 ad hoc 编写的,并且没有被维护。在这种情况下,也很明显该程序自 Stata 11 以来 6 年没有更新。
您也可以像您暗示的那样,自己创建交互变量。我认为没有人编写过真正通用的工具来实现自动化:(自 2009 年以来)没有必要(自 2009 年起)使用因子变量表示法的复杂替代方法。
我相信人们可以通过 Baum-Shaffer-Stillman 将 ivreg2
骗到 运行 OLS 中,从而实现双向聚类和交互:
. webuse nlswork
(National Longitudinal Survey. Young Women 14-26 years of age in 1968)
. ivreg2 ln_w grade c.age##c.ttl_exp tenure, cluster(idcode year)
OLS estimation
--------------
Estimates efficient for homoskedasticity only
Statistics robust to heteroskedasticity and clustering on idcode and year
Number of clusters (idcode) = 4697 Number of obs = 28099
Number of clusters (year) = 15 F( 5, 14) = 674.29
Prob > F = 0.0000
Total (centered) SS = 6414.823933 Centered R2 = 0.3206
Total (uncentered) SS = 85448.21266 Uncentered R2 = 0.9490
Residual SS = 4357.997339 Root MSE = .3938
---------------------------------------------------------------------------------
| Robust
ln_wage | Coef. Std. Err. z P>|z| [95% Conf. Interval]
----------------+----------------------------------------------------------------
grade | .0734785 .002644 27.79 0.000 .0682964 .0786606
age | -.0005405 .002259 -0.24 0.811 -.0049681 .0038871
ttl_exp | .0656393 .0068499 9.58 0.000 .0522138 .0790648
|
c.age#c.ttl_exp | -.0010539 .0002217 -4.75 0.000 -.0014885 -.0006194
|
tenure | .0197137 .0029555 6.67 0.000 .013921 .0255064
_cons | .5165052 .0529343 9.76 0.000 .4127559 .6202544
---------------------------------------------------------------------------------
Included instruments: grade age ttl_exp c.age#c.ttl_exp tenure
------------------------------------------------------------------------------
为了确保将其与 OLS 系数进行比较:
. reg ln_w grade c.age##c.ttl_exp tenure
Source | SS df MS Number of obs = 28,099
-------------+---------------------------------- F(5, 28093) = 2651.79
Model | 2056.82659 5 411.365319 Prob > F = 0.0000
Residual | 4357.99734 28,093 .155127517 R-squared = 0.3206
-------------+---------------------------------- Adj R-squared = 0.3205
Total | 6414.82393 28,098 .228301798 Root MSE = .39386
---------------------------------------------------------------------------------
ln_wage | Coef. Std. Err. t P>|t| [95% Conf. Interval]
----------------+----------------------------------------------------------------
grade | .0734785 .0010414 70.55 0.000 .0714373 .0755198
age | -.0005405 .000663 -0.82 0.415 -.0018401 .0007591
ttl_exp | .0656393 .0030809 21.31 0.000 .0596007 .0716779
|
c.age#c.ttl_exp | -.0010539 .0000856 -12.32 0.000 -.0012216 -.0008862
|
tenure | .0197137 .0008568 23.01 0.000 .0180344 .021393
_cons | .5165052 .0206744 24.98 0.000 .4759823 .557028
---------------------------------------------------------------------------------