Skip to content

lme4 version 1 out soon!

Just a quick post to say that on Saturday Ben Bolker (on behalf of the lme4 authors) announced that lme4 version 1 will be released to CRAN on the 1st August.

https://stat.ethz.ch/pipermail/r-sig-mixed-models/2013q2/020503.html

So to anyone thats written code based on the current lme4 that uses the @ to access parts of the model object…its time to start rethinking those functions…

Advertisements

Boolean in LaTeX macros

Much like in the statistical environment R, it is possible to make custom functions (or “macros”) in LaTeX. This is done using the

\newcommand{commandname}[number_of_arguments]{definition}

command, where commandname and definition are replaced with a name (e.g. \mysubscript) and a definition, respectively, and number_of_arguments can be provided or not. For example see my previous post on subscripts.

What do you do if you want to control the behaviour of that macro? Say you want the command to do one of two things, print Fig or Figs for instance.

One way to accomplish this is to use the etoolbox package:

\usepackage{etoolbox}
\newcommand{\fig}[1]{
\ifblank{#1}{Fig.}{Figs.}
}
\begin{document}
\fig{} is singular, while \fig{1} is plural.
\end{document}

All this does is checks to see if there is an argument (#1) and prints its second argument if not (spaces or blank), or its third argument if there is something.

Hope this helps someone!

[UPDATE] A better method seems to be to use the following

\makeatletter
\def\fig{\@ifnextchar[{\@with}{\@without}}
\def\@with{Figs.}
\def\@without{Fig.}
\makeatother

otherwise it sometimes adds in extra space for no apparent (to me) reason. I’m not sure what the \makeatletter and \makeatother lines are about, but the other parts define a command called \fig which if defined as “if the next character is a [ do \@with else do \@without“.  \@with and \@without are then defined separately.

 

Subscript text in LaTeX

How to produce subscript text in LaTeX without entering “math mode” seems to be a common question on tex.stackexchange.com. There are various reasons for wanting to do so. I wanted to do it to avoid the italics it produces and have the subscript in the same font as the rest of the document.

Heres a little macro which does the job

\newcommand{\mysubscript}[1]{\raisebox{-.4ex}{\scriptsize #1}}

To write something simply write some\mysubscript{thing}.

You could always shorten the macro name to \myss{}.

updating R

Updating versions of R can be a pain in terms of getting all those packages you had on the old version onto the new version. Some people suggest copying the library folder from one installation to another and running update.packages(). Other people have other methods.

Here’s my simple method:

On the old installation make a vector of names of packages and write it to a file:

packs <- row.names(installed.packages())
write.table(packs, "packs.txt") # could also use dump or save or....

On the new version of R just read in the file and run install.packages:

packs <- read.table("packs.txt")
install.packages(as.character(packs$x)) # *

Select the mirror and wait a few minutes.

Simple!

* because this method ends up having packs as a table with 1 column (interpreted as a factor), we have to force R to think its a character

PDFs of R output – Sweave

One of the great things about R is that if you use scripts, you have a record of what you’ve done. If you copy the console output into the script then you also have a copy of that. Brilliant…until you forget or get lazy.

But what if you could make a pdf of your work? Using Sweave (S being then language that R is based on and weave being the verb), you can. But it does use LaTeX, so you have to learn a little bit of that too, as well as install it. Check out CTAN – the LaTeX equivalent of CRAN

If you use RStudio, this is really easy though. You open a new “R Sweave” file which already has most of what you need to begin – the bones of the LaTeX document:

\documentclass{article}

\begin{document}
\SweaveOpts{concordance=TRUE}


\end{document}

After the \SweaveOpts line you can start typing any description of the analysis youre doing. To start an “R chunk” (some code for R to interpret) you type

<<>>=

enter your R code and then type

@

to end it. So a short file might look like this:

\documentclass{article}
\usepackage[top=1in, bottom=1in, left=1in, right=1in]{geometry}
\usepackage[noae]{Sweave}
\title{Cars}
\begin{document}
\SweaveOpts{concordance=TRUE}
\maketitle

Open the cars dataset:

<<>>=
data(cars)
@

Show a summary of the dataset:

<<>>=
summary(cars)
@

Make a figure

<<fig=TRUE>>=
plot(cars[,1], cars[,2])
@

\end{document}

I added a couple of lines to the code to make it look a little different – the line with top, bottom etc just alters the margins using the geometry package. I also added \usepackage[noae]{Sweave} because ‘ symbols stop it working…the [noae] allows it to include them.For the figure I included the fig=TRUE between the << and >> to tell LaTeX to include the figure. There are other arguments to tell it to ignore the section or just return the result etc.

Once youve got that, you just hit the “Compile PDF” button on the RStudio tool bar.

If you dont use RStudio, you have to use the Sweave function

help("Sweave", package="utils")

I hope someone finds this helpful!!

Import/Export data to and from xlsx files

As Ive already written, getting data into R from your precious xlsx files is really handy. No need to clutter up your computer with txt or csv files. The previous post I wrote about the gdata package for importing data from xlsx files and was pointed to, among others, the xlsx package. xlsx seems to be a good package, easy to use and, importantly, fast. Its based on java, but it comes with all the relevant jar files in an accompanying package which installs on its own if you have the install dependencies setting to TRUE.

To read in with xlsx its the same as any other read function, you just need to tell it which sheet to read, by either name (sheetName argument) or number (sheetIndex):

library(xlsx)
dat <- read.xlsx("testfile.xlsx", sheetName="")

There are various other options that other packages for importing excel files dont seem to have such as rowIndex and colIndex for specifying which rows or columns you want to import. There is also a second function (read.xlsx2) which is apparently an order of magnitude faster for those particularly big files. Once youve selected the data and run the code, you can happily work with the data.

Writing to xlsx files might be useful too, for storage or data sharing with people who dont use R for instance. This is dead easy with xlsx!

If you want just a single dataframe in the workbook you simply do something like the following:

data(cars)
write.xlsx(cars, "cars_dataframe.xlsx")

To create a new file containing multiple dataframes from R, you first create the workbook, add sheets to that workbook and then add the dataframes to the sheets and save the workbook to whatever file you want.

cars <- createWorkbook()
cars1 <- createSheet(wb=cars, sheetName="Cars")
cars2 <- createSheet(wb=cars, sheetName="MTCars")
data(cars); data(mtcars)
addDataFrame(x=cars, sheet=cars1)
addDataFrame(x=mtcars, sheet=cars2)
saveWorkbook(cars, "Cars_datasets.xlsx")

By default this will add both column and row names, but this can be overridden using the row.names or col.names arguments in the addDataFrame function. You can also add the dataframes to a particular starting place in the sheet using the startRow and startCol arguments to the addDataFrame function.

Theres also some funky styling stuff you can do using the CellStyle, Fill, Alignment, Font and setCellStyle functions of the following sort (from ?CellStyle).

  wb <- createWorkbook()
  sheet <- createSheet(wb, "Sheet1")
  rows  <- createRow(sheet, rowIndex=1)    
  cell.1 <- createCell(rows, colIndex=1)[[1,1]]     
  setCellValue(cell.1, "Hello R!")
  cs <- CellStyle(wb) +
    Font(wb, heightInPoints=20, isBold=TRUE, isItalic=TRUE,
      name="Courier New", color="orange") + 
    Fill(backgroundColor="lavender", foregroundColor="lavender",
      pattern="SOLID_FOREGROUND") +
    Alignment(h="ALIGN_RIGHT")
   setCellStyle(cell.1, cs)  
# you need to save the workbook now if you want to see this art

Enjoy!

Mixed model R2 (UPDATED)

R2 is a useful tool for determining how strong the relationship between two variables is. Unfortunately, the definition of R2 for mixed effects models is difficult – do you include the random variable or just the fixed effects? Including just the fixed effects is essentially a standard linear model, while including the random effects could confuse some readers (you have a much higher R2). So which do you report? Nakagawa and Schielzeth (2013) say both! They even provide the formulae for their calculation.

Over on the sample(ECOLOGY) blog, an R function has been written for lme and lmer models and reports both R2 based on just the fixed effects (marginal R2) and that incorporating the random effects (conditional R2). Simply go to the link, copy the code into the console, hit enter and give the function a list of your models.

e.g. (from the sample(ECOLOGY) page)

Example
mod1=lmer(rnorm(100,5,10)~rnorm(100,20,100)+(1|rep(c("A","B"),50)))
mod2=lmer(rnorm(100,5,10)~rnorm(100,20,100)+rnorm(100,0.5,2)+(1|rep(c("A","B"),50)))
rsquared.lme(list(mod1,mod2))

A couple of warnings for lmer users though:

  1. you might have to tweak the code if you only have a single random effect for in lmer models. If you have multiple random effect levels or lme models, you should be fine
  2. the function is currently written for “mer” class models from lmer – the newer development versions of lmer use the “merMod” class and do away with @ as a slot

UPDATE: The function name has been changed to rsquared.glmm(). It now also handles models with poisson and binomial models. I think that lmer issues have been solved now too.

Nakagawa, S., and H. Schielzeth. 2013. A general and simple method for obtaining R2 from generalized linear mixed-effects models. Methods in Ecology and Evolution 4(2): 133-142.