zoukankan      html  css  js  c++  java
  • Machine Learning for hackers读书笔记(九)MDS:可视化地研究参议员相似性

    library('foreign')

    library('ggplot2')

    data.dir <- file.path('G:\dataguru\ML_for_Hackers\ML_for_Hackers-master\09-MDS\data\roll_call')

    data.files <- list.files(data.dir)

    rollcall.data <- lapply(data.files,function(f) {  read.dta(file.path(data.dir, f), convert.factors = FALSE) })

    #看一下数据情况,103行,647列

    #每一行对应一个议员,包括个人信息及投票结果

    dim(rollcall.data[[1]])

    rollcall.simplified <- function(df)

    {

    #99的很少投票,干脆删掉

      no.pres <- subset(df, state < 99)  

    #10列后才是投票数据

      for(i in 10:ncol(no.pres))

      {

    #有10种投票类型,分为三组,赞成全放一起,反对全放一起,无效全放一起,>6的是无效,1~3是赞成,4~6是反对票

        no.pres[,i] <- ifelse(no.pres[,i] > 6, 0, no.pres[,i])

        no.pres[,i] <- ifelse(no.pres[,i] > 0 & no.pres[,i] < 4, 1, no.pres[,i])

        no.pres[,i] <- ifelse(no.pres[,i] > 1, -1, no.pres[,i])

      }  

      return(as.matrix(no.pres[,10:ncol(no.pres)]))

    rollcall.simple <- lapply(rollcall.data, rollcall.simplified)

    #来一个矩离矩阵

    rollcall.dist <- lapply(rollcall.simple, function(m) dist(m %*% t(m)))

    rollcall.mds <- lapply(rollcall.dist,function(d) as.data.frame((cmdscale(d, k = 2)) * -1))

    congresses <- 101:111 

    for(i in 1:length(rollcall.mds))

    {

      names(rollcall.mds[[i]]) <- c("x", "y")  

      congress <- subset(rollcall.data[[i]], state < 99)  

      congress.names <- sapply(as.character(congress$name),function(n) strsplit(n, "[, ]")[[1]][1])  

      rollcall.mds[[i]] <- transform(rollcall.mds[[i]], name = congress.names,party = as.factor(congress$party),congress = congresses[i])

    }

    cong.110 <- rollcall.mds[[9]] 

    base.110 <- ggplot(cong.110, aes(x = x, y = y)) +  scale_size(range = c(2,2), guide = 'none') +  scale_alpha(guide = 'none') +  theme_bw() +

      theme(axis.ticks = element_blank(),        axis.text.x = element_blank(),        axis.text.y = element_blank(),        panel.grid.major = element_blank()) +

      ggtitle("Roll Call Vote MDS Clustering for 110th U.S. Senate") +  xlab("") +  ylab("") +  scale_shape(name = "Party", breaks = c("100", "200", "328"),

                  labels = c("Dem.", "Rep.", "Ind."), solid = FALSE) +  scale_color_manual(name = "Party", values = c("100" = "black","200" = "dimgray","328"="grey"),

                         breaks = c("100", "200", "328"), labels = c("Dem.", "Rep.", "Ind.")) 

    print(base.110 + geom_point(aes(shape = party, alpha = 0.75,  size = 2)))

    print(base.110 + geom_text(aes(color = party, alpha = 0.75,  label = cong.110$name, size = 2)))

    all.mds <- do.call(rbind, rollcall.mds)

    all.plot <- ggplot(all.mds, aes(x = x, y = y)) +

      geom_point(aes(shape = party, alpha = 0.75, size = 2)) +

      scale_size(range = c(2, 2), guide = 'none') +

      scale_alpha(guide = 'none') +

      theme_bw() +

      theme(axis.ticks = element_blank(),

            axis.text.x = element_blank(),

            axis.text.y = element_blank(),

            panel.grid.major = element_blank()) +

      ggtitle("Roll Call Vote MDS Clustering for U.S. Senate (101st - 111th Congress)") +

           xlab("") +

           ylab("") +

           scale_shape(name = "Party",

                       breaks = c("100", "200", "328"),

                       labels = c("Dem.", "Rep.", "Ind."),

                       solid = FALSE) +

          facet_wrap(~ congress)

    print(all.plot)

  • 相关阅读:
    数据结构-树与二叉树-思维导图
    The last packet successfully received from the server was 2,272 milliseconds ago. The last packet sent successfully to the server was 2,258 milliseconds ago.
    idea连接mysql报错Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezone' property
    redis学习笔记
    AJAX校验注册用户名是否存在
    AJAX学习笔记
    JSON学习笔记
    JQuery基础知识学习笔记
    Filter、Listener学习笔记
    三层架构学习笔记
  • 原文地址:https://www.cnblogs.com/MarsMercury/p/4947969.html
Copyright © 2011-2022 走看看