Friday, August 24, 2012

How to Roll Back Packages in R

Today I was in an unusual position where changes to a common dependency in one R package broke compatibility with several other packages I use for predictive modeling.  When I went to roll back the package, I was surprised to learn that there is no option to install.packages which will simply install a version of my choice.  Instead, Matt taught me a rather simple and straightforward way to install an earlier version using Hadley's devtools package.  The first step is to install the package and load it:

install.packages("devtools")
library(devtools)


Next, I can look up the package and version of my choice on the CRAN archive.

Finally, once I find the package and version I want, I can copy the link from the CRAN archive and use.  For example,

install_url("http://cran.r-project.org/src/contrib/Archive/survival/survival_2.36-12.tar.gz")

Just like that, I've rolled back the package to an earlier version.  Thanks to Matt for introducing me Hadley's useful devtools package!

Sunday, August 19, 2012

The Yacht Race

I'd never sailed before this week, but my friend Matt invited me to a yacht race and then again sailing the next day this week and I'm sold.  There's nothing quite like the sensation of the wind pulling you through the water when you have the sails aligned just right, and the views of Seattle and Mount Rainier from the Puget Sound are really marvellous.  Let it be known:  a cooler full of beer, some friends, and a yacht are a formula for a great evening!  We were in such a hurry to get out the door that I forgot to bring my camera to the yacht race, so the pictures posted here are crude cell-phone photographs that don't quite do the day justice. 




The funny thing is, I think Matt actually appreciated the opportunity to just enjoy the ride and not drive for a change.  Keeping the boat aligned to catch the wind just right takes some getting used to, but just trying is a lot of fun!


Saturday, August 4, 2012

Playing Chicken with History

I watched with some interest the news regarding Chick-Fil-A Appreciation Day.  The concept is that because Chick-Fil-A as a company publicly opposes gay marriage, then you can show your opposition to the same by supporting them.  I wondered if this move could be less a function of the company owners' core beliefs and politics than a marketing scheme designed to drum up sales based on the general opinion of those that live near Chick-Fil-A stores.

I do my best to keep politics out of my blog and focus on science, but this time I feel the need to preface a study with what shouldn't be a politically charged statement.  Majority vote is a pretty good way to deal with mundane, day to day problems.  If most people believe that the patent system should be overhauled, or most people believe that a road should be built, then that should probably happen.  No majority vote, government, religion or individual, however, has the right to disenfranchise anybody of their civil or human rights.  Marriage is a human right, and states that have constitutionally banned gay marriage based on a referendum are wrong.  People who believe that gay marriage should be illegal are also wrong.  They are also on the losing side of history.  I personally will not eat at a Chick-Fil-A restaurant until they publicly renounce their support of bigotry.

As a scientist, what I'm mostly interested in as I read about Chick-Fil-A Appreciation Day is whether the Chick-Fil-A debacle is a marketing move, and whether we will see other businesses take controversial political positions in order to boost sales and brand awareness.

First, I need some data to work with.  I obtained data on the locations of Chick-Fil-A stores using a map of the geographic distribution from their company website.  That map, correct on June 1, 2012 is reproduced below.

Figure 1:  Geographical distribution of Chick-Fil-A stores on June 1, 2012.  This map is copyright CFA properties Inc., and is reproduced for non-profit educational purposes as permitted by section 107 of the copyright law (title 17, US Code.)

It is remarkably difficult to find data on the state-wise public opinion on gay marriage, so as a proxy I used raw data from the Pew Research Center's 2011 Political Typology Survey.  In particular, one question was "q37u:  Should homosexuality be accepted by society?", and I assume that one's answer to this question equals whether or not they support legal recognition of same-sex marriage.  Admittedly, it's possible that some people may see societal acceptance of homosexuality as something different than marriage equality, but it's the best proxy I could find.  If you have better data by state, please let me know!

The first step after downloading the SPSS file was to convert it to an open format that I could actually use.  I found GNU PSPP to be inadequate for this purpose, and instead used R to read a SPSS save file into a data frame:

library(foreign)
pew <- read.spss('~/Desktop/2011 Political Typology public.sav', to.data.frame=T)


Now I am able to break down the answers to this question by state.  Answers 1 and 2 to the question "q37u" felt that homosexuality should be accepted by society, and 3 and 4 said that homosexuality should be discouraged.  Those counts are then given by

accept <- table(pew[as.numeric(pew,"q37u") %in% c(1,2), "state")


and

reject <- table(pew[as.numeric(pew,"q37u") %in% c(3,4), "state")


Some states had very few data points (Wyoming had only 3), and Alaska and Hawaii were totally unrepresented.  As such, the error bars on the ratio of accepters versus discouragers can be quite large.  That said, producing a map of acceptance of homosexuality by state produced a very telling result.  With help from the Revolution Analytics Blog, then:

# gives proportion acceptance
acceptanceRatio <- accept/reject        

# To lower case
names(acceptanceRatio) <- tolower(names(acceptanceRatio))

# Ok, let's get the appropriate regions from the map
require(maps)

# Get the region names
regionNames <- map("state",namesonly=TRUE)

# Need to parse out the name, exactly
stateName <- unlist(lapply(as.list(regionNames), function(x) {return(strsplit(x, ":")[[1]][1])}))

# Set the ratios by state
stateRatios <- acceptanceRatio[stateName]


# Generally accepting, generally rejecting, about even. (60% majority)
color <- apply(stateRatios, 1, function(x) {
  if (x <= 2/3) {return(rgb(red=1, green=0, blue=0))}
  if (x >= 3/2) {return(rgb(red=0, green=1, blue=0))}
  return(rgb(red=1,green=1,blue=0))
})

map("state", fill=TRUE, col=color)


Figure 2:  Opinion on societal acceptance of homosexuality by state.  In green states, over 60% of respondents to Pew Research's 2011 Political Typology Survey supported societal acceptance of homosexuality.  In red states, over 60% of respondents felt that society should discourage homosexuality.  In yellow states, there was no 60% majority.  Please note that there is substantial margin of error here, as some states had as few as three respondents.

Figure 2 was, to me, an extremely telling plot.  First, I'll point out that there is obviously strong overlap between the number of Chick-Fil-A stores in a state (red in figure 1) and sentiment that the state should discourage homosexuality (red and yellow in figure 2).  What really shocked me, though, is that this plot tells me that a vocal minority is responsible for the outcry against same-sex marriage.  Even in North Carolina, where a constitutional amendment was voted into effect to ban gay marriage, the ratio of those who felt that society should accept versus discourage homosexuality is nearly 1:1.  With voter turnout below 50% in most of the country, this means that all that has to happen in order to win marriage equality in most states is for those who support equality to go out and vote.

So, was it a good idea for Chick-Fil-A to market their bigoted position?  Nationwide, no.  More people support equality than reject it.  In the regions of the country with the most Chick-Fil-A stores, it's a dicey measure as the population is mostly split.  The reality, though, is that a look at the national political scene shows that the vocal minority is much more active in promoting their historically doomed cause than the majority which supports acceptance.  In that sense, Chick-Fil-A's move is a pretty safe bet.  Gay rights supporters simply aren't as active as their opponents and may not care enough to give up fried chicken.  Meanwhile, Chick-Fil-A enjoys a surge of popularity from the vocal minority.

In a sense, Amazon quickly followed Chick-Fil-A's experiment with their own.  In Jeff Bezos' case, it makes sense to side with the majority of people when your company makes sales nationwide.  President Obama also recently took the side of the majority, a wise choice when running for office with a nationwide constituency.  It will be interesting to see whether or not we see more examples of high-ranking representatives of companies taking politically controversial positions to market to their primary customer base in the future.

In conclusion,

  • A majority of, or at least a roughly equal number of people, support societal acceptance of homosexuality most of the lower 48 states.
  • Proponents of inequality are apparently much more vocal and active in promoting their position than those who support equality.
  • Chick-Fil-A's position actually conflicts with that of about half of their potential customers even in the Southeast, making their move a dicey one at worst but a good one at best based on the activeness of the anti-gay rights crowd.

Finally,

  • "All that is necessary for the triumph of evil is that good men do nothing." -Edmund Burke

Get out and vote!  If you support equality, you're in good company.  In most states, you represent 60% or more of the constituency.  In nearly all states, you represent 40% or more of the constituency.  All that has to happen for equality to win the day is for you to tell the government what you think!