我正在尝试使用条件矩条件在gmm软件包中应用“ gel”来估计两个参数。如果我设置“ smooth = F”,我的代码将运行良好,但是由于时刻是有条件的,因此我必须在以下代码中使用“ smooth = T”:
install.packages("gmm")
library(gmm)
#dummy data
W<-matrix(rbinom(10000,1,0.7),nrow=100,ncol=100)
ind <- upper.tri(W)
W[ind] <- t(W)[ind]
isSymmetric(W)
ni<-rowSums(W)
library(MASS)
psi<-mvrnorm(1,rep(0,100),0.01*diag(100))
x<-cbind(psi,ni,W)
#moment condition
g_rho<-function(tet,x){
psi<-x[,1]
ni<-x[,2]
w<-x[,3:dim(x)[2]]
rho<-c()
sigma2<-c()
for(i in 1:n)
{
rho[i]<-tet[1]/(1-tet[1]+tet[1]*ni[i])*sum(psi[which(w[i,]==1)])
sigma2[i]<-tet[2]/(1-tet[1]+tet[1]*ni[i])
}
g<-cbind(sum(rho),sum(sigma2))
return(g)
}
#derivative of moment condition
dg_rho<-function(tet,x)
{
psi<-x[,1]
ni<-x[,2]
w<-x[,3:dim(x)[2]]
d_rho<-c()
d_sigma2<-c()
for(i in 1:n)
{
d_rho[i]<-sum(psi[which(w[i,]==1)])/(1-tet[1]+tet[1]*ni[i])*
(tet[1]/(1-tet[1]+tet[1]*ni[i]))
d_sigma2[i]<-1/(1-tet[1]+tet[1]*ni[i])
}
dg<-matrix(c(sum(d_rho),sum(d_sigma2)),nrow=2,ncol=1)
return(dg)
}
# code for estimating the parameters using GEL method
gel(g_rho, x, tet0 =c(runif(1,0,1),0.01), gradv = dg_rho, smooth = T,
type = "EL", kernel = c("Truncated"), bw = bwNeweyWest,
approx = "AR(1)", prewhite = 1, ar.method = "ols",
tol_weights = 1e-7, tol_lam = 1e-9, tol_obj = 1e-9, tol_mom = 1e-9,
maxiterlam = 100, constraint = FALSE, optfct = "nlminb",
optlam = "Wu", Lambdacontrol = list(), model = TRUE, X = FALSE,
Y = FALSE,TypeGel = "baseGel", alpha = NULL, eqConst = NULL,
eqConstFullVcov = FALSE)
运行此代码将产生以下错误:
Error in ar.ols(x, aic = aic, order.max = order.max,
na.action = na.action, : 'order.max' must be < 'n.used'
如果我们使用选项“ smooth = F”,则代码可以正常工作。我正在寻找解决错误的指南,因为我需要使用smooth = TRUE选项。
谢谢。