Exporting Deviance Tables to (ugh) Word

I do my absolute best to minimize the amount of time Word is open on my computer desktop.  Alas, when one collaborates widely, particularly with non-technical people, it is a necessary evil.

In these collaborations, I am typically doing either statistical or modeling work (or both). This often means that I am generating objects in R, such as Analysis of Deviance Tables, that I would like to put into the working document.  This can be an enormous hassle, particularly when your data set is repeatedly updated and the table changes ever so slightly with each iteration of the paper.  It's not such a big deal for small tables, but when there are lots of covariates on the model it is a maddening, time-consuming, hypertensogenic experience.

This has been a particularly acute problem of late as we wind down to the final draft of two big papers with substantial data analysis (and fair sized analysis of deviance tables). I am not joking when I say a solution came to me in a fevered dream.  I'm surprised it hadn't occurred to me before, given past posts on related topics, but what can I say? 

So, here is my basic Kekulé-esque idea: Output the table to a dummy LaTeX document using xtable. After texing, convert the file using LaTeX2rtf.  It's then a simple matter of opening the rtf file in Word.  Copy and paste and voilà tout!  Seems like it works pretty well.  Can anybody think of a less Byzantine way to do this?

More on Science in the Obama Times

As a follow-up to my post on science and the Obama Inaugural, I wanted to note a terrific essay  by Dennis Overbye on the civic virtues of science in the New York Times. He argues that virtue emerges from the process of science: "Science is not a monument of received Truth but something that people do to look for truth."  Continuing, he writes,

That endeavor, which has transformed the world in the last few centuries, does indeed teach values. Those values, among others, are honesty, doubt, respect for evidence, openness, accountability and tolerance and indeed hunger for opposing points of view. These are the unabashedly pragmatic working principles that guide the buzzing, testing, poking, probing, argumentative, gossiping, gadgety, joking, dreaming and tendentious cloud of activity — the writer and biologist Lewis Thomas once likened it to an anthill — that is slowly and thoroughly penetrating every nook and cranny of the world.

There is a certain egalitarian, round-table ethos to science done well.  It doesn't matter what degrees you have or where from.  What matters is whether you ask and answer interesting questions. Of course, institutions that support science frequently care about degrees and where they're from, but in my experience, good scientists don't. While there are certainly barriers to entry (e.g., the cost of higher education, the difficulty of mastering a subject), there is no fundamentally esoteric knowledge in science.  When it's working right, everything is transparent.  It has to be because no one will believe you unless it can be repeated.

I certainly hope the rhetoric of respect for science and the idea that empirical research will inform policy continues and gets translated into tangible support for research in the coming years.

Data, Statistics, Science, Imagination and Common Purpose

In President Obama's Inaugural Address, "data" and "statistics" were the 247th and 249th words spoken. Science was very much foregrounded in the President's address:

We will restore science to its rightful place and wield technology's wonders to raise health care's quality and lower its costs.

We will harness the sun and the winds and the soil to fuel our cars and run our factories. And we will transform our schools and colleges and universities to meet the demands of a new age.

All this we can do. All this we will do.

There is tremendous congruence between this stated respect for science and the somber chastisement over our collective "failure to make hard choices and prepare the nation for a new age."  The Bush administration sought to suppress science because facts about the world can be politically inconvenient.  The implications of scientific research don't always jibe so well with our desire for short-term gratification.  I hope that President Obama can truly help to focus our political debates onto the serious decisions that we need to make as individuals and as a society.  

I am thrilled by the prospect that the age of know-nothingness in Washington DC might be over, but am also realistic that these things take time.  Let's hope we can make this change while we still actually have time!

A Great Career for a Biological Anthropologist

The fabulous Cambridge-based singer-songwriter, Mieka Pauley turns out, like Pete Seeger and Bonnie Raitt, to be a Harvard graduate.  Mieka attended Harvard when I was a grad student, concentrated (Harvardese for "majored") in biological anthropology, and took a class I taught when I was a lecturer there for the 2000-2001 academic year.  I just got back in touch with her and she informed me that bio-anthro actually comes up all the time in interviews she does.  What do you know?  It seems that biological anthropology is useful for something after all! I suppose it shouldn't really surprise me (note my recent post on folk songs and Human Nature) all that much.  Maybe it's her training in biological anthropology that brought out the deep appreciation of the human condition that comes through in Mieka's music?  I heartily recommend her new album, Elijah Drop Your Gun.

New York Times Discovers R

A recent article in the New York Times extolls the virtues of the R statistical programming language.  Better late than never, I suppose.  I first discovered R in 1999, just as I began writing my dissertation. At the time, I used Matlab for all my computational needs.  I still occasionally use Matlab when doing hardcore matrix algebra or numerically solving differential equations.  I also sometimes use Mathematica to check my algebra or to solve equations when I'm feeling lazy (I think there are actually lots more possibilities but exploring these hasn't been a priority), but mostly I now use R. When looking for a post-doc, one of my training goals was learning R. I certainly scored in that department by landing in the Center for Statistics in the Social Sciences at the University of Washington working with Mark Handcock, sharing an office with Steve Goodreau, and interacting with people life Adrian Raftery, Peter Hoff, and Kevin Quinn, I learned a lot about R. I'd like to think that I saw the writing on the wall.  Mostly though, I think I liked the idea of free, open-source, state-of-the-art numerical software.

I use R in many of the classes I teach, including Demography and Life History Theory, Applied Bayesian Methods in the Social Sciences, Data Analysis in the Anthropological Sciences, and our NICHD-funded Workshop in Formal Demography. While I don't expect the students to learn it, I also use R to make most of the figures I show in slides in other classes like Evolutionary Theory, Environmental Change and Emerging Infectious Disease, and even The Evolution of Human Diet. My colleague Ian Robertson also teaches his quantitative classes in R.  Anthropology is also very lucky to have an academic technology specialist, Claudia Engel, with a strong interest in supporting both faculty and student use of R. The Human Spatial Dynamics Laboratory has a growing list of R resources for student (and other's) use.  My lab site will soon host all of our R material for the summer workshops as well as my R package demogR.

I sometimes wonder if other anthropologists are learning R.  I'm sure Steve's students get some R up at UW.  But is there anyone else out there?  Perhaps this is one of the great comparative advantages we can give our students here at Stanford.  Since the New York Times says it's cool, it must be true.

Post-Tenure Teaching Project

I've had this idea for a couple years now and thought that I would put it down on paper as it were. When I first got to Stanford (in fact, when I was being recruited for the job), I was asked if I would teach a class on Human Nature. I remember thinking to myself, "Sweet, this'll be easy!" You see, I taught for Irv DeVore's famous class, Science B-29: Human Behavioral Biology more times than I care to recollect. This is a topic for another blog post altogether, but Irv co-taught this class with various luminaries throughout the years including Bob Trivers, Mel Konner, Terry Deacon, and Marc Hauser. When Irv retired, Richard Wrangham took his place, co-teaching with Marc Hauser. Anyway, the key point for the current thread is that if there was one class I was qualified to teach when I came to Stanford, it was a class on Human Nature.

Shortly after I arrived in the summer of 2003, I had a lunch meeting at the faculty club with Bill Durham and Rob Robinson, the director of the program for which I would teach my Human Nature class. I remember being curious about why there would be a program director there. Wasn't I just teaching the class for Anthropological Sciences? Well, no. It seems that the Human Nature class that everyone wanted would be part of the compulsory freshman program, Introduction to the Humanities (IHUM). "Humanities?!" I exclaimed. "You do realize who you're talking to here, no?" To say that I was not well prepared to teach a course introducing freshmen at Stanford to the Humanities would be a rather large under-statement. Everyone assured me that everything would be fine. I just needed to find the right co-instructor. I had a very nice meeting with Don Kennedy who was the Stanford President when IHUM was introduced and who was the only scientist to co-teach a class before me (an enviable class on the environment with Richard White). That helped me to conceive of how I might teach such a class and formulate at least a semi-coherent plan for putting the class together.

The details of the class that I ended up teaching for two years should be saved for another post. I found a terrific humanist with whom to share lectures and had a terrific group of IHUM fellows do the hard work of sections, grading papers, etc. When my co-instructor, who was a lecturer at Stanford at the time, moved to a tenure-track position in the History department at San José State University, the class just sort of ended.

Since I stopped teaching that class, I've thought from time to time that it would be fun to try the idea again, this time really sticking to the core ideas in the study of Human Nature. One day, I was listening to the Fairport Convention (1969) recording of the seventeenth century English folk song, Matty Groves. While I am no expert folklorist, I am nonetheless struck by the observation that so many of these old folk songs (English and otherwise) are simply riddled with sex, honor, jealousy, loyalty, betrayal, lust, murder, ... In short, they are studies in Human Nature. What a great conceit for an IHUM class on Human Nature!

We could go through various folk songs and analyze what they tell us of people's ideas of Human Nature. What are the commonalities across cultures and through time? How do these ideas relate to current scientific and philosophical work on Human Nature? The thing that would really make this class come together would be performative. These are folk songs, after all, that were meant to be sung. Maybe I could overcome my own performance anxiety and put my under-used guitar to good use and sing a song myself? As I said, a post-tenure project.

I won't go into too many details here, but I will do a little analysis of Matty Groves, providing some interpretations of the lyrics and pointing to directions that I might take in such a class. The idea is generally to give a flavor of the content of this class. There is a lot of contextual information that is implicit in the song about the social organization of mediaeval/early modern English society. This is definitely something we would want to talk about extensively in class, but I will gloss that for now.

A holiday, a holiday, and the first one of the year.
Lord Donald's wife came into church, the gospel for to hear.

And when the meeting it was done, she cast her eyes about,
And there she saw little Matty Groves, walking in the crowd.

Remember that holidays used to be holy days. The reason you got out of work, if you were so lucky, was so you could go to church. And what better venue for looking for a little action could there be than church?

Why is Lord Donald's wife on the make? Chances are that she is much, much younger than her husband. As Mildred Dickemann, and many others, have observed, strongly hierarchical societies are highly hypergynous. Women marry up in social status and marry older men. Chances are, Lord Donald's wife had no say in her own marriage as it was arranged by her father and emissaries for Lord Donald. For a sense of the degree that status structured this society, it is interesting to note who remains unnamed in the song. Really the main character in the narrative, "Lord Donald's Wife" as well as "the servant." My interpretation is that Lord Donald's wife is bored and not attracted to her brutal, high-status husband who is probably old enough to be her father. The holiday, during which her husband is away, provides her with an opportunity to explore the field. My understanding of human reproductive psychology tells me that young women, despite the mid-century folklore, want to have sex too. Yep, it's not just boys. There is an interesting Gail Collins's essay on Twilight in which she writes "This sure sounds like trouble to me: A generation of guys who will settle for nothing less than a porn star meets a generation of women who expect their boyfriend to crawl through their bedroom window at night and just nuzzle gently until they fall asleep." I agree there is trouble, but I have serious doubts that a whole generation of girls only want to nuzzle and fall asleep.

"Come home with me, little Matty Groves, come home with me tonight.
Come home with me, little Matty Groves, and sleep with me till light."

"Oh, I can't come home, I won't come home and sleep with you tonight,
By the rings on your fingers I can tell you are Lord Donald's wife."

Again, note that Lord Donald's wife has no identity other than that of the Lord's wife. Her status as such is communicated by conspicuous display of jewelry. Matty is no fool. He knows that there is real danger here...

"What if I am Lord Donald's wife? Lord Donald's not at home.
For he is out in the far cornfields, bringing the yearlings home."

...but the lord's wife was very persuasive. A rich and powerful lord can afford a beautiful wife (even in a dowry society -- the details of this apparent paradox are fodder for lecture, of course). It's also possible that Matty and Lord Donald's wife knew each other from childhood.

Note also that wealth came from the land in this agrarian society. Even a rich and powerful lord brings the yearlings home from the far cornfields.

And a servant who was standing by and hearing what was said,
He swore Lord Donald he would know before the sun would set.

And in his hurry to carry the news, he bent his breast and ran,
And when he came to the broad mill stream, he took off his shoes and swam.

A loyal (unnamed) servant may just move up in the hierarchy of servants.

Little Matty Groves, he lay down and took a little sleep.
When he awoke, Lord Donald he was standing at his feet.

Saying "How do you like my feather bed? And how do you like my sheets?
How do you like my lady who lies in your arms asleep?"

Things like beds, sheets, and pillows were luxury goods Matty had probably never experienced as a peasant boy.

Imagine walking into your own bedroom and finding your spouse in bed with a lover. Crimes of passion account for an amazingly large fraction of homicides in a variety of societies. Lord Donald's anger is probably compounded by the fact that his wife is not only his reproductive partner, but is his property in a very real sense.

"Oh, well I like your feather bed, and well I like your sheets.
But better I like your lady gay who lies in my arms asleep."

Cheeky boys get spanked.

"Well, get up, get up," Lord Donald cried, "get up as quick as you can!
It'll never be said in fair England that I slew a naked man."

Lord Donald is rightly concerned about his reputation because this is the key to his continued social status. Gossip among peers can destroy the social standing of even the very rich. And, of course, what else do elites have to occupy their time than gossip? Certainly not work or anything else productive.

Rules concerning combat are frequently a central component of honor cultures. Why is that? Concepts like honor, duty, and loyalty and the degree to which they generalize would be major topics for the class.

"Oh, I can't get up, I won't get up, I can't get up for my life.
For you have two long beaten swords and I not a pocket-knife."

"Well it's true I have two beaten swords, and they cost me deep in the purse.
But you will have the better of them and I will have the worse."

"And you will strike the very first blow, and strike it like a man.
I will strike the very next blow, and I'll kill you if I can."

Again, a steel sword would have been a serious luxury good. No peasant would have owned such goods nor have had any training in their use. Lord Donald is honorable in the sense that he refuses to slay the naked Matty Groves, but the magnanimity of his offer must be weighed against the serious asymmetry in human capital that is embodied in this duel. Would you know how to use a longsword in a duel? I assume that the weapon in question is some form of longsword since it is "beaten." Consider also the fact that stature (along with strength) is very much determined by status. One of the most amazing demonstrations of this fact is provided in a plot of the secular trend in men's stature in the United States published in the Cambridge Encyclopedia of Human Growth and Development (Ulijaszek et al., eds., 1998). Overlain on this plot of the secular trend are the heights of the first 36 presidents (through Nixon) as well as various other period-specific averages. All US presidents but three (Benjamin Harrison, William McKinley, and Harry Truman) were substantially above the median period stature (though James Madison was just above the median). Compare this to a sample of working class in 1830 or British class IV in 1885.

Not a happy situation for Matty. Of course, what are Lord Donald's options? His social standing would take an express ride to the basement if word got out that his wife was sleeping around -- and with a peasant boy no less!

So Matty struck the very first blow, and he hurt Lord Donald sore.
Lord Donald struck the very next blow, and Matty struck no more.

Sucks to be a peasant in a highly stratified agrarian society. Here's this guy who is seduced into the bed of a noble woman, against his better first judgement, who finds himself slain because of some bad luck and his social position.

And then Lord Donald he took his wife and he sat her on his knee,
Saying, "Who do you like the best of us, Matty Groves or me?"

And then up spoke his own dear wife, never heard to speak so free.
"I'd rather a kiss from dead Matty's lips than you and your finery."

Herein lie the clues to Lord Donald's age and brutality, I think. Of course, we have the prima facie evidence of his brutality in the Lord having just slain his wife's lover before her eyes. But is this really any more than could be expected from the age and society? I don't think so. But one thing is utterly clear: she doesn't want him anymore. Surely she has to know what she has coming when she speaks in such an impudent manner. "Never heard to speak so free": Remember, she's nameless. Nameless people don't speak freely in the presence of nobility. Lord Donald appears to be giving her an out, which is actually quite remarkable. I don't think that this would necessarily be expected in this society. She could easily have blamed the whole affair on (the now dead) Matty Groves. But, instead, she chose to take a principled stand and, oh, does it cost her.

Lord Donald he jumped up and loudly he did bawl,
He struck his wife right through the heart and pinned her against the wall.

This is pretty horrible. But the question remains: what were his options? This was a violent society -- as all pre-modern societies (and most modern ones) are. Lord Donald's social standing was greatly jeopardized by this incident. It's unclear to me how it could have ended otherwise given his wife's newfound freedom. Men's control of women's sexuality is a major theme in hierarchical societies -- a theme that could fill several quarters worth of classes and would occupy a central place in my proposed class.

"A grave, a grave!'' Lord Donald cried, "to put these lovers in.
But bury my lady at the top for she was of noble kin."

Just in case you had forgotten for a second that this was an intensely stratified society. Even in the context of a double homicide, we need to maintain appearances...

I find all three main characters in this narrative sympathetic to some degree or another. All of them are victims of their social position. Of course, there are better and worse forms of victimhood. Most obviously, Lord Donald lives. He led the most privileged life but he was not immune from intense and potentially overwhelming social expectations. Lord Donald's wife found herself trapped in a loveless marriage (what is love? what is it for? how universal is it?) to a man to whom she is not attracted with no possibility (because of her social position) for even same-sex friendship for lack of peers. And poor Matty. He is the most hapless really; a total victim of his circumstances.

So this is the general idea for my future IHUM class: An analysis of Human Nature through folk songs. There is lots of potential for higher-order analysis here too. Things having to do with the nature of cultural transmission and how ideas are transformed across time and space. This class would be a blast, particularly if we could have a performance aspect to it.

Plotting Recruitment Curves

I was asked how I plotted the recruitment curve from my last post. It's pretty easy to do in R.

[r]
Dn <- expression(r * (1 - N/K) * N) r <- 0.004 K <- 10^10 N <- seq(0,10^10,by=10^7) png(file="recruitment.png") ## sends output to .png file plot(N,eval(Dn),type="l", xlab="Human Population", ylab="Recruitment") dev.off() [/r]

Now we can generalize to the generalized theta logistic model where

 \frac{dN}{dt} = rN(1-N/K)^{\theta}.

This model changes the shape of the density dependence when \theta<1 the density dependence is felt only at higher densities, whereas when \theta>1 the density dependence kicks in at low population size.

Consider our problem of what the optimal harvest size of humans by Kanamits consumers would be if human population size was actually regulated according to the theta-logistic model. First, we assume that \theta<1.

[r]
Dn.gen <- expression((r * (1 - N/K))^theta * N) theta <- 0.1 png(file="recruitment-thless1.png") plot(N,eval(Dn.gen),type="l", xlab="Human Population", ylab="Recruitment") title("theta=0.1") dev.off() [/r]

Now, we assume that \theta>1.

[r]
theta <- 10 png(file="recruitment-thgr1.png") plot(N,eval(Dn.gen),type="l", xlab="Human Population", ylab="Recruitment") title("theta=10") dev.off() [/r]

I sent the output to a portable network graphics (png) device because I wanted to include the figures with this post. Normally, in interactive work, you would send this to the default on-screen device (e.g., x11 or Quartz). When making a figure for publication or to include with a LaTeX document, one would send the output to a pdf device.

From this example, we can see that the shape of the density dependence can pretty drastically change the MSY, at least in the case where the discount rate is zero. When \theta=10, we can see that the Kanamits would only be able to sustainably harvest on the order of a billion people, whereas if \theta=0.1, they could take nearly 9 billion and still have a viable population. I'm guessing that the former is closer to the truth than the latter, but let's hope that the answer remains in the realm of absurdist speculation where it belongs.

To Serve Man

So there I was. 23 hundred hours, New Year's Eve 2008 at my in-laws' in Corvallis, Oregon. Kids asleep in the other room. Spouse headed in the same direction. Insomniac from lack of activity and feelings of general physical displacement. Not much prospect for a party or other New Year's antics. Sounds like the perfect set-up for a Twilight Zone marathon, no? I think that I managed to make it through about 8 episodes in the wee hours of the first day of 2009. I wonder what that augers for the new year?

One of the episodes I managed to watch was "To Serve Man" (viewable freely in its entirety here). It's your standard aliens land, aliens altruistically transfer advanced technology, aliens solve all of humanity's problems, aliens turn out to have ulterior motives, aliens plan to eat all of humanity kind of plots. You see, the Kanamits have come to Earth to bring humanity back to their home world to eat them. The book that the Kanamits spokesman leaves in the United Nations (the big tease...) turns out to be a cookbook, a fact that the crack cryptographic team headed by protagonist Michael Chambers discovers too late. Chambers is already boarding the ship bound for his vacation getaway to the Kanamits' home world when the real brains of his outfit, the beautiful Pat, comes running to inform him (and, maddeningly for humanity, only him) of the horrible news. He's shipping off to get eaten. Get it? "Serve" as in provide assistance vs. "serve" as in provide a meal?

Toward the end of the episode, Michael Chambers, says in soliloquy that the recollections he has just shared represent the life cycle of Man "from dust to dessert." He comments that every person will ultimately share his fate (i.e., being served): "Sooner or later, we'll all of us be on the menu." This is what got me. Surely not. This doesn't seem like very good natural resource management on the part of the Kanamits.

The classical theory of natural resource management suggests that there should be a maximally sustainable yield for farmed humans. We will assume that human population growth can be written in terms of its current size and some function of size \dot{N}= N f(N), where \dot{N} indicates the derivative of population size with respect to time t. For simplicity's sake, let's assume that the effect of density on the human growth rate is linear such that f(N) = r(1 - N/K), where r is the intrinsic rate of natural increase K is the carrying capacity (i.e., the maximum number of people the Earth could potentially support).

Now, I should note that this is a purely hypothetical exercise meant simply to illustrate basic concepts. In any real Soylent Green like future where we are faced with fattening Peter to save Paul, we would need a much more sophisticated population model. Among other things, the idea of a carrying capacity for humanity is a maddeningly elusive one. The eminent demographer Joel Cohen has written a whole book on the matter and comes up with no particularly satisfying answer to the seemingly basic question of How Many People Can The Earth Support?

For argument's sake, let's just say that the maximum intrinsic rate of increase is r=0.04 (4% annual growth, which is high, but remember, humanity has just received this remarkable technology transfer) and the carrying capacity is K=10^{10} (i.e., 10 billion). Population growth changes with population size. We can plot the incremental increase (recruitment) as a function of population size. The maximum point of this so-called recruitment curve, where d\dot{N}/dN=0 is the MSY.

For the simple linear model of density-dependence (i.e., the logistic growth equation) and with no time preferences on the part of the Kanamits, the maximum sustainable yield (MSY) occurs at MSY=K/2 or 5 billion souls. So, you can now understand my dismay at 0200 (or whatever it was) on the first day of 2009. Why would everyone necessarily meet the fate of Michael Chambers? Under optimal harvest management, only half should be harvested. I can think of at least three explanations for this conundrum:

  1. Michael Chambers is either misinformed (he was a cryptographer, after all, and not a wildlife manager) or was exaggerating for dramatic effect.
  2. The Kanamits, while advanced in the physical sciences, had not developed an adequate theory of natural resource management or population dynamics more generally. Perhaps this is why they needed to come all the way to Earth to acquire their dessert?
  3. Perhaps the Kanamits actually had a very sophisticated understanding of population biology and realized that the human intrinsic rate of increase was less than the prevailing discount rate. Under these conditions, the optimal resource-management strategy is liquidation of the stock.

I'm leaning toward explanation #3. That was an awful long way to come, after all.

Sobering thoughts with which to commence the new year.