Wednesday, November 30, 2011

Truncated normal distribution with tnorm()

Truncated normal distributions can be worked with using the tnorm() function in the msm package.

To generate random numbers from a truncated normal distribution you can use the code
rtnorm(n, mean=0, sd=1, lower=-Inf, upper=Inf)

Wednesday, November 23, 2011

Tie-dying your charts: inserting backgrounds in R plots

Ben Bolker (author of a great R ecology text book) just put out a cool chunk of code that lets you add some fancy touches to charts. The overall goals was make R do some of the fancier - and admittedly pointless - things Excel does. This includes background images on plots, rounded corners on plots, and shadowed points.

Tuesday, November 22, 2011

Webscrapping baby names


This exercise in webscraping and plotting with ggplot, courtesy of the Command-Line Worldview blog, reminds me of the chapter in Freakanomics where these discuss trends in -- and trendy -- baby names.

Saturday, November 19, 2011

multitable: a new R package for complex ecological data

From ECOLOG:

From: Steve Walker
Date: November 15, 2011 4:19:44 PM GMT+01:00
Subject: multitable: a new R package for complex ecological data
Reply-To: Steve Walker


Studies in contemporary community ecology make use of data from a wide
variety of sources, including information on dispersal barriers, functional
traits, species interactions, and spatiotemporal and phylogenetic
autocorrelation. The recognition of the importance of these factors has led
to complex multiple-table data structures. For a simple example, when both
environmental variables and functional traits are used to help understand
variation in community composition, one obtains a multiple-table data set
that cannot easily be organized into a standard data table with rows and
columns representing replicates and variables. Nevertheless, most
statistical tools require such a single-table data format. Storing
multiple-table data in a single table often results in either large numbers
of meaningless missing values or storage of redundant information. Even in
the more flexible R computing environment, storing complex data in multiple
tables (i.e. data frames in R-speak) often results in long difficult-to-read
workflows. My frustration with these issues led me to develop the
multitable package (on CRAN). This package introduces a new data object
called a data.list, which organizes several data tables as a single R
object. The primary goal of multitable is to provide a more intuitive
framework for manipulating multiple-table data in R. As data.lists can be
coerced to data.frames, they can be used with all R functions that accept an
object that is coercible to a data.frame (e.g. lm; plot; lme; and many
more). In essence, data.list objects work much like familiar data.frame
objects, but handle a richer variety of data structures.

Here are the package websites:
http://multitable.r-forge.r-project.org/
http://cran.r-project.org/web/packages/multitable/index.html

A quick introduction to multitable is here:
http://cran.r-project.org/web/packages/multitable/vignettes/multitable.pdf

I am very interested in constructive feedback, as I plan to continue to
develop multitable to meet new challenges that arise in multiple-table data
analysis -- particularly in ecology.

Steve Walker

--
----------------------------------------------------
Steven C Walker
Postdoctoral researcher
Département de Sciences Biologiques
Université de Montréal
https://sites.google.com/site/stevencarlislewalker/
----------------------------------------------------

R-blog post: fine-tuning read.csv for NAs and other issues

The blog indiacrunchin has a helpful (if short and terse) post on how to deal with complicated data read-in issues.

The key bits of code:

"stringsAsFactors = FALSE mitigates character columns being turned into factor type"

Tell R what different NA values are in the dataset: eg, "na.strings = c("","999","—-","MISS"))"

Not sure exactly what he means here:
"function argument colClasses predefine the column types in the input file"
inData <- read.csv("inputData.csv",header=T,colClasses = c("numeric","character"))

sapply(inData,class)



Friday, November 18, 2011

Econometric Analysis of Carbon Markets

Econometric Analysis of Carbon Markets (Spring 2011) looks like an interesting book. It is not "R enabled" with code, though.

Wednesday, November 16, 2011

making simplewidgets and GUIs in R

These two posts discuss how to make simple, ad hoc GUIs within R to carry out basic tasks. This could be useful in a teaching environment.

fgui: Automatically Creating Widgets for Arguments of a Function

Simple Dummy R GUI Generator