
#### OUTSIDE LONDON WARDS ####

Outside.London <- function(lookup,wards.to.project,LBound.Age,UBound.Age,Years.to.Project,census.ward.pop,snpp){
 
        #Add borough GSS code to wards to project
        wards.to.project$Borough <- lookup[match(wards.to.project$HomeWard,lookup$Ward),3] 
        setnames(wards.to.project,c("Ward","Borough"))
        
                        
        #Group census sya ward data into age cohort
        census.ward.pop <- filter(census.ward.pop, Age %in% c(LBound.Age:UBound.Age)) %>%
                group_by(Ward)%>%
                summarise(Popn = sum(Popn))
        
        
        #Group snpp sya ward data into age cohort
        snpp <- filter(snpp, snpp$Age %in% c(LBound.Age:UBound.Age)) %>%
                select(-areaname,-areatype,-AgeGroup,-sex) %>%
                group_by(areacode) %>%
                summarise_each(funs(sum)) %>%
                setnames("areacode","District")
        
               
        #calculate year on year growth rates for boroughs
        borough.growth <- cbind(select(snpp,District),
                   lapply(3:ncol(snpp), function(x) (snpp[x]-snpp[x-1])/snpp[x-1]))                       
        

        #snpp projects to 2037, GLA to 2041
        #extra years are added by rolling forward 2037 rates
        
        last.Column <- ncol(borough.growth)
        extra.Columns <- Years.to.Project - last.Column
        
        borough.growth <- cbind(borough.growth,
                                lapply((last.Column + 1):(last.Column + extra.Columns),
                                 function(x) borough.growth[last.Column]))  %>%
                as.data.frame() 
        
                    
        #apply borough growth rates to wards to approximate ward population changes
        
        ward.growth <- left_join(wards.to.project, borough.growth, by=c("Borough"="District"))
                        
        EW.ward.projection <- left_join(wards.to.project, census.ward.pop, by="Ward") %>%
                select(Ward,Popn)
        
                
        for(x in 2:Years.to.Project){
        
                EW.ward.projection[x+1] <- EW.ward.projection[x] * (ward.growth[x+1] + 1)
        
        } 

return(EW.ward.projection)

}
        