Created
April 12, 2013 14:31
-
-
Save jayyonamine/5372422 to your computer and use it in GitHub Desktop.
District-month predictions of violence in Afghanistan
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##programmer: JEY | |
##2/28/2013 | |
##District-month forecasts | |
##note: this takes about 5 minutes to run. It throws lots of errors due to many all zero time series, but it will eventually complete and build predictions | |
rm(list=ls()) | |
library(foreign) | |
library(forecast) | |
library(sos) | |
data<-read.dta("raw_AFG.dta") | |
month=0 | |
error.naive=0 | |
error.arfima=0 | |
start=580 | |
end=627 | |
for (k in start:end){ | |
month<-k | |
error<-matrix(data=0, nrow=317, ncol=3) | |
colnames(error)<-c("true", "naive", "arfima") | |
for (i in 1:317){ | |
test<-subset(data, newid==i &monthly<month) | |
a<-test$count | |
unif<-runif(length(a), min=0, max=.1) | |
a<-a+unif #this minor addition allows the arfima to converge | |
fit<-arfima(a) | |
y_hat<-forecast(fit, h=1) | |
y<-as.vector(y_hat$mean) | |
error[i,3]<-y | |
true<-subset(data, newid==i &monthly==month) #actual leve of violence | |
error[i,1]<-true$count | |
naive<-subset(data, newid==i &monthly==(month-1)) #naive predict of t-1 | |
error[i,2]<-naive$count | |
} | |
error<-round(error, digits = 0) | |
error.naive[k-(start-1)]<-(sum(abs(error[,2]-error[,1])))/317 | |
error.arfima[k-(start-1)]<-(sum(abs(error[,3]-error[,1])))/317 | |
} | |
error.naive | |
error.arfima | |
compare<-cbind(error.naive, error.arfima) | |
dif<-as.matrix(error.naive-error.arfima) | |
results<-cbind(compare, dif) | |
colnames(results)<-c("error.naive", "error.arfima", "difference") | |
write.csv(results, "results_district.csv") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment