You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在fixest的etable中自定义特定变量的系数输出格式?

在fixest的etable中自定义单个变量的系数显示格式

问题背景

需要在fixest包的etable输出中,仅将Sepal.Width变量的系数乘以100展示,同时保留其他变量的原始系数和标准误,且不通过重新回归缩放变量的方式实现,还要添加调整说明。

示例代码及原输出:

library(fixest)
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

期望输出(Sepal.Width系数乘100,添加调整说明):

model_fit
Dependent Var.:     Sepal.Length
*注:Sepal.Width系数已乘以100*
                                
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

解决方案

可以通过etable的两个参数实现:coef.transform用于自定义系数转换,add.lines用于添加说明文本。

完整代码

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

etable(model_fit,
       # 自定义系数转换:仅Sepal.Width乘100,其他变量保持原样
       coef.transform = list(Sepal.Width = function(x) x * 100,
                             Petal.Length = identity,
                             Petal.Width = identity),
       # 添加调整说明
       add.lines = list("*注:Sepal.Width系数已乘以100*"),
       # 可选:调整系数的显示小数位数,匹配期望输出的49.59
       digits = 4)

代码说明

  • coef.transform:接受一个命名列表,键是变量名,值是对应的转换函数。这里对Sepal.Width用x*100,其他变量用identity函数(即返回原数值)。
  • add.lines:接受一个字符串列表,会将文本添加到因变量行下方,用于标注调整说明。
  • digits:可选参数,控制系数的小数位数,这里设为4可以让Sepal.Width的系数显示为49.59(原系数0.4959乘100后是49.59)。

运行上述代码后,即可得到符合需求的输出。

内容的提问来源于stack exchange,提问作者Matthew Son

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 00:25:31