<div>Last April, I wrapped up the <a rel="nofollow" href="http://www.deeplytrivial.com/p/2017-blogging-to-z.html" target="_blank">A to Z of Statistics</a> with a post about <a rel="nofollow" href="http://www.deeplytrivial.com/2017/04/z-is-for-z-scores.html" target="_blank">Z-scores</a>. It seems only fitting that I’m wrapping this April A to Z with the same topic. Z-scores are frequently used, sometimes when you don’t even realize it. When you take your child to the doctor and they say he’s at the x percentile on height, or when you take a standardized test and are told you scored in the y percentile, those values are being derived from Z-scores. You create a Z-score when you subtract the population mean from a value and divide that result by the population standard deviation.
<p>Of course, we often will standardize variables in statistics, and the results are similar to Z-scores (though technically not the same if the mean and standard deviation aren’t population values). In fact, when I demonstrated the <a rel="nofollow" href="http://www.deeplytrivial.com/2018/04/g-is-for-glm-function.html" target="_blank">GLM function</a> earlier this month, I skipped a very important step when conducting an analysis with interactions. I should have standardized my continuous predictors first, which means subtracting the variable mean and dividing by the variable standard deviation, creating a new variable with a mean of 0 and a standard deviation of 1 (just like the normal distribution).</p>
<p>Let’s revisit that GLM analysis. I was predicting verdict (guilty, not guilty) with binomial regression. I did one analysis where I used a handful of attitude items and the participant’s guilt rating, and a second analysis where I created interactions between each attitude item and the guilt rating. The purpose was to see if an attitude impacts the threshold – how high the guilt rating needed to be before a participant selected “guilty”. When working with interactions, the individual variables are highly correlated with the interaction variables based on them, so we solve that problem, and make our analysis and output a bit cleaner, by centering our variables and using those centered values to create interactions.</p>
<p>I’ll go ahead and load my data. Also, since I know I have some missing values, which caused an error when I tried to work with <a rel="nofollow" href="https://www.deeplytrivial.com/2018/04/y-is-for-ys-y-hats-and-residuals.html" target="_blank">predicted values and residuals yesterday</a>, I’ll also go ahead and identify that case/those cases.</p>
<div>
<div>
<div>
<pre><span>dissertation</span><span>&lt;-</span><span>read.delim</span><span>(</span><span>&quot;dissertation_data.txt&quot;</span><span>,</span><span>header</span><span>=</span><span>TRUE</span><span>)</span><br><span>predictors</span><span>&lt;-</span><span>c</span><span>(</span><span>&quot;obguilt&quot;</span><span>,</span><span>&quot;reasdoubt&quot;</span><span>,</span><span>&quot;bettertolet&quot;</span><span>,</span><span>&quot;libertyvorder&quot;</span><span>,</span><br>              <span>&quot;jurevidence&quot;</span><span>,</span><span>&quot;guilt&quot;</span><span>)</span><br><span>library</span><span>(psych)</span><br></pre>
</div>
<div>
<pre>## Warning: package 'psych' was built under R version 3.4.4<br></pre>
</div>
<div>
<pre><span>describe</span><span>(dissertation[predictors])</span><br></pre>
</div>
<div>
<pre>##               vars   n mean   sd median trimmed  mad min max range  skew<br>## obguilt          1 356 3.50 0.89      4    3.52 0.00   1   5     4 -0.50<br>## reasdoubt        2 356 2.59 1.51      2    2.68 1.48  -9   5    14 -3.63<br>## bettertolet      3 356 2.36 1.50      2    2.38 1.48  -9   5    14 -3.41<br>## libertyvorder    4 355 2.74 1.31      3    2.77 1.48  -9   5    14 -3.89<br>## jurevidence      5 356 2.54 1.63      2    2.66 1.48  -9   5    14 -3.76<br>## guilt            6 356 4.80 1.21      5    4.90 1.48   2   7     5 -0.59<br>##               kurtosis   se<br>## obguilt          -0.55 0.05<br>## reasdoubt        26.92 0.08<br>## bettertolet      25.47 0.08<br>## libertyvorder    34.58 0.07<br>## jurevidence      25.39 0.09<br>## guilt            -0.54 0.06<br></pre>
</div>
<div>
<pre><span>dissertation</span><span>&lt;-</span><span>subset</span><span>(dissertation,</span> <span>!</span><span>is.na</span><span>(libertyvorder))</span><br></pre>
</div>
</div>
</div>
<p>R has a built-in function that will do this for you: scale. The scale function has 3 main arguments – the variable or variables to be scaled, and whether you want those variables centered (subtract mean) and/or scaled (divided by standard deviation). For regression with interactions, we want to both center and scale. For instance, to center and scale the guilt rating:</p>
<div>
<div>
<div>
<pre><span>dissertation</span><span>$</span><span>guilt_c</span><span>&lt;-</span><span>scale</span><span>(dissertation</span><span>$</span><span>guilt,</span> <span>center</span><span>=</span><span>TRUE</span><span>,</span> <span>scale</span><span>=</span><span>TRUE</span><span>)</span><br></pre>
</div>
</div>
</div>
<p>I have a set of predictors I want to do this to, so I want to apply a function across those specific columns:</p>
<div>
<div>
<div>
<pre><span>dissertation[</span><span>46</span><span>:</span><span>51</span><span>]</span><span>&lt;-</span><span>lapply</span><span>(dissertation[predictors],</span> <span>function</span><span>(</span><span>x</span><span>) {</span><br>  <span>y</span><span>&lt;-</span><span>scale</span><span>(x,</span> <span>center</span><span>=</span><span>TRUE</span><span>,</span> <span>scale</span><span>=</span><span>TRUE</span><span>)</span><br>  <span>}</span><br><span>)</span><br></pre>
</div>
</div>
</div>
<p>Now, let’s rerun that binomial regression, this time using the centered variables in the model.</p>
<div>
<div>
<div>
<pre><span>pred_int</span><span>&lt;-</span><span>'verdict ~ obguilt.1 + reasdoubt.1 + bettertolet.1 + libertyvorder.1 + <br> jurevidence.1 + guilt.1 + obguilt.1*guilt.1 + reasdoubt.1*guilt.1 +<br> bettertolet.1*guilt.1 + libertyvorder.1*guilt.1 + jurevidence.1*guilt.1'</span><br><span>model2</span><span>&lt;-</span><span>glm</span><span>(pred_int,</span> <span>family</span><span>=</span><span>&quot;binomial&quot;</span><span>,</span> <span>data</span><span>=dissertation)</span><br><span>summary</span><span>(model2)</span><br></pre>
</div>
<div>
<pre>## <br>## Call:<br>## glm(formula = pred_int, family = &quot;binomial&quot;, data = dissertation)<br>## <br>## Deviance Residuals: <br>##     Min       1Q   Median       3Q      Max  <br>## -2.6101  -0.5432  -0.1289   0.6422   2.2805  <br>## <br>## Coefficients:<br>##                         Estimate Std. Error z value Pr(>|z|)    <br>## (Intercept)             -0.47994    0.16264  -2.951  0.00317 ** <br>## obguilt.1                0.25161    0.16158   1.557  0.11942    <br>## reasdoubt.1             -0.09230    0.20037  -0.461  0.64507    <br>## bettertolet.1           -0.22484    0.20340  -1.105  0.26899    <br>## libertyvorder.1          0.05825    0.21517   0.271  0.78660    <br>## jurevidence.1            0.07252    0.19376   0.374  0.70819    <br>## guilt.1                  2.31003    0.26867   8.598  &lt; 2e-16 ***<br>## obguilt.1:guilt.1        0.14058    0.23411   0.600  0.54818    <br>## reasdoubt.1:guilt.1     -0.61724    0.29693  -2.079  0.03764 *  <br>## bettertolet.1:guilt.1    0.02579    0.30123   0.086  0.93178    <br>## libertyvorder.1:guilt.1 -0.27492    0.29355  -0.937  0.34899    <br>## jurevidence.1:guilt.1    0.27601    0.36181   0.763  0.44555    <br>## ---<br>## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1<br>## <br>## (Dispersion parameter for binomial family taken to be 1)<br>## <br>##     Null deviance: 490.08  on 354  degrees of freedom<br>## Residual deviance: 300.66  on 343  degrees of freedom<br>## AIC: 324.66<br>## <br>## Number of Fisher Scoring iterations: 6<br></pre>
</div>
</div>
</div>
<p>The results are essentially the same; the constant and slopes of the predictors variables are different but the variables that were significant before still are. So standardizing doesn’t change the results, but it is generally recommended because it makes results easier to interpret, because the variables are centered around the mean. So negative numbers are below the mean, and positive numbers are above the mean.</p>
<p>Hard to believe A to Z is over! Don’t worry, I’m going to keep blogging about statistics, R, and whatever strikes my fancy. I almost kept this post going by applying the prediction work from yesterday to the binomial model, but decided that would make for a fun future post. And I’ll probably sprinkle in posts in the near future on topics I didn’t have room for this month or that I promised to write a future post on. Thanks for reading and hope you keep stopping by, even though April A to Z is officially over!</p>
<div><a rel="nofollow" href="https://i0.wp.com/3.bp.blogspot.com/-PjGgD68S1hw/Wq53Z7b4HlI/AAAAAAAAK-I/8YYeq22dNPY2s1LBXuhVSPo7BhJyGpa2wCPcBGAYYCw/s1600/a%2Bto%2Bz%2Bof%2BR.jpg?ssl=1" target="_blank"><img border="0" src="https://i1.wp.com/3.bp.blogspot.com/-PjGgD68S1hw/Wq53Z7b4HlI/AAAAAAAAK-I/8YYeq22dNPY2s1LBXuhVSPo7BhJyGpa2wCPcBGAYYCw/s320/a%2Bto%2Bz%2Bof%2BR.jpg?resize=450%2C840&ssl=1" data-original-width="450" width="450" data-original-height="840" data-recalc-dims="1"></a></div>
<p></p>
	<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://www.deeplytrivial.com/2018/04/z-is-for-z-scores-and-standardizing.html"> Deeply Trivial</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/qDqKAEG86AM" height="1"></a></div></div>