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)

  • 相关阅读:
    如果网站文字不让复制怎么办,谷歌浏览器
    Mac 微信双开
    git 线上一不小心拉取代码了,如何恢复
    php 验证港澳台身份证
    把照片弄成50k以内
    Gson序列化时排除字段
    实战Springboot内置Tomcat配置调优
    svg图标爽使用
    laravel的post请求分页数据
    php的isset函数相关问题
  • 原文地址:https://www.cnblogs.com/MarsMercury/p/4947969.html
Copyright © 2011-2022 走看看