R/Untitled.R

rm(list=ls())

if(!exists("completeVotes")) {
  largeData <- load("inst/extdata/UN-73new_small.RData")
}
load("inst/extdata/UN-73new_small.RData")


competitor1 = "POL"
competitor2 = "RUS"
competed = "UKR"
years = c(2005:2015)
out = "pi"
weights = c(0.5,0.5)
important=F
issue="all"

triad <- c(competitor1,competitor2,competed)

if(!exists("completeVotes")) {
  largeData <- load("inst/extdata/UN-73new_small.RData")
  print(paste0('Data Source: Voeten, Erik; Strezhnev, Anton; Bailey, Michael, 2009, "UN-73new.RData", United Nations General Assembly Voting Data, https://doi.org/10.7910/DVN/LEJUQZ/KKG7SW, Harvard Dataverse, V21 '))
}

un <-
  completeVotes %>%
  ungroup() %>%
  filter(Country %in% triad) %>%
  filter(year %in% years)

rm(completeVotes)

unwide <-
  un %>%
  spread(Country,vote) %>%
  select(rcid,triad)

match <-
  left_join(un %>%
              filter(Country %in% triad),
            unwide,
            by = c("rcid"))

# Replace vote by coding (1 = yes, 0.5 abstain, 0 no)
match2 <-
  match %>%
  mutate_at(vars(vote,triad),
            funs(ifelse(. == 1,1,
                        ifelse(. %in% c(2),0.5,
                               ifelse(. %in% c(3),0,NA)))))

match3 <-
  match2 %>%
  mutate_at(vars(triad),
            funs(abs(vote - .))) %>%
  select(-vote)

match4 <-
  if(important==T) match3[match3$importantvote==1,] else match3

match5 <-
  if(issue=="all") match4 else
    if (issue=="me") match4[match4$me==1,] else
      if (issue=="nu") match4[match4$nu==1,] else
        if (issue=="di") match4[match4$di==1,] else
          if (issue=="hr") match4[match4$hr==1,] else
            if (issue=="co") match4[match4$co==1,] else
              if (issue=="ec") match4[match4$ec==1,]

match6 <-
  match5 %>%
  group_by(Country,year) %>%
  summarise_at(vars(triad),
               funs(mean(.,na.rm = T)))


dist_DON_DON <-
  match6 %>%
  ungroup() %>%
  filter(Country == competitor1) %>%
  select(year,Country,competitor2) %>%
  gather(Country2,value,-year,-Country)

epsilon1 <-
  match6%>%
  ungroup() %>%
  filter(Country == competed) %>%
  select(-competed)

epsilon2 <-
  purrr::pmap(list(epsilon1 %>% pull(competitor1),
                   epsilon1%>% pull(competitor2)),
              function(a,b){
                ((abs(a-b)-1)*-1)
              }) %>%
  unlist() %>%
  data.frame(epsilon1,epsilon = .)

names(epsilon2)[3:4] <- c("competition1","competition2")

pi <-
  data.frame(dist_DON_DON,epsilon2 %>%
               rename(competed = Country) %>%
               select(-year)) %>%
  mutate(pi = weights[1]*value * weights[2]*epsilon)
#mutate(pi = value * epsilon)

pre_out <- pi %>% select(year,
                         competitor1 = Country,
                         competition1,
                         competitor2 = Country2,
                         competition2,
                         competed,
                         delta = value,
                         epsilon,
                         pi,
)

if(out == "all") output <- pre_out else
  if(out == "pi") output <- pre_out %>% select(-delta,
                                               -epsilon,
                                               -competition1,
                                               -competition2) else
                                                 if(out == "epsilon") out <- pre_out %>% select(-delta,
                                                                                                -pi,
                                                                                                -competition1,
                                                                                                -competition2) else
                                                                                                  if(out == "delta") out <- pre_out %>% select(-pi,
                                                                                                                                               -epsilon,
                                                                                                                                               -competition1,
                                                                                                                                               -competition2)


return(output)
library(microbenchmark)
rm(list=ls())
load("inst/extdata/UN-73new.RData")
names(completeVotes)
completeVotes<-completeVotes[c("rcid","Country","vote","year","importantvote","me","nu","di","hr","co","ec")]
save(completeVotes, file = "inst/extdata/UN-73new_small.RData")
save(completeVotes, file = "inst/extdata/UN-73new_smallest.RData", compress="xy")


system.time({
  result <-
    load("inst/extdata/UN-73new.RData")
  print(result)
})

system.time({
  result <-
    load("inst/extdata/UN-73new_small.RData")
  print(result)
})


table(df$importantvote)
names(df)
data_small<-as.data.frame(data_small)

rm(list=ls())


load("inst/extdata/UN-73new_small.RData")
names(df)

# original data
user  system elapsed
6.490   0.111   6.616

# subsetted data
user  system elapsed
2.989   0.068   3.064

# subsetted data xz compressed
user  system elapsed
3.574   0.104   3.695
LuMesserschmidt/UNPC documentation built on Nov. 25, 2019, 8:17 a.m.