Skip to content

Instantly share code, notes, and snippets.

@kylebutts
Created November 20, 2021 00:23
Show Gist options
  • Select an option

  • Save kylebutts/afe187c018c227d92498c04e0113dea7 to your computer and use it in GitHub Desktop.

Select an option

Save kylebutts/afe187c018c227d92498c04e0113dea7 to your computer and use it in GitHub Desktop.
Stata Regressions to R
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fixest Stata to R</title>
<!-- prism.js -->
<link href="https://cdn.jsdelivr.net/npm/prismjs@1.25.0/themes/prism.css" rel="stylesheet" />
<!-- tailwindCSS -->
<script
src="https://cdn-tailwindcss.vercel.app/3.0.0-alpha.2?plugins=forms@0.4.0-alpha.2,typography@0.5.0-alpha.3,aspect-ratio@0.3.0,line-clamp@0.2.2">
</script>
<style type="text/tailwindcss">
pre[class*="language-"] {
@apply bg-white px-4 py-4 lg:py-2 m-0;
}
</style>
</head>
<body>
<script src="https://cdn.jsdelivr.net/npm/prismjs@1.25.0/components/prism-core.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/prismjs@1.25.0/plugins/autoloader/prism-autoloader.min.js"></script>
<script
src="https://cdn.jsdelivr.net/npm/prismjs@1.25.0/plugins/normalize-whitespace/prism-normalize-whitespace.js">
</script>
<script src="https://cdn.jsdelivr.net/npm/prismjs@1.25.0/components/prism-r.js"></script>
<script src="https://cdn.jsdelivr.net/npm/prismjs@1.25.0/components/prism-stan.js"></script>
<script type="text/javascript">
// Optional
Prism.plugins.NormalizeWhitespace.setDefaults({
'remove-trailing': true,
'remove-indent': true,
'left-trim': true,
'right-trim': true,
'break-lines': 40
});
</script>
<div class="min-h-screen bg-gray-50 flex flex-col justify-center items-center pb-96">
<!-- Splash -->
<div class="w-full max-w-5xl mx-auto px-8 pt-36 pb-24 divide-y">
<div class="w-full pb-4">
<h1 class="text-black text-4xl font-bold">Running regressions in R <span
class="text-emerald-800 italic font-semibold">without the hassle.</span></h1>
</div>
<div class="w-full pt-4 flex flex-col gap-y-4">
<p class="text-gray-700 text-xl max-w-prose">
Many a few individuals have complained about why is running regressions in R so difficult: Robust
standard errors? Select covariates with * operator? Absorb fixed effects? <span class="italic">Why
is it so hard to do things that Stata makes simple?</span>
</p>
<p class="text-gray-700 text-xl max-w-prose">
This is where the package <a href="https://lrberge.github.io/fixest/index.html"
class="text-emerald-800 font-semibold underline">fixest</a> comes in.
</p>
</div>
</div>
<!-- Formula Creation -->
<div class="max-w-[84rem] w-[95%] px-4 divide-y">
<div class="grid grid-cols-8 gap-x-8 py-4 items-stretch my-4">
<h2 class="col-span-8 lg:col-span-2 font-bold text-2xl text-emerald-700">Formula Creation</h2>
<div class="lg:col-span-3"></div>
<div class="lg:col-span-3"></div>
</div>
</div>
<div class="max-w-[84rem] w-[95%] px-4 divide-y">
<div class="grid grid-row lg:grid-col lg:grid-cols-8 gap-y-4 lg:gap-y-0 lg:gap-x-8 py-4 items-stretch">
<h2 class="lg:col-span-2 lg:py-4 self-center text-gray-600 text-lg font-semibold">Fixed Effects</h2>
<pre class="lg:col-span-3 bg-white border-2 rounded-lg shadow-lg flex flex-col justify-center">
<code class="language-text">reghdfe y x, absorb(fe)</code>
</pre>
<pre class="lg:col-span-3 bg-white border-2 rounded-lg shadow-lg flex flex-col justify-center">
<code class="language-r">feols(y ~ x | fe, data)</code>
</pre>
</div>
<div class="grid grid-row lg:grid-col lg:grid-cols-8 gap-y-4 lg:gap-y-0 lg:gap-x-8 py-4 items-stretch">
<h2 class="lg:col-span-2 lg:py-4 self-center text-gray-600 text-lg font-semibold">Categorical Variables</h2>
<pre class="lg:col-span-3 bg-white border-2 rounded-lg shadow-lg flex flex-col justify-center">
<code class="language-text">reghdfe y x i.cat</code>
</pre>
<pre class="lg:col-span-3 bg-white border-2 rounded-lg shadow-lg flex flex-col justify-center">
<code class="language-r">feols(y ~ x + i(cat), data)</code>
</pre>
</div>
<div class="grid grid-row lg:grid-col lg:grid-cols-8 gap-y-4 lg:gap-y-0 lg:gap-x-8 py-4 items-stretch">
<h2 class="lg:col-span-2 lg:py-4 self-center text-gray-600 text-lg lg:ml-4 italic">With Baseline</h2>
<pre class="lg:col-span-3 bg-white border-2 rounded-lg shadow-lg flex flex-col justify-center">
<code class="language-text">reghdfe y x ib0.cat</code>
</pre>
<pre class="lg:col-span-3 bg-white border-2 rounded-lg shadow-lg flex flex-col justify-center">
<code class="language-r">feols(y ~ x + i(cat, ref = 0), data)</code>
</pre>
</div>
<div class="grid grid-row lg:grid-col lg:grid-cols-8 gap-y-4 lg:gap-y-0 lg:gap-x-8 py-4 items-stretch">
<h2 class="lg:col-span-2 lg:py-4 self-center text-gray-600 text-lg lg:ml-4 italic">Interact Categoricals</h2>
<pre class="lg:col-span-3 bg-white border-2 rounded-lg shadow-lg flex flex-col justify-center">
<code class="language-text">reghdfe y x i.cat#i.cat2</code>
</pre>
<pre class="lg:col-span-3 bg-white border-2 rounded-lg shadow-lg flex flex-col justify-center">
<code class="language-r">feols(y ~ x + i(cat, i.cat2), data)</code>
</pre>
</div>
<div class="grid grid-row lg:grid-col lg:grid-cols-8 gap-y-4 lg:gap-y-0 lg:gap-x-8 py-4 items-stretch">
<h2 class="lg:col-span-2 lg:py-4 self-center text-gray-600 text-lg lg:ml-4 italic">Categorical and Continuous</h2>
<pre class="lg:col-span-3 bg-white border-2 rounded-lg shadow-lg flex flex-col justify-center">
<code class="language-text">reghdfe y x c.cont#i.cat</code>
</pre>
<pre class="lg:col-span-3 bg-white border-2 rounded-lg shadow-lg flex flex-col justify-center">
<code class="language-r">feols(y ~ x + i(cat, cont), data)</code>
</pre>
</div>
<div class="grid grid-row lg:grid-col lg:grid-cols-8 gap-y-4 lg:gap-y-0 lg:gap-x-8 py-4 items-stretch">
<h2 class="lg:col-span-2 lg:py-4 self-center text-gray-600 text-lg font-semibold">Macros</h2>
<pre class="lg:col-span-3 bg-white border-2 rounded-lg shadow-lg flex flex-col justify-center">
<code class="language-text">local vars varlist
reghdfe y x `vars'</code>
</pre>
<pre class="lg:col-span-3 bg-white border-2 rounded-lg shadow-lg flex flex-col justify-center">
<code class="language-r">vars = c(...)
feols(y ~ x + .[vars], data)</code>
</pre>
</div>
<div class="grid grid-row lg:grid-col lg:grid-cols-8 gap-y-4 lg:gap-y-0 lg:gap-x-8 py-4 items-stretch">
<h2 class="lg:col-span-2 lg:py-4 self-center text-gray-600 text-lg lg:ml-4 italic">Wildcard</h2>
<pre class="lg:col-span-3 bg-white border-2 rounded-lg shadow-lg flex flex-col justify-center">
<code class="language-text">reghdfe y x age_*</code>
</pre>
<pre class="lg:col-span-3 bg-white border-2 rounded-lg shadow-lg flex flex-col justify-center">
<code class="language-r">feols(y ~ x + ..('age_'), data)</code>
</pre>
</div>
<div class="grid grid-row lg:grid-col lg:grid-cols-8 gap-y-4 lg:gap-y-0 lg:gap-x-8 py-4 items-stretch">
<h2 class="lg:col-span-2 lg:py-4 self-center text-gray-600 text-lg lg:ml-4 italic">Variables v1 ... v4</h2>
<pre class="lg:col-span-3 bg-white border-2 rounded-lg shadow-lg flex flex-col justify-center">
<code class="language-text">reghdfe y x v1-v4</code>
</pre>
<pre class="lg:col-span-3 bg-white border-2 rounded-lg shadow-lg flex flex-col justify-center">
<code class="language-r">feols(y ~ x + v.[1:4], data)</code>
</pre>
</div>
<div class="grid grid-row lg:grid-col lg:grid-cols-8 gap-y-4 lg:gap-y-0 lg:gap-x-8 py-4 items-stretch">
<h2 class="lg:col-span-2 lg:py-4 self-center text-gray-600 text-lg lg:ml-4 italic">Regex</h2>
<pre class="lg:col-span-3 bg-white border-2 rounded-lg shadow-lg flex flex-col justify-center">
<code class="language-text"></code>
</pre>
<pre class="lg:col-span-3 bg-white border-2 rounded-lg shadow-lg flex flex-col justify-center">
<code class="language-r">feols(y ~ x + ..('regex_exp'), data)</code>
</pre>
</div>
</div>
<!-- Standard Errors -->
<div class="max-w-[84rem] w-[95%] px-4 divide-y">
<div class="grid grid-cols-8 gap-x-8 py-4 items-stretch my-4">
<h2 class="col-span-8 lg:col-span-2 font-bold text-2xl text-emerald-700">Standard Errors</h2>
<div class="lg:col-span-3"></div>
<div class="lg:col-span-3"></div>
</div>
</div>
<div class="max-w-[84rem] w-[95%] px-4 divide-y">
<div class="grid grid-row lg:grid-col lg:grid-cols-8 gap-y-4 lg:gap-y-0 lg:gap-x-8 py-4 items-stretch">
<h2 class="lg:col-span-2 lg:py-4 self-center text-gray-600 text-lg font-semibold">Robust</h2>
<pre class="lg:col-span-3 bg-white border-2 rounded-lg shadow-lg flex flex-col justify-center">
<code class="language-text">reghdfe y x, r</code>
</pre>
<pre class="lg:col-span-3 bg-white border-2 rounded-lg shadow-lg flex flex-col justify-center">
<code class="language-r">feols(y ~ x | fe, data, "hetero")</code>
</pre>
</div>
<div class="grid grid-row lg:grid-col lg:grid-cols-8 gap-y-4 lg:gap-y-0 lg:gap-x-8 py-4 items-stretch">
<h2 class="lg:col-span-2 lg:py-4 self-center text-gray-600 text-lg font-semibold">Cluster</h2>
<pre class="lg:col-span-3 bg-white border-2 rounded-lg shadow-lg flex flex-col justify-center">
<code class="language-text">reghdfe y x, cluster(clust)</code>
</pre>
<pre class="lg:col-span-3 bg-white border-2 rounded-lg shadow-lg flex flex-col justify-center">
<code class="language-r">feols(y ~ x, data, ~clust)</code>
</pre>
</div>
<div class="grid grid-row lg:grid-col lg:grid-cols-8 gap-y-4 lg:gap-y-0 lg:gap-x-8 py-4 items-stretch">
<h2 class="lg:col-span-2 lg:py-4 self-center text-gray-600 text-lg lg:ml-4 italic">Two-way</h2>
<pre class="lg:col-span-3 bg-white border-2 rounded-lg shadow-lg flex flex-col justify-center">
<code class="language-text">reghdfe y x, cluster(clust1 clust2)</code>
</pre>
<pre class="lg:col-span-3 bg-white border-2 rounded-lg shadow-lg flex flex-col justify-center">
<code class="language-r">feols(y ~ x, data, ~clust1 + clust2)</code>
</pre>
</div>
</div>
<!-- Panel Data -->
<div class="max-w-[84rem] w-[95%] px-4 divide-y">
<div class="grid grid-cols-8 gap-x-8 py-4 items-stretch my-4">
<h2 class="col-span-8 lg:col-span-2 font-bold text-2xl text-emerald-700">Panel Data</h2>
<div class="lg:col-span-3"></div>
<div class="lg:col-span-3"></div>
</div>
</div>
<div class="max-w-[84rem] w-[95%] px-4 divide-y">
<div class="grid grid-row lg:grid-col lg:grid-cols-8 gap-y-4 lg:gap-y-0 lg:gap-x-8 py-4 items-stretch">
<h2 class="lg:col-span-2 lg:py-4 self-center text-gray-600 text-lg font-semibold">Lag Variables</h2>
<pre class="lg:col-span-3 bg-white border-2 rounded-lg shadow-lg flex flex-col justify-center">
<code class="language-text">xtset unit time
reg y x l1.x l2.x</code>
</pre>
<pre class="lg:col-span-3 bg-white border-2 rounded-lg shadow-lg flex flex-col justify-center">
<code class="language-r">panel(df, ~unit + time)
feols(y ~ x + l(x, 1:2), df</code>
</pre>
</div>
<div class="grid grid-row lg:grid-col lg:grid-cols-8 gap-y-4 lg:gap-y-0 lg:gap-x-8 py-4 items-stretch">
<h2 class="lg:col-span-2 lg:py-4 self-center text-gray-600 text-lg font-semibold">Lead Variables</h2>
<pre class="lg:col-span-3 bg-white border-2 rounded-lg shadow-lg flex flex-col justify-center">
<code class="language-text">xtset unit time
reg y x f1.x f2.x</code>
</pre>
<pre class="lg:col-span-3 bg-white border-2 rounded-lg shadow-lg flex flex-col justify-center">
<code class="language-r">panel(df, ~unit + time)
feols(y ~ x + l(x, -2:-1), df</code>
</pre>
</div>
<div class="grid grid-row lg:grid-col lg:grid-cols-8 gap-y-4 lg:gap-y-0 lg:gap-x-8 py-4 items-stretch">
<h2 class="lg:col-span-2 lg:py-4 self-center text-gray-600 text-lg font-semibold">First Difference</h2>
<pre class="lg:col-span-3 bg-white border-2 rounded-lg shadow-lg flex flex-col justify-center">
<code class="language-text">xtset unit time
reg y x D.x</code>
</pre>
<pre class="lg:col-span-3 bg-white border-2 rounded-lg shadow-lg flex flex-col justify-center">
<code class="language-r">panel(df, ~unit + time)
feols(y ~ x + d(x), df</code>
</pre>
</div>
</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment