[英]How can I calculate the studentized residual per observation by group in R?
我正在研究精益库存管理对公司财务业绩的影响,为此我需要创建一个新变量。
此变量通过 2 个步骤计算:
所以从数学上讲,我知道我应该如何使用 go 来做到这一点,但是我的数据集有超过 3000 个观察值,这需要很长时间才能手工完成。
我的数据集如下(来自 dput(head()))。 该数据集仅显示相同的 NAICS (315),但还有更多,总共 46 个。
mydata <- structure(list(NAICS = c(315, 315, 315, 315, 315, 315),
Year = c(2016, 2017, 2018, 2019, 2020, 2016),
`Total Inventories` = c(487.591, 548.722, 574.226, 593.987, 599.262, 487.997),
`Net Sales` = c(3241.999, 3443.591, 3501.199, 3553.923, 3050.61, 2387.289)),
row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"))
谁能帮助我如何在 R 中轻松做到这一点? 所有帮助将不胜感激。
试试下面的。
数据
为了说明我的方法,请考虑以下基于您的示例数据
> df
# A tibble: 12 x 4
NAICS Year `Total Inventories` `Net Sales`
<dbl> <dbl> <dbl> <dbl>
1 315 2016 488. 3242.
2 315 2017 549. 3444.
3 315 2018 574. 3501.
4 315 2019 594. 3554.
5 315 2020 599. 3051.
6 315 2016 488. 2387.
7 320 2016 488. 3242.
8 320 2017 549. 3444.
9 320 2018 574. 3501.
10 320 2019 594. 3554.
11 320 2020 599. 3051.
12 320 2016 488. 2387.
步骤1
首先,您根据NAICS
值在
> df_list <- split(df, df$NAICS)
> df_list
$`315`
# A tibble: 6 x 4
NAICS Year `Total Inventories` `Net Sales`
<dbl> <dbl> <dbl> <dbl>
1 315 2016 488. 3242.
2 315 2017 549. 3444.
3 315 2018 574. 3501.
4 315 2019 594. 3554.
5 315 2020 599. 3051.
6 315 2016 488. 2387.
$`320`
# A tibble: 6 x 4
NAICS Year `Total Inventories` `Net Sales`
<dbl> <dbl> <dbl> <dbl>
1 320 2016 488. 3242.
2 320 2017 549. 3444.
3 320 2018 574. 3501.
4 320 2019 594. 3554.
5 320 2020 599. 3051.
6 320 2016 488. 2387.
第2步
Then you use lapply()
to go over all data.frames in that list, estimating the model and using the function rstud()
from the MASS
package to calculate the studentized residuals for every firm
Out <- lapply(df_list, function(z) { z$stud_res <- MASS::studres(lm(log(z$`Total Inventories`) ~ log(z$`Net Sales`)))*(-1); z})
> Out
$`315`
# A tibble: 6 x 5
NAICS Year `Total Inventories` `Net Sales` stud_res
<dbl> <dbl> <dbl> <dbl> <dbl>
1 315 2016 488. 3242. 2.25
2 315 2017 549. 3444. 0.313
3 315 2018 574. 3501. -0.153
4 315 2019 594. 3554. -0.519
5 315 2020 599. 3051. -1.63
6 315 2016 488. 2387. 0.318
$`320`
# A tibble: 6 x 5
NAICS Year `Total Inventories` `Net Sales` stud_res
<dbl> <dbl> <dbl> <dbl> <dbl>
1 320 2016 488. 3242. 2.25
2 320 2017 549. 3444. 0.313
3 320 2018 574. 3501. -0.153
4 320 2019 594. 3554. -0.519
5 320 2020 599. 3051. -1.63
6 320 2016 488. 2387. 0.318
最终 Output
> Out
$`315`
# A tibble: 6 x 5
NAICS Year `Total Inventories` `Net Sales` stud_res
<dbl> <dbl> <dbl> <dbl> <dbl>
1 315 2016 488. 3242. 2.25
2 315 2017 549. 3444. 0.313
3 315 2018 574. 3501. -0.153
4 315 2019 594. 3554. -0.519
5 315 2020 599. 3051. -1.63
6 315 2016 488. 2387. 0.318
$`320`
# A tibble: 6 x 5
NAICS Year `Total Inventories` `Net Sales` stud_res
<dbl> <dbl> <dbl> <dbl> <dbl>
1 320 2016 488. 3242. 2.25
2 320 2017 549. 3444. 0.313
3 320 2018 574. 3501. -0.153
4 320 2019 594. 3554. -0.519
5 320 2020 599. 3051. -1.63
6 320 2016 488. 2387. 0.318
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.