Skip to content

Commit

Permalink
Updates book with new random effects language.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkshaw committed Nov 14, 2024
1 parent d410ede commit 4d81d95
Show file tree
Hide file tree
Showing 98 changed files with 835 additions and 763 deletions.
10 changes: 7 additions & 3 deletions docs/01-module-1.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Chapter 1 Introduction | Introduction to Multilevel Modelling</title>
<meta name="description" content="This is an introduction to multilevel modelling. We establish a comprehensive foundational understanding of multilevel modelling that prepares readers to recognize when such models are needed, conduct their own, and critically analyze their use in the literature." />
<meta name="generator" content="bookdown 0.29 and GitBook 2.6.7" />
<meta name="generator" content="bookdown 0.40 and GitBook 2.6.7" />

<meta property="og:title" content="Chapter 1 Introduction | Introduction to Multilevel Modelling" />
<meta property="og:type" content="book" />
Expand Down Expand Up @@ -91,7 +91,7 @@
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.bu { color: #008000; } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
Expand All @@ -104,7 +104,7 @@
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.im { color: #008000; font-weight: bold; } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
Expand All @@ -118,6 +118,10 @@
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>

<style type="text/css">

div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
</style>

<link rel="stylesheet" href="style.css" type="text/css" />
</head>
Expand Down
72 changes: 38 additions & 34 deletions docs/02-multiple-regression.html

Large diffs are not rendered by default.

44 changes: 24 additions & 20 deletions docs/03-module-3.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Chapter 3 Approaches to Multilevel Data | Introduction to Multilevel Modelling</title>
<meta name="description" content="This is an introduction to multilevel modelling. We establish a comprehensive foundational understanding of multilevel modelling that prepares readers to recognize when such models are needed, conduct their own, and critically analyze their use in the literature." />
<meta name="generator" content="bookdown 0.29 and GitBook 2.6.7" />
<meta name="generator" content="bookdown 0.40 and GitBook 2.6.7" />

<meta property="og:title" content="Chapter 3 Approaches to Multilevel Data | Introduction to Multilevel Modelling" />
<meta property="og:type" content="book" />
Expand Down Expand Up @@ -91,7 +91,7 @@
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.bu { color: #008000; } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
Expand All @@ -104,7 +104,7 @@
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.im { color: #008000; font-weight: bold; } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
Expand All @@ -118,6 +118,10 @@
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>

<style type="text/css">

div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
</style>

<link rel="stylesheet" href="style.css" type="text/css" />
</head>
Expand Down Expand Up @@ -345,11 +349,11 @@ <h2><span class="header-section-number">3.2</span> Data Demonstration<a href="03
<div id="load-data-and-dependencies" class="section level3 hasAnchor" number="3.2.1">
<h3><span class="header-section-number">3.2.1</span> Load Data and Dependencies<a href="03-module-3.html#load-data-and-dependencies" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>First, let’s load in the data and packages we’ll be using for this data demo. We will use the following packages:</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb15-1"><a href="03-module-3.html#cb15-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(dplyr) <span class="co"># for data processing</span></span>
<span id="cb15-2"><a href="03-module-3.html#cb15-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(lmtest) <span class="co"># for cluster-robust standard errors</span></span>
<span id="cb15-3"><a href="03-module-3.html#cb15-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(sandwich) <span class="co"># for cluster-robust standard errors</span></span></code></pre></div>
<div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb15-1"><a href="03-module-3.html#cb15-1" tabindex="-1"></a><span class="fu">library</span>(dplyr) <span class="co"># for data processing</span></span>
<span id="cb15-2"><a href="03-module-3.html#cb15-2" tabindex="-1"></a><span class="fu">library</span>(lmtest) <span class="co"># for cluster-robust standard errors</span></span>
<span id="cb15-3"><a href="03-module-3.html#cb15-3" tabindex="-1"></a><span class="fu">library</span>(sandwich) <span class="co"># for cluster-robust standard errors</span></span></code></pre></div>
<p>We’ll store the data in an object called <code>data</code>. If you want to read the data in with the relative filepath (i.e., just referencing “heck2011.csv”), make sure the file is in the same folder as your R script. If the file is in a different folder, tell your computer exactly where to find it with an absolute file path.</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb16-1"><a href="03-module-3.html#cb16-1" aria-hidden="true" tabindex="-1"></a>data <span class="ot">&lt;-</span> <span class="fu">read.csv</span>(<span class="st">&#39;heck2011.csv&#39;</span>)</span></code></pre></div>
<div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb16-1"><a href="03-module-3.html#cb16-1" tabindex="-1"></a>data <span class="ot">&lt;-</span> <span class="fu">read.csv</span>(<span class="st">&#39;heck2011.csv&#39;</span>)</span></code></pre></div>
</div>
<div id="dealing-with-dependence" class="section level3 hasAnchor" number="3.2.2">
<h3><span class="header-section-number">3.2.2</span> Dealing with Dependence<a href="03-module-3.html#dealing-with-dependence" class="anchor-section" aria-label="Anchor link to header"></a></h3>
Expand All @@ -361,8 +365,8 @@ <h3><span class="header-section-number">3.2.2</span> Dealing with Dependence<a h
<h3><span class="header-section-number">3.2.3</span> Cluster-Robust Standard Errors<a href="03-module-3.html#cluster-robust-standard-errors" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Cluster-robust standard errors account for clustering but retain the interpretation of regular regression models. That is, the coefficients do not delineate within and between effects, but provide average effects pooled across the whole dataset and unbiased standard errors that take into account the clustering. For more information on clustered standard errors, see this helpful overview: <a href="https://mldscenter.maryland.gov/egov/Publications/ResearchSeries/Clustered%20Data,%20Are%20Multilevel%20Models%20Really%20Necessary.pdf" class="uri">https://mldscenter.maryland.gov/egov/Publications/ResearchSeries/Clustered%20Data,%20Are%20Multilevel%20Models%20Really%20Necessary.pdf</a>. Let’s look at cluster-robust standard errors in action!</p>
<p>In chapter 2, we conducted a linear regression predicting math achievement from socioeconomic status and sex. Let’s run that same model.</p>
<div class="sourceCode" id="cb17"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb17-1"><a href="03-module-3.html#cb17-1" aria-hidden="true" tabindex="-1"></a>model <span class="ot">&lt;-</span> <span class="fu">lm</span>(math <span class="sc">~</span> ses <span class="sc">+</span> female, <span class="at">data =</span> data)</span>
<span id="cb17-2"><a href="03-module-3.html#cb17-2" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(model)</span></code></pre></div>
<div class="sourceCode" id="cb17"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb17-1"><a href="03-module-3.html#cb17-1" tabindex="-1"></a>model <span class="ot">&lt;-</span> <span class="fu">lm</span>(math <span class="sc">~</span> ses <span class="sc">+</span> female, <span class="at">data =</span> data)</span>
<span id="cb17-2"><a href="03-module-3.html#cb17-2" tabindex="-1"></a><span class="fu">summary</span>(model)</span></code></pre></div>
<pre><code>##
## Call:
## lm(formula = math ~ ses + female, data = data)
Expand All @@ -372,26 +376,26 @@ <h3><span class="header-section-number">3.2.3</span> Cluster-Robust Standard Err
## -30.923 -4.606 1.176 5.317 48.054
##
## Coefficients:
## Estimate Std. Error t value Pr(&gt;|t|)
## (Intercept) 58.1330 0.1390 418.093 &lt; 0.0000000000000002 ***
## ses 4.2269 0.1255 33.679 &lt; 0.0000000000000002 ***
## female -1.0626 0.1960 -5.422 0.0000000609 ***
## Estimate Std. Error t value Pr(&gt;|t|)
## (Intercept) 58.1330 0.1390 418.093 &lt; 2e-16 ***
## ses 4.2269 0.1255 33.679 &lt; 2e-16 ***
## female -1.0626 0.1960 -5.422 6.09e-08 ***
## ---
## Signif. codes: 0 &#39;***&#39; 0.001 &#39;**&#39; 0.01 &#39;*&#39; 0.05 &#39;.&#39; 0.1 &#39; &#39; 1
##
## Residual standard error: 8.115 on 6868 degrees of freedom
## Multiple R-squared: 0.1467, Adjusted R-squared: 0.1464
## F-statistic: 590.3 on 2 and 6868 DF, p-value: &lt; 0.00000000000000022</code></pre>
## F-statistic: 590.3 on 2 and 6868 DF, p-value: &lt; 2.2e-16</code></pre>
<p>Now, let’s run the same model with cluster-robust standard errors and compare the coefficients and their significance between the regular and cluster-robust models.</p>
<div class="sourceCode" id="cb19"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb19-1"><a href="03-module-3.html#cb19-1" aria-hidden="true" tabindex="-1"></a>model_crse <span class="ot">&lt;-</span> <span class="fu">coeftest</span>(model, <span class="at">vcov =</span> vcovCL, <span class="at">cluster =</span> <span class="sc">~</span> schcode)</span>
<span id="cb19-2"><a href="03-module-3.html#cb19-2" aria-hidden="true" tabindex="-1"></a>model_crse</span></code></pre></div>
<div class="sourceCode" id="cb19"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb19-1"><a href="03-module-3.html#cb19-1" tabindex="-1"></a>model_crse <span class="ot">&lt;-</span> <span class="fu">coeftest</span>(model, <span class="at">vcov =</span> vcovCL, <span class="at">cluster =</span> <span class="sc">~</span> schcode)</span>
<span id="cb19-2"><a href="03-module-3.html#cb19-2" tabindex="-1"></a>model_crse</span></code></pre></div>
<pre><code>##
## t test of coefficients:
##
## Estimate Std. Error t value Pr(&gt;|t|)
## (Intercept) 58.13305 0.16528 351.7188 &lt; 0.00000000000000022 ***
## ses 4.22690 0.14951 28.2714 &lt; 0.00000000000000022 ***
## female -1.06257 0.21089 -5.0386 0.000000481 ***
## Estimate Std. Error t value Pr(&gt;|t|)
## (Intercept) 58.13305 0.16528 351.7188 &lt; 2.2e-16 ***
## ses 4.22690 0.14951 28.2714 &lt; 2.2e-16 ***
## female -1.06257 0.21089 -5.0386 4.81e-07 ***
## ---
## Signif. codes: 0 &#39;***&#39; 0.001 &#39;**&#39; 0.01 &#39;*&#39; 0.05 &#39;.&#39; 0.1 &#39; &#39; 1</code></pre>
<p>As expected, the coefficients are the same between the two models, but the significance levels differ. In this case, the differences are trivial, and all coefficients retain their significance. But in your case, correcting for clustering might make the difference between significant and non-significant results.</p>
Expand Down
Loading

0 comments on commit 4d81d95

Please sign in to comment.