ICEW

Published:

```{r setup, include=FALSE} if (!require("pacman")) install.packages("pacman") #install.packages("experiment") p_load(readr, dplyr,lubridate,magrittr,textreadr,rebus,data.table, tidyr,stringi) p_load(ggplot2) p_load(alluvial, ggplot2) ``` # Read datasets with clusters ```{r} d <- fread("all_years_aijk_distances_gov_to_gov.csv", stringsAsFactors = F) d<-as.data.table(d) ``` set up data ```{r} p_load(plm) d[,key:=paste0(source,"-",target)] d1<-d[,c("key","year","positive")] ``` # Does the A-Score predict the S-Score? ```{r} dat<-fread("final-final-weights-and-s-score.csv") dat[,key:=paste0(source,"-",target)] dat1<-dat[complete.cases(dat),] dat1[,lag.a:=c(NA, a_score[-.N]), by=key] dat1[,growth.a:=(a_score-lag.a)/lag.a,by=key] dat1[,lag.s:=c(NA, s_score[-.N]), by=key] dat1[,growth.s:=(s_score-lag.s)/lag.s,by=key] dat1[,lag.positive:=c(NA, positive[-.N]), by=key] dat1[,g.positive:=(positive-lag.positive)/lag.positive,by=key] dat1[,lag.negative:=c(NA, negative[-.N]), by=key] dat1[,g.negative:=(negative-lag.negative)/lag.negative,by=key] dat1[,lag.total:=c(NA, total[-.N]), by=key] dat1[,g.total:=( total-lag.total)/lag.total,by=key] ``` Read Ciara's data: ```{r} trade<-fread("Cepii_trade_95_14.csv") ``` ```{r} set1<-merge(d,trade,by=c("key","year")) set2<-merge(set1,dat,by=c("key","year")) ``` ```{r} p_load( dplyr, plyr, ggplot2, rlang, estimatr, texreg, dotwhisker, multiwayvcov, lmtest) colnames(set1)[5]<-"positive.y" ``` Now with logs: ```{r} set2$log.FLOW_r0<-log(set2$FLOW_r0+0.1) set1$log.FLOW_r0<-log(set1$FLOW_r0+0.1) ``` ```{r} set2[,lag.a:=c(NA, a_score[-.N]), by=key] set2[,lag.s:=c(NA, s_score[-.N]), by=key] set2[,lag.positive:=c(NA, positive.y[-.N]), by=key] set2[,lag.negative:=c(NA, negative.y[-.N]), by=key] set2[,lag.total:=c(NA, total.y[-.N]), by=key] ``` # Parallel: ```{r} #devtools::install_github('kevinblighe/RegParallel') p_load(RegParallel) ``` ```{r} set2$key<-as.factor(set2$key) p_load(dummies) set_a<-set2[!is.na(set2$lag.a),] set_a<-set_a[!is.na(set_a$log.FLOW_r0),] dummy<-dummy(set_a$key) #dummy<-dcast(melt(set2, id="key"), key ~ value, drop=FALSE) m2<-cbind(set_a$lag.a,dummy) #m2.1<-m2[!is.na(m2),] #m2.1<-m2[-1,] #y<-as.matrix(set2$log.FLOW_r0) #y<-y[-1,] #mod2<-.lm.fit(y=set_a$log.FLOW_r0,x=m2) ``` ```{r} set_b<-set_a[!is.na(set_a$lag.s),] dummy<-dummy(set_b$key) #dummy<-dcast(melt(set2, id="key"), key ~ value, drop=FALSE) m1<-cbind(set_b$lag.s,dummy) #m2.1<-m2[!is.na(m2),] #m2.1<-m2[-1,] #y<-as.matrix(set2$log.FLOW_r0) #y<-y[-1,] # ``` ```{r} set_c<-set_a[!is.na(set_a$lag.total),] dummy<-dummy(set_c$key) #dummy<-dcast(melt(set2, id="key"), key ~ value, drop=FALSE) m3<-cbind(set_b$lag.total,dummy) ``` ```{r} set_d<-set_a[!is.na(set_a$lag.positive),] dummy<-dummy(set_d$key) #dummy<-dcast(melt(set2, id="key"), key ~ value, drop=FALSE) m4<-cbind(set_d$lag.positive,dummy) ``` ```{r} set_e<-set_a[!is.na(set_a$lag.negative),] dummy<-dummy(set_e$key) #dummy<-dcast(melt(set2, id="key"), key ~ value, drop=FALSE) m5<-cbind(set_e$lag.negative,dummy) ``` ```{r} set_f<-set_a[!is.na(set_a$lag.total),] set_f<-set_a[!is.na(set_a$lag.s),] dummy<-dummy(set_f$key) #dummy<-dcast(melt(set2, id="key"), key ~ value, drop=FALSE) m6<-cbind(set_f$lag.s,set_f$lag.total,dummy) ``` ```{r} set_g<-set_a[!is.na(set_a$lag.total),] set_g<-set_a[!is.na(set_a$lag.a),] dummy<-dummy(set_g$key) #dummy<-dcast(melt(set2, id="key"), key ~ value, drop=FALSE) m7<-cbind(set_g$lag.a,set_f$lag.total,dummy) ``` ```{r} set_h<-set_a[!is.na(set_a$lag.negative),] set_h<-set_a[!is.na(set_a$lag.a),] set_h<-set_a[!is.na(set_a$lag.positive),] dummy<-dummy(set_h$key) #dummy<-dcast(melt(set2, id="key"), key ~ value, drop=FALSE) m8<-cbind(set_h$lag.a,set_f$negative,set_f$positive,dummy) ``` # Models: ```{r} mod2<-.lm.fit(y=set_a$log.FLOW_r0,x=m1) mod2<-.lm.fit(y=set_a$log.FLOW_r0,x=m2) mod3<-.lm.fit(y=set_a$log.FLOW_r0,x=m3) mod4<-.lm.fit(y=set_a$log.FLOW_r0,x=m4) mod5<-.lm.fit(y=set_a$log.FLOW_r0,x=m5) mod6<-.lm.fit(y=set_a$log.FLOW_r0,x=m6) mod7<-.lm.fit(y=set_a$log.FLOW_r0,x=m7) mod8<-.lm.fit(y=set_a$log.FLOW_r0,x=m8) ```