rm(list=ls())
RDatDir<-'~/Google Drive/kenya_macro_micro/'
setwd(RDatDir)
library(plyr)
library(reshape2)
library(stringr)
library(ggplot2)
#========================================================================================
load('01_kenyacrop_at89_boundaries.Rdata') #dmz has maize data
load('01_KenyaCHIRPS_83to2012_byDistrict_withSPI.Rdata')
d0<-d
d2<-subset(d,month==3)
names(dmz)[1:2]<-c('did','district')
dmz<-subset(dmz,year !=2007) #2007 has questionable data
dmz$year<-dmz$year-1 #assuming year is reporting year (calendar year+1)
d<-merge(dmz,d2)
fun.cor<-function(df,test=TRUE){
x<-df$yield
if(test==FALSE){ #no confidence intervals
mar.apr.may<-cor(x,df$precip_mam,use='complete.obs')
mar.to.aug<-cor(x,df$precip_ma,use='complete.obs')
df2<-cbind(mar.apr.may,mar.to.aug)
}
if(test==TRUE){ #no confidence intervals
mar.apr.may<-cor.test(x,df$precip_mam,use='complete.obs')
mar.to.aug<-cor.test(x,df$precip_ma,use='complete.obs')
mamlo<-mar.apr.may$conf.int[1]
mamhi<-mar.apr.may$conf.int[2]
mar.apr.may<-mar.apr.may$estimate
malo<-mar.to.aug$conf.int[1]
mahi<-mar.to.aug$conf.int[2]
mar.to.aug<-mar.to.aug$estimate
df2<-cbind(mar.apr.may,mar.to.aug,mamlo,mamhi,malo,mahi)
}
return(df2)
}
pd1<-ddply(d,'year',fun.cor,test=FALSE)
pd1<-melt(pd1,id.vars='year',variable.name='season',value.name='correlation')
p1<-ggplot(data=pd1,aes(x=year,y=correlation,color=season))+geom_line()+geom_point(color='black')+facet_wrap(~season,ncol=1)+theme_bw()+labs(title='Correlation Between Yields and Rainfall Overtime\n(by season)')
p1

pd2<-ddply(d,'year',fun.cor,test=TRUE)
pd2<-pd2[,c('year','mar.to.aug','malo','mahi')]
p2<-ggplot(data=pd2,aes(x=year,y=mar.to.aug))+geom_line(color='blue',size=1.5)+geom_pointrange(color='black',aes(ymin=malo,ymax=mahi))+theme_bw()+geom_hline(y=0,linetype=2)
p2<-p2+labs(title='Cor. Between Yields and March to August Rainfall Overtime\n(with confidence intervals around correlation estimates)')
p2
