Yang Feng's Blog

A blog about R, Latex and many other stuff.

How to source all the .r files in a folder

Posted by yangfeng on October 19, 2009

The regular expression are used to find a match with the ending characters of .R, .r, .S, .s, .Q, .q.

You can easily change this code to match any type of input files.

## If you want to source() a bunch of files, something like## the following may be useful:
sourceDir <- function(path, trace = TRUE, …) {
for (nm in list.files(path, pattern = “\\.[RrSsQq]$”)) {
if(trace) cat(nm,”:”)
source(file.path(path, nm), …)
if(trace) cat(“\n”)
}
}

Posted in R Tips | Leave a Comment »

Car location identification problem

Posted by yangfeng on September 16, 2009

We have a infinity road and a car whose initial position(at time t=0) is
some integer coordinate which you don’t know.

The car is running at a fixed velocity of some other integer per second
which you do not know neither.

You even do not know the car is running towards +infinity or -infinity.

What you can only do is at a single integral time point you can make a query
like this: Is the car at point x now? (you can choose the integer x).

You can do query only once at a time and will be given the answer “yes” or “
no” based on the truth.

Design a query strategy such that you can guarantee you will get a “yes”
answer in finite time.
################ANSWER############################

note the set of integer pairs (A,B) is countable, so pick some arbitrary one-to-one mapping from (A,B) to natural numbers N (basically arranging the pair in order):

( A[n], B[n] ) <-> n

one choice is:

(0,0);
(0,1), (1,0);
(0,-1), (1,1), (-1,0);
(0,2), (1,-1), (-1,1), (2,0);
(0,-2), (1,2), (-1,-1), (2,1), (-2,0);

(same spirit as the “Cantor pairing”, see http://en.wikipedia.org/wiki/Cantor_pairing_function#Cantor_pairing_function)

then at time n, pick x = A[n] + B[n] * n

Posted in Tricky / Interesting Problems | Leave a Comment »

Reference sheet for natbib usage

Posted by yangfeng on September 14, 2009

Français
Back
English
Reference sheet for natbib usage
(Describing version 7.0b from 2002/02/27)

For a more detailed description of the natbib package, LATEX the source file natbib.dtx.

Overview

The natbib package is a reimplementation of the LATEX \cite command, to work with both author-year and numerical citations. It is compatible with the standard bibliographic style files, such as plain.bst, as well as with those for harvard, apalike, chicago, astron, authordate.

Loading

Load with \usepackage[options]{natbib}. See list of options at the end.

Replacement bibliography styles

I provide three new .bst files to replace the standard LATEX numerical ones:

plainnat.bst         abbrvnat.bst         unsrtnat.bst

Basic commands

The natbib package has two basic citation commands, \citet and \citep for textual and parenthetical citations, respectively. There also exist the starred versions \citet* and \citep* that print the full author list, and not just the abbreviated one. All of these may take one or two optional arguments to add some text before and after the citation.

\citet{jon90} –> Jones et al. (1990)
\citet[chap. 2]{jon90} –> Jones et al. (1990, chap. 2)
\citep{jon90} –> (Jones et al., 1990)
\citep[chap. 2]{jon90} –> (Jones et al., 1990, chap. 2)
\citep[see][]{jon90} –> (see Jones et al., 1990)
\citep[see][chap. 2]{jon90} –> (see Jones et al., 1990, chap. 2)
\citet*{jon90} –> Jones, Baker, and Williams (1990)
\citep*{jon90} –> (Jones, Baker, and Williams, 1990)

Multiple citations

Multiple citations may be made by including more than one citation key in the \cite command argument.

\citet{jon90,jam91} –> Jones et al. (1990); James et al. (1991)
\citep{jon90,jam91} –> (Jones et al., 1990; James et al. 1991)
\citep{jon90,jon91} –> (Jones et al., 1990, 1991)
\citep{jon90a,jon90b} –> (Jones et al., 1990a,b)

Numerical mode

These examples are for author-year citation mode. In numerical mode, the results are different.

\citet{jon90} –> Jones et al. [21]
\citet[chap. 2]{jon90} –> Jones et al. [21, chap. 2]
\citep{jon90} –> [21]
\citep[chap. 2]{jon90} –> [21, chap. 2]
\citep[see][]{jon90} –> [see 21]
\citep[see][chap. 2]{jon90} –> [see 21, chap. 2]
\citep{jon90a,jon90b} –> [21, 32]

Suppressed parentheses

As an alternative form of citation, \citealt is the same as \citet but without parentheses. Similarly, \citealp is \citep without parentheses. Multiple references, notes, and the starred variants also exist.

\citealt{jon90} –> Jones et al. 1990
\citealt*{jon90} –> Jones, Baker, and Williams 1990
\citealp{jon90} –> Jones et al., 1990
\citealp*{jon90} –> Jones, Baker, and Williams, 1990
\citealp{jon90,jam91} –> Jones et al., 1990; James et al., 1991
\citealp[pg. 32]{jon90} –> Jones et al., 1990, pg. 32
\citetext{priv. comm.} –> (priv. comm.)

The \citetext command allows arbitrary text to be placed in the current citation parentheses. This may be used in combination with \citealp.

Partial citations

In author-year schemes, it is sometimes desirable to be able to refer to the authors without the year, or vice versa. This is provided with the extra commands

\citeauthor{jon90} –> Jones et al.
\citeauthor*{jon90} –> Jones, Baker, and Williams
\citeyear{jon90} –> 1990
\citeyearpar{jon90} –> (1990)

Forcing upper cased names

If the first author’s name contains a von part, such as “della Robbia”, then \citet{dRob98} produces “della Robbia (1998)”, even at the beginning of a sentence. One can force the first letter to be in upper case with the command \Citet instead. Other upper case commands also exist.

when \citet{dRob98} –> della Robbia (1998)
then \Citet{dRob98} –> Della Robbia (1998)
\Citep{dRob98} –> (Della Robbia, 1998)
\Citealt{dRob98} –> Della Robbia 1998
\Citealp{dRob98} –> Della Robbia, 1998
\Citeauthor{dRob98} –> Della Robbia

These commands also exist in starred versions for full author names.

Citation aliasing

Sometimes one wants to refer to a reference with a special designation, rather than by the authors, i.e. as Paper I, Paper II. Such aliases can be defined and used, textual and/or parenthetical with:

\defcitealias{jon90}{Paper I}
\citetalias{jon90} -> Paper I
\citepalias{jon90} -> (Paper I)

These citation commands function much like \citet and \citep: they may take multiple keys in the argument, may contain notes, and are marked as hyperlinks.

Selecting citation style and punctuation

Use the command \bibpunct with one optional and 6 mandatory arguments:

  1. the opening bracket symbol, default = (
  2. the closing bracket symbol, default = )
  3. the punctuation between multiple citations, default = ;
  4. the letter `n’ for numerical style, or `s’ for numerical superscript style, any other letter for author-year, default = author-year;
  5. the punctuation that comes between the author names and the year
  6. the punctuation that comes between years or numbers when common author lists are suppressed (default = ,);

The optional argument is the character preceding a post-note, default is a comma plus space. In redefining this character, one must include a space if one is wanted.

Example 1, \bibpunct{[}{]}{,}{a}{}{;} changes the output of

\citepjon90,jon91,jam92

into [Jones et al. 1990; 1991, James et al. 1992].Example 2, \bibpunct[; ]{(}{)}{,}{a}{}{;} changes the output of

\citep[and references therein]{jon90}

into (Jones et al. 1990; and references therein).

Other formatting options

  • Redefine \bibsection to the desired sectioning command for introducing the list of references. This is normally \section* or \chapter*.
  • Define \bibpreamble to be any text that is to be printed after the heading but before the actual list of references.
  • Define \bibfont to be a font declaration, e.g. to apply to the list of references.
  • Define \citenumfont to be a font declaration or command like \itshape or \textit.
  • Redefine \bibnumfmt as a command with an argument to format the numbers in the list of references. The default definition is [#1].
  • The indentation after the first line of each reference is given by \bibhang; change this with the \setlength command.
  • The vertical spacing between references is set by \bibsep; change this with the \setlength command.

Automatic indexing of citations

If one wishes to have the citations entered in the .idx indexing file, it is only necessary to issue \citeindextrue at any point in the document. All following \cite commands, of all variations, then insert the corresponding entry to that file. With \citeindexfalse, these entries will no longer be made.

Use with chapterbib package

The natbib package is compatible with the chapterbib package which makes it possible to have several bibliographies in one document.

The package makes use of the \include command, and each \included file has its own bibliography.

The order in which the chapterbib and natbib packages are loaded is unimportant.

The chapterbib package provides an option sectionbib that puts the bibliography in a \section* instead of \chapter*, something that makes sense if there is a bibliography in each chapter. This option will not work when natbib is also loaded; instead, add the option to natbib.

Every \included file must contain its own \bibliography command where the bibliography is to appear. The database files listed as arguments to this command can be different in each file, of course. However, what is not so obvious, is that each file must also contain a \bibliographystyle command, preferably with the same style argument.

Sorting and compressing citations

Do not use the \cite package with natbib; rather use one of the options sort or sort&compress.These also work with author-year citations, making multiple citations appear in their order in the reference list.

Long author list on first citation

Use option longnamesfirst to have first citation automatically give the full list of authors.Suppress this for certain citations with \shortcites{key-list}, given before the first citation.

Local configuration

Any local recoding or definitions can be put in natbib.cfg which is read in after the main package file.

Options that can be added to \usepackage

  • round: (default) for round parentheses;
  • square: for square brackets;
  • curly: for curly braces;
  • angle: for angle brackets;
  • colon: (default) to separate multiple citations with colons;
  • comma: to use commas as separaters;
  • authoryear: (default) for author-year citations;
  • numbers: for numerical citations;
  • super: for superscripted numerical citations, as in Nature;
  • sort: orders multiple citations into the sequence in which they appear in the list of references;
  • sort&compress: as sort but in addition multiple numerical citations are compressed if possible (as 3-6, 15);
  • longnamesfirst: makes the first citation of any reference the equivalent of the starred variant (full author list) and subsequent citations normal (abbreviated list);
  • sectionbib: redefines \thebibliography to issue \section* instead of \chapter*; valid only for classes with a \chapter command; to be used with the chapterbib package;
  • nonamebreak: keeps all the authors’ names in a citation on one line; causes overfull hboxes but helps with some hyperref problems.

About this document …

This document was generated using the LaTeX2HTML translator Version 200 2-1 (1.68), © 1993, 1994, 1995, 1996, Nikos Drakos, Computer Based Learning Unit, University of Leeds, © 1997, 1998, 1999, Ross Moore, Mathematics Department, Macquarie University, Sydney.

The command line arguments were: latex2html -split 0 -no_navigation -t 'Natbib reference sheet' natnote s.tex

Original file: natnotes.tex, © 1993-2002 Patrick W Daly, Max-Planck-Institut fr Aeronomie, D-37191 Katlenburg-Lindau

The original LATEX file was modified by S. Merkel, Institute for Solid State Physics, Univ. of Tokyo, on 12/26/2002 to be converted to HTML properly.

The resulting HTML file was modified by S. Merkel as well.

© Email
Home > Latex > natbib.php

Posted in Latex Tips | 2 Comments »

Two simple examples using the LaTeX “ifthen” package

Posted by yangfeng on September 12, 2009

Here is a sample Latex file where I’m experimenting with the Latex
“ifthen” package (ifthen.sty). These are simple examples where I’m
playing with the if/then decision making capability in Latex. These
two examples are pretty easy, but make a nice introduction to the
“ifthen” package.

Without any further ado, here is the sample Latex code:

\documentclass[a4paper,11pt]{article}
\author{Al Alexander}
\title{}

\usepackage{ifthen}

\begin{document}

%—————————————–
% (1) a simple test to get started.
%—————————————–
\newcommand{\printTrueOrFalse}[1]
{
\ifthenelse{\equal{#1}{true}}{TRUE}{}
\ifthenelse{\equal{#1}{false}}{FALSE}{}
}

\printTrueOrFalse{true}

%—————————————–
% (2) print the day of the week
%—————————————–
\newcommand{\dayOfWeek}[1]
{
\ifthenelse{\equal{#1}{0}}{Sunday}{}
\ifthenelse{\equal{#1}{1}}{Monday}{}
\ifthenelse{\equal{#1}{2}}{Tuesday}{}
\ifthenelse{\equal{#1}{3}}{Wednesday}{}
\ifthenelse{\equal{#1}{4}}{Thursday}{}
\ifthenelse{\equal{#1}{5}}{Friday}{}
\ifthenelse{\equal{#1}{6}}{Saturday}{}
}

\dayOfWeek{0} \dayOfWeek{1} \dayOfWeek{2} \dayOfWeek{3}
\dayOfWeek{4} \dayOfWeek{5} \dayOfWeek{6}

\end{document}

The following weekday macro uses the ifthen package. Note the use of ‘%’ characters to suppress spaces in the final output. weekday{3} should print out Wednesday.

\usepackage{ifthen}
\newcommand{\weekday}[1]%
{%
\ifthenelse{\equal{#1}{1}}{Monday}{}%
\ifthenelse{\equal{#1}{2}}{Tuesday}{}%
\ifthenelse{\equal{#1}{3}}{Wednesday}{}%
\ifthenelse{\equal{#1}{4}}{Thursday}{}%
\ifthenelse{\equal{#1}{5}}{Friday}{}%
\ifthenelse{\equal{#1}{6}}{Saturday}{}%
\ifthenelse{\equal{#1}{7}}{Sunday}{}%
}

\weekday{3}

Posted in Latex Tips | Leave a Comment »

My first R package “SIS” uploaded to CRAN

Posted by yangfeng on September 4, 2009

SIS: Sure Independence Screening

(Iterative) Sure Independence Screening for Generalized Linear Models and Cox’s Proportional Hazard’s Models

Version: 0.2
Depends: survival, R (≥ 2.0)
Published: 2009-08-19
Author: Jianqing Fan, Yang Feng, Richard Samworth, Yichao Wu
Maintainer: Yang Feng <yangfeng at princeton.edu>

Posted in R Package | 2 Comments »

Use “save.image” inside a function?

Posted by yangfeng on September 3, 2009

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Jan 9 17:43:32 CET 2008


save.image() saves the global workspace: it does not save the environments
of executing functions.  If you want to save the latter, use the 'envir'
argument of save().

On Tue, 8 Jan 2008, Hua Li wrote:

> Hi, All
>
> I'm trying to save the workspace as a Check Point for
> possible future come back. The "save.image" function
> works if the whole coding is in a main function:
>
> rm(list=ls())
> t = 1
> t = t+1
> print(paste("before",t))
> # [1] "before 2"
> save.image("tt.RData")
>
> t = t+1
> print(paste("after",t))
> # [1] "after 3"
>
> load("tt.RData")
> print(paste("check",t))
> # [1] "check 2"
>
> As you can see, the value right before I save the
> workspace is restored.
>
> However, if I run the saving inside a function, it
> seems that it restores to the vale outside the
> function rather than the one before the save command:
>
> rm(list=ls())
> test.fcn <- function(t=1){
>         t = t+1
>         print(paste("before",t))
>         save.image("tt.RData")
>
>         t = t+1
>         print(paste("after",t))
>
>         load("tt.RData")
>         print(paste("check",t))
>
> }
>
> t = 1
> test.fcn(t=t)
> # [1] "before 2"
> # [1] "after 3"
> # [1] "check 1"
>
> So it restores the value before the program enters the
> function (t=1), though what I really want is the value
> inside the function(t=2).
>
> Is there anything wrong with the way I use the
> "save.image" function? Or is there other command I can
> use to reach my goal?
>
> Thanks a lot!
>
> Hua
>
>
>
>      ____________________________________________________________________________________
> Never miss a thing.  Make Yahoo your home page.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

Posted in R Tips | Tagged: | Leave a Comment »

Roman numerals in Latex

Posted by yangfeng on September 3, 2009

Ok, we have to admit that it is not easy to input roman numerals in Latex. There is still a way.
In the preamble, i.e. before \begin{document}, define

\makeatletter
\newcommand{\rmnum}[1]{\romannumeral #1}
\newcommand{\Rmnum}[1]{\expandafter\@slowromancap\romannumeral #1@}
\makeatother

Then in the document part, \rmnum{num} is used to input the roman numeral of num, \Rmnum is for the uppercase. If you prefer to use other commands, just change the red part in the definition part.

If only lowercase roman numerals are occasionally used, \romannumeralnum can do it. In this approach, definition is not necessary.

Someone anonymous commented that “Nice, but it fails for N=4000 and above.” However, in my system, it is working for 4000 and above.

Posted in Latex Tips | Tagged: | Leave a Comment »

Making Directory / Folder Using R

Posted by yangfeng on September 3, 2009

dir.create()
Other useful functions:

File and Directory Manipulation

Description

These functions provide a low-level interface to the computer’s file system.

Usage

file.create(…, showWarnings = TRUE)
file.exists(…)
file.remove(…)
file.rename(from, to)
file.append(file1, file2)
file.copy(from, to, overwrite = FALSE)
file.symlink(from, to)
dir.create(path, showWarnings = TRUE, recursive = FALSE, mode = “0777″)
Sys.chmod(paths, mode = “0777″)
Sys.umask(mode = “0000″)

Posted in R Tips | Tagged: | Leave a Comment »

Including arguments in R CMD BATCH mode

Posted by yangfeng on September 3, 2009

When you have multiple computers or processors at your disposal and wish to run the same script with different arguments, use the following at the command line (here described for Linux; remove the linebreak, it is just there for display purposes):

$ R CMD BATCH --no-save --no-restore '--args a=1 b=c(2,5,6)'
test.R test.out &

Where test.R is the R script file you wish to run and test.out is a text file to include the screen output of the R terminal. A key point here is that each argument must have no spaces because –args is space delimited.

To include the variables listed in –args, adapt the following code from test.R:

##First read in the arguments listed at the command line
args=(commandArgs(TRUE))

##args is now a list of character vectors
## First check to see if arguments are passed.
## Then cycle through each element of the list and evaluate the expressions.
if(length(args)==0){
    print("No arguments supplied.")
    ##supply default values
    a = 1
    b = c(1,1,1)
}else{
    for(i in 1:length(args)){
         eval(parse(text=args[[i]]))
    }
}
print(a*2)
print(b*3)

This produces the following in test.out:

> print(a*2)
[1] 2
> print(b*3)
[1]  6 15 18

ANOTHER EXAMPLE of this:

The program below was called with the following command line

 /mnt/nfs/usr/local/bin/R --no-restore --no-save --no-readline
numI=2.5 meanN=3.4 stdN=4.4 tr  <  test.R > test.Rout

The options passed in to the program  when R runs in batch mode.
The problem with BATCH mode is that R mangles

any arguments that you try to pass in on the command line.
–no-restore causes R not to look for a default .RData file, you can change that to –restore if you have objects that R needs
–nosave causes R not to save the workspace upon exiting, you can change the behavior by using –save
–no-readline causes R to not load the package for command line editing

It takes the command line args and uses them to generate the random normals and also to name the object created and the name
of the file used to store the results. I was unable to store only the object we created so had to be satisfied with all the variables. Of
coures you could use the rm() command to remove objects you do not want to save.

You can either copy and paste the section below or you can source it into your program on the cluster using this:

source(“/mnt/nfs/r-source/commandLineArgs.R”)

############################################################################################################
## This is an example of using command line arguments to control the operation
## of the R program. Using this example it should be clear that much can be done
## to minimize the duplication of R programs that need only a few modifications
## then rerun several times

## list the arguments passed into the program.
commandArgs()

## Now to parse the command line options
## If there is an = sign in the arg then it is parsed and the
## first part of the arg is treated as the variable name with
## the second being the value of the argument.
## If there is no = sign then the arg value is assigned TRUE
##
## Note that we do character it integer conversion for variables whose name ends
## with an I (integer) and N (floating/integer). This is done of course because
## the command Line arguments come in as strings.

for (e in commandArgs()) {
ta = strsplit(e,”=”,fixed=TRUE)
if(! is.na(ta[[1]][2])) {
temp = ta[[1]][2]
if(substr(ta[[1]][1],nchar(ta[[1]][1]),nchar(ta[[1]][1])) == “I”) {
temp = as.integer(temp)
}
if(substr(ta[[1]][1],nchar(ta[[1]][1]),nchar(ta[[1]][1])) == “N”) {
temp = as.numeric(temp)
}
assign(ta[[1]][1],temp)
cat(“assigned “,ta[[1]][1],” the value of |”,temp,”|\n”)
} else {
assign(ta[[1]][1],TRUE)
cat(“assigned “,ta[[1]][1],” the value of TRUE\n”)
}
}

## generate the name we will use for the vector
name = paste(“normal”,numI,meanN,stdN,sep=”_”,collapse=”")

## generate the random normals
assign(name,rnorm(numI,meanN,stdN))

## generate the name we will use for the data file
file = paste(numI,meanN,stdN,”normal”,sep=”-”,collapse=”")
file = paste(file,”RData”,sep=”.”,collapse=”")
file

## save the data
save.image(file=file,ascii=TRUE)

Posted in R Tips | Tagged: | 1 Comment »

My first blog on wordpress

Posted by yangfeng on September 3, 2009

This site looks amazing with so many functions here.

Posted in Uncategorized | 1 Comment »