首页 > 解决方案 > 如何将系数设置为特定值,并在模型摘要中保留预测变量?

问题描述

我正在运行以下类型的线性回归:

y <- lm(x ~ z, data)

我希望 z 设置为 0.8,然后我希望能够使用 tidy 函数从模型输出中提取 z 的结果估计值。我看过offset(),但我无法在模型输出中看到 z 估计值,我需要一个汇总表。简单地包括就足够了I(z*0.8)吗?这将导致以下代码:

y <- lm(x ~ I(z*0.8), data)

标签: rregressionlinear-regressiontransformation

解决方案


我会推荐ggeffects。例如:


library(ggeffects)
#> Warning: package 'ggeffects' was built under R version 3.6.2
library(ggplot2)
#> Registered S3 methods overwritten by 'ggplot2':
#>   method         from 
#>   [.quosures     rlang
#>   c.quosures     rlang
#>   print.quosures rlang
data(efc)
fit <- lm(barthtot ~ c12hour + neg_c_7 + c161sex + c172code, data = efc)

mydf <- ggpredict(fit, terms = c("c12hour [30:80]", "c172code [1,3]"))
mydf
#> # Predicted values of Total score BARTHEL INDEX
#> # x = average number of hours of care per week
#> 
#> # c172code = low level of education
#> 
#>  x | Predicted |         95% CI
#> -------------------------------
#> 30 |     67.15 | [64.04, 70.26]
#> 38 |     65.12 | [62.06, 68.18]
#> 47 |     62.84 | [59.81, 65.88]
#> 55 |     60.81 | [57.78, 63.85]
#> 63 |     58.79 | [55.72, 61.85]
#> 80 |     54.48 | [51.28, 57.68]
#> 
#> # c172code = high level of education
#> 
#>  x | Predicted |         95% CI
#> -------------------------------
#> 30 |     68.58 | [65.42, 71.75]
#> 38 |     66.56 | [63.39, 69.73]
#> 47 |     64.28 | [61.08, 67.47]
#> 55 |     62.25 | [59.01, 65.50]
#> 63 |     60.23 | [56.91, 63.54]
#> 80 |     55.92 | [52.39, 59.45]
#> 
#> Adjusted for:
#> * neg_c_7 = 11.84
#> * c161sex =  1.76

ggplot(mydf, aes(x, predicted, colour = group)) + geom_line()

reprex 包于 2020-12-04 创建(v0.3.0)

这里


推荐阅读