# Chapter 6 – Age-Period-Cohort Analysis: New Models, Methods, and Empirical Applications

Table 6.3 Mortality Rates per 1,000,000 for Lung Cancer in Males in the U.S.-Observed, Fitted and Predicted Values

R codes for APC linear and ARIMA projection with prediction intervals:

```#=======step 1: data preparation
lung_mx30<-lung_mx[lung_mx\$age>25&lung_mx\$period<2000,]
#truncate data at age 25 and limit period to before 2000
aage<-seq(from=30,to=85,by=5) #age category in the lung cancer mortality data
ppyear<-seq(from=1970,to=1995,by=5)          #period category
bbyear<-seq(from=1885,to=1965,by=5)          #cohort category

#=======step 2: make projection for period years 2000 and 2005
#in order to conduct ARIMA projection, library “forecast” must be first
source(“ie.pred.r”)     #function for making projection from IE APC model.
#Click the file name to download and then unzip it.
#==linear projection
mx.pred<-ie.pred(c(2000,2005),lung_mx30\$death,lung_mx30\$exp,aage,ppyear,bbyear,type.p="wp",weights=c(0,0,0,0,1,1), type.c="10c",type.re="All")
# weighted linear projection using the last two period coefficients and the last ten cohort coefficients

#==ARIMA projection
mx.pred<-ie.pred(c(2000,2005),lung_mx30\$death,lung_mx30\$exp,aage,ppyear,bbyear,type.p="Auto",type.c="Auto",type.re="All")
# automatically select the best ARIMA model

#=======step 3: construct 95% prediction intervals from bootstrap method
source(“bootstrap.r”)          #function for conducting bootstrap.
#Click on the file name to download and then unzip it.

#==prediction intervals from linear projection
temp.linear<-sapply(1:5000,function(np,mx,exposure,age,pyear,byear,type.p,weights,order,type.c,order.c) resample.ie( c(2000,2005), lung_mx30\$death, lung_mx30\$exp, aage, ppyear, bbyear, "wp", c(rep(0,4),rep(1,2)), NULL,"10c",NULL))
#generate bootstrap samples
PI.hl<-exp(log(mx.pred)+1.96*apply(log(temp.linear),1,sd))*1000000
#upper boundary
PI.ll<-exp(log(mx.pred)-1.96*apply(log(temp.linear),1,sd))*1000000
#lower boundary

#==prediction intervals from Auto ARIMA projection
temp.auto<-sapply(1:5000,function(np,mx,exposure,age,pyear,byear,type.p,weights,order,type.c,order.c) resample.ie( c(2000,2005), lung_mx30\$death, lung_mx30\$exp, aage, ppyear, bbyear,"Auto", NULL, NULL,"10c",NULL))
#generate bootstrap samples
PI.ha<-exp(log(mx.pred)+1.96*apply(log(temp.auto),1,sd))*1000000
#upper boundary
PI.la<-exp(log(mx.pred)-1.96*apply(log(temp.auto),1,sd))*1000000
#lower boundary```