Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
truncreg包装中的重量是否损坏?_R_Truncation - Fatal编程技术网

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

truncreg包装中的重量是否损坏?,r,truncation,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)

我错过什么了吗

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
库(truncreg)

是的,我可以重现你的问题。

然后,我尝试在
lm
运行中设置
method=“model.frame”
,并得到与您得到的相同的“意外”结果,即相同的系数,无论是否应用权重。我瞥了一眼
truncreg
源代码,没有看到它“选择”而不是使用
method=“model.frame”
的任何明显位置;然后我深入到
trunreg.fit
源代码中,再次没有看到任何对权重值的引用。我不清楚正在做什么,因此权重可能会传递到fit代码中,但我可能会从更仔细地挖掘代码开始。

我不知道为什么您希望组件数据的子集具有相同的系数。但至于权重是否“起作用”,我会先画出拟合模型(所有模型)的图形,看看曲线是否有明显不同的形状。如果没有,那么你的权重集就没有“戏剧性”到重要的程度。@CarlWitthoft我做了一些编辑——现在是否更清楚为什么我希望model.w和model.subset具有相同的系数(就像它们对lm所做的那样)?变量df$component的构造使它们完全相同。另一个注意事项是:
truncreg
没有将
weights
参数视为公式的一部分,因此即使它确实使用了权重,您也必须将其作为
df$component
输入——我通过
debug
验证了这一点,这是正确的,配重似乎不用于安装。似乎,要包含权重,需要修改内部函数
ml.truncreg
,该函数包含对数似然函数、hessian函数和梯度函数,然后将这些函数传递给
maxLik
进行拟合。@nateppe感谢您的确认。我想廉价的解决方法是在调用
truncreg
之前,使用权重值“增加”输入
x
y
数据。这个错误是否曾向包维护人员报告过?@Carl不是我说的:-);您可能需要检查软件包自2014年1月以来是否有更新,然后尝试自己复制明显的错误。@CarlWitthoft FWIW,软件包作者告诉我使用
crch
软件包,而不是
truncreg