【问题标题】:Are weights in truncreg package broken?truncreg 包装中的重量是否损坏?
【发布时间】:2014-01-05 20:17:17
【问题描述】:

我错过了什么吗?

library(truncreg)

n <- 10^4
lambda <- 0.3  # Proba y is taken from component 0

df <- data.frame(x=rnorm(n))
df$y0 <- pmax(rnorm(n, 10 + df$x, 5), 0)
df$y1 <- pmax(rnorm(n, 2 - 5*df$x, 2), 0)
df$component <- ifelse(runif(n) < lambda, 0, 1)
df$y <- ifelse(df$component == 0, df$y0, df$y1)  # Mixture of censored regressions

plot(df$x, df$y)

model <- truncreg(y ~ x, data=df)  # All data
model.w <- truncreg(y ~ x, data=df, weights=component)  # Only component 1, using weights
model.subset <- truncreg(y ~ x, data=subset(df, component == 1))  # Only component 1, using subset

identical(coefficients(model), coefficients(model.w))  # True -- I expected this to be false
identical(coefficients(model.w), coefficients(model.subset))  # False -- I expected this to be true

## For comparison, here is the same using lm:
model <- lm(y ~ x, data=df)
model.w <- lm(y ~ x, data=df, weights=component)
model.subset <- lm(y ~ x, data=subset(df, component == 1))

identical(coefficients(model), coefficients(model.w))  # False as expected
identical(coefficients(model.w), coefficients(model.subset))  # True as expected

【问题讨论】:

  • 我不知道为什么您希望组件数据的子集具有相同的系数。但至于权重是否“起作用”,我首先绘制拟合模型(所有模型),看看曲线是否具有明显不同的形状。如果不是,那么你的权重组就不够“戏剧化”。
  • @CarlWitthoft 我做了一些编辑——现在是否更清楚为什么我希望 model.w 和 model.subset 具有相同的系数(就像它们对 lm 一样)?构造变量 df$component 以使它们相同。
  • 另一个注意事项:truncreg 不会将 weights 参数视为公式的一部分,因此即使它确实使用了权重,您也必须将其输入为 df$component -- 我通过debug验证了这一点

标签: r truncation


【解决方案1】:

是的,我可以重现您的问题。
然后我尝试在lm 运行中设置method="model.frame",并获得与您相同的“意外”结果,即应用或不应用权重的相同系数。我偷看了truncreg 的源代码,并没有看到它“选择” 使用method="model.frame" 的任何明显位置;然后我挖掘了truncreg.fit 源,再次没有看到任何对权重值的引用。我不清楚正在做什么,所以权重很可能被传递到 fit 代码中,但我可能会从更仔细地挖掘代码开始。

【讨论】:

  • 没错,权重似乎没有用于拟合。看来,要包含权重,可以修改内部函数 ml.truncreg,其中包含对数似然、粗麻布和梯度的函数,然后将其传递给 maxLik 进行拟合。
  • @NatePope 感谢您的确认。我想便宜的解决方法是在调用truncreg之前使用权重值来“增加”输入xy数据。
  • 这个错误是否曾经报告给包维护者?
  • @Carl 不是我 :-);您可能想检查自 14 年 1 月以来该软件包是否有更新,然后尝试自己重现明显的错误。
  • @CarlWitthoft FWIW,包作者告诉我使用crch 包而不是truncreg
猜你喜欢
  • 1970-01-01
  • 2017-10-02
  • 2010-10-21
  • 1970-01-01
  • 2017-04-02
  • 1970-01-01
  • 2012-04-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多