Models and tables

Start with the expanded example for each command. Open the variants for options and alternative R approaches. Read the qualifications before adapting the code.

areg

Regression absorbing one categorical variable

Stata

areg y x, absorb(id)

R Package:fixest

fixest::feols(y ~ x | id, data = data)

Differs from Stata Match fixest covariance small-sample corrections and singleton handling with Stata; the same formula does not guarantee identical standard errors or retained observations.

avplots

Added-variable plots

Stata

avplots

R Package:car

car::avPlots(fit)

bootstrap

Bootstrap standard errors

Stata

bootstrap, reps(500): regress y x

R Package:sandwich

lmtest::coeftest(fit, vcov = sandwich::vcovBS(fit, R = 500))

Differs from Stata The R code estimates covariance for an existing fit; resampling units, centering and t/normal inference must match the Stata bootstrap/jackknife prefix options.

cluster

K-means clustering

Stata

cluster kmeans x1 x2, k(3)

R Package:stats

kmeans(na.omit(select(data, x1, x2)), centers = 3)

Differs from Stata R defaults to Hartigan-Wong k-means and random initial centers; Stata initialization and iteration defaults differ. Control the starting centers to compare clusters.

csdid

Staggered difference in differences

Stata

csdid y, ivar(id) time(year) gvar(first_treat)

R Package:did

did::att_gt(yname = "y", tname = "year", idname = "id", gname = "first_treat", data = data)

Differs from Stata Match control-group, anticipation and estimation-method settings; csdid and att_gt() defaults and aggregation/inference need not coincide.

didregress

Difference in differences (Stata 17+)

Stata

didregress (y) (treated), group(id) time(year)

R Package:fixest

fixest::feols(y ~ treated | id + year, data = data)

Differs from Stata The TWFE coefficient alone omits didregress default group-clustered standard errors and diagnostics; specify clustering and verify the DID design.

estat

Breusch-Pagan test for heteroskedasticity

Stata

estat hettest

R Package:lmtest

lmtest::bptest(fit, ~ fitted(fit), studentize = FALSE)

These options match Stata’s default test against the fitted values.

Information criteria

Stata

estat ic

R Package:stats

c(AIC = AIC(fit), BIC = BIC(fit))

Differs from Stata Compare the same fitted sample and likelihood constants; R classes and Stata can count scale parameters and likelihood terms differently.

RESET test for omitted variables

Stata

estat ovtest

R Package:lmtest

lmtest::resettest(fit, power = 2:4)

Variance-covariance matrix

Stata

estat vce

R Package:stats

vcov(fit)

estimates

Keep a model for later

Stata

estimates store m1

R Package:stats

m1 <- lm(y ~ x, data = data)

A fitted model is an ordinary object; keep as many as you like under different names.

Compare models side by side

Stata

estimates table m1 m2

R Package:modelsummary

modelsummary::modelsummary(list(m1, m2), output = "markdown")

eststo

Store a model for a table

Stata

eststo m1: regress y x

R Package:stats

m1 <- lm(y ~ x, data = data)

esttab

Regression table

Stata

esttab m1 m2

R Package:modelsummary

modelsummary::modelsummary(list(m1, m2))

etable

Table of the last model (Stata 17+)

Stata

etable

R Package:modelsummary

modelsummary::modelsummary(fit)

glm

Generalised linear model

Stata

glm y x, family(binomial)

R Package:stats

glm(y ~ x, family = binomial, data = data)

heckman

Heckman selection model

Stata

heckman y x, select(s = z)

R Package:sampleSelection

sampleSelection::selection(s ~ z, y ~ x, data = data)

ivreg2

Instrumental variables with robust errors

Stata

ivreg2 y x1 (x2 = z), robust

R Package:fixest

fixest::feols(y ~ x1 | x2 ~ z, data = data, vcov = "hetero")

Differs from Stata fixest::fitstat(fit, ~ ivf + ivwald) reports first-stage statistics. Match fixest covariance small-sample corrections and singleton handling with Stata; the same formula does not guarantee identical standard errors or retained observations.

ivregress

Instrumental variables (2SLS)

Stata

ivregress 2sls y x1 (x2 = z)

R Package:fixest

fixest::feols(y ~ x1 | x2 ~ z, data = data)

Differs from Stata Match fixest covariance small-sample corrections and singleton handling with Stata; the same formula does not guarantee identical standard errors or retained observations.

jackknife

Jackknife standard errors

Stata

jackknife: regress y x

R Package:sandwich

lmtest::coeftest(fit, vcov = sandwich::vcovJK(fit))

Differs from Stata The R code estimates covariance for an existing fit; resampling units, centering and t/normal inference must match the Stata bootstrap/jackknife prefix options.

lincom

Linear combination of coefficients

Stata

lincom x1 - x2

R Package:marginaleffects

marginaleffects::hypotheses(fit, "x1 - x2 = 0")

Differs from Stata hypotheses() uses the fitted R covariance and degrees-of-freedom defaults; Stata can use a different t, F or normal reference distribution.

logistic

Logit model reported as odds ratios

Stata

logistic y x

R Package:broom

broom::tidy(glm(y ~ x, family = binomial, data = data), exponentiate = TRUE, conf.int = TRUE)

Differs from Stata broom confidence intervals are model-specific; glm profile-likelihood intervals can differ from Stata default Wald intervals, even when odds ratios agree.

logit

Logit model

Stata

logit y x1 x2

R Package:stats

glm(y ~ x1 + x2, family = binomial, data = data)
1 more examples and alternatives

Odds ratios

Stata

logit y x, or

R Package:broom

broom::tidy(glm(y ~ x, family = binomial, data = data), exponentiate = TRUE)

Differs from Stata broom confidence intervals are model-specific; glm profile-likelihood intervals can differ from Stata default Wald intervals, even when odds ratios agree.

lrtest

Likelihood-ratio test

Stata

lrtest m1 m2

R Package:lmtest

lmtest::lrtest(m1, m2)

Differs from Stata Compare the same fitted sample and likelihood constants; R classes and Stata can count scale parameters and likelihood terms differently.

margins

Predictive margins by group

Stata

margins group

R Package:marginaleffects

marginaleffects::avg_predictions(fit, by = "group")

Differs from Stata by = “group” averages within observed groups; margins group sets each level for every observation before averaging. Use counterfactual predictions for that Stata estimand.

Average marginal effects

Stata

margins, dydx(*)

R Package:marginaleffects

marginaleffects::avg_slopes(fit)

Differs from Stata avg_slopes() averages model derivatives; discrete factor changes, weights and covariance must match margins and the fitted-model context.

marginsplot

Plot predictive margins

Stata

marginsplot

R Package:marginaleffects

marginaleffects::plot_predictions(fit, condition = "x")

melogit

Multilevel logit

Stata

melogit y x || id:

R Package:lme4

lme4::glmer(y ~ x + (1 | id), data = data, family = binomial)

Differs from Stata glmer() defaults to Laplace integration (nAGQ = 1); melogit defaults to adaptive quadrature, which can change estimates and likelihoods.

mi

Fit and combine models across imputations

Stata

mi estimate: regress y x

R Package:mice

mice::pool(with(imp, lm(y ~ x)))

Differs from Stata Rubin pooling still requires matching complete-data degrees of freedom and small-sample adjustments to mi estimate.

Multiple imputation

Stata

mi impute chained (regress) x1 x2 = y

R Package:mice

imp <- mice::mice(data, m = 20)

Differs from Stata mice() chooses methods by R column type, often predictive mean matching; it does not default to the specified chained linear regressions. Match methods, predictors and m explicitly.

mixed

Multilevel (mixed) linear model

Stata

mixed y x || id:

R Package:lme4

lme4::lmer(y ~ x + (1 | id), data = data)

Differs from Stata lme4 reports no p-values; lmerTest::lmer() adds them. lmer() defaults to REML; Stata mixed defaults to ML. Use REML = FALSE for ML and select a separate method for fixed-effect p-values.

mlogit

Multinomial logit

Stata

mlogit y x1 x2

R Package:nnet

nnet::multinom(y ~ x1 + x2, data = data)

Differs from Stata multinom() needs a factor y for multinomial classification; reference outcome categories and optimizer defaults can differ from Stata mlogit.

nbreg

Negative binomial model

Stata

nbreg y x

R Package:MASS

MASS::glm.nb(y ~ x, data = data)

Differs from Stata fixest::fenegbin() adds fixed effects. glm.nb() uses NB2 like default nbreg, but theta is the reciprocal of Stata alpha and likelihood/optimization reporting differs.

nlcom

Nonlinear combination of coefficients

Stata

nlcom _b[x1] / _b[x2]

R Package:marginaleffects

marginaleffects::hypotheses(fit, "x1 / x2 = 0")

Differs from Stata Standard errors use the delta method, as in Stata. hypotheses() uses the fitted R covariance and degrees-of-freedom defaults; Stata can use a different t, F or normal reference distribution.

oaxaca

Blinder-Oaxaca decomposition

Stata

oaxaca y x1 x2, by(group)

R Package:oaxaca

oaxaca::oaxaca(y ~ x1 + x2 | group, data = data)

Differs from Stata oaxaca() reports several reference-weight decompositions; explicitly select the reference coefficients and variance method used by Stata oaxaca.

ologit

Ordered logit

Stata

ologit y x1 x2

R Package:MASS

MASS::polr(factor(y) ~ x1 + x2, data = data, Hess = TRUE)

Differs from Stata Hess = TRUE lets summary() show standard errors. polr() uses ordered factor levels and a threshold parameterization; set the order explicitly and check the cutpoint sign convention against Stata.

oprobit

Ordered probit

Stata

oprobit y x1 x2

R Package:MASS

MASS::polr(factor(y) ~ x1 + x2, data = data, method = "probit", Hess = TRUE)

Differs from Stata polr() uses ordered factor levels and a threshold parameterization; set the order explicitly and check the cutpoint sign convention against Stata.

outreg2

Regression table in a Word file

Stata

outreg2 using table.doc

R Package:modelsummary

modelsummary::modelsummary(list(m1, m2), output = "table.docx")

poisson

Poisson model

Stata

poisson y x1 x2

R Package:stats

glm(y ~ x1 + x2, family = poisson, data = data)

ppmlhdfe

Poisson model with fixed effects

Stata

ppmlhdfe y x, absorb(id year)

R Package:fixest

fixest::fepois(y ~ x | id + year, data = data)

Differs from Stata Poisson fixed-effect coefficient targets agree, but separation/singleton removal and covariance corrections can differ; check the retained sample and uncertainty.

predict

Fitted values

Stata

predict yhat

R Package:stats

mutate(data, yhat = predict(fit, newdata = data))

Differs from Stata residuals(fit) gives residuals for the rows used in the model. predict() depends on the R model: response/link scale, residual definition and prediction sample must match the Stata postestimation context.

2 more examples and alternatives

Predicted probabilities

Stata

predict p, pr

R Package:stats

mutate(data, p = predict(fit, newdata = data, type = "response"))

Differs from Stata predict() depends on the R model: response/link scale, residual definition and prediction sample must match the Stata postestimation context.

Residuals for every row

Stata

predict r, residuals

R Package:stats

mutate(data, r = y - predict(fit, newdata = data))

Differs from Stata predict() depends on the R model: response/link scale, residual definition and prediction sample must match the Stata postestimation context.

probit

Probit model

Stata

probit y x1 x2

R Package:stats

glm(y ~ x1 + x2, family = binomial(link = "probit"), data = data)

putdocx

Write a table to Word

Stata

putdocx begin

R Package:flextable

flextable::save_as_docx(flextable::flextable(results), path = "results.docx")

officer builds whole Word documents.

putexcel

Write results to Excel

Stata

putexcel set results.xlsx

R Package:writexl

writexl::write_xlsx(results, "results.xlsx")

openxlsx2 gives cell-level control over formatting.

qreg

Median (quantile) regression

Stata

qreg y x

R Package:quantreg

quantreg::rq(y ~ x, tau = 0.5, data = data)

Differs from Stata rq() default standard-error methods depend on sample size and can differ from Stata qreg; choose the covariance/inference method explicitly.

rdrobust

Regression discontinuity

Stata

rdrobust y x, c(0)

R Package:rdrobust

rdrobust::rdrobust(data$y, data$x, c = 0)

Differs from Stata The R and Stata implementations share the estimator; version, bandwidth, mass-point and variance settings must also match for finite-sample output.

reghdfe

High-dimensional fixed effects

Stata

reghdfe y x, absorb(id year)

R Package:fixest

fixest::feols(y ~ x | id + year, data = data)

Differs from Stata Match fixest covariance small-sample corrections and singleton handling with Stata; the same formula does not guarantee identical standard errors or retained observations.

regress

Linear regression

Stata

regress y x1 x2

R Package:stats

fit <- lm(y ~ x1 + x2, data = data)

summary(fit) shows the coefficient table; broom::tidy(fit) returns it as a data frame.

5 more examples and alternatives

Factor variables and interactions

Stata

regress y i.group c.x1##c.x2

R Package:stats

lm(y ~ factor(group) + x1 * x2, data = data)

Differs from Stata factor() plays the role of i.; x1 * x2 is c.x1##c.x2 and x1:x2 is c.x1#c.x2. factor() chooses its reference from R level order; Stata i.group uses its numeric base level. Set factor levels and contrasts explicitly.

Choose the base category

Stata

regress y ib3.group

R Package:stats

lm(y ~ relevel(factor(group), ref = "3"), data = data)

Regression with sampling weights

Stata

regress y x [pweight = wt]

R Package:fixest

fixest::feols(y ~ x, data = data, weights = ~wt, vcov = "hetero")

Differs from Stata For full survey designs with strata and clusters, see svyset. Match fixest covariance small-sample corrections and singleton handling with Stata; the same formula does not guarantee identical standard errors or retained observations.

Heteroskedasticity-robust standard errors

Stata

regress y x1 x2, robust

R Package:fixest

fixest::feols(y ~ x1 + x2, data = data, vcov = "hetero")

Differs from Stata Match fixest covariance small-sample corrections and singleton handling with Stata; the same formula does not guarantee identical standard errors or retained observations.

Clustered standard errors

Stata

regress y x1 x2, vce(cluster id)

R Package:fixest

fixest::feols(y ~ x1 + x2, data = data, cluster = ~id)

Differs from Stata Match fixest covariance small-sample corrections and singleton handling with Stata; the same formula does not guarantee identical standard errors or retained observations.

rreg

Robust regression

Stata

rreg y x

R Package:MASS

MASS::rlm(y ~ x, data = data)

Differs from Stata rlm() uses Huber M-estimation; Stata’s rreg combines Huber and biweight steps, so estimates can differ slightly. rlm() defaults to Huber M-estimation; rreg screens outliers and combines Huber and biweight iterations, so estimates and inference can differ.

rvfplot

Residuals against fitted values

Stata

rvfplot

R Package:stats

plot(fit, which = 1)

sem

Structural equation model

Stata

sem (y <- x1 x2)

R Package:lavaan

lavaan::sem("y ~ x1 + x2", data = data)

Differs from Stata lavaan treats observed exogenous predictors as fixed by default; match fixed.x and missing-data likelihood settings with Stata sem.

synth

Synthetic control

Stata

synth y x1 x2, trunit(1) trperiod(2000)

R Package:Synth

Synth::synth(data.prep.obj = prep)

Synth::dataprep() builds prep from the panel first; tidysynth offers a tidier interface.

teffects

Inverse-probability weighting

Stata

teffects ipw (y) (t x1 x2)

R Package:WeightIt

WeightIt::weightit(t ~ x1 + x2, data = data, method = "glm")

Propensity-score matching

Stata

teffects psmatch (y) (t x1 x2)

R Package:MatchIt

MatchIt::matchit(t ~ x1 + x2, data = data, method = "nearest")

Estimate the effect on the matched data, for example lm(y ~ t, data = MatchIt::match.data(m), weights = weights).

test

Test a hypothesis about coefficients

Stata

test x1 = x2

R Package:marginaleffects

marginaleffects::hypotheses(fit, "x1 = x2")

Differs from Stata hypotheses() uses the fitted R covariance and degrees-of-freedom defaults; Stata can use a different t, F or normal reference distribution.

tobit

Censored (tobit) regression

Stata

tobit y x, ll(0)

R Package:AER

AER::tobit(y ~ x, left = 0, data = data)

vif

Variance inflation factors

Stata

vif

R Package:car

car::vif(fit)

Differs from Stata car::vif() gives generalized VIFs for multi-degree-of-freedom terms; Stata estat vif reports regressor-column VIFs, so factor-term tables differ.

xtabond

Dynamic panel (Arellano-Bond)

Stata

xtabond y x

R Package:plm

plm::pgmm(y ~ lag(y, 1) + x | lag(y, 2:99), data = data, index = c("id", "year"))

Differs from Stata pgmm() needs matching transformation, lag instruments, collapse and one/two-step choices; the formula alone does not reproduce xtabond default instruments or standard errors.

xtlogit

Conditional (fixed-effects) logit

Stata

xtlogit y x, fe

R Package:survival

survival::clogit(y ~ x + strata(id), data = data)

Differs from Stata fixest::feglm(y ~ x | id, family = binomial) is faster but estimates unconditional fixed effects. clogit() targets conditional fixed-effects logit; exact likelihood, weights and covariance settings must match xtlogit, fe rather than unconditional dummy-variable logit.

xtpoisson

Fixed-effects Poisson

Stata

xtpoisson y x, fe

R Package:fixest

fixest::fepois(y ~ x | id, data = data)

Differs from Stata Poisson fixed-effect coefficient targets agree, but separation/singleton removal and covariance corrections can differ; check the retained sample and uncertainty.

xtreg

Panel fixed effects

Stata

xtreg y x, fe

R Package:fixest

fixest::feols(y ~ x | id, data = data)

Differs from Stata Match fixest covariance small-sample corrections and singleton handling with Stata; the same formula does not guarantee identical standard errors or retained observations.

1 more examples and alternatives

Panel random effects

Stata

xtreg y x, re

R Package:plm

plm::plm(y ~ x, data = data, index = c("id", "year"), model = "random")

Differs from Stata plm and xtreg, re default to different variance-component estimators; match random.method and the panel sample before comparing results.