Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. @dsparks dsparks created this gist Dec 11, 2012.
    21 changes: 21 additions & 0 deletions Herfindahl–Hirschman_Index.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    doInstall <- TRUE # Change to FALSE if you don't want packages installed.
    toInstall <- c("plyr", "ggplot2")
    if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
    lapply(toInstall, library, character.only = TRUE)

    ANES <- read.csv("http://www.oberlin.edu/faculty/cdesante/assets/downloads/ANES.csv")
    head(ANES)
    ANES$PID3 <- factor(ANES$pid7) # Convert to three-level Party ID:
    levels(ANES$PID3) <- c("Dem", "Dem", "Dem", "Ind", "Rep", "Rep", "Rep")

    # Using plyr to estimate the "Effective numbers of parties" by year and region
    ENpid3 <- ddply(.data = ANES,
    .progress = "text",
    .variables = .(year, south),
    summarize, # Calculate an inverse HHI
    invHHI = sum(table(PID3))^2 / sum(table(PID3)^2))

    zp1 <- ggplot(ENpid3)
    zp1 <- zp1 + geom_line(aes(x = year, y = invHHI, colour = factor(south)))
    zp1 <- zp1 + ggtitle("Effective Number of Parties-in-the-Electorate")
    print(zp1)