source("https://gist.githubusercontent.com/aghaynes/7fd9d1c52b1cc4cb566a/raw/")

I changed a couple of the function names though and added an argument to specify_decimal so that you can provide a minimum value. The function arguments are now:

specify_decimal(x, k, min)

npropconv(n, perc, dp.n, dp.perc)

valciconv(val, CIlower, CIupper, dp.val, dp.CI)

minval(values, min)

minval is the new name for minp.

Hope thats useful…

]]>

specify_decimal <- function(x, dp){ out <- format(round(x, dp), nsmall=dp) out <- gsub(" ", "", out) out } npropconv <- function(n, perc, dp.n=0, dp.perc=1){ n <- specify_decimal(n, dp.n) perc <- specify_decimal(perc, dp.perc) OUT <- paste(n, " (", perc, ")", sep="") OUT }

valciconv <- function(val, CIlower, CIupper, dp.val=2, dp.CI=2){ val <- specify_decimal(val, dp.val) CIlower <- specify_decimal(CIlower, dp.CI) CIupper <- specify_decimal(CIupper, dp.CI) OUT <- paste(val, " (", CIlower, " - ", CIupper, ")", sep="") OUT }

minp <- function(pvalues, min=0.001){ pv <- as.numeric(pvalues) for(i in 1:length(pv)){ if(pv[i] < min) pvalues[i] <- paste("<", min) } pvalues }

And heres what they do…

> specify_decimal(x=c(0.01, 0.000001), dp=3) [1] "0.010" "0.000" > npropconv(n=7, perc=5, dp.n=0, dp.perc=1) [1] "7 (5.0)" > valciconv(val=7, CIlower=3, CIupper=9, dp.val=2, dp.CI=2) [1] "7.00 (3.00 - 9.00)" > minp(0.00002, min=0.05) [1] "< 0.05" > minp(0.00002, min=0.001) [1] "< 0.001"

Any arguments with beginning with dp specify the number of decimal places for the relevant parameter (i.e. dp.n sets the decimal places for the n parameter). It would make sence to follow specify_decimal with a call to minp to deal with the 0s:

> decim <- specify_decimal(c(0.01, 0.000001),3) > minp(decim, 0.001) [1] "0.010" "< 0.001"

Incidently, although I had p values in mind for minp, it can of course be used with any value at all!

Hope someone finds them handy!!!

]]>

UPDATE: These confidence intervals, together with many more, have actually been programmed in the binom package (binom.confint). Use them instead. For Stata users, CIs from proportions are available with the ci program.

Firstly I give you the Simple Asymtotic Method:

simpasym <- function(n, p, z=1.96, cc=TRUE){ out <- list() if(cc){ out$lb <- p - z*sqrt((p*(1-p))/n) - 0.5/n out$ub <- p + z*sqrt((p*(1-p))/n) + 0.5/n } else { out$lb <- p - z*sqrt((p*(1-p))/n) out$ub <- p + z*sqrt((p*(1-p))/n) } out }

which can be used thusly….

```
simpasym(n=30, p=0.3, z=1.96, cc=TRUE)
$lb
[1] 0.119348
$ub
[1] 0.480652
```

Where n is the sample size, p is the proportion, z is the z value for the % interval (i.e. 1.96 provides the 95% CI) and cc is whether a continuity correction should be applied. The returned results are the lower boundary ($lb) and the upper boundary ($ub).

The second method is the Score method and is define as follows:

scoreint <- function(n, p, z=1.96, cc=TRUE){ out <- list() q <- 1-p zsq <- z^2 denom <- (2*(n+zsq)) if(cc){ numl <- (2*n*p)+zsq-1-(z*sqrt(zsq-2-(1/n)+4*p*((n*q)+1))) numu <- (2*n*p)+zsq+1+(z*sqrt(zsq+2-(1/n)+4*p*((n*q)-1))) out$lb <- numl/denom out$ub <- numu/denom if(p==1) out$ub <- 1 if(p==0) out$lb <- 0 } else { out$lb <- ((2*n*p)+zsq-(z*sqrt(zsq+(4*n*p*q))))/denom out$ub <- ((2*n*p)+zsq+(z*sqrt(zsq+(4*n*p*q))))/denom } out }

and is used in the same manner as simpasym…

```
scoreint(n=30, p=0.3, z=1.96, cc=TRUE)
$lb
[1] 0.1541262
$ub
[1] 0.4955824
```

These formulae (and a couple of others) are discussed in Newcombe, R. G. (1998) who suggests that the score method should be more frequently available in statistical software packages.

Hope that help someone!!!

Reference:

Newcombe, R. G. (1998) Two-sided confidence intervals for the single proportion: comparison of seven methods. Statist. Med., 17: 857-872. doi: 10.1002/(SICI)1097-0258(19980430)17:8<857::AID-SIM777>3.0.C

]]>

\begin{figure} \includegraphics[trim= left bottom right top, clip=true]{file.jpeg} \end{figure}

This method doesnt always work however. For instance, if you use xelatex, like me.

There is a way around this though – the *adjustbox* package. It includes, among other handy functions, the function *\adjincludegraphics* which does the trick. One of the best things about it is that it even seems to use the same coding, so no need to change all of the argument names…just add adj to the function call.

\usepackage{adjustbox} ... \begin{figure} \adjincludegraphics[trim= left bottom right top, clip=true]{file.jpeg} \end{figure}

I think that its even possible to have it export its settings so that *\includegraphics* is recoded to do the same as *\adjincludegraphics*. I’ve not yet tried that though. (It should be just a case of using *\usepackage[Export]{adjustbox}* instead of *\usepackage{adjustbox}*)

]]>

\setcounter{figure}{0} \makeatletter \renewcommand{\thefigure}{S\arabic{figure}}

So we have reset the figure counter to zero and have told LaTeX to redefine \thefigure to have an S before the figure number. Easy huh!?!

For documents with chapters it is almost as easy to do:

\setcounter{figure}{0} \makeatletter \renewcommand{\thefigure}{\arabic{chapter}.S\arabic{figure}}

To change tables instead of figures, just swap any instances of figure for table. Piece of cake.

Do remember to reset it though, otherwise your next chapter continues to use the changes you’ve made!!

]]>

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…

]]>

\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 **def**ine a command called **\fig** which if defined as “**if** the **next** **char**acter is a **[** do **\@with** else do **\@without**“.** **\@with and \@without are then defined separately.

]]>

Heres a little macro which does the job

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

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

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

]]>

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

]]>

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!!

]]>