<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/sampling-weights-and-multilevel-modeling-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://hagutierrezro.blogspot.com/2017/06/sampling-weights-and-multilevel.html"> Data Literacy - The blog of Andrés Gutiérrez</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>So many things <a rel="nofollow" href="http://www.nber.org/papers/w18859.pdf" target="_blank">have been said about weighting</a>, but on my personal view of statistical inference processes, you do have to weight. From a single statistic until a complex model, you have to weight, because of the probability measure that induces the variation of the sample comes from an (almost always) complex sampling design that you should not ignore. Weighting is a complex issue that has been discussed by several authors in recent years. The social researchers have no found consensus about the appropriateness of the use of weighting when it comes to the fit of statistical models. Angrist and Pischke (2009, p. 91) claim that <em>few things are as confusing to applied researchers as the role of sample weights. Even now, 20 years post-Ph.D., we read the section of the Stata manual on weighting with some dismay</em>.</p>
<p>Anyway, despite the fact that researchers do not have consensus on when to weight, the reality is that you have to be careful when doing so. For example, when it comes to estimating totals, means or proportions, you can use the inverse probability as a way for weighting, and it looks like every social researcher agrees to weight in order to estimate this kind of descriptive statistics. The rationale behind this practice is that you suppose that every unit belonging to the sample represents itself and many others that were not selected in the sample.</p>
<p>Now, when using weights to estimate parameter models, you have to keep in mind the nature of the sampling design. For example, when it comes to estimates multilevel parameters, you have to take into account not only the final sampling unit weights but also the first sampling unit weights. For example, let’s assume that you have a sample of students, selected from a national frame of schools. Then, we have two sets of weights, the first one regarding schools (notice that one selected school represents itself as well as others not in the sample) and the second one regarding students.</p>
<p>Now, let’s assume that in the finite population we have 10.000 students and 40 schools. For the sake of my example, let’s consider that you have selected 500 students allocated in 8 schools. For the sake of easiness, let’s think that a simple random sample is used (I know, this kind of sampling design is barely used) to select students. Think about it, if you take into account only the student’s weights to fit your multilevel model, you will find that you are estimating parameters with an expanded sample that represents 10.000 students that are allocated in a sample of just eight schools. So, any conclusion stated will be wrong. For example, when performing a simple analysis of variance, the percentage of variance explained by the schools will be extremely low, because of you are expanding the sample of schools. Now, if you take into account both sets of weights (students and schools), you will find yourself fitting a model with expanded samples that represent 10.000 students and 40 schools (which is good).</p>
<p>Unfortunately, as far as I know, the R suitcase lacks of a package that performs this kind of design-based inference to fitting multilevel models. So, right about now, we can unbiasedly estimate model parameters, but when it comes to estimate standard errors (from a design-based perspective) we need to use other computational resources and techniques like bootstrapping or Jackknife. According to the assumption of independence, most of the applied statistical methods cannot be used to analyze this kind of data directly due to dependency among sampled observation units. Inaccurate standard errors may be produced if no adjustment is made when analyzing complex survey data</p>
<p>Now, when it comes to educational studies (based on large-assessment tests), we can distinguish (at least) four set of weights: total student weight, student house-weight, student <a rel="nofollow" href="http://www.ierinstitute.org/fileadmin/Documents/IERI_Monograph/IERI_Monograph_Volume_05_Technical_Notes.pdf" target="_blank">senate-weight</a> and school weight. TIMMS team <a rel="nofollow" href="http://www.iea.nl/sites/default/files/irc/IRC-2013_Liou_Hung.pdf" target="_blank">claims that </a><em> total student weight is appropriate for single-level student-level analyses. Student house weight, also called normalized weight, is used when analyses are sensitive to sample size. Student house weight is essentially a linear transformation of total student weight so that the sum of the weights is equal to the sample size. Student Senate weight is used when analyses involve more than one country because it is total student weight scaled in such a way that all students’ senate weights sum to 500 (or 1000) in each country. School weight should be used when analyzing school-level data, as it is the inverse of the probability of selection for the selected school</em>.</p>
<h2>R workshop</h2>
<p>We will use the student house-weight to fit a multilevel model. As stated before, the sum of these weights is equal to the sample. For the R workshop, we will use PISA 2012 data (<a rel="nofollow" href="http://www.oecd.org/pisa/pisaproducts/pisa2012database-downloadabledata.htm" target="_blank">available in the OECD website</a>). I have done a filter for the Colombian case and saved this data to be directly compatible with R (<a rel="nofollow" href="https://drive.google.com/open?id=0ByzoHkgSB8F3T2pDcU9mOUh0Z0U" target="_blank">available here</a>). Let’s load the data into R.</p>
<pre>rm(<span>list</span> <span>=</span> ls())<br>library(<span>dplyr</span>)<br>library(<span>ggplot2</span>)<br>library(<span>lme4</span>)<br><br>setwd(<span>&quot;/your working directory&quot;</span>)<br>load(<span>&quot;PisaCol.RData&quot;</span>)<br><br>head(names(<span>PisaCol</span>))<br>summary(<span>PisaCol</span><span>$</span><span>STRATUM</span>)</pre>
<p>Now, we create an object containing the student house-weights and summarize some results based on that set of weights. Notice that the total student weights are stored in the column <strong>W_FSTUWT</strong> of the PISA database. I recall you that I am working with the first plausible value of the mathematics test and that score will be defined as our (dependent) variable of interest for the modeling.  </p>
<pre><span>n</span> <span>&lt;-</span> nrow(<span>PisaCol</span>)<br><span>PisaCol</span><span>$</span><span>W_HOUSEWHT</span> <span>&lt;-</span> <span>n</span> <span>*</span> <span>PisaCol</span><span>$</span><span>W_FSTUWT</span> <span>/</span> sum(<span>PisaCol</span><span>$</span><span>W_FSTUWT</span>)<br><br><span>PisaCol</span> %<span>></span>%<br>  group_by(<span>STRATUM</span>) %<span>></span>%<br>  summarise(<span>avg1</span> <span>=</span> weighted.mean(<span>PV1MATH</span>, <span>w</span> <span>=</span> <span>W_HOUSEWHT</span>),<br>            <span>avg2</span> <span>=</span> weighted.mean(<span>PV2MATH</span>, <span>w</span> <span>=</span> <span>W_HOUSEWHT</span>))<br></pre>
<p>We use the function <strong>lmer</strong> of the <strong>lme4 </strong>package to obtain the estimation of the model coefficients in the null model (where schools are defined as independent variables).</p>
<pre><span>##################</span><br><span>### Null model ###</span><br><span>##################</span><br><br><span>HLM0</span> <span>&lt;-</span> lmer(<span>PV1MATH</span> <span>~</span> (<span>1</span> <span>|</span> <span>SCHOOLID</span>), <span>data</span> <span>=</span> <span>PisaCol</span>,<br>             <span>weights</span> <span>=</span> <span>W_HOUSEWHT</span>)<br>coef(<span>HLM0</span>)<br>summary(<span>HLM0</span>)<br><br><span># 62.81% of the variance is due to students</span><br><span># 37.19% of the variance is due to schools</span><br><span>100</span> <span>*</span> <span>3569</span> <span>/</span> (<span>3569</span> <span>+</span> <span>2113</span>)<br></pre>
<p>Now, as you may know, the PISA index of economic, social and cultural status has a strong relationship to student achievement, so it is a good idea to control for this variable in a more refined model. </p>
<pre><span>#################</span><br><span>### ESCS mdel ###</span><br><span>#################</span><br><br><span>HLM1</span> <span>&lt;-</span> lmer(<span>PV1MATH</span> <span>~</span> <span>ESCS</span> <span>+</span> (<span>1</span> <span>+</span> <span>ESCS</span> <span>|</span> <span>SCHOOLID</span>), <span>data</span> <span>=</span> <span>PisaCol</span>,<br>             <span>weights</span> <span>=</span> <span>W_HOUSEWHT</span>)<br>coef(<span>HLM1</span>)<br>summary(<span>HLM1</span>)<br><br><span># After contoling for ESCE, 34.58% of the variance is due to schools</span><br><span>100</span> <span>*</span> (<span>96.12</span> <span>+</span> <span>1697.36</span>) <span>/</span> (<span>3392.58</span> <span>+</span> <span>96.12</span> <span>+</span> <span>1697.36</span>)</pre>
<p>So then, in summary: we have 3569 units of within-schools variance (63%), after controlling for ESCE that figure turns out to 3392 units (student background explains 5% of that variation). We have 2113 (37%) units of between-school variances, after controlling for ESCE that figure turns out to 1793 (student background explains 15% of that variation). The following code makes a graph that summarizes the relationship of the student achievement with ESCE.</p>
<pre>ggplot(<span>data</span> <span>=</span> <span>PisaCol</span>, aes(<span>x</span> <span>=</span> <span>ESCS</span>, <span>y</span> <span>=</span> <span>PV1MATH</span>, <span>size</span> <span>=</span> <span>W_HOUSEWHT</span>)) <span>+</span><br>  theme_minimal() <span>+</span> geom_point() <span>+</span> theme(<span>legend.position</span>=<span>&quot;none&quot;</span>)<br><br>ggplot(<span>data</span> <span>=</span> <span>PisaCol</span>, aes(<span>x</span> <span>=</span> <span>ESCS</span>, <span>y</span> <span>=</span> <span>PV1MATH</span>, <span>size</span> <span>=</span> <span>W_HOUSEWHT</span>)) <span>+</span><br>  geom_point(aes(<span>colour</span> <span>=</span> <span>SCHOOLID</span>)) <span>+</span> theme(<span>legend.position</span>=<span>&quot;none&quot;</span>)</pre>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://hagutierrezro.blogspot.com/2017/06/sampling-weights-and-multilevel.html"> Data Literacy - The blog of Andrés Gutiérrez</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...

	<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/GfiZ1qRWGIk" height="1"></a></div>