在我的回归中,我想用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希望您在一个分隔符|之后 for each 回归变量提供一个工具,所以不是

> 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:

> data("CigarettesSW", package="AER")
> CigarettesSW <- transform(CigarettesSW,
+                           rprice=price/cpi,
+                           rtdiff=(taxs - tax)/cpi
+ )
> c1995 <- subset(CigarettesSW, year == "1995")

R相关问答推荐

从有序数据中随机抽样

计算R中的威布尔分布的EDF

基于不同组的列的相关性

如何动态更新selectizeInput?

在R中,如何在每个堆叠的条上放置误差条,特别是当使用facet_grid时?

在R中,如何将变量(A,B和C)拟合在同一列中,如A和B,以及A和C在同一面板中?

在ggplot中为不同几何体使用不同的 colored颜色 比例

将饼图插入条形图

使用Scale_*_MANUAL时在图例中保留未使用的系数级别

为什么我使用geom_density的绘图不能到达x轴?

TreeNode打印 twig 并为其上色

调换行/列并将第一行(原始数据帧的第一列)提升为标题的Tidyr类似功能?

防止正则表达式覆盖以前的语句

是否有一个R函数可以输出在输入的字符向量中找到的相应正则表达式模式?

如何移动点以使它们的打印不重叠

填充图例什么时候会有点?

将边列表转换为路径长度列表

删除r中每个因素级别的最后2行

使用nls()函数的非线性模型的半正态图

向内存不足的数据帧添加唯一行