在我的回归中,我想用 lgrade 检测变量 PastS 并在 R 中运行以下代码:
modiv <- ivreg(servS ~ horS + size + henley + lpd + tri + turn + rail +
enc20 + wheat | pastS | lgrade, data = regdata)
我收到错误:
length(formula)[2] %in% 1:2 is not TRUE
。显然,我的语法有些不对劲,但我找不到错误。你能帮忙吗?
AER::ivreg
希望您在一个分隔符 |
之后为每个回归量提供一个工具,所以而不是
> library(AER)
> ivreg(log(packs) ~ population + tax | log(rprice) | rtdiff, data=c1995)
Error in ivreg(log(packs) ~ population + tax | log(rprice) | rtdiff, data = c1995) :
length(formula)[2] %in% 1:2 is not TRUE
是
> ivreg(log(packs) ~ log(rprice) + population + tax | rtdiff + population + tax, data=c1995)
Call:
ivreg(formula = log(packs) ~ log(rprice) + population + tax | rtdiff + population + tax, data = c1995)
Coefficients:
(Intercept) log(rprice) population tax
7.241e+00 -5.089e-01 -7.907e-09 -4.202e-03
您可以考虑使用
lfe::felm
,它更简洁,可能更符合您的直觉。
> library(lfe)
> felm(log(packs) ~ population + tax| 0 | (log(rprice) ~ rtdiff), data=c1995)
(Intercept) population tax `log(rprice)(fit)`
7.241e+00 -7.907e-09 -4.202e-03 -5.089e-01
数据:
> data("CigarettesSW", package="AER")
> CigarettesSW <- transform(CigarettesSW,
+ rprice=price/cpi,
+ rtdiff=(taxs - tax)/cpi
+ )
> c1995 <- subset(CigarettesSW, year == "1995")