You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Stata中coefplot的Y轴标签设置、变量重命名及headings选项报错问题咨询

Stata coefplot: Y-axis Labeling, Variable Renaming, and Fixing headings Error

Hey there! Let's work through your coefplot questions step by step:

1. Fixing the command headings is unrecognized Error

The headings option is a relatively new feature in coefplot (added in version 1.1.0 or later). If you're seeing this error, your installed version of coefplot is outdated. Here are two ways to fix it:

Option 1: Update coefplot

Run this command in Stata to install the latest version from the Boston College archive:

ssc install coefplot, replace

Once updated, your original code with the headings option should run without issues.

Option 2: Workaround for Older coefplot Versions

If you can't update, you can mimic the section header effect using coeflabels() and hiding marker symbols for header lines. Here's how to adjust your example:

sysuse auto, clear
keep if rep78>=3
regress mpg headroom i.rep##i.foreign
coefplot, xline(0) omitted baselevels drop(_cons) ///
coeflabels(3.rep78 = "{bf:Repair Record}" 3.rep78 = "Repair Score 3" ///
           0.foreign = "{bf:Car Type}" 0.foreign = "Domestic" ///
           3.rep78#0.foreign = "{bf:Interaction Effects}" 3.rep78#0.foreign = "Repair 3 × Domestic") ///
marker(1, ms(none) mlabcolor(black)) marker(3, ms(none) mlabcolor(black)) marker(5, ms(none) mlabcolor(black))

This uses custom coefficient labels for section headers and hides their markers to make them stand out like the headings option would.


2. Setting the Y-axis Title (Overall Axis Label)

To set the main label for the entire Y-axis, use the ytitle() option in your coefplot command. Here's how to add it to your initial regression code:

sysuse auto, clear
regress price mpg trunk length turn if foreign==0
iestimates store Option1
regress price mpg trunk length turn if foreign==1
iestimates store Option2
coefplot Option1 Option2, drop(_cons) xline(1) ///
ytitle("Regression Variables")  // This defines the Y-axis title

3. Renaming Individual Y-axis Variables (Coefficient Labels)

To rename specific variables on the Y-axis (like changing "Mileage (mpg)" to "Mileage"), you have two easy options:

Option 1: Use coeflabels() Directly in coefplot

Map each coefficient name to your custom label within the command:

sysuse auto, clear
regress price mpg trunk length turn if foreign==0
iestimates store Option1
regress price mpg trunk length turn if foreign==1
iestimates store Option2
coefplot Option1 Option2, drop(_cons) xline(1) ///
ytitle("Regression Variables") ///
coeflabels(mpg = "Mileage" ///
           trunk = "Trunk Space" ///
           length = "Car Length" ///
           turn = "Turn Radius")

Option 2: Set Variable Labels Before Regression

Define variable labels in Stata first, and coefplot will automatically use them:

sysuse auto, clear
label variable mpg "Mileage"
label variable trunk "Trunk Space"
label variable length "Car Length"
label variable turn "Turn Radius"

regress price mpg trunk length turn if foreign==0
iestimates store Option1
regress price mpg trunk length turn if foreign==1
iestimates store Option2

coefplot Option1 Option2, drop(_cons) xline(1) ytitle("Regression Variables")

This avoids repeating labels in the coefplot command and keeps your code cleaner.


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

火山引擎 最新活动