r - `stats::confint` 对象的 `mlm` 置信区间错误?

标签 r statistics

我不确定我是否做错了什么,或者这是R本身的confint函数中的一个错误,但我得到了回归估计的置信区间,这不不包含估计值。

这是reprex:

# model (converting all numeric columns in data to z-scores)
mod <-
  stats::lm(
    formula = cbind(mpg, disp) ~ wt,
    data = purrr::modify_if(.x = mtcars, .p = is.numeric, .f = scale)
  )

# tidy dataframe
broom::tidy(mod, conf.int = TRUE)
#> # A tibble: 4 x 8
#>   response term         estimate std.error statistic  p.value conf.low conf.high
#>   <chr>    <chr>           <dbl>     <dbl>     <dbl>    <dbl>    <dbl>     <dbl>
#> 1 mpg      (Intercept)  8.05e-17    0.0893  9.01e-16 1.00e+ 0   -0.182     0.182
#> 2 mpg      wt          -8.68e- 1    0.0908 -9.56e+ 0 1.29e-10   -1.05     -0.682
#> 3 disp     (Intercept) -1.70e-16    0.0826 -2.06e-15 1.00e+ 0   -0.182     0.182
#> 4 disp     wt           8.88e- 1    0.0840  1.06e+ 1 1.22e-11   -1.05     -0.682


confint(mod)
#>                   2.5 %     97.5 %
#> :(Intercept) -0.1824544  0.1824544
#> :wt          -1.0530332 -0.6822855
#> :(Intercept) -0.1824544  0.1824544
#> :wt          -1.0530332 -0.6822855

如果您绘制估计值,这会变得更加明显:

<sup>Created on 2020-02-22 by the [reprex package](https://reprex.tidyverse.org) (v0.3.0.9001)</sup>

我在这里做错了什么吗?或者这是预期的行为?

最佳答案

confint() 现在可以与传销一起使用:

confint(lm(mpg~wt,data=mtcars))
                2.5 %    97.5 %
(Intercept) 33.450500 41.119753
wt          -6.486308 -4.202635

confint(lm(disp~wt,data=mtcars))
                2.5 %   97.5 %
(Intercept) -204.0914 -58.2054
wt            90.7579 134.1984

confint(lm(cbind(mpg,disp)~wt,data=mtcars))
                       2.5 %     97.5 %
mpg:(Intercept)    33.450500  41.119753
mpg:wt             -6.486308  -4.202635
disp:(Intercept) -204.091436 -58.205395
disp:wt            90.757897 134.198380

问题在于在 dplyr 或 purrr 中使用scale,我在scale之后使用了as.data.frame,因为无论如何你的所有列都是数字:

confint(lm(cbind(mpg,disp)~wt,data=as.data.frame(scale(mtcars))))

                          2.5 %     97.5 %
    mpg:(Intercept)  -0.1824544  0.1824544
    mpg:wt           -1.0530332 -0.6822855
    disp:(Intercept) -0.1687740  0.1687740
    disp:wt           0.7165054  1.0594545

对于 dplyr 和 purrr,您可以看到变量名称也很困惑:

library(dplyr)
library(purrr)

confint(lm(cbind(mpg,disp)~wt,data=modify_if(.x = mtcars, .p = is.numeric, .f = scale)))

                  2.5 %     97.5 %
:(Intercept) -0.1824544  0.1824544
:wt          -1.0530332 -0.6822855
:(Intercept) -0.1824544  0.1824544
:wt          -1.0530332 -0.6822855

confint(lm(cbind(mpg,disp)~wt,data=mutate_if(mtcars,is.numeric,scale))

                  2.5 %     97.5 %
:(Intercept) -0.1824544  0.1824544
:wt          -1.0530332 -0.6822855
:(Intercept) -0.1824544  0.1824544
:wt          -1.0530332 -0.6822855

我猜当你进行缩放时,结转的属性会与限制中的某些内容混淆(仍在查看代码)。解决此问题的一种方法是强制输出为向量(使用 c 或 as.数字):

confint(lm(cbind(mpg,disp)~wt,data=modify_if(mtcars,is.numeric,~c(scale(.x)))))

confint(lm(cbind(mpg,disp)~wt,data=mutate_if(mtcars,is.numeric,~c(scale(.x)))))

                      2.5 %     97.5 %
mpg:(Intercept)  -0.1824544  0.1824544
mpg:wt           -1.0530332 -0.6822855
disp:(Intercept) -0.1687740  0.1687740
disp:wt           0.7165054  1.0594545

不幸的是,tidy(..,conf.int=TRUE) 不太有效,因此为了绘图,您需要使用 stats 中的 confint 进行一些操作.

关于r - `stats::confint` 对象的 `mlm` 置信区间错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60351258/

相关文章:

datetime - 将每小时数据汇总为每日汇总

r - 在点图(ggplot)中添加线交叉因子

R:用向量中的项替换NA

python - 如何使用 matplotlib 绘制线段的线性回归

R 错误 : Error in `row.names<-.data.frame` (`*tmp*` , 值 = 值)

r - "no observations from which to fit a model"错误

r - 如何折叠具有相同标识符的行并保留非空列值?

python - 自举二项式分布

r - xtable 用于不支持的功能(使用 R)

php - 从价格数据集中移除异常值的算法