Skip to content
Shravan Vasishth edited this page Jul 17, 2016 · 7 revisions

Welcome to the Statistics-lecture-notes-Potsdam wiki! These are notes I keep for obscure things that I have to do periodically, but forget how to do. This page may be useful to students in my courses.

R maintenance

-Always reinstall X11, R, RStudio after upgrading Mac OS X.

Obscure R commands

  • You want to change a subject's id to just a number, where the id has a number and letter or more than one letter

Example:

 library(gsubfn)
 #test:
 strapply("201tt", "[0-9]+")
 subj<-strapply(as.character(datcrit$subject),"[0-9]+")
 datcrit$subj<-as.numeric(unlist(subj))
  • You want to search a column of a data frame using regular expression search, and recode that column.

Example:

## binarize status:
status2<-ifelse(grepl("^dead",AHprostate$status) & 
         apply(AHprostate,1,
               function(x) 
                 grepl("^dead",x[4])),1,0)

Note to self: see MAS6012Ex3 for context.

Obscure Sweave stuff: Generating figures in multicol

In a multicol environment, if you want to generate Sweave figures without having them look outsized, make sure you specify:

 \usepackage{float}
 \setkeys{Gin}{width=0.25\textwidth}

And then do:

\begin{figure}[H]
\caption{Kaplan-Meier plot for Harrell example.}
<<fig=TRUE>>=
plot(harrell_fit)
@
\end{figure}

Obscure Sweave stuff: math environment in LaTeX tables

to-do

Stan stuff

In the data, if a continuous predictor is included, it has to be a vector. Use as.vector to enforce that. This is some weird R-specific problem.

Writing APA style papers using markdown

install.packages("rmarkdown")
devtools::install_github("crsh/papaja")
rmarkdown::draft("mymanuscript.Rmd", template = "apa6_pdf", package = "papaja")
rmarkdown::draft("mymanuscript.Rmd", template = "apa6_word", package = "papaja")

How to install JAGS on ubuntu machines

Execute the following commands (get the latest rjags version)

wget http://cran.r-project.org/src/contrib/Archive/rjags/rjags_3-4.tar.gz sudo R CMD INSTALL --configure-args='--with-jags-include=/usr/local/include/JAGS --with-jags-lib=/usr/local/lib{64} --enable-rpath' rjags_3-4.tar.gz