Part 1:

Produce a map using tmap. In this case, show the zip code where more people gave more cash than in kind

setwd("C:/Users/xwb/Desktop/Data Analysis/Session6")
link='https://github.com/EvansDataScience/DataDriven_ManagementAndPolicy/raw/master/Session6/contriWA_2016.RData'
load(file = url(link))
str(contriWA_2016,width = 60, strict.width = 'cut')
## 'data.frame':    374584 obs. of  10 variables:
##  $ id                  : chr  "3982630.rcpt" "3982631.rcp"..
##  $ contributor_state   : chr  "WA" "WA" "WA" "WA" ...
##  $ contributor_zip     : num  98683 98683 98683 98168 9850..
##  $ amount              : num  50 50 50 500 900 900 50 225 ..
##  $ election_year       : int  2016 2016 2016 2016 2016 201..
##  $ party               : Factor w/ 9 levels "","CONSTITUT"..
##  $ cash_or_in_kind     : Factor w/ 2 levels "Cash","In ki"..
##  $ contributor_location: chr  "(45.60817, -122.51972)" "("..
##  $ Lat                 : num  45.6 45.6 45.6 47.5 47 ...
##  $ Lon                 : num  -123 -123 -123 -122 -123 ...
library(utils)
zippedSHP= "https://github.com/EvansDataScience/data/raw/master/WAzips.zip"
temp=tempfile()
download.file(zippedSHP, temp)
unzip(temp)
library(rgdal)
## Loading required package: sp
## rgdal: version: 1.4-3, (SVN revision 828)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 2.2.3, released 2017/11/20
##  Path to GDAL shared files: C:/Users/xwb/Documents/R/win-library/3.4/rgdal/gdal
##  GDAL binary built with GEOS: TRUE 
##  Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
##  Path to PROJ.4 shared files: C:/Users/xwb/Documents/R/win-library/3.4/rgdal/proj
##  Linking to sp version: 1.3-1
Mapdata <- readOGR("SAEP_ZIP_Code_Tabulation_Areas.shp",stringsAsFactors=F) 
## OGR data source with driver: ESRI Shapefile 
## Source: "C:\Users\xwb\Desktop\Data Analysis\Session6\SAEP_ZIP_Code_Tabulation_Areas.shp", layer: "SAEP_ZIP_Code_Tabulation_Areas"
## with 598 features
## It has 101 fields
## Integer64 fields read as strings:  OBJECTID POP2010 HHP2010 GQ2010 HU2010 OHU2010
library(tmap)
maps = tm_shape(Mapdata) + tm_polygons()
maps
## Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3

library(rmapshaper)
baseMap <- ms_dissolve(Mapdata)
Mapborder = tm_shape(baseMap) + tm_polygons(col = 'white',lwd = 1)
Mapborder

library(raster)
mapCRS=crs(Mapdata)
ccash = function(x){
  return(sum(x=="Cash")/length(x))
}
ccash(contriWA_2016$cash_or_in_kind)
## [1] 0.9825246
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:raster':
## 
##     intersect, select, union
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
WA_zip_contri= contriWA_2016   %>% 
  group_by(contributor_zip)   %>% 
  summarize('morecash'=ccash(cash_or_in_kind))
WA_zip_contri
## # A tibble: 848 x 2
##    contributor_zip morecash
##              <dbl>    <dbl>
##  1           98001    0.986
##  2           98002    0.997
##  3           98003    0.973
##  4           98004    0.989
##  5           98005    0.931
##  6           98006    0.994
##  7           98007    0.996
##  8           98008    0.994
##  9           98009    0.998
## 10           98010    1    
## # ... with 838 more rows
dd=data.frame(WA_zip_contri)
cc=as.numeric(WA_zip_contri$morecash)
morecashplace=dd[dd$morecash>0.5,]
head(morecashplace)
##   contributor_zip  morecash
## 1           98001 0.9858223
## 2           98002 0.9970674
## 3           98003 0.9727811
## 4           98004 0.9885682
## 5           98005 0.9309377
## 6           98006 0.9942787
str(dd$contributor_zip)
##  num [1:848] 98001 98002 98003 98004 98005 ...
zipcodemc=as.character(dd$contributor_zip)
zipcodemc
##   [1] "98001" "98002" "98003" "98004" "98005" "98006" "98007" "98008"
##   [9] "98009" "98010" "98011" "98012" "98013" "98014" "98015" "98017"
##  [17] "98019" "98020" "98021" "98022" "98023" "98024" "98025" "98026"
##  [25] "98027" "98028" "98029" "98030" "98031" "98032" "98033" "98034"
##  [33] "98035" "98036" "98037" "98038" "98039" "98040" "98041" "98042"
##  [41] "98043" "98044" "98045" "98046" "98047" "98049" "98050" "98051"
##  [49] "98052" "98053" "98055" "98056" "98057" "98058" "98059" "98061"
##  [57] "98062" "98063" "98064" "98065" "98067" "98068" "98069" "98070"
##  [65] "98071" "98072" "98073" "98074" "98075" "98076" "98077" "98080"
##  [73] "98082" "98083" "98086" "98087" "98089" "98090" "98092" "98093"
##  [81] "98094" "98095" "98096" "98097" "98099" "98100" "98101" "98102"
##  [89] "98103" "98104" "98105" "98106" "98107" "98108" "98109" "98110"
##  [97] "98111" "98112" "98113" "98114" "98115" "98116" "98117" "98118"
## [105] "98119" "98120" "98121" "98122" "98123" "98124" "98125" "98126"
## [113] "98127" "98128" "98130" "98132" "98133" "98134" "98136" "98138"
## [121] "98139" "98141" "98144" "98145" "98146" "98148" "98149" "98152"
## [129] "98154" "98155" "98156" "98158" "98159" "98160" "98161" "98162"
## [137] "98163" "98164" "98165" "98166" "98167" "98168" "98175" "98176"
## [145] "98177" "98178" "98180" "98181" "98184" "98185" "98186" "98188"
## [153] "98189" "98191" "98193" "98194" "98195" "98196" "98197" "98198"
## [161] "98199" "98200" "98201" "98202" "98203" "98204" "98205" "98206"
## [169] "98207" "98208" "98209" "98210" "98211" "98212" "98213" "98218"
## [177] "98220" "98221" "98222" "98223" "98224" "98225" "98226" "98227"
## [185] "98228" "98229" "98230" "98231" "98232" "98233" "98234" "98235"
## [193] "98236" "98237" "98238" "98239" "98240" "98241" "98243" "98244"
## [201] "98245" "98246" "98247" "98248" "98249" "98250" "98251" "98252"
## [209] "98253" "98254" "98255" "98256" "98257" "98258" "98259" "98260"
## [217] "98261" "98262" "98263" "98264" "98265" "98266" "98267" "98269"
## [225] "98270" "98271" "98272" "98273" "98274" "98275" "98276" "98277"
## [233] "98278" "98279" "98280" "98281" "98282" "98283" "98284" "98286"
## [241] "98287" "98288" "98290" "98291" "98292" "98293" "98294" "98295"
## [249] "98296" "98297" "98298" "98299" "98301" "98302" "98303" "98304"
## [257] "98305" "98307" "98308" "98310" "98311" "98312" "98313" "98315"
## [265] "98317" "98319" "98320" "98321" "98322" "98323" "98324" "98325"
## [273] "98326" "98327" "98328" "98329" "98330" "98331" "98332" "98333"
## [281] "98335" "98336" "98337" "98338" "98339" "98340" "98341" "98342"
## [289] "98343" "98344" "98345" "98346" "98347" "98348" "98349" "98350"
## [297] "98351" "98352" "98353" "98354" "98355" "98356" "98357" "98358"
## [305] "98359" "98360" "98361" "98362" "98363" "98364" "98365" "98366"
## [313] "98367" "98368" "98370" "98371" "98372" "98373" "98374" "98375"
## [321] "98376" "98377" "98378" "98379" "98380" "98381" "98382" "98383"
## [329] "98384" "98385" "98386" "98387" "98388" "98389" "98390" "98391"
## [337] "98392" "98393" "98394" "98395" "98396" "98397" "98400" "98401"
## [345] "98402" "98403" "98404" "98405" "98406" "98407" "98408" "98409"
## [353] "98411" "98412" "98413" "98415" "98417" "98418" "98419" "98421"
## [361] "98422" "98423" "98424" "98431" "98432" "98433" "98438" "98439"
## [369] "98442" "98443" "98444" "98445" "98446" "98448" "98454" "98460"
## [377] "98463" "98464" "98465" "98466" "98467" "98468" "98474" "98484"
## [385] "98490" "98492" "98493" "98495" "98496" "98497" "98498" "98499"
## [393] "98500" "98501" "98502" "98503" "98504" "98505" "98506" "98507"
## [401] "98508" "98509" "98511" "98512" "98513" "98516" "98520" "98522"
## [409] "98524" "98527" "98528" "98530" "98531" "98532" "98533" "98534"
## [417] "98535" "98536" "98537" "98538" "98539" "98540" "98541" "98542"
## [425] "98544" "98546" "98547" "98548" "98550" "98551" "98552" "98554"
## [433] "98555" "98556" "98557" "98558" "98560" "98561" "98562" "98563"
## [441] "98564" "98565" "98566" "98567" "98568" "98569" "98570" "98571"
## [449] "98572" "98575" "98576" "98577" "98578" "98579" "98580" "98581"
## [457] "98582" "98583" "98584" "98585" "98586" "98587" "98588" "98589"
## [465] "98590" "98591" "98592" "98593" "98594" "98595" "98596" "98597"
## [473] "98601" "98602" "98603" "98604" "98605" "98606" "98607" "98609"
## [481] "98610" "98611" "98612" "98613" "98614" "98615" "98616" "98617"
## [489] "98618" "98619" "98620" "98621" "98622" "98623" "98624" "98625"
## [497] "98626" "98627" "98628" "98629" "98630" "98631" "98632" "98635"
## [505] "98636" "98637" "98638" "98639" "98640" "98641" "98642" "98643"
## [513] "98644" "98645" "98647" "98648" "98649" "98650" "98651" "98653"
## [521] "98660" "98661" "98662" "98663" "98664" "98665" "98666" "98667"
## [529] "98668" "98670" "98671" "98672" "98673" "98674" "98675" "98680"
## [537] "98681" "98682" "98683" "98684" "98685" "98686" "98687" "98689"
## [545] "98694" "98701" "98702" "98705" "98708" "98721" "98730" "98753"
## [553] "98761" "98762" "98767" "98801" "98802" "98805" "98807" "98808"
## [561] "98810" "98812" "98813" "98814" "98815" "98816" "98817" "98818"
## [569] "98819" "98821" "98822" "98823" "98824" "98826" "98827" "98828"
## [577] "98829" "98830" "98831" "98832" "98833" "98834" "98835" "98836"
## [585] "98837" "98838" "98839" "98840" "98841" "98843" "98844" "98845"
## [593] "98846" "98847" "98848" "98849" "98850" "98851" "98852" "98853"
## [601] "98855" "98856" "98857" "98858" "98859" "98860" "98862" "98866"
## [609] "98867" "98880" "98881" "98892" "98901" "98902" "98903" "98904"
## [617] "98905" "98906" "98907" "98908" "98909" "98911" "98916" "98920"
## [625] "98921" "98922" "98923" "98925" "98926" "98927" "98928" "98930"
## [633] "98932" "98933" "98934" "98935" "98936" "98937" "98938" "98939"
## [641] "98940" "98941" "98942" "98943" "98944" "98945" "98946" "98947"
## [649] "98948" "98950" "98951" "98952" "98953" "98956" "98957" "98958"
## [657] "98959" "98961" "98962" "98963" "98968" "98975" "98977" "98981"
## [665] "98983" "98987" "98990" "99000" "99001" "99003" "99004" "99005"
## [673] "99006" "99008" "99009" "99010" "99011" "99012" "99013" "99014"
## [681] "99016" "99017" "99019" "99020" "99021" "99022" "99023" "99025"
## [689] "99026" "99027" "99029" "99030" "99031" "99032" "99033" "99034"
## [697] "99036" "99037" "99038" "99040" "99055" "99101" "99102" "99103"
## [705] "99105" "99106" "99108" "99109" "99110" "99111" "99113" "99114"
## [713] "99115" "99116" "99117" "99118" "99119" "99121" "99122" "99123"
## [721] "99124" "99125" "99126" "99128" "99129" "99130" "99131" "99133"
## [729] "99134" "99135" "99136" "99137" "99138" "99139" "99140" "99141"
## [737] "99143" "99145" "99146" "99147" "99148" "99149" "99150" "99152"
## [745] "99153" "99154" "99155" "99156" "99157" "99158" "99159" "99161"
## [753] "99163" "99164" "99166" "99167" "99168" "99169" "99170" "99171"
## [761] "99173" "99176" "99177" "99179" "99180" "99181" "99185" "99200"
## [769] "99201" "99202" "99203" "99204" "99205" "99206" "99207" "99208"
## [777] "99209" "99210" "99211" "99212" "99213" "99214" "99215" "99216"
## [785] "99217" "99218" "99219" "99220" "99223" "99224" "99225" "99227"
## [793] "99228" "99230" "99234" "99251" "99252" "99254" "99258" "99260"
## [801] "99269" "99273" "99292" "99293" "99301" "99302" "99304" "99320"
## [809] "99321" "99322" "99323" "99324" "99325" "99326" "99328" "99329"
## [817] "99330" "99334" "99335" "99336" "99337" "99338" "99341" "99343"
## [825] "99344" "99345" "99346" "99347" "99348" "99349" "99350" "99351"
## [833] "99352" "99353" "99354" "99356" "99357" "99360" "99361" "99362"
## [841] "99363" "99365" "99371" "99380" "99382" "99401" "99402" "99403"

Part 2:

Remake the plot of dimensionality reduction (where the multidemiensional scaling plot cases where colored according to the k-means output). This time, use only the variables that represent input.

library(rio)
link="https://github.com/EvansDataScience/data/raw/master/safeCitiesIndexAll.xlsx"

safe=import(link)
names(safe)
##  [1] "city"                         "D_In_PrivacyPolicy"          
##  [3] "D_In_AwarenessDigitalThreats" "D_In_PubPrivPartnerships"    
##  [5] "D_In_TechnologyEmployed"      "D_In_CyberSecurity"          
##  [7] "D_Out_IdentityTheft"          "D_Out_CompInfected"          
##  [9] "D_Out_InternetAccess"         "H_In_EnvironmentPolicies"    
## [11] "H_In_AccessHealthcare"        "H_In_Beds_1000"              
## [13] "H_In_Doctors_1000"            "H_In_AccessFood"             
## [15] "H_In_QualityHealthServ"       "H_Out_AirQuality"            
## [17] "H_Out_WaterQuality"           "H_Out_LifeExpectY"           
## [19] "H_Out_InfMortality"           "H_Out_CancerMortality"       
## [21] "H_Out_AttacksBioChemRad"      "I_In_EnforceTransportSafety" 
## [23] "I_In_PedestrianFriendliness"  "I_In_QualityRoad"            
## [25] "I_In_QualityElectricity"      "I_In_DisasterManagement"     
## [27] "I_Out_DeathsDisaster"         "I_Out_VehicularAccidents"    
## [29] "I_Out_PedestrianDeath"        "I_Out_LiveSlums"             
## [31] "I_Out_AttacksInfrastructure"  "P_In_PoliceEngage"           
## [33] "P_In_CommunityPatrol"         "P_In_StreetCrimeData"        
## [35] "P_In_TechForCrime"            "P_In_PrivateSecurity"        
## [37] "P_In_GunRegulation"           "P_In_PoliticalStability"     
## [39] "P_Out_PettyCrime"             "P_Out_ViolentCrime"          
## [41] "P_Out_OrganisedCrime"         "P_Out_Corruption"            
## [43] "P_Out_DrugUse"                "P_Out_TerroristAttacks"      
## [45] "P_Out_SeverityTerrorist"      "P_Out_GenderSafety"          
## [47] "P_Out_PerceptionSafety"       "P_Out_ThreaTerrorism"        
## [49] "P_Out_ThreatMilitaryConf"     "P_Out_ThreatCivUnrest"
input=safe[c(1:6,10:15,22:26,32,38)]
input
##                city D_In_PrivacyPolicy D_In_AwarenessDigitalThreats
## 1         Abu Dhabi                 50                     66.66667
## 2         Amsterdam                100                    100.00000
## 3            Athens                 75                    100.00000
## 4           Bangkok                 25                     66.66667
## 5         Barcelona                100                    100.00000
## 6           Beijing                 75                     66.66667
## 7            Bogota                 50                     33.33333
## 8          Brussels                100                    100.00000
## 9      Buenos Aires                 75                     33.33333
## 10            Cairo                 50                     33.33333
## 11          Caracas                 25                     33.33333
## 12       Casablanca                 75                     33.33333
## 13          Chicago                100                    100.00000
## 14           Dallas                100                    100.00000
## 15            Delhi                 25                     66.66667
## 16            Dhaka                  0                     33.33333
## 17             Doha                 50                     66.66667
## 18        Frankfurt                100                    100.00000
## 19 Ho Chi Minh City                 25                     33.33333
## 20        Hong Kong                100                    100.00000
## 21         Istanbul                 25                    100.00000
## 22          Jakarta                 25                     66.66667
## 23           Jeddah                 50                     66.66667
## 24     Johannesburg                 50                     66.66667
## 25          Karachi                 25                    100.00000
## 26     Kuala Lumpur                 50                     66.66667
## 27      Kuwait City                 50                     33.33333
## 28             Lima                 50                     66.66667
## 29           London                100                    100.00000
## 30      Los Angeles                100                    100.00000
## 31           Madrid                100                    100.00000
## 32           Manila                 25                     33.33333
## 33        Melbourne                100                    100.00000
## 34      Mexico City                 50                     66.66667
## 35            Milan                100                     66.66667
## 36           Moscow                 50                     33.33333
## 37           Mumbai                 25                     66.66667
## 38         New York                100                    100.00000
## 39            Osaka                 75                     66.66667
## 40            Paris                100                     66.66667
## 41            Quito                  0                     66.66667
## 42   Rio de Janeiro                 25                     66.66667
## 43           Riyadh                 50                     66.66667
## 44             Rome                100                    100.00000
## 45    San Francisco                100                    100.00000
## 46         Santiago                 50                     66.66667
## 47        Sao Paulo                 25                     66.66667
## 48            Seoul                100                     33.33333
## 49         Shanghai                 75                     66.66667
## 50        Singapore                 75                    100.00000
## 51        Stockholm                 75                    100.00000
## 52           Sydney                100                    100.00000
## 53           Taipei                 75                     66.66667
## 54           Tehran                  0                     33.33333
## 55            Tokyo                 75                     66.66667
## 56          Toronto                100                    100.00000
## 57    Washington DC                100                    100.00000
## 58       Wellington                 75                     66.66667
## 59           Yangon                  0                     33.33333
## 60           Zurich                 75                    100.00000
##    D_In_PubPrivPartnerships D_In_TechnologyEmployed D_In_CyberSecurity
## 1                        50                     100                 50
## 2                        50                     100                 50
## 3                         0                      75                 50
## 4                         0                       0                 50
## 5                        50                     100                 50
## 6                         0                      75                100
## 7                         0                      75                 50
## 8                        50                     100                 50
## 9                        50                      75                100
## 10                        0                       0                 50
## 11                       50                      75                 50
## 12                        0                      75                 50
## 13                      100                     100                100
## 14                      100                     100                100
## 15                       50                      75                100
## 16                       50                       0                 50
## 17                       50                      75                 50
## 18                       50                     100                 50
## 19                        0                       0                 50
## 20                      100                     100                100
## 21                       50                      75                 50
## 22                        0                       0                 50
## 23                        0                      75                100
## 24                       50                      75                 50
## 25                        0                       0                 50
## 26                       50                      75                 50
## 27                        0                      75                 50
## 28                       50                      75                 50
## 29                       50                     100                 50
## 30                      100                     100                100
## 31                       50                     100                 50
## 32                       50                       0                 50
## 33                       50                     100                100
## 34                       50                      75                 50
## 35                      100                     100                 50
## 36                        0                      75                 50
## 37                       50                      75                100
## 38                      100                     100                100
## 39                       50                     100                100
## 40                       50                     100                 50
## 41                       50                      75                 50
## 42                        0                      75                 50
## 43                        0                      75                 50
## 44                       50                     100                 50
## 45                      100                     100                100
## 46                       50                      75                 50
## 47                        0                      75                 50
## 48                       50                     100                 50
## 49                        0                      75                 50
## 50                      100                     100                100
## 51                       50                     100                 50
## 52                       50                     100                100
## 53                      100                     100                100
## 54                        0                       0                 50
## 55                      100                     100                100
## 56                       50                     100                100
## 57                       50                     100                100
## 58                       50                     100                 50
## 59                        0                       0                 50
## 60                       50                     100                 50
##    H_In_EnvironmentPolicies H_In_AccessHealthcare H_In_Beds_1000
## 1                 62.790698              73.33333       8.265146
## 2                 97.674419             100.00000      38.019671
## 3                 41.860465              73.33333      38.846186
## 4                 72.093023              80.00000      16.530292
## 5                 81.395349              93.33333      24.795438
## 6                 79.069767              86.66667      47.899860
## 7                 62.790698              73.33333      11.571204
## 8                 97.674419             100.00000      52.896934
## 9                 90.697674              93.33333      38.019671
## 10                46.511628              60.00000       3.306058
## 11                23.255814              53.33333       6.612117
## 12                62.790698              60.00000       6.612117
## 13                90.697674              93.33333      19.836350
## 14                74.418605              86.66667      18.183321
## 15                90.697674              80.00000      21.572031
## 16                 4.651163              46.66667       4.132573
## 17                27.906977              80.00000       9.091660
## 18                90.697674             100.00000      66.947682
## 19                62.790698              73.33333      15.703777
## 20                97.674419              93.33333      42.152244
## 21                72.093023              60.00000      19.836350
## 22                44.186047              73.33333       6.612117
## 23                32.558140              73.33333      11.188475
## 24                86.046512              80.00000      19.836350
## 25                13.953488              66.66667       4.132573
## 26                62.790698              73.33333      14.877263
## 27                 0.000000              80.00000      17.356806
## 28                69.767442              86.66667      11.571204
## 29                83.720930              86.66667      23.142408
## 30                95.348837              93.33333      14.050748
## 31                81.395349              93.33333      24.795438
## 32                27.906977              73.33333       7.438631
## 33                81.395349             100.00000      31.407554
## 34                86.046512              86.66667      11.571204
## 35                83.720930              93.33333      27.274981
## 36                65.116279              80.00000      89.180927
## 37                32.558140              86.66667      26.448467
## 38                95.348837              93.33333      21.489379
## 39                90.697674             100.00000     100.000000
## 40                97.674419             100.00000      52.070419
## 41                23.255814              60.00000      12.397719
## 42                60.465116              73.33333      18.183321
## 43                32.558140              80.00000      12.704341
## 44                69.767442              93.33333      27.274981
## 45                90.697674              93.33333      14.050748
## 46                48.837209              80.00000      16.530292
## 47                60.465116              80.00000      18.183321
## 48                93.023256              93.33333      84.304488
## 49                79.069767              86.66667      44.544961
## 50                97.674419              93.33333      15.703777
## 51                86.046512              86.66667      21.489379
## 52                86.046512             100.00000      31.407554
## 53                97.674419              86.66667      75.328540
## 54                62.790698              66.66667       0.000000
## 55                86.046512             100.00000      77.551864
## 56                95.348837             100.00000      21.489379
## 57               100.000000              93.33333      13.224233
## 58                86.046512              93.33333      18.183321
## 59                37.209302              60.00000       4.959088
## 60                86.046512             100.00000      40.499215
##    H_In_Doctors_1000 H_In_AccessFood H_In_QualityHealthServ
## 1          16.365171            99.7                   75.0
## 2          38.000482           100.0                  100.0
## 3          73.010130           100.0                   62.5
## 4           2.327545            98.2                   62.5
## 5          43.632417           100.0                  100.0
## 6          44.609262            96.2                   62.5
## 7          16.485769            92.8                   62.5
## 8          33.405692           100.0                  100.0
## 9          42.945007            99.2                   75.0
## 10          7.392668            99.5                   37.5
## 11         20.791124            94.2                   37.5
## 12          5.028944            87.7                   37.5
## 13         30.318379            99.3                   87.5
## 14         23.299566            99.3                   87.5
## 15          6.319344            82.5                   62.5
## 16          2.267246            76.5                   37.5
## 17         21.261457            87.5                   75.0
## 18         47.322721           100.0                  100.0
## 19         11.806561            85.5                   37.5
## 20         20.489629            96.2                   87.5
## 21         18.668596           100.0                   50.0
## 22          0.000000            76.9                   37.5
## 23         24.213421            97.5                   62.5
## 24          6.825856            94.3                   50.0
## 25          7.296189            48.1                   50.0
## 26         13.024602            98.5                   62.5
## 27         21.080560            99.2                   87.5
## 28         11.034732            88.8                   62.5
## 29         31.415822           100.0                   87.5
## 30         29.232996            99.3                   87.5
## 31         43.632417           100.0                   87.5
## 32         10.962373            93.1                   62.5
## 33         38.265798           100.0                  100.0
## 34         22.551857            96.7                   50.0
## 35         45.151954           100.0                   75.0
## 36         37.445731            97.4                   75.0
## 37          6.319344            82.5                   37.5
## 38         40.243608            99.3                   87.5
## 39         30.692233           100.0                  100.0
## 40         36.493005           100.0                  100.0
## 41         17.655572            89.0                   37.5
## 42         19.910757            98.4                   62.5
## 43         23.202224            97.5                   62.5
## 44         45.151954           100.0                   75.0
## 45         29.232996            99.3                   87.5
## 46         10.033767            99.2                   62.5
## 47         19.910757            98.4                   62.5
## 48         24.481428            99.7                   87.5
## 49         28.931500            96.2                   62.5
## 50         20.646406           100.0                  100.0
## 51         47.105644           100.0                   87.5
## 52         38.265798           100.0                  100.0
## 53         14.063372            96.2                   87.5
## 54         15.557164            84.9                   62.5
## 55         36.577424           100.0                  100.0
## 56         27.448143            99.8                  100.0
## 57        100.000000            99.3                   87.5
## 58         31.970574           100.0                   87.5
## 59          4.425953            71.2                   37.5
## 60         47.190063           100.0                  100.0
##    I_In_EnforceTransportSafety I_In_PedestrianFriendliness
## 1                        100.0                         100
## 2                         70.0                         100
## 3                         60.0                         100
## 4                         52.5                          50
## 5                         82.5                         100
## 6                         77.5                          50
## 7                         40.0                         100
## 8                         67.5                         100
## 9                         62.5                         100
## 10                        60.0                          50
## 11                        40.0                           0
## 12                        57.5                           0
## 13                        77.5                         100
## 14                        77.5                          50
## 15                        37.5                           0
## 16                        30.0                           0
## 17                        77.5                          50
## 18                        75.0                         100
## 19                        65.0                          50
## 20                        77.5                         100
## 21                        30.0                         100
## 22                        65.0                           0
## 23                        60.0                           0
## 24                        35.0                          50
## 25                        30.0                          50
## 26                        50.0                          50
## 27                        62.5                          50
## 28                        37.5                           0
## 29                        87.5                         100
## 30                        77.5                         100
## 31                        82.5                         100
## 32                        42.5                          50
## 33                        77.5                         100
## 34                        42.5                         100
## 35                        72.5                         100
## 36                        67.5                         100
## 37                        37.5                           0
## 38                        77.5                         100
## 39                        82.5                         100
## 40                        87.5                         100
## 41                        55.0                           0
## 42                        70.0                         100
## 43                        60.0                           0
## 44                        72.5                         100
## 45                        77.5                         100
## 46                        45.0                         100
## 47                        70.0                         100
## 48                        72.5                         100
## 49                        77.5                          50
## 50                        82.5                         100
## 51                        75.0                         100
## 52                        77.5                         100
## 53                        77.5                         100
## 54                        67.5                         100
## 55                        82.5                         100
## 56                        77.5                         100
## 57                        77.5                         100
## 58                        87.5                         100
## 59                        50.0                           0
## 60                        75.0                         100
##    I_In_QualityRoad I_In_QualityElectricity I_In_DisasterManagement
## 1               100                      75                      75
## 2               100                     100                     100
## 3                50                      75                      75
## 4                25                     100                      75
## 5               100                     100                     100
## 6                75                     100                      50
## 7                50                      75                      50
## 8                75                     100                     100
## 9                75                     100                     100
## 10               50                      75                      50
## 11               50                      50                      50
## 12               50                      75                      75
## 13               75                     100                     100
## 14               75                     100                     100
## 15               50                      50                      50
## 16                0                      50                      25
## 17               75                      75                     100
## 18              100                     100                     100
## 19               25                      50                      25
## 20              100                     100                     100
## 21               50                      75                      75
## 22               50                      50                      50
## 23               75                     100                       0
## 24               50                      50                      75
## 25               25                      25                       0
## 26               75                     100                      75
## 27               75                     100                      75
## 28               75                     100                      75
## 29               75                     100                     100
## 30               75                     100                     100
## 31              100                     100                     100
## 32               50                      75                      25
## 33              100                     100                     100
## 34               50                      50                      75
## 35               75                     100                     100
## 36               75                     100                      75
## 37                0                      75                      50
## 38               75                     100                     100
## 39              100                     100                     100
## 40               75                     100                     100
## 41               50                      75                      25
## 42               50                     100                      75
## 43               50                      75                       0
## 44              100                     100                     100
## 45               75                     100                     100
## 46               75                     100                     100
## 47               75                      75                      75
## 48               75                     100                     100
## 49               75                     100                      50
## 50              100                     100                     100
## 51              100                     100                     100
## 52              100                     100                     100
## 53               75                      75                      75
## 54               50                      50                      75
## 55              100                     100                     100
## 56               75                     100                     100
## 57               75                     100                     100
## 58              100                     100                     100
## 59               25                      50                      25
## 60              100                     100                     100
##    P_In_PoliceEngage P_In_PoliticalStability
## 1                100                      55
## 2                100                      80
## 3                 50                      70
## 4                 50                      40
## 5                100                      70
## 6                 50                      45
## 7                 50                      65
## 8                 50                      75
## 9                 50                      60
## 10                50                      50
## 11                50                      25
## 12               100                      40
## 13               100                      80
## 14               100                      80
## 15               100                      75
## 16               100                      45
## 17               100                      60
## 18               100                      85
## 19                 0                      45
## 20               100                      60
## 21                50                      35
## 22                50                      60
## 23                50                      45
## 24                50                      65
## 25               100                      40
## 26                50                      65
## 27                50                      40
## 28                50                      65
## 29               100                      75
## 30               100                      80
## 31               100                      70
## 32                50                      50
## 33               100                      90
## 34                50                      50
## 35                50                      60
## 36                 0                      40
## 37               100                      75
## 38               100                      80
## 39               100                      85
## 40                50                      65
## 41                50                      50
## 42                50                      60
## 43                50                      45
## 44                50                      60
## 45               100                      80
## 46                50                      80
## 47                50                      60
## 48               100                      55
## 49                50                      45
## 50               100                      80
## 51                50                      85
## 52               100                      90
## 53               100                      65
## 54                50                      30
## 55               100                      85
## 56               100                      95
## 57               100                      80
## 58               100                      85
## 59                50                      50
## 60                50                      85
library(reshape2)
safeinput=melt(input,
           id.vars = 'city') 
head(safeinput)
##        city           variable value
## 1 Abu Dhabi D_In_PrivacyPolicy    50
## 2 Amsterdam D_In_PrivacyPolicy   100
## 3    Athens D_In_PrivacyPolicy    75
## 4   Bangkok D_In_PrivacyPolicy    25
## 5 Barcelona D_In_PrivacyPolicy   100
## 6   Beijing D_In_PrivacyPolicy    75
library(ggplot2)
meltgraph=ggplot(data = safeinput, aes(x = variable,
                                y =city)) 

graph1= meltgraph +  geom_tile(aes(fill = value)) 
graph1

library(ggiraph)
library(ggiraphExtra)
distanceAmong <- dist(input[,-1])
resultMDS <- cmdscale(distanceAmong,eig=TRUE, k=2)
dim1 <- resultMDS$points[,1]
dim2 <- resultMDS$points[,2]
coordinates=data.frame(dim1,dim2,city=input$city)
head(coordinates)
##         dim1      dim2      city
## 1 -21.580506  2.664374 Abu Dhabi
## 2 -84.364052  8.461246 Amsterdam
## 3   7.040038 49.402597    Athens
## 4  88.942349 26.868937   Bangkok
## 5 -78.393608  8.069760 Barcelona
## 6  15.879587 19.141895   Beijing
base= ggplot(coordinates,aes(x=dim1, y=dim2,label=city)) 
base + geom_text(size=2)

library(cluster)
set.seed(123)
resultKM = kmeans(input[,-c(1)], 
                 centers = 3)  
coordinates$cluster=as.factor(resultKM$cluster)
base = ggplot(coordinates, aes(x=dim1, y=dim2, label=city, color=cluster)) 
base + geom_text(size=2)

Part 3 (For final project):

Prepare a report of the status of your final project, informing if the data collection process is over.

I’ve finished almost three-week data collectoin and will finish my data collection on May 21th.

The passive data I collected is daily step.

The active data I collected is Sleep Time every day.

The administrative data I collected is the daily transaction.

My intervention is changing the times I take lunch to school to 0. I expect it will increase my daily transaction and also increase daily steps. Maybe the intervention will have some effect on my sleep time too, because I do not need to prepare for lunches.