eval(predvars, data, env) 中的 systemfit 错误:找不到对象

systemfit Error in eval(predvars, data, env) : object not found

我正在尝试使用 systemfit 包来估计计量经济学模型。我不断得到 error: Error in eval(predvars, data, env) : object 'gdp_gro' not found。问题是 gdp_gro 肯定在我的数据集中。

我试图Google这个错误,但我想不出解决办法。基于 Whosebug 和其他地方的其他问题,我认为问题在于 R 处理公式环境的方式。我将不胜感激关于为什么我的代码不起作用以及如何修复它的一些指导。非常感谢你。

我正在附加一个 reprex 和部分数据集(如下):

library(systemfit)
#> Loading required package: Matrix
#> Loading required package: car
#> Loading required package: carData
#> Loading required package: lmtest
#> Loading required package: zoo
#> 
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#> 
#>     as.Date, as.Date.numeric
#> 
#> Please cite the 'systemfit' package as:
#> Arne Henningsen and Jeff D. Hamann (2007). systemfit: A Package for Estimating Systems of Simultaneous Equations in R. Journal of Statistical Software 23(4), 1-40. http://www.jstatsoft.org/v23/i04/.
#> 
#> If you have questions, suggestions, or comments regarding the 'systemfit' package, please use a forum or 'tracker' at systemfit's R-Forge site:
#> https://r-forge.r-project.org/projects/systemfit/

#Prepare a new list
DF = list()

#Splitting the data in list elements by years
for (i in seq(from = 1, by =1, to =length(unique(as.numeric(df$year))))){

  DF[[i]] <- df[df$year == unique(as.numeric(df$year))[i],]

}
#> Error: object of type 'closure' is not subsettable

Equation = list()
Inststruments = list()

#### SPECIFCIATION OF EQUATIONS
for (i in seq(from = 1, by =1, to =length(unique(as.numeric(df$year))))){

  #Automatically pick up the instruments
  instruments <- select_vars(colnames(as.data.frame(DF[i])), ends_with("_I", ignore.case = FALSE))

  #Variables picked out automatically
  variables <- colnames(as.data.frame(DF[i]))[!colnames(as.data.frame(DF[i])) %in% c(instruments, "gdp_gro")]

  #Automatizing the equation specification 
  Equation[[i]] <- with(DF[i] , as.formula(paste("gdp_gro ~ ", paste(variables, collapse="+"))) )

  Inststruments[[i]] <- with(DF[i] , as.formula(paste("~ ", paste(instruments, collapse="+"))) )

}
#> Error: object of type 'closure' is not subsettable

#Finally run the key regressions
reg <- systemfit(Equation, method = "3SLS", inst = Inststruments, methodResidCov = "noDfCor" )
#> Error in formula[[i]]: subscript out of bounds

部分数据(用dput生成):

  structure(list(country_code = c("AUS", "AUS", "AUS", "AUS", "AUT", 
"AUT", "AUT", "AUT", "BEL", "BEL", "BEL", "BEL", "BGR", "BGR", 
"BGR", "BGR"), year = c(1995L, 2000L, 2005L, 2010L, 1995L, 2000L, 
2005L, 2010L, 1995L, 2000L, 2005L, 2010L, 1995L, 2000L, 2005L, 
2010L), tot_lif = c(0.0128486367909746, 0.0126208212768577, 0.0123698898778096, 
0.0122406329302881, 0.0130432016288096, 0.0127997002997003, 0.0126053003750845, 
0.0124099521762818, 0.0130139312547017, 0.0128663779576979, 0.0126613550738064, 
0.0124714828897338, 0.0140739189685534, 0.0139541215710299, 0.013781512605042, 
0.0136031851360319), cap_aset = c(NA, 6.9, 6.9, 5.43, NA, 5.2, 
4.8, 7.49276, NA, 2.8, 2.7, 4.96531, NA, 15.3, 7.4, 10.4677), 
    cb_assets = c(3.06318, 2.46202, 2.20076, 3.33956, 0.399895, 
    0.536933, 0.551538, 0.815212, 0.972776, 0.466959, 0.248117, 
    0.823367, 7.90828, 8.51751, 3.15831, 0.095416), tot_fert = c(1.822, 
    1.756, 1.807, 1.928, 1.42, 1.36, 1.41, 1.44, 1.56, 1.67, 
    1.76, 1.86, 1.23, 1.26, 1.37, 1.57), nat_res = c(2.09714172369929, 
    3.51179102271941, 7.72238072354596, 8.01628506180971, 0.11853075155191, 
    0.150961988690622, 0.206953989580903, 0.216777594515187, 
    0.0223053533152314, 0.0203362079028292, 0.0177518400351335, 
    0.019336002220052, 1.47167055527445, 1.33790542432826, 2.31972541945117, 
    2.08046786610067), trad_term = c(NA, 100, 131.0849232, 178.9001645, 
    NA, 100, 95.91930131, 92.25883781, NA, 100, 98.77006835, 
    96.59919868, NA, 100, 84.20837461, 125.8550667), gov_cons = c(0.1471103005, 
    0.1474746565, 0.151790273666667, NA, 0.149812934333333, 0.154511682833333, 
    0.1511594605, NA, 0.171062330333333, 0.180379932166667, 0.174480045833333, 
    NA, 0.289015879333333, 0.323738674333333, 0.276189208166667, 
    NA), gdp_gro = c(3.8293687531652, 3.94318908229896, 3.19031634041626, 
    2.05294200395332, 2.66798366552709, 3.37572197324998, 2.24406681223554, 
    1.83709459152502, 2.3847572377606, 3.63365201492995, 2.09427270847185, 
    2.74421327182894, 2.86018944148833, 4.94055377635733, 7.12351107638302, 
    1.32408780093829), gross_inv = c(0.266632934333333, 0.260118643666667, 
    0.300801128, 0.264703502, 0.335587352333333, 0.260061338333333, 
    0.270549208, 0.287363499333333, 0.2805128, 0.246921166666667, 
    0.308350493333333, 0.324811398666667, 0.0818940103333333, 
    0.136577869666667, 0.236514484, 0.19643417), gdp_cap_1 = c(0.00192864730372135, 
    0.00222430451645368, 0.00250264618548625, 0.00273814839048662, 
    0.0027345490098763, 0.0032819778735874, 0.00334421790851598, 
    0.0037646138629738, NA, 0.00344158786452203, 0.00350312141684792, 
    0.00395886153650055, 0.000862928006781417, 0.000734707837639529, 
    0.000935582564325206, 0.0013229694175178), gdp_cap_2 = c(0.00191622588648934, 
    0.00221469960943122, 0.00254717484680898, 0.00285420952389829, 
    0.00252904621475959, 0.00305993171114882, 0.00329798344078102, 
    0.00354070266940731, NA, 0.00289716367632824, 0.00329402073984151, 
    0.00365521257262544, 0.000834744862348611, 0.000765775240349386, 
    0.000991473707456532, 0.0012880711598984), econ_open = c(1.11564470333333, 
    1.07036188216667, 1.31764187416667, NA, 1.19112350533333, 
    1.13605700933333, 1.37068624266667, NA, 1.14728215333333, 
    1.1020498675, 1.34182663783333, NA, 1.01287280516667, 0.982563148, 
    1.2535847325, NA), demo_2 = c(0.0156360833333333, NA, NA, 
    0.0272981816666667, 0.0162439233333333, 0.016152265, 0.0163356866666667, 
    0.0192567566666667, 0.0179158466666667, 0.02126817, 0.023894505, 
    0.0293736833333333, NA, 0.05795811, 0.0639238566666667, 0.052836915
    ), male_edu_1 = c(5.81, 5.82, 5.84, 5.87, 4.09, 3.86, 3.86, 
    3.87, 5.53, 5.59, 5.63, 5.59, 5.38, 5.43, 5.9, 6.3), male_edu_2 = c(4.78, 
    4.76, 4.81, 4.61, 5.48, 5.77, 6.08, 6.28, 3.79, 3.96, 4.31, 
    4.38, 2.87, 3.11, 3.72, 4.31), male_edu_3 = c(0.88, 0.78, 
    0.93, 1.12, 0.35, 0.48, 0.64, 0.71, 0.8, 0.88, 0.95, 1.05, 
    0.53, 0.58, 0.61, 0.71), male_edu_4 = c(11.48, 11.35, 11.58, 
    11.6, 9.92, 10.11, 10.58, 10.86, 10.12, 10.43, 10.88, 11.02, 
    8.79, 9.13, 10.23, 11.32), tot_lif_I = c(0.0128682464785598, 
    0.0127095722174518, 0.012465515344814, 0.0122834370967119, 
    0.0131404621173837, 0.0128830665052315, 0.012685638515979, 
    0.0124510515974525, 0.0130783197148458, 0.0129052789260619, 
    0.0127617321099324, 0.0125262669490894, 0.0140154543995514, 
    0.0140966755510497, 0.0138573536026272, 0.0136964325672041
    ), tot_fert_I = c(1.822, 1.756, 1.807, 1.928, 1.42, 1.36, 
    1.41, 1.44, 1.56, 1.67, 1.76, 1.86, 1.23, 1.26, 1.37, 1.57
    ), gov_cons_I = c(0.147556354666667, 0.147787615333333, 0.14822341, 
    0.154233063333333, 0.143237327333333, 0.154914428666667, 
    0.150818939, 0.160776774333333, 0.167895371666667, 0.174698651, 
    0.174371118333333, 0.185355464666667, 0.318261792333333, 
    0.319208641666667, 0.313319266, 0.274927805), gross_inv_I = c(0.271324028333333, 
    0.263693382333333, 0.298435052333333, 0.269873092666667, 
    0.342670599333333, 0.287529100666667, 0.262940883333333, 
    0.273333341, 0.290991584333333, 0.259098499666667, 0.269275173666667, 
    0.313822090666667, 0.0746616733333333, 0.12165235, 0.171543354666667, 
    0.245303109333333), gdp_cap_1_I = c(0.00192864730372135, 
    0.00222430451645368, 0.00250264618548625, 0.00273814839048662, 
    0.0027345490098763, 0.0032819778735874, 0.00334421790851598, 
    0.0037646138629738, NA, 0.00344158786452203, 0.00350312141684792, 
    0.00395886153650055, 0.000862928006781417, 0.000734707837639529, 
    0.000935582564325206, 0.0013229694175178), gdp_cap_2_I = c(0.00191622588648934, 
    0.00221469960943122, 0.00254717484680898, 0.00285420952389829, 
    0.00252904621475959, 0.00305993171114882, 0.00329798344078102, 
    0.00354070266940731, NA, 0.00289716367632824, 0.00329402073984151, 
    0.00365521257262544, 0.000834744862348611, 0.000765775240349386, 
    0.000991473707456532, 0.0012880711598984), econ_open_I = c(1.08680443966667, 
    1.05545339966667, 1.146271606, 1.401552578, 1.17984314766667, 
    1.122572442, 1.21375773333333, 1.43774867033333, 1.14471817, 
    1.08343706533333, 1.18125647333333, 1.41352349533333, 1.00123712466667, 
    0.956378658666667, 1.057932824, 1.33654979833333), demo_2_I = c(0.0174405, 
    0.0141582, NA, 0.02649142, 0.01485111, 0.01894116, 0.01685453, 
    0.01725761, 0.01650168, 0.01752877, 0.02481076, 0.02306411, 
    0.0366033, 0.0530792, 0.06323963, 0.05811036)), row.names = c(NA, 
16L), class = "data.frame")

您需要为 systemfit 提供数据参数,并且 运行 列表中各个项目的函数。

reg <- systemfit::systemfit(Equation[[1]], method = "3SLS", inst = Inststruments[[1]], 
methodResidCov = "noDfCor", data = DF[[1]])
> Error in intI(i, n = x@Dim[1], dn[[1]], give.dn = FALSE) : 
   index larger than maximal 0

这解决了您的第一个问题,但随后又给您带来了另一个问题:)

我联系了软件包开发人员,感谢他的努力,我的问题在 systemfit 1.1-23 中得到了解决。此版本在 CRAN 上尚不可用,但可以从 R-Forge 获得:http://r-forge.r-project.org/R/?group_id=242 或使用命令:

install.packages("systemfit",repos="http://R-Forge.R-project.org")