gpt4 book ai didi

r - 进行 Tobit 回归时的奇异性错误

转载 作者:行者123 更新时间:2023-12-03 16:52:33 26 4
gpt4 key购买 nike

我正在尝试估计一个标准的 tobit 模型,该模型被审查为零。

变量是

因变量 : 幸福

自变量 :

  • 城市(芝加哥,纽约),
  • 性别(男,女),
  • 就业(0=失业,1=就业),
  • 工作类型(失业,蓝色,白色),
  • 假期(失业,每周1天,每周2天)

  • “Worktype”和“Holiday”变量与“Employment”变量相互作用。

    我正在使用 censReg tobit回归包。
    censReg(Happiness ~ City + Gender + Employment:Worktype + Employment:Holiday)

    但是 summary()返回以下错误。
    Error in printCoefmat(coef(x, logSigma = logSigma), digits = digits) : 
    'x' must be coefficient matrix/data frame

    为了找出原因,我运行了 OLS 回归。

    有一些 NA 值,我认为这是因为模型设计和变量设置(某些变量似乎有奇点。而具有 'Employment' = 0 的人具有值 'Worktype' = Unemployed'Holidays' = Unemployed 。这可能是原因? )
    lm(Happiness ~ City + Gender + Employment:Worktype + Employment:Holiday)


    Coefficients: (2 not defined because of singularities)
    Estimate Std. Error t value Pr(>|t|)
    (Intercept) 41.750 9.697 4.305 0.0499 *
    CityNew York -44.500 11.197 -3.974 0.0579 .
    Gender1 2.750 14.812 0.186 0.8698
    Employment:WorktypeUnemployed NA NA NA NA
    Employment:WorktypeBluecolor 35.000 17.704 1.977 0.1867
    Employment:WorktypeWhitecolor 102.750 14.812 6.937 0.0202 *
    Employment:Holiday1 day a week -70.000 22.394 -3.126 0.0889 .
    Employment:Holiday2 day a week NA NA NA NA

    我怎样才能忽略 NA 值并无错误地运行 tobit 回归?

    下面是可重现的代码。
    Happiness <- c(0, 80, 39, 0, 69, 90, 100, 30)

    City <- as.factor(c("New York", "Chicago", "Chicago", "New York", "Chicago",
    "Chicago", "New York", "New York"))
    Gender <- as.factor(c(0, 1, 0, 1, 1, 1, 0, 1)) # 0 = man, 1 = woman.
    Employment <- c(0,1, 0, 0, 1 ,1 , 1 , 1) # 0 = unemployed, 1 = employed.
    Worktype <- as.factor(c(0, 2, 0, 0, 1, 1, 2,2))
    levels(Worktype) <- c("Unemployed", "Bluecolor", "Whitecolor")
    Holiday <- as.factor(c(0, 1, 0, 0, 2, 2, 2, 1))
    levels(Holiday) <- c("Unemployed", "1 day a week", "2 day a week")

    data <- data.frame(Happiness, City, Gender, Employment, Worktype, Holiday)
    reg <- lm(Happiness ~ City + Gender + Employment:Worktype +
    Employment:Holiday)
    summary(reg)

    install.packages("censReg")
    library(censReg)
    tobitreg <- censReg(Happiness ~ City + Gender + Employment:Worktype +
    Employment:Holiday)
    summary(tobitreg)

    最佳答案

    如果您逐步调试对 censReg 的调用,则会达到以下 maxLik 优化:

    result <- maxLik(censRegLogLikCross, start = start, 
    yVec = yVec, xMat = xMat, left = left, right = right,
    obsBelow = obsBelow, obsBetween = obsBetween, obsAbove = obsAbove,
    ...)
    初始条件向量 start这是使用 OLS 回归确定的包含 NA正如您已经发现的那样,对于两个 coefs:
  • 就业:工作类型失业
  • 就业:每周放假2天

  • 这会导致 maxLik返回NULL,并带有错误消息:
    Return code 100: Initial value out of range.
    summary函数得到这个 NULL这解释了您得到的最终错误消息。
    要覆盖它,您可以设置 start范围 :
    tobitreg <- censReg(formula = Happiness ~ City + Gender + Employment:Worktype +      
    Employment:Holiday, start = rep(0,9) )
    summary(tobitreg)

    Call:
    censReg(formula = Happiness ~ City + Gender + Employment:Worktype +
    Employment:Holiday, start = rep(0, 9))

    Observations:
    Total Left-censored Uncensored Right-censored
    8 2 6 0

    Coefficients:
    Estimate Std. error t value Pr(> t)
    (Intercept) 38.666 Inf 0 1
    CityNew York -50.669 Inf 0 1
    Gender1 -360.633 Inf 0 1
    Employment:WorktypeUnemployed 0.000 Inf 0 1
    Employment:WorktypeBluecolor 345.674 Inf 0 1
    Employment:WorktypeWhitecolor 56.210 Inf 0 1
    Employment:Holiday1 day a week 346.091 Inf 0 1
    Employment:Holiday2 day a week 55.793 Inf 0 1
    logSigma 1.794 Inf 0 1

    Newton-Raphson maximisation, 141 iterations
    Return code 1: gradient close to zero
    Log-likelihood: -19.35431 on 9 Df
    即使错误消息消失了,结果也不可靠:
  • 错误 = 信息
  • 梯度接近 0 :没有最优值,解决方案是超平面

  • 回归中的 NA 系数表明这些系数与其他系数线性相关,因此您需要删除其中的一些以获得唯一解。
    正如您所怀疑的,这是因为您只有 Employement = 0worktype = Unemployed ,因此模型无法估计 Employment:WorktypeUnemployed 的系数.与 Employment:Holiday 相同的问题系数。
    所以我担心您正在评估的回归模型没有单一的最佳解决方案。
    如果你摆脱了链接的变量,这有效:
    tobitreg <- censReg(formula = Happiness ~ City + Gender + Employment )
    summary(tobitreg)
    Call:
    censReg(formula = Happiness ~ City + Gender + Employment)

    Observations:
    Total Left-censored Uncensored Right-censored
    8 2 6 0

    Coefficients:
    Estimate Std. error t value Pr(> t)
    (Intercept) 38.6141 5.7188 6.752 1.46e-11 ***
    CityNew York -50.1813 6.4885 -7.734 1.04e-14 ***
    Gender1 -70.3859 8.2943 -8.486 < 2e-16 ***
    Employment 111.5672 10.0927 11.054 < 2e-16 ***
    logSigma 1.7930 0.2837 6.320 2.61e-10 ***
    ---
    Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

    Newton-Raphson maximisation, 8 iterations
    Return code 1: gradient close to zero
    Log-likelihood: -19.36113 on 5 Df

    关于r - 进行 Tobit 回归时的奇异性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47988969/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com