[英]R Generalized Method Of Moments Regression Estimation With Instruments
我正在嘗試使用R中的廣義矩方法訓練回歸模型。我有3個內生回歸變量,它們與6個我所知的外生變量相關。
為了進行培訓,我將數據設置在data.matrix dat
。 第一列為y,第二列均為1,第三至第八列為工具x1-x6。 第9到第11列是回歸變量(z1,z2和z1 * z2)。
然后我將矩條件設置如下:
moments <- function(theta, data) {
y <- as.numeric(data[, 1])
x <- data.matrix(data[, c(2,3:8)])
z <- data.matrix(data[, c(2,9,10,11)])
m <- x * as.vector((y - z %*% theta))
return(cbind(m))
}
然后嘗試訓練模型:
gmm_model <- gmm(
g = moments,
x = dat,
t0 = (lm(y ~ z1 + z2 + z1:z2,
data=dat))$coefficients
)
運行此model order: 1 singularities in the computation of the projection matrix results are only valid up to model order 0Error in AA %*% t(X) : requires numeric/complex matrix/vector arguments
出現錯誤: model order: 1 singularities in the computation of the projection matrix results are only valid up to model order 0Error in AA %*% t(X) : requires numeric/complex matrix/vector arguments
該錯誤表明x的秩不夠大,無法估計與z對應的變量的系數。
但是當我檢查Matrix::rankMatrix(dat[,3:8])
告訴我x的排名為5時,而Matrix::rankMatrix(dat[2,9,10,11])
告訴我z的排名為4。 , rankMatrix(t(x) %*% z )
得出4。對於我來說,這些值對於GMM似乎很好。 我究竟做錯了什么?
我也嘗試使用plm
pgmm
,但是我的數據實際上只是一個橫截面(而不是面板數據集),當我嘗試使用它時,由於每個人有多個觀察值而出現錯誤。
dat
看起來像:
y i x1 x2 x3 x4 x5 x6 z1 z2 z1*z2
[1,] 0 1 31 0 123 0.12 123456 1234567 0 0.2954545 0
[2,] 0 1 44 0 123 0.12 123456 1234567 0 0.1555556 0
[3,] 0 1 31 0 123 0.12 123456 1234567 0 0.2325581 0
[4,] 0 1 47 0 123 0.12 123456 1234567 0 0.2537313 0
[5,] 0 1 33 0 123 0.12 123456 1234567 0 0.1500000 0
[6,] 0 1 49 0 123 0.12 123456 1234567 0 0.2553191 0
x1是[30-100]中的整數x2是二進制0,1 x3取兩個值x4取兩個值x5取兩個值x6取兩個值
z1是二進制0,1 z2在[0,1]中是連續的
嘗試了許多不同的事情后,什么工作一度下探變量x,直到變量x的數量為基質,包括所有的人都為列排名:看來gmm
不喜歡有在使用外源性儀器的矩陣中的任何奇異當下條件。 明確地,當我更改為這組矩條件時,相同的gmm調用起作用了:
moments <- function(theta, data) {
y <- as.numeric(data[, 1])
x <- data.matrix(data[, c(2,3,4,5,6)]) # this is rank 5 still
z <- data.matrix(data[, c(2,9,10,11)]) # rank 4
m <- x * as.vector((y - z %*% theta))
return(cbind(m))
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.