Yep, I can reproduce your problem.
Then I tried setting method="model.frame"
in the lm
runs, and get the same "unexpected" results you got, i.e. same coefficients with or without weights applied. I peeked at the truncreg
source and didn't see any obvious place that it "chooses" not to use method="model.frame"
; then I dug into the truncreg.fit
source and again didn't see any reference to the weights values. It's not clear to me what is being done, so the weights may well be being passed into the fit code but I might start by digging more carefully thru that code.
Are weights in truncreg package broken?
-
24-09-2022 - |
سؤال
Am I missing something?
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
المحلول
لا تنتمي إلى StackOverflow