【问题标题】:SLR of transformed data in RR中转换数据的SLR
【发布时间】:2020-09-10 23:19:38
【问题描述】:

对于 Y = 收入低于贫困线的人口百分比和 X = 人口的人均收入,我构建了一个箱形图,发现 lambda = 0.02020:

bc <- boxcox(lm(Percent_below_poverty_level ~ Per_capita_income, data=tidy.CDI), plotit=T)
bc$x[which.max(bc$y)] # gives lambda

现在我想用转换后的数据拟合一个简单的线性回归,所以我输入了这段代码

transform <- lm((Percent_below_poverty_level**0.02020) ~ (Per_capita_income**0.02020))
transform

但我得到的只是错误消息 'terms.formula(公式,数据 = 数据)中的错误:公式中的无效功率'。我的错误是什么?

【问题讨论】:

    标签: r


    【解决方案1】:

    您可以使用car 包中的bcPower()

    ## make sure you do install.packages("car") if you haven't already
    library(car)
    data(Prestige)
    p <- powerTransform(prestige ~ income + education + type , 
                        data=Prestige, 
                        family="bcPower")
    
    summary(p)
    # bcPower Transformation to Normality 
    # Est Power Rounded Pwr Wald Lwr Bnd Wald Upr Bnd
    # Y1    1.3052           1       0.9408       1.6696
    # 
    # Likelihood ratio test that transformation parameter is equal to 0
    # (log transformation)
    # LRT df       pval
    # LR test, lambda = (0) 41.67724  1 1.0765e-10
    # 
    # Likelihood ratio test that no transformation is needed
    # LRT df    pval
    # LR test, lambda = (1) 2.623915  1 0.10526
    
    mod <- lm(bcPower(prestige, 1.3052) ~ income + education + type, data=Prestige)
    summary(mod)
    # 
    # Call:
    #   lm(formula = bcPower(prestige, 1.3052) ~ income + education + 
    #        type, data = Prestige)
    # 
    # Residuals:
    #   Min      1Q  Median      3Q     Max 
    # -44.843 -13.102   0.287  15.073  62.889 
    # 
    # Coefficients:
    #   Estimate Std. Error t value Pr(>|t|)    
    # (Intercept) -3.736e+01  1.639e+01  -2.279   0.0250 *  
    #   income       3.363e-03  6.928e-04   4.854 4.87e-06 ***
    #   education    1.205e+01  2.009e+00   5.999 3.78e-08 ***
    #   typeprof     2.027e+01  1.213e+01   1.672   0.0979 .  
    # typewc      -1.078e+01  7.884e+00  -1.368   0.1746    
    # ---
    #   Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
    # 
    # Residual standard error: 22.25 on 93 degrees of freedom
    # (4 observations deleted due to missingness)
    # Multiple R-squared:  0.8492,  Adjusted R-squared:  0.8427 
    # F-statistic:   131 on 4 and 93 DF,  p-value: < 2.2e-16
    

    【讨论】:

      【解决方案2】:

      幂(在 R,FWIW 中更多地由^ 表示,而不是**)在公式中具有特殊含义[它们表示变量之间的相互作用而不是数学运算]。因此,如果您确实想对等式的两边进行幂变换,您可以使用 I() 或“原样”运算符:

      I(Percent_below_poverty_level^0.02020) ~ I(Per_capita_income^0.02020)
      

      然而,我认为你应该按照@DaveArmstrong 的建议去做:

      • 只有预测变量会被转换
      • Box-Cox transformation 实际上是 (y^lambda-1)/lambda(尽管移位和规模可能对您的结果无关紧要)

      【讨论】:

        猜你喜欢
        • 2014-04-03
        • 2021-08-29
        • 2017-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-11
        相关资源
        最近更新 更多