TL; DR
我在R中使用VGAM软件包运行Tobit回归 - 这是一个玩具数据集,它一直给我一个我无法诊断的错误:
library(data.table)
library(VGAM)
> sessionInfo()$otherPkgs
$VGAM
Package: VGAM
Version: 0.9-7
Date: 2015-03-06
... <ommitted> ...
reg_data <- structure(list(S = c(1.83271488441825, 0.75411550370994, 0.904938604451928,
0.75411550370994, 0.75411550370994), H = c(0.6429, 0.7788,
0.6292, 0.8892, 0.2035), W= c(1.52497, 1.1391, 1.59722,
1.8406, 1.01865)), .Names = c("S", "H", "W"), class = c("data.table",
"data.frame"), row.names = c(NA, -5L))
minS <- 0.75411550370994
maxS <- 1.83271488441825
m <- vglm(S ~ H, tobit(Upper = maxS, Lower = minS), weights = W, data = reg_data)
Error in lm.wfit(x = cbind(x[!use.i11, ]), y = y[!use.i11, ii], w = w[!use.i11, :
incompatible dimensions
尝试诊断
使用追溯:
> traceback()
6: stop("incompatible dimensions")
5: lm.wfit(x = cbind(x[!use.i11, ]), y = y[!use.i11, ii], w = w[!use.i11,
ii])
4: eval(expr, envir, enclos)
3: eval(slot(family, "initialize"))
2: 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, ...)
1: vglm(y ~ x, tobit(Upper = maxy, Lower = miny), weights = w, data = X)
我查看了lm.wfit
的源代码并找到了错误的来源:
function (x, y, w, offset = NULL, method = "qr", tol = 1e-07,
singular.ok = TRUE, ...)
{
<ommitted...>
if (NROW(y) != n | length(w) != n)
stop("incompatible dimensions")
<ommitted...>
}
我在vglm
的源代码中找到了以下内容:
vglm.fitter <- get(method)
fit <- 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,
...)
该方法默认为vglm.fit
。
我仍然无法找到排除标准use.i11
的创建位置,它正在做什么以及为什么它会导致权重,回归和回归之间存在冲突。
我观察到将minS
和maxS
四舍五入到十个或更少的位置会导致成功运行,但这是因为maxS
增加了所以第一次观察不再正确审查和minS
增加,因此第2,第4和第5次观察不再受到审查。两者都改变了最大似然函数中观察的处理,所以我怀疑我会以错误的结果污染回归。
有人可以帮助诊断出现此类错误的原因吗?