<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Rense Nieuwenhuis &#187; R-Project</title>
	<atom:link href="http://www.rensenieuwenhuis.nl/tag/r-project/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rensenieuwenhuis.nl</link>
	<description>&#34;The extra-ordinary lies within the curve of normality&#34;</description>
	<lastBuildDate>Thu, 12 Mar 2026 14:58:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.2.2</generator>
	<item>
		<title>A novel method for modelling interaction between categorical variables</title>
		<link>http://www.rensenieuwenhuis.nl/a-novel-method-for-modelling-interaction-between-categorical-variables/</link>
		<comments>http://www.rensenieuwenhuis.nl/a-novel-method-for-modelling-interaction-between-categorical-variables/#comments</comments>
		<pubDate>Tue, 18 Apr 2017 08:00:43 +0000</pubDate>
		<dc:creator><![CDATA[Rense Nieuwenhuis]]></dc:creator>
				<category><![CDATA[Peer Reviewed]]></category>
		<category><![CDATA[R-Project]]></category>
		<category><![CDATA[Science]]></category>
		<category><![CDATA[dummy coding]]></category>
		<category><![CDATA[interaction]]></category>
		<category><![CDATA[moderation]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[wec]]></category>
		<category><![CDATA[weighted effect coding]]></category>

		<guid isPermaLink="false">http://www.rensenieuwenhuis.nl/?p=6075</guid>
		<description><![CDATA[We have been developing weighted effect coding in an ongoing series of publications (hint: a publication in the R Journal will follow). To include nominal and ordinal variables as predictors in regression models, their categories ...]]></description>
				<content:encoded><![CDATA[<p>We have been developing weighted effect coding in an ongoing series of publications (hint: a publication in the R Journal will follow). To include nominal and ordinal variables as predictors in regression models, their categories first have to be transformed into so-called &#8216;dummy variables&#8217;. There are many transformations available, and popular is &#8216;dummy coding&#8217; in which the estimates represent deviations from a preselected &#8216;reference category&#8217;. </p>
<p>To avoid choosing a reference category, weighted effect coding provides estimates representing deviations from the sample mean. This is particularly useful when the data are unbalanced (i.e., categories holding different numbers of observation). The basics of this  technique, with applications in R, were <a href="http://www.rensenieuwenhuis.nl/when-size-matters-weighted-effect-coding/">detailed here</a>.</p>
<p><a href="http://link.springer.com/article/10.1007/s00038-016-0902-0">In a new publication, available open access,</a>, we show that weighted effect coding can also be applied to regression models with interaction effects (also commonly referred to as moderation). The weighted effect coded interactions represent the additional effects over and above the main effects obtained from the model without these interactions. </p>
<p>To apply the procedures introduced in these papers, called weighted effect coding, procedures are made available for R, SPSS, and Stata. For R, we created the &#8216;wec&#8217; package which can be installed by typing:</p>
<blockquote><p>
install.packages(&#8220;wec&#8221;)
</p></blockquote>
<h1>References (Open Access!)</h1>
<p>Grotenhuis, M., Ben Pelzer, Eisinga, R., Nieuwenhuis, R., Schmidt-Catran, A., &#038; Konig, R. (2017). <b>A novel method for modelling interaction between categorical variables</b>. <I>International Journal of Public Health</i>, 62(3), 427–431. <a href="http://link.springer.com/article/10.1007/s00038-016-0902-0">http://link.springer.com/article/10.1007/s00038-016-0902-0</a> </p>
<p>Grotenhuis, M., Ben Pelzer, Eisinga, R., Nieuwenhuis, R., Schmidt-Catran, A., &#038; Konig, R. (2017). <b>When size matters: advantages of weighted effect coding in observational studies</b>. <I>International Journal of Public Health</i>, 62(1), 163–167. <a href="http://doi.org/10.1007/s00038-016-0901-1">http://doi.org/10.1007/s00038-016-0901-1</a> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.rensenieuwenhuis.nl/a-novel-method-for-modelling-interaction-between-categorical-variables/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>When Size Matters: Weighted Effect Coding</title>
		<link>http://www.rensenieuwenhuis.nl/when-size-matters-weighted-effect-coding/</link>
		<comments>http://www.rensenieuwenhuis.nl/when-size-matters-weighted-effect-coding/#comments</comments>
		<pubDate>Fri, 24 Feb 2017 07:50:02 +0000</pubDate>
		<dc:creator><![CDATA[Rense Nieuwenhuis]]></dc:creator>
				<category><![CDATA[Peer Reviewed]]></category>
		<category><![CDATA[R-Project]]></category>
		<category><![CDATA[Science]]></category>
		<category><![CDATA[dummy]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[wec]]></category>
		<category><![CDATA[weighted effect coding]]></category>

		<guid isPermaLink="false">http://www.rensenieuwenhuis.nl/?p=6056</guid>
		<description><![CDATA[Categorical variables in regression models are often included by dummy variables. In R, this is done with factor variables with treatment coding. Typically, the difference and significance of each category are tested against a preselected ...]]></description>
				<content:encoded><![CDATA[<p>Categorical variables in regression models are often included by dummy variables. In R, this is done with factor variables with treatment coding. Typically, the difference and significance of each category are tested against a preselected reference category. We present a useful alternative. </p>
<p>If all categories have (roughly) the same number of observations, you can also test all categories against the grand mean using effect (ANOVA) coding. In observational studies, however, the number of observations per category typically varies. <a href="http://link.springer.com/article/10.1007/s00038-016-0901-1">Our new paper shows how categories of a factor variable can be tested against the sample mean</a>. Although the paper has been online for some time now (and this post is an update to an earlier post some time age), we are happy to announce that our paper has now officially been published a the International Journal of Public Health.</p>
<p>To apply the procedures introduced in these papers, called weighted effect coding, procedures are made available for R, SPSS, and Stata. For R, we created the ‘wec’ package which can be installed by typing:</p>
<blockquote><p>
install.packages(&#8220;wec&#8221;)
</p></blockquote>
<h3>References</h3>
<p>Grotenhuis, M., Ben Pelzer, Eisinga, R., Nieuwenhuis, R., Schmidt-Catran, A., &#038; Konig, R. (2017). When size matters: advantages of weighted effect coding in observational studies. <I>International Journal of Public Health</i>, (62), 163–167. <a href="http://doi.org/10.1007/s00038-016-0901-1">http://doi.org/10.1007/s00038-016-0901-1</a> </p>
<p>Sweeney R, Ulveling EF (1972) A transformation for simplifying the interpretation of coefficients of binary variables in regression analysis. <i>Am Stat</i> 26:30–32</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rensenieuwenhuis.nl/when-size-matters-weighted-effect-coding/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New version of WEC: focus on interactions</title>
		<link>http://www.rensenieuwenhuis.nl/new-version-of-wec-focus-on-interactions/</link>
		<comments>http://www.rensenieuwenhuis.nl/new-version-of-wec-focus-on-interactions/#comments</comments>
		<pubDate>Tue, 17 Jan 2017 11:00:52 +0000</pubDate>
		<dc:creator><![CDATA[Rense Nieuwenhuis]]></dc:creator>
				<category><![CDATA[My Publications]]></category>
		<category><![CDATA[R-Project]]></category>
		<category><![CDATA[Academic Software]]></category>
		<category><![CDATA[open-source]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[wec]]></category>
		<category><![CDATA[weighted effect coding]]></category>

		<guid isPermaLink="false">http://www.rensenieuwenhuis.nl/?p=6037</guid>
		<description><![CDATA[We have uploaded a new version of WEC, an R package to apply &#8216;weighted effect coding&#8217; to your dummy variables. With weighted effect coding, your dummy variables represent the deviation of their respective category from ...]]></description>
				<content:encoded><![CDATA[<p>We have uploaded a new version of WEC, an R package to apply &#8216;weighted effect coding&#8217; to your dummy variables. With weighted effect coding, your dummy variables represent the deviation of their respective category from the sample mean, rather than the deviation from a reference category. Particularly with observational data, which are often unbalanced, this can have attractive interpretations. We recently published two articles in which we discuss some of the advantages:</p>
<p><a href="http://doi.org/10.1007/s00038-016-0901-1"> Grotenhuis, M., Ben Pelzer, Eisinga, R., Nieuwenhuis, R., Schmidt-Catran, A., &#038; Konig, R. (2016b). When size matters: advantages of weighted effect coding in observational studies. <i>International Journal of Public Health</i>, 1–5. http://doi.org/10.1007/s00038-016-0901-1 </a></p>
<p><a href="http://doi.org/10.1007/s00038-016-0902-0"> Grotenhuis, M., Ben Pelzer, Eisinga, R., Nieuwenhuis, R., Schmidt-Catran, A., &#038; Konig, R. (2016a). A novel method for modelling interaction between categorical variables. <i>International Journal of Public Health</i>, 1–5. http://doi.org/10.1007/s00038-016-0902-0 </a></p>
<p>As some of the real advantages of  weighted effect coding come into play when using interactions, that was what we focused in the current update to our &#8216;wec&#8217; package (version 0.4). The package now supports interactions between a weighted effect coded factor variable and an interval variable, and the calculation of interactions between two weighted effect coded factor variables was much improved. An example is given below (with more to follow, hopefully soon).</p>
<p><code><br />
library(wec)<br />
data(PUMS)<br />
PUMS$race.wec <- factor(PUMS$race)<br />
contrasts(PUMS$race.wec) <- contr.wec(PUMS$race.wec, "White")<br />
PUMS$race.educint <- wec.interact(PUMS$race.wec, PUMS$education.int)<br />
m.wec.educ <- lm(wage ~ race.wec + education.int + race.educint, data=PUMS)<br />
summary(m.wec.educ)$coefficients<br />
</code></p>
<p>The code above results in a regression model (shown below) in which the main effect for education (9048) remains the same, whether the interaction terms are included or not (you can try this yourself). Thus, the interaction terms represent how much the average education effect varies by race.</p>
<pre>
                            Estimate Std. Error t value Pr(>|t|)
(Intercept)                     52320        559    93.5  0.0e+00
race.wecHispanic                -4955       1736    -2.9  4.3e-03
race.wecBlack                  -11276       1817    -6.2  5.7e-10
race.wecAsian                    5151       2381     2.2  3.1e-02
education.int                    9048        287    31.6 2.3e-208
race.educintinteractHispanic    -3266        977    -3.3  8.3e-04
race.educintinteractBlack       -3293        990    -3.3  8.8e-04
race.educintinteractAsian        3575       1217     2.9  3.3e-03
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.rensenieuwenhuis.nl/new-version-of-wec-focus-on-interactions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Weighted Effect Coding: Dummy coding when size matters</title>
		<link>http://www.rensenieuwenhuis.nl/weighted-effect-coding-dummy-coding-when-size-matters/</link>
		<comments>http://www.rensenieuwenhuis.nl/weighted-effect-coding-dummy-coding-when-size-matters/#comments</comments>
		<pubDate>Mon, 31 Oct 2016 11:00:32 +0000</pubDate>
		<dc:creator><![CDATA[Rense Nieuwenhuis]]></dc:creator>
				<category><![CDATA[My Publications]]></category>
		<category><![CDATA[Peer Reviewed]]></category>
		<category><![CDATA[R-Project]]></category>
		<category><![CDATA[Science]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[dummies]]></category>
		<category><![CDATA[R]]></category>

		<guid isPermaLink="false">http://www.rensenieuwenhuis.nl/?p=5979</guid>
		<description><![CDATA[If your regression model contains a categorical predictor variable, you commonly test the significance of its categories against a preselected reference category. If all categories have (roughly) the same number of observations, you can also ...]]></description>
				<content:encoded><![CDATA[<p>If your regression model contains a categorical predictor variable, you commonly test the significance of its categories against a preselected reference category. If all categories have (roughly) the same number of observations, you can also test all categories against the grand mean using effect (ANOVA) coding. In observational studies, however, the number of observations per category typically varies. We <a href="http://link.springer.com/article/10.1007/s00038-016-0901-1">published a paper in the International Journal of Public Health</a>, showing how all categories can be tested against the sample mean.</p>
<p>In a <a href="http://link.springer.com/article/10.1007/s00038-016-0902-0">second paper in the same journal</a>, the procedure is expanded to regression models that test interaction effects. Within this framework, the weighted effect coded interaction displays the extra effect on top of the main effect found in a model without the interaction effect. This offers a promising new route to estimate interaction effects in observational data, where different category sizes often prevail.</p>
<p>To apply the procedures introduced in these papers, called weighted effect coding, procedures are <a href="http://www.ru.nl/sociology/mt/wec/downloads/">made available for R, SPSS, and Stata</a>. For R, we created the <a href="https://cran.r-project.org/web/packages/wec/index.html">‘wec’ package</a> which can be installed by typing:</p>
<blockquote><p>
install.packages(&#8220;wec&#8221;)
</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.rensenieuwenhuis.nl/weighted-effect-coding-dummy-coding-when-size-matters/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introducing Influence.ME: Tools for detecting influential data in mixed models</title>
		<link>http://www.rensenieuwenhuis.nl/introducing-influenceme/</link>
		<comments>http://www.rensenieuwenhuis.nl/introducing-influenceme/#comments</comments>
		<pubDate>Wed, 29 Apr 2009 09:03:25 +0000</pubDate>
		<dc:creator><![CDATA[Rense Nieuwenhuis]]></dc:creator>
				<category><![CDATA[Influence.ME]]></category>
		<category><![CDATA[lmer]]></category>
		<category><![CDATA[mixed models]]></category>
		<category><![CDATA[R-Project]]></category>
		<category><![CDATA[regression]]></category>

		<guid isPermaLink="false">http://www.rensenieuwenhuis.nl/?p=914</guid>
		<description><![CDATA[I&#8217;m highly excited to announce that influence.ME is now available. Influence.ME is a new software package for R, providing statistical tools for detecting influential data in mixed models. It has been developed by Rense Nieuwenhuis, ...]]></description>
				<content:encoded><![CDATA[<p>I&#8217;m highly excited to announce that <a href="rensenieuwenhuis.nl/r-project/influenceme/">influence.ME</a> is now available. <a href="rensenieuwenhuis.nl/r-project/influenceme/">Influence.ME</a> is a new software package for <a href="www.r-project.org">R</a>, providing statistical tools for detecting influential data in mixed models. It has been developed by <a href="www.rensenieuwenhuis.nl">Rense Nieuwenhuis</a>, <a href="http://benpelzer.ruhosting.nl/"> Ben Pelzer</a>, and <a href="http://www.ru.nl/methodenentechnieken/syntax/mtg/">Manfred te Grotenhuis</a>. The basic rationale behind identifying influential data is that when iteratively single units are omitted from the data, models based on these data should not produce substantially different estimates. To standardize the assessment of how influential data is, several measures of influence are commonly used, such as DFBETAS and Cook&#8217;s Distance.<br />
<!--adsense--><br />
<span id="more-914"></span><br />
Mixed effects regression models tend to become common practice in the field of Social Sciences. However, diagnostic tools to evaluate these models lag behind. For instance there is no general applicable tool to check whether all units (or cases) roughly have the same influence on the regression parameters. It is however commonly accepted that tests for influential cases should be performed, especially when estimates are based on a relatively small number of cases. Testing for influence with mixed effects models is especially important in Social Science applications, for two reasons. First, models in the Social Sciences are frequently based on large numbers of individuals while the number of higher level units is often relatively small. Secondly, often the higher level units are remarkably similar, for instance in the case of neighboring countries. Influence.ME is a new package for R which provides two innovations for evaluating influential cases: it extends existing procedures for use with mixed effects models, and it allows to not only search for single influential cases, but for combinations of cases that as a combination exert too much influence.</p>
<p>I plan to use my blog to provide more information about influence.ME. For instance, you can expect some example analyses soon. Other developments, new features, or exciting applications in research papers will be discussed here as well in due time. <a href="http://www.rensenieuwenhuis.nl/r-project/influenceme/">A static page on influence.ME</a> is available as well, where all important information is collected.  </p>
<p>Questions, comments, thoughts, experiences, notes on bugs (and other vermin), feature requests, and what more: it is all highly appreciated. They can be sent by e-mail, or placed in the comments-section on this blog.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rensenieuwenhuis.nl/introducing-influenceme/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>useR! 2009 acceptance: presenting influence.ME</title>
		<link>http://www.rensenieuwenhuis.nl/user-2009-acceptance-presenting-influenceme/</link>
		<comments>http://www.rensenieuwenhuis.nl/user-2009-acceptance-presenting-influenceme/#comments</comments>
		<pubDate>Thu, 23 Apr 2009 10:23:56 +0000</pubDate>
		<dc:creator><![CDATA[Rense Nieuwenhuis]]></dc:creator>
				<category><![CDATA[Influence.ME]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[glmer]]></category>
		<category><![CDATA[influential data]]></category>
		<category><![CDATA[lme4]]></category>
		<category><![CDATA[lmer]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[R-Project]]></category>
		<category><![CDATA[useR! 2009]]></category>

		<guid isPermaLink="false">http://www.rensenieuwenhuis.nl/?p=935</guid>
		<description><![CDATA[The organizing committee of the useR! 2009 conference just informed me, that my submission for presenting my extension package influence.ME, has been accepted! Influence.ME is a new R package that I&#8217;m currently developing, with the ...]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.rensenieuwenhuis.nl/r-project/influenceme/"><img src="http://i1.wp.com/www.rensenieuwenhuis.nl/wp-content/uploads/2009/04/logo-influence.jpg?w=450" alt="Logo influence.ME" title="Logo influence.ME" data-recalc-dims="1" /></a></p>
<p><!--adsense--></p>
<p>The organizing committee of the useR! 2009 conference just informed me, that my submission for presenting my extension package influence.ME, has been accepted! Influence.ME is a new R package that I&#8217;m currently developing, with the indispensable help of <a href="http://benpelzer.ruhosting.nl/">Ben Pelzer</a> and <a href="http://www.ru.nl/methodenentechnieken/methoden_technieken/medewerkers/vm_medewerkers/manfred_te/">Manfred te Grotenhuis</a>. Although I did not yet introduce influence.ME on this blog, rest assured that I will do so within just a few weeks. Now is time for celebration!<br />
<span id="more-935"></span></p>
<p><a href="http://www.rensenieuwenhuis.nl/r-project/influenceme/">Influence.ME</a> is an <a href="www.r-project.org">R</a> package that provides a collection of tools for detecting influential data in  mixed effects models. Testing for inï¬‚uence with mixed effects models is especially important in Social Science applications, for two reasons. First, models in the Social Sciences are frequently based on large numbers of individuals while the number of higher level units is often relatively small. Secondly, often the higher level units are remarkably similar, for instance in the case of neighboring countries. </p>
<p>useR! is a yearly user conference on exciting applications in R. The <a href="http://www2.agrocampus-ouest.fr/math/useR-2009/">useR! 2009 edition</a> will be held in Rennes, France. A great variety of packages, applications, and other developments relating to R will be discussed. I&#8217;ve visited the <a href="http://www.rensenieuwenhuis.nl/category/science/user-2008/">useR! 2008 conference</a> last year (in Dortmund, Germany), and found it a highly stimulating environment for those interested in exciting, practical applications in statistics using R. </p>
<p>Influence.ME is a project I&#8217;ve been working on for the last months, together with Ben Pelzer and Manfred te Grotenhuis. I&#8217;m still working &#8211; quite hard!- to iron out the last quirks, and we have tons of ideas for extending its functionality. I&#8217;m very happy to be able to present the result of this work to an R-minded audience this summer.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rensenieuwenhuis.nl/user-2009-acceptance-presenting-influenceme/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>R-Sessions 32: Forward.lmer: Basic stepwise function for mixed effects in R</title>
		<link>http://www.rensenieuwenhuis.nl/r-sessions-32/</link>
		<comments>http://www.rensenieuwenhuis.nl/r-sessions-32/#comments</comments>
		<pubDate>Fri, 13 Feb 2009 10:59:03 +0000</pubDate>
		<dc:creator><![CDATA[Rense Nieuwenhuis]]></dc:creator>
				<category><![CDATA[R-Project]]></category>
		<category><![CDATA[R-Sessions]]></category>
		<category><![CDATA[forward]]></category>
		<category><![CDATA[hierarchical]]></category>
		<category><![CDATA[lme4]]></category>
		<category><![CDATA[mixed effects models]]></category>
		<category><![CDATA[multilevel]]></category>
		<category><![CDATA[stepwise]]></category>

		<guid isPermaLink="false">http://www.rensenieuwenhuis.nl/?p=897</guid>
		<description><![CDATA[Intended to be a customized solution, it may have grown to be a little more. forward.lmer is an early installment of a full stepwise function for mixed effects regression models in R-Project. I may put ...]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.rensenieuwenhuis.nl/archive/category/r-project/r-sessions/"><img title="R-Sessions" src="http://i1.wp.com/www.rensenieuwenhuis.nl/wp-content/uploads/2008/07/r-sessions.jpg?w=470" alt="" data-recalc-dims="1" /></a> </p>
<p>Intended to be a customized solution, it may have grown to be a little more. forward.lmer is an early installment of a full stepwise function for mixed effects regression models in R-Project. I may put in some work to extend it, or I may not. Nevertheless, in a &#8216;forward sense of stepwise&#8217;, I think it can be pretty useful as it is. Also, it has an interesting take on the stepwise concept, I think.<br />
<!--adsense--><br />
<span id="more-897"></span></p>
<p>Most stepwise functions (as far as I know) take a base model and a bunch of variables, and then iteratively adds and/or subtracts some variables, according to various criteria, to come to the best fitting regression model. All very interesting, but how to deal with interaction variables? And moreover: most existing functions do not work with mixed effects models ((I use the term &#8216;mixed effects model&#8217; to describe this stepwise function to refer to what is often referred to as hierarchical or multilevel regression models, as well)). </p>
<p>Built around the lme4 package in R, forward.lmer provides a forward stepwise procedure to mixed effects models. Also, it allows the user not only to enter single variables to models, but also to do the same with blocks of variables. This opens up many options: users can add the complete interactions at once (i.e. both the original and the multiplicative terms), or add these consequetively. Future development will focus on additional selection criteria for interactions, such as the criterium that at least the multiplicative term needs to be statistically significant. </p>
<p>The user provides a starting model and a set of variables to evaluate. The procedure then updates the starting model with the addition of every single variable (or block of variables). The models are ordered based on their LogLikelihood (other criteria, i.e. BIC and AIC following soon), after which the best fitting model is evaluated against one of two criteria. The first criterium is that at least one of the added parameters is statistically significant. The other criterium is that the addition of the parameters together is statistically significant. </p>
<p>There are several parameters to be specified:</p>
<ul>
<li>start.model: The starting model the procedure starts with. This can be a null-model, or a model already containing several variables. All lmer-models (i.e. logistic, poisson, linear) are supported.</li>
<li>blocks: a vector of variable names (as character strings) to be added to a model. Several variables can a concatenated within the same character string, so that these are added as a block of variables, instead of a single variables at once.</li>
<li>max.iter: The maximum number of variables that are evaluated. If max.iter is reached, the procedure stops without adding more variables. </li>
<li>sig.level: This is the p-value against which it is tested whether the new model fits better than a base model. Either sig.level or zt needs to be specified, but not both at once.</li>
<li>zt: This is either the T or Z value that is used to test whether (at least) one of the added variables is statistically significant. T values are used for linear regression, Z values for binary response models.</li>
<li>print.log: Should a log be printed? The log contains information on which variables (and on which criteria) were added in each step.</li>
</ul>
<p>The forward.lmer function returns the best fitting model (according to the given criteria). Of course, one can use this resulting model as a starting model for a new stepwise procedure.</p>
<p><code><br />
forward.lmer <- function(<br />
	start.model, blocks,<br />
	max.iter=1, sig.level=FALSE,<br />
	zt=FALSE, print.log=TRUE)<br />
	{</p>
<p>	# forward.lmer: a function for stepwise regression using lmer mixed effects models<br />
	# Author: Rense Nieuwenhuis</p>
<p>	# Initialysing internal variables<br />
	log.step <- 0<br />
	log.LL <- log.p <- log.block <- zt.temp <- log.zt <- NA<br />
	model.basis <- start.model</p>
<p>	# Maximum number of iterations cannot exceed number of blocks<br />
	if (max.iter > length(blocks)) max.iter <- length(blocks)</p>
<p>	# Setting up the outer loop<br />
	for(i in 1:max.iter)<br />
		{</p>
<p>		models <- list()</p>
<p>		# Iteratively updating the model with addition of one block of variable(s)<br />
		# Also: extracting the loglikelihood of each estimated model<br />
		for(j in 1:length(blocks))<br />
			{<br />
			models[[j]] <- update(model.basis, as.formula(paste(". ~ . + ", blocks[j])))<br />
			}</p>
<p>		LL <- unlist(lapply(models, logLik))</p>
<p>		# Ordering the models based on their loglikelihood.<br />
		# Additional selection criteria apply<br />
		for (j in order(LL, decreasing=TRUE))<br />
			{</p>
<p>			##############<br />
			############## Selection based on ANOVA-test<br />
			##############</p>
<p>			if(sig.level != FALSE)<br />
				{<br />
				if(anova(model.basis, models[[j]])[2,7] < sig.level)<br />
					{</p>
<p>					model.basis <- models[[j]]</p>
<p>					# Writing the logs<br />
					log.step <- log.step + 1<br />
					log.block[log.step] <- blocks[j]<br />
					log.LL[log.step] <- as.numeric(logLik(model.basis))<br />
					log.p[log.step] <- anova(model.basis, models[[j]])[2,7]</p>
<p>					blocks <- blocks[-j]</p>
<p>					break<br />
					}<br />
				}</p>
<p>			##############<br />
			############## Selection based significance of added variable-block<br />
			##############	</p>
<p>			if(zt != FALSE)<br />
				{<br />
				b.model <- summary(models[[j]])@coefs<br />
				diff.par <- setdiff(rownames(b.model), rownames(summary(model.basis)@coefs))<br />
				if (length(diff.par)==0) break<br />
				sig.par <- FALSE</p>
<p>				for (k in 1:length(diff.par))<br />
					{<br />
					if(abs(b.model[which(rownames(b.model)==diff.par[k]),3]) > zt)<br />
						{<br />
						sig.par <- TRUE<br />
						zt.temp <- b.model[which(rownames(b.model)==diff.par[k]),3]<br />
						break<br />
						}<br />
					}					</p>
<p>				if(sig.par==TRUE)<br />
					{<br />
					model.basis <- models[[j]]</p>
<p>					# Writing the logs<br />
					log.step <- log.step + 1<br />
					log.block[log.step] <- blocks[j]<br />
					log.LL[log.step] <- as.numeric(logLik(model.basis))<br />
					log.zt[log.step] <- zt.temp<br />
					blocks <- blocks[-j]</p>
<p>					break<br />
					}<br />
				}<br />
			}<br />
		}</p>
<p>	## Create and print log<br />
	log.df <- data.frame(log.step=1:log.step, log.block, log.LL, log.p, log.zt)<br />
	if(print.log == TRUE) print(log.df, digits=4)</p>
<p>	## Return the 'best' fitting model<br />
	return(model.basis)<br />
	} </p>
<p></code></p>
<p>As always, you're invited to use this function, or to adapt it and use that. However, it is required to make mention of this function and its author. Additionally, since I intend to continue working on this function (perhaps even evolve it to a 'package' on CRAN), I would love to hear about any experiences in using it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rensenieuwenhuis.nl/r-sessions-32/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>R-Sessions 31: Combining lmer output in a single table (UPDATED)</title>
		<link>http://www.rensenieuwenhuis.nl/r-sessions-31-combining-lmer-output-in-a-single-table/</link>
		<comments>http://www.rensenieuwenhuis.nl/r-sessions-31-combining-lmer-output-in-a-single-table/#comments</comments>
		<pubDate>Thu, 05 Feb 2009 11:00:38 +0000</pubDate>
		<dc:creator><![CDATA[Rense Nieuwenhuis]]></dc:creator>
				<category><![CDATA[R-Project]]></category>
		<category><![CDATA[R-Sessions]]></category>
		<category><![CDATA[lme4]]></category>
		<category><![CDATA[lmer]]></category>
		<category><![CDATA[mixed effect models]]></category>

		<guid isPermaLink="false">http://www.rensenieuwenhuis.nl/?p=891</guid>
		<description><![CDATA[There are various ways of getting your output from R to your publication draft. Most of them are highly efficient, but unfortunately I couldn&#8217;t find a function that combines the output from several (lmer) models ...]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.rensenieuwenhuis.nl/archive/category/r-project/r-sessions/"><img title="R-Sessions" src="http://i1.wp.com/www.rensenieuwenhuis.nl/wp-content/uploads/2008/07/r-sessions.jpg?w=470" alt="" data-recalc-dims="1" /></a><br />
<!--adsense--></p>
<p>There are various ways of getting your output from R to your publication draft. Most of them are highly efficient, but unfortunately I couldn&#8217;t find a function that combines the output from several (lmer) models and presents it in a single table. lmer is the mixed effects model function from the lme4 package. So, I wrote a simple function that does exactly that.<br />
<span id="more-891"></span></p>
<p>Using it for a specific purpose, it is not a general function or something, but it can easily be adapted for use in other settings. Here it goes:</p>
<p><code><br />
require(lme4)<br />
require(mlmRev)<br />
require(lme4)<br />
require(mlmRev)</p>
<p>model.1 <- lmer(normexam ~ 1 + (1 | school), data=Exam)<br />
model.2 <- lmer(normexam ~ standLRT + (1 | school), data=Exam)<br />
model.3 <- lmer(normexam ~ standLRT + sex + (1 | school), data=Exam)<br />
model.4 <- lmer(normexam ~ standLRT + sex + schavg + (1 | school), data=Exam)</p>
<p>model.a <- lmer(use ~ 1 + (1 | district), family=binomial, data=Contraception)<br />
model.b <- lmer(use ~ livch + (1 | district), family=binomial, data=Contraception)<br />
model.c <- lmer(use ~ age + (1 | district), family=binomial, data=Contraception)<br />
model.d <- lmer(use ~ livch + age + (1 | district), family=binomial, data=Contraception)</p>
<p>m1 <- c(model.1, model.2, model.3, model.4)<br />
m2 <- c(model.a, model.b, model.c, model.d)</p>
<p>combine.output.lmer <- function(models, labels=FALSE)<br />
	{</p>
<p>	fix.coef <- lapply(models, function(x) summary(x)@coefs)<br />
	var.coef <- lapply(models, function(x) summary(x)@REmat)<br />
	n.par <- dim(summary(models[[1]])@coefs)[2]</p>
<p>	ifelse(labels==FALSE,<br />
		fix.labels <- colnames(summary(models[[1]])@coefs),<br />
		fix.labels <- labels)</p>
<p>	var.labels <- colnames(var.coef[[1]])</p>
<p>	# Creating table with fixed parameters<br />
	output.coefs <- data.frame(Row.names=row.names(fix.coef[[1]]))<br />
	for (i in 1:length(models))<br />
		{</p>
<p>		a <- fix.coef[[i]]<br />
		colnames(a) <- paste("Model", i, fix.labels)<br />
		output.coefs <- merge(output.coefs, a, by.x=1, by.y=0, all=T, sort=FALSE)</p>
<p>		}<br />
	output.coefs[,1] <- as.character(output.coefs[,1])<br />
	output.coefs[dim(output.coefs)[1]+2, 1] <- "Loglikelihood"<br />
	LL <- unlist(lapply(models, function(x) as.numeric(logLik(x))))<br />
	output.coefs[dim(output.coefs)[1], 1:length(models)*n.par-n.par+2] <- LL</p>
<p>	# Creating table with random parameters<br />
	output.vars <- data.frame(var.coef[[1]])[,1:2]<br />
	for (i in 1:length(models))<br />
		{</p>
<p>		a <- var.coef[[i]]<br />
		colnames(a) <- paste("Model", i, var.labels)<br />
		output.vars <- merge(output.vars, a, by.x=1:2, by.y=1:2, all=T, sort=FALSE)</p>
<p>		}</p>
<p>	# Combining output.coefs and output.vars<br />
	n.cols <- dim(output.coefs)[2]<br />
	n.coefs <- dim(output.coefs)[1]<br />
	n.vars <- dim(output.vars)[1]</p>
<p>	output <- matrix(ncol=n.cols +1 , nrow=n.vars+n.coefs+2)</p>
<p>	output[1:n.coefs, -2] <- as.matrix(output.coefs)<br />
	output[n.coefs+2, 1] <- "Variance Components"<br />
	output[(n.coefs+3) : (n.coefs+n.vars+2), 1:2] <- as.matrix(output.vars[,1:2])<br />
	output[<br />
		(n.coefs+3) : (n.coefs+n.vars+2),<br />
		which(rep(c(1,1,rep(0, n.par-2)),length(models))!=0)+2] <- as.matrix(output.vars[,c(-1,-2)])</p>
<p>	colnames(output) <- c("Parameter", "Random", colnames(output.coefs)[-1])</p>
<p>	return(output)<br />
	}</p>
<p>combined <- combine.output.lmer(m1)<br />
combined <- combine.output.lmer(m2)</p>
<p>combined <- combine.output.lmer(m1, labels=c("appel", "banaan", "grapefruit"))<br />
combined <- combine.output.lmer(m2, labels=c("appel", "peer", "banaan", "grapefruit"))</p>
<p>write.csv(combined, "combined.csv", na=" ")<br />
</code></p>
<p>In this example I estimate four mixed effects models, which are concatenated in a single object 'm'. The function itself is called 'combine.output.lmer', and is used on the object 'm'. The output is a data.frame with the variable names in the first column. Not-estimated parameters in models are indicated by 'NA' in their respective columns. By writing the 'combined'-object to an external file, the NA's are lost and the file can be read into other software, such as Open Office Spreadsheet or Excell. Use the xtable-package to get it in your latex document. </p>
<p>UPDATE<br />
I updated and improved the code somewhat, for I wasn't satisfied with the results. Now the code adapts to the number of parameters derived form the models' summary, allows to add your own names to the columns, and, most importantly, also reports the random slopes.</p>
<p>Please note: due to the internal matching procedure, errors may occur when the same variable is random 'within' more than one other variable. This is only the case when other variables are random within each nesting factor as well. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.rensenieuwenhuis.nl/r-sessions-31-combining-lmer-output-in-a-single-table/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>R-Sessions 30: Visualizing missing values</title>
		<link>http://www.rensenieuwenhuis.nl/r-sessions-30-visualizing-missing-values/</link>
		<comments>http://www.rensenieuwenhuis.nl/r-sessions-30-visualizing-missing-values/#comments</comments>
		<pubDate>Thu, 08 Jan 2009 10:00:39 +0000</pubDate>
		<dc:creator><![CDATA[Rense Nieuwenhuis]]></dc:creator>
				<category><![CDATA[R-Sessions]]></category>
		<category><![CDATA[GSS]]></category>
		<category><![CDATA[GSS cumulative file]]></category>
		<category><![CDATA[Missing values]]></category>
		<category><![CDATA[R-Project]]></category>

		<guid isPermaLink="false">http://www.rensenieuwenhuis.nl/?p=872</guid>
		<description><![CDATA[<a href="http://www.rensenieuwenhuis.nl/archive/category/r-project/r-sessions/"><img title="R-Sessions" src="http://www.rensenieuwenhuis.nl/wp-content/uploads/2008/07/r-sessions.jpg" alt="" width="470" /></a> 

It always takes some time to get a grip on a new dataset, especially large ones. The code-books are often as indispensable as they are massive, and not always as clear as one would want. Routings, and resulting and strange patterns of missing values are at times difficult to find.

I found a nice way to plot missing values, using R. Basically, I thought it would be nice to calculate the percentage of missings on each variable, and do so for each year represented in the data. These numbers could be visualized using a levelplot(), which resulted in the graph below.]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.rensenieuwenhuis.nl/archive/category/r-project/r-sessions/"><img title="R-Sessions" src="http://i0.wp.com/www.rensenieuwenhuis.nl/wp-content/uploads/2008/07/r-sessions.jpg?w=470" alt="" data-recalc-dims="1" /></a> </p>
<p>It always takes some time to get a grip on a new dataset, especially large ones. The code-books are often as indispensable as they are massive, and not always as clear as one would want. Routings, and resulting and strange patterns of missing values are at times difficult to find.</p>
<p>I found a nice way to plot missing values, using R. Basically, I thought it would be nice to calculate the percentage of missings on each variable, and do so for each year represented in the data. These numbers could be visualized using a levelplot(), which resulted in the graph below.</p>
<p><a href="http://i1.wp.com/www.rensenieuwenhuis.nl/wp-content/uploads/2009/01/missings.jpg"><img src="http://i0.wp.com/www.rensenieuwenhuis.nl/wp-content/uploads/2009/01/missings.jpg?w=450" alt="missings" title="missings" class="alignnone size-medium wp-image-873" data-recalc-dims="1" /></a><br />
<span id="more-872"></span><br />
In this example I used a small subset of variables from the <a href="http://www.icpsr.umich.edu/cocoon/ICPSR/STUDY/04697.xml">cumulative file of the General Social Survey</a>, which is freely available from the web. I used this syntax:</p>
<p><code><br />
testing.NA <- matrix(ncol=26, nrow=21)<br />
for (i in 1:dim(GSS)[2])<br />
	{<br />
	testing.NA[i,] <- tapply(GSS[[i]], GSS$year, function(x) sum(is.na(x)) / length(x))<br />
	}</p>
<p>dimnames(testing.NA) <- list(<br />
	names(GSS),<br />
	sort(unique(GSS$year)))</p>
<p>library(lattice)</p>
<p>levelplot(testing.NA,<br />
	scales=list(x=list(rot=90)),<br />
	main="Percentage missing values on variables in GSS",<br />
	xlab="Variable",<br />
	ylab="Year")<br />
</code></p>
<p>First, I defined the testing.NA matrix, using the number of years and variables. Then, in a loop, I calculate the percentage missing values, basically using is.na() and length(). I assign dimnames to the matrix and use the levelplot() function from the lattice-library to plot the matrix. That's it, easy does it.</p>
<p>But: does it help? I think it does. Of course, all this information can be gained from the code-book, and needs to be verified. However, it does give us some immediate notes on the availability of these variables. For instance, we see that in the first few years, the abany variable is missing, whereas other variables on abortion don't. When creating scales this needs to be taken into account, not to lose the complete data on the first few years. The speduc-variable (spouse's educational level) has a high number of missings, as does the denom variable. This, however, makes sense: not everybody has a spouse and the denom-variable only applies to protestants. Finally, this graph gives some pointers on a change in survey-strategy from 1988 onwards regarding the items on induced abortion. The percentage missing values increased sharply at that point, and does so for all abortion-related variables. </p>
<p>This graph does not tell what exactly happened, but does provides nice pointers on what to look for when reading the code-book. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.rensenieuwenhuis.nl/r-sessions-30-visualizing-missing-values/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>R-Sessions 29: Running R-Project twice on Apple Mac OS X</title>
		<link>http://www.rensenieuwenhuis.nl/r-sessions-29-running-r-project-twice-on-apple-mac-os-x/</link>
		<comments>http://www.rensenieuwenhuis.nl/r-sessions-29-running-r-project-twice-on-apple-mac-os-x/#comments</comments>
		<pubDate>Mon, 24 Nov 2008 10:00:40 +0000</pubDate>
		<dc:creator><![CDATA[Rense Nieuwenhuis]]></dc:creator>
				<category><![CDATA[R-Project]]></category>
		<category><![CDATA[R-Sessions]]></category>
		<category><![CDATA[computing]]></category>
		<category><![CDATA[statistical software]]></category>
		<category><![CDATA[twice]]></category>

		<guid isPermaLink="false">http://www.rensenieuwenhuis.nl/?p=835</guid>
		<description><![CDATA[Working with statistics can be quite time consuming. As anyone working with relatively advanced models and large amounts of data knows, especially the waiting can be excruciating. Your statistical software is locked up while crunching those numbers, while you'd actually prefer to run some minor procedures, such as post-estimations, testing some loops, or simply displaying the output of a previously estimated model. With Apple's Mac OS X you now can run R-Project twice, making the most of your dual core processor. ]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.rensenieuwenhuis.nl/archive/category/r-project/r-sessions/"><img title="R-Sessions" src="http://i0.wp.com/www.rensenieuwenhuis.nl/wp-content/uploads/2008/07/r-sessions.jpg?w=470" alt="" data-recalc-dims="1" /></a><br />
Working with statistics can be quite time consuming. As anyone working with relatively advanced models and large amounts of data knows, especially the waiting can be excruciating. Your statistical software is locked up while crunching those numbers, while you&#8217;d actually prefer to run some minor procedures, such as post-estimations, testing some loops, or simply displaying the output of a previously estimated model. With Apple&#8217;s Mac OS X you now can run R-Project twice, making the most of your dual core processor. <span id="more-835"></span></p>
<p>The procedure is very easy, and it works like a charm. Mind though that, obviously, it drains your computers&#8217; resources heavily, so performance of each instance of R-Project decreases slightly at least. For that to change, we would need dual-hard disk laptops, and dual-RAM laptops and the such. Dual laptop-laptops basically.</p>
<p><img src="http://i1.wp.com/www.rensenieuwenhuis.nl/wp-content/uploads/2008/11/r-copy.jpg?resize=500%2C287" alt="" title="Duplicating R-Project to run it twice" class="aligncenter size-full wp-image-837" data-recalc-dims="1" /></p>
<p>Back to running R-Project twice. Just start R-Project as usual. Then go to your applications folder and secondary-click on the R-Project app. Select &#8216;duplicate&#8217;, and there you are: an app named R copy emerges. Start this as usual and start working. </p>
<p>in the image below you see two instances of R-Project running. The first is working on a heavy-weight function that results in some output every hour or so and runs 96 times. In other words: it takes ages. However, it stores the output in an external file, and since each little bit of output needs some post-estimation before being interpreted, I can use the second instance to load that data and examine it (not shown).</p>
<p><a href="http://i1.wp.com/www.rensenieuwenhuis.nl/wp-content/uploads/2008/11/2-r.jpg"><img src="http://i2.wp.com/www.rensenieuwenhuis.nl/wp-content/uploads/2008/11/2-r.jpg?w=470" alt="" title="R-Project running twice on Mac OS X" class="alignnone size-medium wp-image-836" data-recalc-dims="1" /></a></p>
<p>Although you don&#8217;t need to re-install packages, the only thing I did not (yet) find out how to do is to share resources between these two instances of R-Project. Being able to share variables, models, and such would be great. Ideas anyone? </p>
<p><!--adsense--></p>
<p>&#8211; &#8211; &#8212; &#8212; &#8212;&#8211; &#8212;&#8212;&#8211;</p>
<ul>
<li><strong><a href="http://www.rensenieuwenhuis.nl/r-forum/topic/r-sessions-29-running-r-project-twice-on-apple-mac-os-x">Discuss this article and pose additional questions in the R-Sessions Forum</a></strong></li>
</ul>
<p>&#8211; &#8211; &#8212; &#8212; &#8212;&#8211; &#8212;&#8212;&#8211;<br />
<a href="http://www.rensenieuwenhuis.nl/archive/category/r-project/r-sessions/">R-Sessions</a> is a collection of manual chapters for R-Project, which are maintained on <a href="www.rensenieuwenhuis.nl">Curving Normality</a>. All posts are linked to the chapters from the R-Project manual on this site. The manual is free to use, for it is paid by the advertisements, but please refer to it in your work inspired by it. Feedback and topic requests are highly appreciated.<br />
&#8212;&#8212;&#8211; &#8212;&#8211; &#8212; &#8212; &#8211; &#8211;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rensenieuwenhuis.nl/r-sessions-29-running-r-project-twice-on-apple-mac-os-x/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
