Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Shusei-E/48c2cc1c36899cd6615480a8fbcf5cc5 to your computer and use it in GitHub Desktop.

Select an option

Save Shusei-E/48c2cc1c36899cd6615480a8fbcf5cc5 to your computer and use it in GitHub Desktop.

Revisions

  1. @dsparks dsparks revised this gist Dec 18, 2012. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions coefficent_plot_walkthrough.R
    Original file line number Diff line number Diff line change
    @@ -35,11 +35,11 @@ interval2 <- -qnorm((1-0.95)/2) # 95% multiplier
    # Plot
    zp1 <- ggplot(allModelFrame, aes(colour = modelName))
    zp1 <- zp1 + geom_hline(yintercept = 0, colour = gray(1/2), lty = 2)
    zp1 <- zp1 + geom_linerange(aes(x = Variable, ymin = Coefficient - SE*1,
    ymax = Coefficient + SE*1),
    zp1 <- zp1 + geom_linerange(aes(x = Variable, ymin = Coefficient - SE*interval1,
    ymax = Coefficient + SE*interval1),
    lwd = 1, position = position_dodge(width = 1/2))
    zp1 <- zp1 + geom_pointrange(aes(x = Variable, y = Coefficient,
    ymin = Coefficient - SE*2, ymax = Coefficient + SE*2),
    zp1 <- zp1 + geom_pointrange(aes(x = Variable, y = Coefficient, ymin = Coefficient - SE*interval2,
    ymax = Coefficient + SE*interval2),
    lwd = 1/2, position = position_dodge(width = 1/2),
    shape = 21, fill = "WHITE")
    zp1 <- zp1 + coord_flip() + theme_bw()
  2. @dsparks dsparks revised this gist Dec 18, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion coefficent_plot_walkthrough.R
    Original file line number Diff line number Diff line change
    @@ -44,4 +44,4 @@ zp1 <- zp1 + geom_pointrange(aes(x = Variable, y = Coefficient,
    shape = 21, fill = "WHITE")
    zp1 <- zp1 + coord_flip() + theme_bw()
    zp1 <- zp1 + ggtitle("Comparing several models")
    print(zp1) # The trick to these cool coefficient plots is position_dodge().
    print(zp1) # The trick to these is position_dodge().
  3. @dsparks dsparks created this gist Dec 18, 2012.
    47 changes: 47 additions & 0 deletions coefficent_plot_walkthrough.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    doInstall <- TRUE
    toInstall <- c("ggplot2")
    if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")}
    lapply(toInstall, library, character.only = TRUE)

    ANES <- read.csv("http://www.oberlin.edu/faculty/cdesante/assets/downloads/ANES.csv")
    ANES <- ANES[ANES$year == 2008, -c(1, 11, 17)] # Limit to just 2008 respondents,
    head(ANES) # remove some non-helpful variables

    # Fit several models with the same DV:
    model1 <- lm(pid7 ~ ideo7 + female + age + south, data = ANES)
    model2 <- lm(pid7 ~ ideo7 + female + age + female:age, data = ANES)
    model3 <- lm(pid7 ~ ideo7, data = ANES) # These are just arbitrary examples

    # Put model estimates into temporary data.frames:
    model1Frame <- data.frame(Variable = rownames(summary(model1)$coef),
    Coefficient = summary(model1)$coef[, 1],
    SE = summary(model1)$coef[, 2],
    modelName = "South Indicator")
    model2Frame <- data.frame(Variable = rownames(summary(model2)$coef),
    Coefficient = summary(model2)$coef[, 1],
    SE = summary(model2)$coef[, 2],
    modelName = "Age Interaction")
    model3Frame <- data.frame(Variable = rownames(summary(model3)$coef),
    Coefficient = summary(model3)$coef[, 1],
    SE = summary(model3)$coef[, 2],
    modelName = "Univariate")
    # Combine these data.frames
    allModelFrame <- data.frame(rbind(model1Frame, model2Frame, model3Frame)) # etc.

    # Specify the width of your confidence intervals
    interval1 <- -qnorm((1-0.9)/2) # 90% multiplier
    interval2 <- -qnorm((1-0.95)/2) # 95% multiplier

    # Plot
    zp1 <- ggplot(allModelFrame, aes(colour = modelName))
    zp1 <- zp1 + geom_hline(yintercept = 0, colour = gray(1/2), lty = 2)
    zp1 <- zp1 + geom_linerange(aes(x = Variable, ymin = Coefficient - SE*1,
    ymax = Coefficient + SE*1),
    lwd = 1, position = position_dodge(width = 1/2))
    zp1 <- zp1 + geom_pointrange(aes(x = Variable, y = Coefficient,
    ymin = Coefficient - SE*2, ymax = Coefficient + SE*2),
    lwd = 1/2, position = position_dodge(width = 1/2),
    shape = 21, fill = "WHITE")
    zp1 <- zp1 + coord_flip() + theme_bw()
    zp1 <- zp1 + ggtitle("Comparing several models")
    print(zp1) # The trick to these cool coefficient plots is position_dodge().