TL;博士
我正在使用 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
and舍入maxS
到十个或更少的位置会导致成功运行,但这是因为maxS
增加了所以第一次观察不再被右删失,并且minS
增加了所以第二、第四和第五次观察不再被左删失。两者都改变了最大似然函数中的观察处理,所以我怀疑我会用错误的结果污染回归。
有人可以帮助诊断为什么会发生这种类型的错误吗?