Written and maintained by CASRAI Editorial Board
Published
On this page
To fit log regression in Stata, create the natural logarithm of the outcome, a predictor, or both, then use regress with those variables. The important next step is checking which scale the coefficients and predictions describe: a fitted log outcome is not automatically an arithmetic-mean prediction in the original units.
What Stata is used for in research
Stata’s regress command estimates ordinary least-squares linear models. Logging a variable changes the model specification rather than selecting a separate estimation command. The official regress reference documents the dependent-variable-first syntax used throughout this guide.
Write the intended relationship before building variables. A log-linear model has ln(y) on the left and x on the right. A linear-log model keeps y in its original units and uses ln(x) as a predictor. A log-log model transforms both. Additional predictors may remain untransformed, so each coefficient needs an interpretation tied to its own variable and the outcome’s scale.
For a researcher, the useful question is what comparison the model should answer. Does an additional unit of x correspond to a multiplicative difference in y, or should a proportional difference in x correspond to an additive difference in y? Those are different questions even when the same dataset supplies both variables. The data table below connects each specification to its Stata command and the scale of its default prediction.
A binary-outcome analysis belongs to the separate Stata logistic-regression workflow. For ordinary model syntax and reporting beyond transformations, use the general regression-in-Stata guide.
How to install or access Stata
Use Stata’s installation guide for downloads, installation and license setup. Before installing, have the License and Activation Key available and identify the edition it specifies: MP, SE or BE. If an .stcodes file was supplied, save it for import during installation. The official guide provides separate Windows and Mac instructions and a Unix/Linux installation PDF.
For this analysis, keep the installation task separate from the model specification. Write down the edition and release used alongside the analysis script, then keep the same transformation definitions when sharing the project. A collaborator should be able to identify exactly which columns were logged without reconstructing decisions from a coefficient table. The Stata do-file workflow guide provides a place to organize those commands.
How to fit log regression in Stata step by step
The following commands assume numeric variables named y and x are already in memory. Substitute the actual variable names from your dataset. Keep the original columns so that the transformed model can be checked against the original measurements.
- Inspect the variables. Run
summarize y xand examine their ranges. The Stata mathematical-functions reference definesln()for positive values. A variable containing zero or negative values cannot be logged unchanged across all its observations. - Create the required transformation. For a logged outcome, run
generate lny = ln(y). For a logged predictor, rungenerate lnx = ln(x). Stata’slog()is another name for the natural logarithm;log10()explicitly requests base ten. - Fit the specification you intended. Use
regress lny xfor log-linear,regress y lnxfor linear-log, orregress lny lnxfor log-log. Choose one for the analysis at hand rather than treating the three commands as interchangeable checks on the same parameter. - Read the sample size and dependent-variable label. Record the reported number of observations and confirm that the coefficient table names the intended outcome. If the result says
lny, its coefficients describe that transformed outcome. Resolve unexpected exclusions before interpreting an apparently strong result. - Obtain predictions on the fitted scale. Immediately after the chosen regression, run
predict log_fit if e(sample), xbwhen the outcome is logged. The postestimation reference definesxbas the linear prediction; the sample condition explicitly restricts the new variable to estimation observations. - Retain residuals for examination. Run
predict log_residual if e(sample), residualsafter the same logged-outcome fit. Keep names that identify the scale. For a linear-log model, use names such asy_fitandy_residualinstead, because its outcome remains untransformed.
Before moving to interpretation, make a short audit record: the outcome column, the predictor columns, which were logged, the log base and the estimation sample size. This is an editorial check on the analysis, not an extra statistical test. It makes a later contradiction visible—for example, a table headed “response units” beside a model whose dependent variable was ln(y).
How to read the output
Start with the dependent-variable scale. A coefficient from regress lny x is a change in expected ln(y), conditional on the included predictors. UCLA’s official interpretation guide explains the corresponding geometric-mean ratios and the distinctions between logging the outcome, predictors or both.
A calculated example with six observations
This is an illustrative composite with synthetic numbers, not observations from a study. Define x as 0, 1, 2, 3, 4 and 5. Define a synthetic residual u as 0.1, −0.1, 0, 0, −0.1 and 0.1, respectively. Construct each response using y = exp(ln(10) + 0.2x + u). The values below are rounded for display; the calculation uses the defining expression.
| x | Synthetic y | ln(y) |
|---|---|---|
| 0 | 11.051709 | 2.402585 |
| 1 | 11.051709 | 2.402585 |
| 2 | 14.918247 | 2.702585 |
| 3 | 18.221188 | 2.902585 |
| 4 | 20.137527 | 3.002585 |
| 5 | 30.041660 | 3.402585 |
With those variables loaded, generate lny = ln(y) followed by regress lny x gives the fitted equation ln(ŷ) = 2.302585 + 0.200000x. The residuals sum to zero and their products with x also sum to zero, so the constructed intercept and slope are recovered by ordinary least squares. The residual sum of squares is 0.04; with six observations and two estimated coefficients, the residual mean square is 0.04/4 = 0.01 and Root MSE is 0.1.
The total sum of squares on the log scale is 0.74. Consequently R-squared is 1 − 0.04/0.74 = 0.945946. That calculation uses ln(y), not y. It would be misleading to label this number as the proportion of original-response variation explained without doing a separate original-scale calculation.
For a one-unit increase in x, the fitted geometric-mean ratio is exp(0.2) = 1.221403. Subtracting one and multiplying by 100 gives 22.1403%. A two-unit increase uses exp(2 × 0.2), giving 49.1825%, rather than doubling the one-unit percentage. For this synthetic exercise, report the comparison explicitly: “A one-unit increase in x corresponds to a 22.14% higher fitted geometric mean of y.” This describes the constructed relationship; it is not evidence of a causal effect.
The same coefficient number would mean something else in another specification. If a log-log model had slope 0.2, a 10% increase in x would imply a fitted geometric-mean change of 100 × (1.10.2 − 1) = 1.92449%. If a linear-log model had slope 0.2, that same predictor change would imply 0.2 × ln(1.1) = 0.019062 response units. These are alternative hypothetical models, not three results from the six-row fit.
Prediction in the original units
At x = 3, the example’s fitted log value is 2.902585 and its exponential is 18.221188. Stata’s log-transformed-outcomes tutorial explains why exponentiating a fitted log value alone does not estimate the conditional arithmetic mean.
Under normally distributed errors independent of the covariates, the tutorial uses exp(xb) × exp(σ²/2). In the synthetic arithmetic, substituting the estimated residual variance 0.01 gives 18.221188 × exp(0.01/2) = 18.312522. This illustrates the correction’s size; six invented residuals do not establish its distributional assumptions.
For a log-outcome regression, a point-prediction command following Stata’s documented expression is generate mean_fit = exp(log_fit)*exp((e(rmse)^2)/2). Do not carry that expression into uncertainty calculations blindly: the tutorial warns that a naive margins calculation treating the estimated RMSE as known gives incorrect standard errors. Its worked alternative uses gsem to estimate the error variance jointly before calculating margins.
Troubleshooting Stata
Check the transformation, sample and prediction scale before changing the estimator. These checks help distinguish an unexpected calculation from a model answering a different question.
Zero, negative or missing values
Inspect every variable you intend to log against the positive domain of ln(). An added constant changes the transformation: ln(y + 1) is a different column from ln(y). Document such a choice rather than silently describing both as “log y.” The wider guide to data transformations discusses the implications of these choices.
For example, a hypothetical response of zero is outside the ordinary logarithm’s domain, while a response of one has natural log zero. Confusing those cases reverses the check: a zero in the transformed column can be perfectly legitimate. Inspect the original measurement beside its transformation instead of deleting every transformed zero.
Log base also matters when reversing predictions. Expressing the synthetic outcome in base ten would rescale its slope to 0.2/ln(10) = 0.086859. Reversing that fitted value requires 10 raised to the prediction, rather than exp(prediction). Record the base instead of assuming that every column called “log” was constructed identically.
Residual variation and prediction scope
Stata documents estat hettest after ordinary regress for testing heteroskedasticity. Logging is not a substitute for examining residual behavior. The regression manual also documents vce(robust); using it addresses standard-error estimation rather than supplying the independent normal errors required by the arithmetic-mean correction above.
The postestimation manual makes predictions available outside the estimation sample for unstarred statistics such as xb. Keep if e(sample) when the purpose is checking the fitted observations. Also verify that the active model is the one whose predictions you want: a second model in the script should not quietly supply predictions for the first model’s report.
A report that preserves the scale
As an editorial reporting checklist, retain the exact model equation, transformation definitions, sample size, coefficient and uncertainty interval, and the comparison expressed in reader-facing units. Name the prediction target separately. In the synthetic example, “predicted response” would conceal the distinction between 18.221188 and the assumption-dependent corrected value 18.312522; “exponentiated fitted log response” and “corrected arithmetic-mean prediction” make that choice visible.
Free alternatives to Stata
R and Python’s statsmodels provide alternative linear-model workflows. Reproduce the transformation and estimation sample before comparing their results with Stata. Similar-looking coefficient tables do not establish that the same columns, intercept and observations were used.
| Tool | Equivalent starting point | Check when transferring the analysis |
|---|---|---|
| R | lm(lny ~ x, data = d) for a previously created log-outcome column |
The official lm documentation specifies an implied intercept and configurable missing-data handling. |
| Python with statsmodels | sm.OLS(lny, X).fit() with a prepared predictor matrix |
The array interface does not add an intercept; construct it explicitly, for example with sm.add_constant(X). Set missing-data handling deliberately. |
The R lm reference documents summaries and accessors for coefficients, fitted values and residuals. The statsmodels OLS reference documents its response/predictor inputs and missing-value options. In either environment, a model fitted to lny still requires the same distinction between a log-scale prediction and an arithmetic-mean prediction of y.
Sources
- Stata: regress — Linear regression, checked 2026-11-17.
- Stata: Mathematical functions, checked 2026-11-17.
- Stata: Postestimation tools for regress, checked 2026-11-17.
- Stata: Interpreting models for log-transformed outcomes, checked 2026-11-17.
- UCLA OARC: Interpreting regression with log-transformed variables, checked 2026-11-17.
- Stata: Installation guide, checked 2026-11-17.
- R: Fitting Linear Models, checked 2026-11-17.
- statsmodels: OLS reference, checked 2026-11-17.
Data: Stata log-model commands and prediction scales
Original crosswalk compiled from Stata mathematical-function, regression and postestimation documentation and UCLA interpretation guidance. Commands assume separately generated lny=ln(y) and lnx=ln(x); contrasts hold other predictors fixed and have no interactions.
| Specification | Stata fitting command | Default xb prediction scale | Original-scale contrast | Interpretation source |
|---|---|---|---|---|
| Log-linear | regress lny x | ln(y) | exp(b*Δx): fitted geometric-mean ratio for change Δx in x | UCLA OARC; Stata regress, predict and mathematical functions |
| Linear-log | regress y lnx | y | b*ln(r): response-unit difference when positive x is multiplied by r | UCLA OARC; Stata regress, predict and mathematical functions |
| Log-log | regress lny lnx | ln(y) | r^b: fitted geometric-mean ratio when positive x is multiplied by r | UCLA OARC; Stata regress, predict and mathematical functions |
Sources
- Mathematical functions (accessed 2026-11-17)
- regress — Linear regression (accessed 2026-11-17)
- regress postestimation — Postestimation tools for regress (accessed 2026-11-17)
- FAQ How do I interpret a regression model when some variables are log transformed? – OARC Stats (accessed 2026-11-17)
Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.







