Tuesday, September 17, 2013

Examining whether the order of scaling and log-transformation matters

The following question has come up as I continue to explore datasets related to my current PostDoc work. Given a dateset that requires log-transformation in order to fit a normal distribution, does it matter if I log-transform then scale the data versus scale then log-transform?
First, let's get some data that could be considered as needing log-transformation to meet the assumptions of normality. I downloaded the dataset of plant specific leaf area (SLA) from Reich 1999, as used and cited in Logan 2012, from the websit associated with Logan 2012 here.
## Require packages
require(ggplot2)
## Loading required package: ggplot2
require(reshape2)
## Loading required package: reshape2

## Read in the data
LeafArea <- read.csv("~/Google Drive/Professional/Short-R-Examples/reich.csv")

## Quick peek at the data
head(LeafArea)
##   LOCATION FUNCTION LEAFAREA
## 1   Newmex    Shrub    105.0
## 2   Newmex     Tree    124.0
## 3   Newmex     Tree     83.8
## 4   Newmex    Shrub     39.7
## 5   Newmex    Shrub     51.2
## 6   Newmex    Shrub     66.0

## Make a histogram of the LeafArea
qplot(LEAFAREA, data = LeafArea, geom = "histogram", binwidth = 10)
plot of chunk unnamed-chunk-1

## Now look at this same data, log10 transformed
qplot(log10(LEAFAREA), data = LeafArea, geom = "histogram")
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust
## this.
## Warning: position_stack requires constant width: output may be incorrect
plot of chunk unnamed-chunk-1
Ok, now lets see how things look when I scale, then log10 tranform, versus log10 transform, then scale.
## First scale the log transform
LeafArea$ScaleLog <- log10(scale(LeafArea$LEAFAREA))
## Warning: NaNs produced

## Next Lof then scale
LeafArea$LogScale <- scale(log10(LeafArea$LEAFAREA))

## Now plot these two
LeafArea_m <- melt(data = LeafArea, id.vars = c(1:3))
p <- ggplot(LeafArea_m, aes(x = value, colour = variable)) + geom_density()
p
## Warning: Removed 33 rows containing non-finite values (stat_density).
## Warning: Removed 1 rows containing non-finite values (stat_density).
plot of chunk unnamed-chunk-2
Defnitely different in the density plots. What about histograms?
h <- ggplot(LeafArea_m, aes(x = value, fill = variable)) + geom_histogram(position = "identity", 
    alpha = 0.4)
h
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust
## this.
plot of chunk unnamed-chunk-3

Let's try this one more time with simulate data.
# Generate some random data that needs a log transform
Sample_Data <- exp(rnorm(n = 500, mean = 0, sd = 1))
# Plot the data before transform
qplot(x = Sample_Data, geom = "histogram", binwidth = 1)
plot of chunk unnamed-chunk-4
# Plot the data after transform
qplot(x = log(Sample_Data), geom = "histogram")
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust
## this.
plot of chunk unnamed-chunk-4

# Now compare Scaling then log transform vs Log transform then scaling
Sample_Data_Test <- data.frame(ScaleLog = log(scale(Sample_Data)), LogScale = scale(log(Sample_Data)))
## Warning: NaNs produced

h <- ggplot(melt(Sample_Data_Test), aes(x = value, fill = variable)) + geom_histogram(position = "identity", 
    alpha = 0.4)
## Using as id variables
h
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust
## this.
plot of chunk unnamed-chunk-4
I'm not sure what to make of this. It's clear that the order of scaling and log transforming matters. However, I'm not sure which order makes more sense. It certainly seems that Log then Scale produces a nice centered distribution. Though the result seems a bit leptokurtic.
However, one observation that is very clear is that by scaling the data first, I ended up with many values equal to 0, which when then log transformed were assigned NA. This happened in both examples. This definitely leads me to think that the order to do thins is Log then Scale.

Friday, September 13, 2013

Hungry, hungry ... something?

I was out and about at various Long Island field sites this past Monday, collecting Glossy Buckthorn leaves for a colleague in Belgium, and I noticed something I've never seen before - rampant herbivory of Glossy Buckthorn. Here's an example of what it looks like.
I've been visiting various field sites to examine Glossy Buckthorn for about five years now, and I've observed signs of herbivory here and there, but never to this extent. What's more, I was hard pressed to find any leaves that were not chewed on.

I sent an email to a friend at UNH who has studied buckthorn many years now, and asked him if he's seen anything like this. No, he hasn't, but he's heard of this type of herbivory on buckthorn from a colleague in the Kittery, Maine area. So based on this lead, the suspect is the larva of the moth Magusa divaricata. Sadly, these days I'm pretty busy finishing my PhD, doing my postdoc work, finishing up various projects, etc, so I don't think I'm going to be able to squeeze in more field time in the immediate future to seek out what's eating the buckthorn, but maybe I'll get lucky when I go out to collect data later this October (though I suspect the moths will be long gone by then). In any case, it's an interesting observation to stow away and come back to in the future.