truncreg包装中的重量是否破损?

时间:2014-01-05 20:17:17

标签: r truncation

我错过了什么吗?

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

1 个答案:

答案 0 :(得分:1)

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