回归lm对R定性和定量变量



我正在尝试对R进行线性回归,具有定性和定量变量。下面是数据集的结构:

trip                             X                 year
A 10                          500                  2019
B 11                          600                  2019
C 9                           450                  2020

我想对" a "(y,变量解释)对其他变量(解释性变量)。问题是,对于像"spsamcialitsamac"这样的定性变量,它会像对待定性变量一样对待其他变量,从而分别在2019年、2020年、……

lm_amb <- lm(reg_spé_amb[,3] ~ reg_spé_amb[,2] + reg_spé_amb[,1] + reg_spé_amb[,4])
summary(lm_amb)
因此,我得到了如下形式的结果:

Estimate            std error

intercept                                         400                   26
reg_spé_amb[, 2]10                                 88                   66
reg_spé_amb[, 2]11                                 64                   10
reg_spé_amb[, 1]A                                  70                   80
reg_spé_amb[, 4]2019                               80                   90

我希望每个a都有一个系数,一个是变量"年"。作为一个整体,而不是作为每年的单独变量,一个用于(" a ")。有人能帮我一下吗?

你的问题表明你只是从统计学和r开始,你会从首先学习基础知识中受益,网上有很多可用的资源。以Modern Statistics with r为例。

有了这些建议,让我们来看看你的问题。

您只提供了三行数据,这不足以估计所提议的回归的参数。所以我添加了三行假数据。

library("tidyverse")
reg_spé_amb <-
tibble::tribble(
~spécialité, ~`nombre de praticiens`, ~`activité en valeur`, ~année,
"anesthésie", 10L, 500L, 2019L,
"chirurgie", 11L, 600L, 2019L,
"anesthésie", 9L, 450L, 2020L,
"chirurgie", 9L, 550L, 2019L,
"chirurgie", 10L, 450L, 2019L,
"anesthésie", 10L, 650L, 2020L
)

使用公式接口可以方便地将模型拟合到数据中。R为我们跟踪了很多信息,包括变量名,所以不需要像reg_spé_amb[,1]这样笨拙的规范。

lm_amb <- lm(
formula = `activité en valeur` ~ `nombre de praticiens` + spécialité + année,
data = reg_spé_amb
)

在我们拟合模型之后,我们验证它。这一步至关重要,因为从一个与数据拟合不佳的模型中提取系数有什么意义?参见Modern Statistics with R"中的8.1.4模型诊断

[验证模型还有很多工作要做…]

一旦我们对模型满意,我们就看系数和其他各种统计数据。

这是我们如何得到年份(annacei)和医生数量(nombre de practicens)对反应(activiten valeur)的线性效应系数的方法。

summary(lm_amb)
#> 
#> Call:
#> lm(formula = `activité en valeur` ~ `nombre de praticiens` + 
#>     spécialité + année, data = reg_spé_amb)
#> 
#> Residuals:
#>          1          2          3          4          5          6 
#> -3.197e-14  6.667e+00 -7.000e+01  7.667e+01 -8.333e+01  7.000e+01 
#> 
#> Coefficients:
#>                          Estimate Std. Error t value Pr(>|t|)
#> (Intercept)            -161620.00  272131.90  -0.594    0.613
#> `nombre de praticiens`      60.00      67.33   0.891    0.467
#> spécialitéchirurgie         33.33     122.93   0.271    0.812
#> année                       80.00     134.66   0.594    0.613
#> 
#> Residual standard error: 106.5 on 2 degrees of freedom
#> Multiple R-squared:   0.32,  Adjusted R-squared:   -0.7 
#> F-statistic: 0.3137 on 3 and 2 DF,  p-value: 0.819

或者更简洁和更好的表格格式:

broom::tidy(lm_amb)
#> # A tibble: 4 × 5
#>   term                    estimate std.error statistic p.value
#>   <chr>                      <dbl>     <dbl>     <dbl>   <dbl>
#> 1 (Intercept)            -161620.   272132.     -0.594   0.613
#> 2 `nombre de praticiens`      60        67.3     0.891   0.467
#> 3 spécialitéchirurgie         33.3     123.      0.271   0.812
#> 4 année                       80.0     135.      0.594   0.613

对于分类变量专业(spcialit)的每个级别,获得一个系数(边际效应)同样容易。

emmeans::emmeans(lm_amb, ~ spécialité)
#>  spécialité emmean   SE df lower.CL upper.CL
#>  anesthésie    530 65.4  2      248      812
#>  chirurgie     563 89.8  2      177      950
#> 
#> Results are averaged over the levels of: année 
#> Confidence level used: 0.95

作为奖励,这里是如何比较两种专业的边际效应。请注意,比较anesthsamcialit - chirurgie是我们从上面的汇总表中得到的系数spsamcialit samchirurgie。这个标志是翻转的,因为spsamcialitsamchirurgie实际上是chirurgie - anesthsamusie的比较。

emmeans::emmeans(lm_amb, pairwise ~ spécialité)
#> $contrasts
#>  contrast               estimate  SE df t.ratio p.value
#>  anesthésie - chirurgie    -33.3 123  2  -0.271  0.8117
#> 
#> Results are averaged over the levels of: année

理解上述所有内容将有助于您理解如何拟合和解释回归模型。

由reprex包(v2.0.1)创建于2022-06-25

最新更新