我不确定我是否做错了什么,或者这是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
如果您绘制估计值,这会变得更加明显:
我在这里做错了什么吗?或者这是预期的行为?
最佳答案
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/