我正在进行一项蒙特卡洛研究。我有一个具有异方差性的线性模型,并且因变量的左删失为 0。删失率的平均值为 25.9。
我得到错误
Error in lm.fit(X.vlm, y = z.vlm, ...) : NA/NaN/Inf in 'x'
在尝试估计一个tobit模型之后。
vglm(y[i,]~X[1,i,]+X[2,i,]+X[3,i,]+X[4,i,],family=tobit(Lower=0))
我的数据是从标准分布模拟的,所以问题不应该来自奇数变量。
我发现另外两个与真实数据有相同问题的问题: lm() NA/NaN/Inf error , lm() NA/NaN/Inf error 但似乎没有任何令人满意的答案。除了我的数据很容易重现,所以它应该有助于识别问题
以下是代码:
library(VGAM)
set.seed(12345)
nobs=100
nsim=100
b=c(2,-2,-3,3)
g=c(1,0.2)
y=matrix(rep(0,nobs*nsim),ncol=nobs,nrow=nsim)
X=array(0,dim=c(4,nsim,nobs))
res=matrix(rep(0,nobs*nsim),ncol=nobs,nrow=nsim)
tobit=vector(mode="list",length=nsim)
for(i in 1:nsim){
# generate covariates :
X[1,i,]=rlnorm(n=nobs)
X[2,i,]=runif(n=nobs)<=.75
X[3,i,]=rnorm(mean = 3,n=nobs)
X[4,i,]=runif(n=nobs,min=0,max=10)
res[i,]=(g[1]+g[2]*X[4,i,])*rnorm(n=nobs)
# generate censored dependent variable
y[i,]=b[1]*X[1,i,]+b[2]*X[2,i,]+b[3]*X[3,i,]+b[4]*X[4,i,]+res[i,]
y[i,]=sapply(y[i,],FUN=function(x){max(0,x)}) #apply censoring
tobit[[i]]<-vglm(y[i,]~X[1,i,]+X[2,i,]+X[3,i,]+X[4,i,],
family = tobit(Lower=0))
}
这是回溯
traceback()
5: lm.fit(X.vlm, y = z.vlm, ...)
4: vlm.wfit(xmat = X.vlm.save, z, Hlist = NULL, U = U, matrix.out =FALSE,
is.vlmX = TRUE, qr = qr.arg, xij = NULL)
3: vglm.fitter(x = x, y = y, w = w, offset = offset, Xm2 = Xm2,
Ym2 = Ym2, etastart = etastart, mustart = mustart, coefstart =coefstart,
family = family, control = control, constraints = constraints,
criterion = control$criterion, extra = extra, qr.arg = qr.arg,
Terms = mt, function.name = function.name, ...)
2: vglm(y[1, ] ~ X[1, 1, ] + X[2, i, ] + X[3, i, ] + X[4, i, ],
family = tobit(Lower = 0))
1: traceback(vglm(y[1, ] ~ X[1, 1, ] + X[2, i, ] + X[3, i, ] + X[4,
i, ], family = tobit(Lower = 0)))
*** 编辑 :
通过删除一个协变量(我尝试使用 X[3,i,] 和 X[4,i,])并将较低的审查设置为 -0.001,正如 BondedDust 建议的那样,它工作正常,我什至将复制次数推到 1000重大问题。
通过将较低的审查设置为 -0.001,并保留所有协变量,我在 100 次迭代中得到了两个错误。值得注意的是,现在的错误是
Error in lm.fit(X.vlm, y = z.vlm, ...) : NA/NaN/Inf in 'y'
除了我得到这些警告
In vglm.fitter(x = x, y = y, w = w, offset = offset, Xm2 = Xm2, ... :
iterations terminated because half-step sizes are very small