r-固定数据表中特定变量的自定义系数格式



我想知道是否有一种方法可以以自定义的方式格式化etable中报告的系数。

例如,在下面的回归中

library(fixest)
feols(Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width | Species, iris)
model_fit = feols(Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width | Species, iris)
etable(model_fit)
model_fit
Dependent Var.:     Sepal.Length

Sepal.Width     0.4959. (0.1206)
Petal.Length    0.8292* (0.0970)
Petal.Width     -0.3152 (0.1096)
Fixed-Effects:  ----------------
Species                      Yes
_______________ ________________
S.E.: Clustered      by: Species
Observations                 150
R2                       0.86731
Within R2                0.65201
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

我只想乘以100乘以Sepal.Width的系数进行报告,并为调整写一个单独的注释,这样etable的报告看起来像

model_fit
Dependent Var.:     Sepal.Length

Sepal.Width     49.59 (0.1206)
Petal.Length    0.8292* (0.0970)
Petal.Width     -0.3152 (0.1096)
Fixed-Effects:  ----------------
Species                      Yes
_______________ ________________
S.E.: Clustered      by: Species
Observations                 150
R2                       0.86731
Within R2                0.65201
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

fixestetable中有没有一种方便的方法可以实现这一点?

缩放Sepal.Width列并重新运行回归对我来说是最不可取的选择。

谢谢。

您可以这样做:

  1. 创建一个助手函数,该函数将etable(model_fit)$model_fit返回的对象中的一个字符串条目作为x,并使用正则表达式提取系数,乘以m=100,然后将其粘贴回原始字符串x
f <- function(x,m=100) {
coef = regmatches(x,regexec("^-?\d+[.]?\d+",x,perl=T))[[1]]
paste0(as.numeric(coef)*m, gsub(coef,"",x))
}
  1. etable(model_fit)分配给对象k
k = etable(model_fit)
  1. 现在,使用f()更改k$model_fit中的一个值
k$model_fit[3] <- f(k$model_fit[3])
print(k)
model_fit
Dependent Var.:     Sepal.Length

Sepal.Width      49.59. (0.1206)
Petal.Length    0.8292* (0.0970)
Petal.Width     -0.3152 (0.1096)
Fixed-Effects:  ----------------
Species                      Yes
_______________ ________________
S.E.: Clustered      by: Species
Observations                 150
R2                       0.86731
Within R2                0.65201
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

最新更新