Although concurve was originally designed to be used in
R, it is possible to achieve very similar results in
Stata. We can use some datasets that are built into
Stata to show how to achieve this. I’ll use the
Statamarkdown R package so that I can obtain Stata outputs
using RMarkdown via my Stata 16 package.
First, let’s load the auto2 dataset which contains data about cars and their characteristics.
Browse the data set in your data browser to get more familiar with some of the variables. Let’s say we’re interested in the relationship between miles per gallon and price. We could fit a very simple linear model to assess that relationship.
First, let’s visualize the data with a scatter plot.
That’s what our data looks like. Clearly there seems to be an inverse relationship between miles per gallon and price.
Now we could fit a very simple linear model with miles per gallon being the predictor and price being the outcome and get some estimates of the relationship.
That’s what our output looks like.
Our output also gives us 95% consonance (confidence) intervals by default. But suppose we wished to fit a fractional polynomial model and graph it and get the confidence bands, here’s what we would do.
sysuse auto2
mfp: glm price mpg
twoway (fpfitci price mpg, estcmd(glm) fcolor(dkorange%20) alcolor(%40)) || scatter price mpg, mcolor(dkorange) scale(0.75)
graph export "mfp.svg", replace That’s what our model looks graphed.
Now suppose we got a single estimate (point or interval) for a parameter, and we wanted all the intervals for it at every level.
Here’s the code that we’ll be using to achieve that in Stata.
postfile topost level pvalue svalue lointerval upinterval using my_new_data, replace
forvalues i = 10/99.9 {
quietly regress price mpg, level(`i')
matrix E = r(table)
matrix list E
post topost (`i') (1-`i'/100) ( ln(1-`i'/100)/ln(2) * -1) (E[5,1]) (E[6,1])
}
postclose topost
use my_new_data, clear
list
twoway (pcscatter level lointerval level upinterval),
ytitle(Consonance Level (%)) xtitle(Consonance Limits) ///
title(Consonance Curve)
subtitle(A function comprised of several consonance intervals at various levels.)
That’s a lot and may seem intimidating at first, but I’ll explain it line by line.
postfile topost level pvalue svalue lointerval upinterval using my_new_data, replace
“postfile” is the command that will be responsible
for pasting the data from our overall loop into a new dataset. Here, we
are telling Stata that the internal Stata
memory used to hold these results (the post) will be named
“topost” and that it will have five variables,
“level”, “pvalue”,
“svalue”, “lointerval”, and
“upinterval.”
“level” will contain the consonance level that corresponds to the limits of the interval, with “lointerval” being the lower bound of the interval and “upinterval” being the upper bound.
“pvalue”is computed by taking 1 - “level”, which is alpha.
“svalue”is computed by taking the of the computed P-value, and this column will be used to plot the surprisal function.
“my_new_data” is the filename that we’ve assigned to our new dataset.
“replace” indicates that if there is an existing filename that already exists, we’re willing to relace it.
Here are the next few major lines
forvalues i = 10/99.9 {
quietly regress price mpg, level(`i')
matrix E = r(table)
matrix list E
post topost (`i') (1-`i'/100) ( ln(1-`i'/100)/ln(2) * -1) (E[5,1]) (E[6,1])
}
The command “forvalues” is responsible for taking a
set of numbers that we provide it, and running the contents within the
braces through those numbers. So here, we’ve set the local macro
“i” to contain numbers between 10 and 99.99 for our
consonance levels. Why 10? Stata cannot compute consonance
intervals lower than 10%.
Our next line contains the actual contents of what we want to do. Here, it says that we will run a simple linear regression where mpg is the predictor and where price is the outcome, and that the outputs for each loop will be suppressed, hence the “quiet.”
Then, we have the command “level” with the local
macro “i” inside of it. As you may already know,
“level” dictates the consonance level that
Stata provides us. By default, this is set to 95%, but
here, we’ve set it “i”, which we established via
“forvalues” as being set to numbers between 10 and
99.
The next line two lines
matrix E = r(table)
matrix list E
indicate that we will take variables of a certain class r(), (this class contains the interval bounds we need) and place them within a matrix called E. Then we will list the contents of this matrix.
post topost (`i') (1-`i'/100) ( ln(1-`i'/100)/ln(2) * -1) (E[5,1]) (E[6,1])
From the contents of this matrix list, we will take the estimates from the fifth and sixth rows (look at the last two paranthesis of this line of code above and then the image below) in the first column which contain our consonance limits, with the fifth row containing the lower bound of the interval and the sixth containing the upper bound.
We will place the contents from the fifth row into the second variable we set originally for our new dataset, which was “lointerval.” The contents of the sixth row will be placed into “upinterval.”
All potential values of “i” (10-99) will be placed into the first variable that we set, “level”. From this first variable, we can compute the second variable we set up, which was “Pvalue” and we’ve done that here by subtracting “level” from 1 and then dividing the whole equation by 100, so that our P-value can be on the proper scale. Our third variable, which is the longest, computes the “Svalue” by using the previous variable, the “Pvalue” and taking the of it.
The relationships between the variables on this line and the variables we set up in the very first line are dictated by the order of the commands we have set, and therefore they correspond to the same order.
“post topost” is writing the results from each loop as new observations in this data structure.
With that, our loop has concluded, and we can now tell
Stata that “post” is no longer needed
postclose topost
We then tell Stata to clear its memory to make room for
the new dataset we just created and we can list the contents of this new
dataset.
use my_new_data, clear
list
Now we have an actual dataset with all the consonance intervals at all the levels we wanted, ranging from 10% all the way up to 99%.
In order to get a function, we’ll need to be able to graph these results, and that can be tricky since for each observation we have one y value (the consonance level), and two x values, the lower bound of the interval and the upper bound of the interval.
So a typical scatterplot will not work, since Stata will
only accept one x value. To bypass this, we’ll have to use a
paired-coordinate scatterplot which will allow us to plot two different
y variables and two different x variables.
Of course, we don’t need two y variables, so we can set both options to the variable “level”, and then we can set our first x variable to “lointerval” and the second x variable to “upinterval.”
This can all be done with the following commands, which will also allow us to set the title and subtitle of the graph, along with the titles of the axes.
twoway (pcscatter level lointerval level upinterval),
ytitle(Consonance Level (%)) xtitle(Consonance Limits) ///
title(Consonance Curve)
subtitle(A function comprised of several consonance intervals at various levels.)
However, I would recommend using the menu to customize the plots as much as possible. Simply go to the Graphics menu and select Twoway Graphs. Then create a new plot definition, and select the Advanced plots and choose a paired coordinate scatterplot and fill in the y variables, both of which will be “levels” and the x variables, which will be “lointerval” and “upinterval”.
So now, here’s what our confidence/consonance function looks like.
clear
sysuse auto2
postfile topost level pvalue svalue lointerval upinterval using my_new_data, replace
forvalues i = 10/99.9 {
quietly regress price weight, level(`i')
matrix E = r(table)
matrix list E
post topost (`i') (1-`i'/100) ( ln(1-`i'/100)/ln(2) * -1) (E[5,1]) (E[6,1])
}
postclose topost
use my_new_data, clear
twoway (pcscatter pvalue lointerval pvalue upinterval, mcolor(maroon)), ytitle(Consonance Level (%)) xtitle(Consonance Limits) scale(0.75) ///
title(Consonance Curve) subtitle(A function comprised of several consonance intervals at various levels.)
graph export "confidence.svg", replacePretty neat, eh? And below is what our surprisal function looks like, which is simply the (p) transformation of the observed P-value. For a more comprehensive discussion on surprisals, see this page and check out some of the references at the bottom.
clear
sysuse auto2
postfile topost level pvalue svalue lointerval upinterval using my_new_data, replace
forvalues i = 10/99.9 {
quietly regress price weight, level(`i')
matrix E = r(table)
matrix list E
post topost (`i') (1-`i'/100) ( ln(1-`i'/100)/ln(2) * -1) (E[5,1]) (E[6,1])
}
postclose topost
use my_new_data, clear
twoway (pcscatter svalue lointerval svalue upinterval, mcolor(maroon)), ytitle(Consonance Level (%)) xtitle(Consonance Limits) scale( 0.75) ///
title(Surprisal Curve) subtitle(A function comprised of several consonance intervals at various levels.)
graph export "surprisal.svg", replaceIt’s clear that in both plots, we’re missing values of intervals with
a confidence/consonance level of less than 10%, but unfortunately, this
is the best Stata can do, and what we’ll have to work with.
It may not look as pretty as an output from R, but it’s far
more useful than blankly staring at a 95% interval and thinking that it
is the only piece of information we have regarding compatibility of
different effect estimates.
The code that I have pasted above can be used for most commands in
Stata that have an option to calculate a consonance level.
Thus, if there’s an option for “level”, then the
commands above will work to produce a data set of several consonance
intervals. Though I am seriously hoping that a Stata expert will see
this post and point out how I am wrong.
Now, suppose we wished to fit a generalized linear model, here’s what our code would look like.
clear
sysuse auto2
postfile topost level pvalue svalue lointerval upinterval using my_new_data, replace
forvalues i = 10/99.9 {
quietly glm price mpg, level(`i')
matrix E = r(table)
matrix list E
post topost (`i') (1-`i'/100) ( ln(1-`i'/100)/ln(2) * -1) (E[5,1]) (E[6,1])
}
postclose topost
use my_new_data, clear
list
twoway (pcscatter level lointerval level upinterval),
ytitle(Confidence Level (%)) xtitle(Confidence Limits) ///
title(Consonance Curve)
subtitle(A function comprised of several consonance intervals at various levels.)We simply replace the first line within the loop with our intended command, just as I’ve replaced
with
If we wanted fit something more complex, like a multilevel mixed model that used restricted maximum likelihood, here’s what our code would look like:
clear
sysuse auto2
postfile topost level pvalue svalue lointerval upinterval using my_new_data, replace
forvalues i = 10/99.9 {
quietly mixed outcome predictor, reml level(`i')
matrix E = r(table)
matrix list E
post topost (`i') (1-`i'/100) ( ln(1-`i'/100)/ln(2) * -1) (E[5,1]) (E[6,1])
}
postclose topost
use my_new_data, clear
list
twoway (pcscatter level lointerval level upinterval),
ytitle(Confidence Level (%)) xtitle(Confidence Limits) ///
title(Consonance Curve)
subtitle(A function comprised of several consonance intervals at various levels.)Basically, our code doesn’t really change that much and with only a few lines of it, we are able to produce graphical tools that can better help us interpret the wide range of effect sizes that are compatible with the model and its assumptions.
It is also important to cite the statistical packages that we have used here, as always.
citation("Statamarkdown")## To cite package 'Statamarkdown' in publications use:
##
## Hemken D, Palmer T (2026). _Statamarkdown: 'Stata' Markdown_.
## doi:10.32614/CRAN.package.Statamarkdown
## <https://doi.org/10.32614/CRAN.package.Statamarkdown>. R package
## version 0.9.7, <https://CRAN.R-project.org/package=Statamarkdown>.
##
## A BibTeX entry for LaTeX users is
##
## @Manual{,
## title = {Statamarkdown: 'Stata' Markdown},
## author = {Doug Hemken and Tom Palmer},
## year = {2026},
## note = {R package version 0.9.7},
## url = {https://CRAN.R-project.org/package=Statamarkdown},
## doi = {10.32614/CRAN.package.Statamarkdown},
## }
## R version 4.6.1 (2026-06-24)
## Platform: aarch64-apple-darwin25.4.0
## Running under: macOS Golden Gate 27.0
##
## Matrix products: default
## BLAS: /opt/homebrew/Cellar/openblas/0.3.34/lib/libopenblasp-r0.3.34.dylib
## LAPACK: /opt/homebrew/Cellar/r/4.6.1/lib/R/lib/libRlapack.dylib; LAPACK version 3.12.1
##
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## time zone: America/New_York
## tzcode source: internal
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## loaded via a namespace (and not attached):
## [1] vctrs_0.7.3 cli_3.6.6 knitr_1.51 rlang_1.3.0
## [5] xfun_0.60 otel_0.2.0 purrr_1.2.2 textshaping_1.0.5
## [9] jsonlite_2.0.0 glue_1.8.1 htmltools_0.5.9 ragg_1.5.2
## [13] sass_0.4.10 rmarkdown_2.31 jquerylib_0.1.4 evaluate_1.0.5
## [17] tibble_3.3.1 fastmap_1.2.0 yaml_2.3.12 lifecycle_1.0.5
## [21] memoise_2.0.1 whisker_0.4.1 compiler_4.6.1 fs_2.1.0
## [25] htmlwidgets_1.6.4 downlit_0.4.5 pkgconfig_2.0.3 rstudioapi_0.19.0
## [29] systemfonts_1.3.2 digest_0.6.39 R6_2.6.1 pillar_1.11.1
## [33] magrittr_2.0.5 bslib_0.12.0 tools_4.6.1 withr_3.0.3
## [37] pkgdown_2.2.1 xml2_1.6.0 cachem_1.1.0 desc_1.4.3