% File apc/vignettes/CheckPrediction.Rnw
% Part of the apc package, http://www.R-project.org
% Copyright 2016 Bent Nielsen
% Distributed under GPL 3

%\VignetteIndexEntry{Reproducing HN2016}

\documentclass[a4paper,twoside,12pt]{article}

\usepackage[english]{babel}
\usepackage{booktabs,rotating,graphicx,amsmath,verbatim,fancyhdr,Sweave}
\usepackage[colorlinks,linkcolor=red,urlcolor=blue]{hyperref}
\newcommand{\R}{\textsf{\bf R}}
\renewcommand{\topfraction}{0.95}
\renewcommand{\bottomfraction}{0.95}
\renewcommand{\textfraction}{0.1}
\renewcommand{\floatpagefraction}{0.9}
\DeclareGraphicsExtensions{.pdf,.jpg}
\setcounter{secnumdepth}{1}
\setcounter{tocdepth}{1}

\oddsidemargin 1mm
\evensidemargin 1mm
\textwidth 160mm
\textheight 230mm
\topmargin -5mm
\headheight 8mm
\headsep 5mm
\footskip 15mm

\begin{document}
\SweaveOpts{concordance=TRUE}

\raggedleft
\pagestyle{empty}
\vspace*{0.1\textheight}
\Huge
{\bf Reproducing \\ Harnau and Nielsen (2016)\\ using the \texttt{apc} package}
\noindent\rule[-1ex]{\textwidth}{5pt}\\[2.5ex]
\Large
14 August 2018
\\
previous version 27 October 2016
\vfill
\normalsize
\begin{tabular}{rl}
 Bent Nielsen  & Department of Economics, University of Oxford \\
               & \small \& Nuffield College \\
               & \small \& Institute for Economic Modelling \\
               & \normalsize \texttt{bent.nielsen@nuffield.ox.ac.uk} \\
               & \url{http://users.ox.ac.uk/~nuff0078}
\end{tabular}
\normalsize
\newpage
\raggedright
\parindent 3ex
\parskip 0ex
\tableofcontents
\cleardoublepage
\setcounter{page}{1}
\pagestyle{fancy}
\renewcommand{\sectionmark}[1]{\markboth{\thesection #1}{\thesection \ #1}}
\fancyhead[OL,ER]{\sl Reproducing Harnau and Nielsen (2016).}
%\fancyhead[ER]{\sl \rightmark}
\fancyhead[EL,OR]{\bf \thepage}
\fancyfoot{}
\renewcommand{\headrulewidth}{0.1pt}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Introduction}

The purpose of this vignette is to
use the
\texttt{apc} package
version 1.2.3
to reproduce some the result in
Harnau and Nielsen (2016):
\textit{Asymptotic theory for over-dispersed age-period-cohort and extended chain-ladder models}.
This is generalises the theory presented in  
Mart\'{i}nez Miranda, Nielsen and Nielsen (2015),
from a Poisson model to an over-dispersed Poisson model.
There is also a vignette available for that paper.
The 
\texttt{apc} package
builds on the identification analysis and the forecast theory in
Kuang, Nielsen and Nielsen (2008a,b),
the development of deviance analysis for general data arrays in
Nielsen (2014).
The package is discussed in
Nielsen (2015).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Table 1: The data}

The data set is taken from Table 1 of Verrall (1991), who 
attributes the data to Taylor and Ashe (1983).
The data consists of a reserving triangle.

The data are available in the 
\texttt{apc} package.
They can be called with the command
<<>>=
library(apc)
data <- data.loss.TA()
data$response
@

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Table 2: Deviance analysis}

The deviance table can be reproduced by the following commands.
The first call has the APC model as reference.
The second call has the AC model as reference.
The third call has the Ad model as reference.
For an overview of the models, see
Nielsen (2014).
The output is wide, so only selected columns are shown.
<<result=tex>>=
apc.fit.table(data,"od.poisson.response")[,c(1,2,4,6)]
apc.fit.table(data,"od.poisson.response","AC")[,c(1,2,4,6)]
apc.fit.table(data,"od.poisson.response","Ad")[,c(1,2,4,6)]
@

Thus, Table 2 in the paper is constructed as follows.
<<result=tex>>=
Table2  <- apc.fit.table(data,"od.poisson.response")[c(1:3,5,8),c(2,1,4,6)]
Table2  <- cbind(Table2,rbind(matrix(nrow=3,ncol=2),apc.fit.table(data,"od.poisson.response","AC")[c(2,4),c(4,6)]))
Table2  <- cbind(Table2,rbind(matrix(nrow=4,ncol=2),apc.fit.table(data,"od.poisson.response","Ad")[2,c(4,6)]))
colnames(Table2)<-c("df","Dsub","F_sub,apc","p","F_sub,ac","p","F_sub,ad","p")
Table2
@

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Table 3: Estimates}

The table of estimates can be reproduced by the following commands.
The two first calls are for an APC design, the last two calls are for an AC call.

The first and the third call are for a Poisson response model, which is inappropriate here.
The second and the fourth call are for an overdispersed response model.
The point estimates are the same, but the standard deviations and p-values differ.

There was previously a bug in the calculation of the standard errors. This bug is corrected in version 1.3.2.
Thus, the standard errors reported in the published paper are incorrect as they are based on version 1.2.3.
The following code gives both the incorrect numbers in the published paper and the correct values.

<<result=tex>>=
apc.fit.model(data,"poisson.response","APC")$coefficients.canonical
apc.fit.model(data,"od.poisson.response","APC")$coefficients.canonical
apc.fit.model(data,"poisson.response","AC")$coefficients.canonical
apc.fit.model(data,"od.poisson.response","AC")$coefficients.canonical
@

Thus, Table 3 with the correct standard errors is constructed as follows.
<<result=tex>>=
Table3  <- apc.fit.model(data,"poisson.response","APC")$coefficients.canonical[,c(1,2)]
Table3  <- cbind(Table3,apc.fit.model(data,"od.poisson.response","APC")$coefficients.canonical[,c(2)])
Tab3  <- apc.fit.model(data,"poisson.response","AC")$coefficients.canonical[,c(1,2)]
Tab3  <- cbind(Tab3,apc.fit.model(data,"od.poisson.response","AC")$coefficients.canonical[,c(2)])
Tab3  <- rbind(Tab3[1:11,],matrix(nrow=8,ncol=3),Tab3[12:19,])
Table3  <- cbind(Table3,Tab3)
colnames(Table3) <- c("apc est","apc se N","apc se t","ac est","ac se N","ac se t")
Table3
@

Thus, Table 3 in the paper with the incorrect standard errors is constructed as follows.
<<result=tex>>=
Table3  <- apc.fit.model(data,"poisson.response","APC",replicate.version.1.3.1=TRUE)$coefficients.canonical[,c(1,2)]
Table3  <- cbind(Table3,apc.fit.model(data,"od.poisson.response","APC",replicate.version.1.3.1=TRUE)$coefficients.canonical[,c(2)])
Tab3  <- apc.fit.model(data,"poisson.response","AC",replicate.version.1.3.1=TRUE)$coefficients.canonical[,c(1,2)]
Tab3  <- cbind(Tab3,apc.fit.model(data,"od.poisson.response","AC",replicate.version.1.3.1=TRUE)$coefficients.canonical[,c(2)])
Tab3  <- rbind(Tab3[1:11,],matrix(nrow=8,ncol=3),Tab3[12:19,])
Table3  <- cbind(Table3,Tab3)
colnames(Table3) <- c("apc est","apc se N","apc se t","ac est","ac se N","ac se t")
Table3
@


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Table 4: Forecasts}

Table 4 with the correct standard errors is reproduced as follows.

<<result=tex>>=
ac.fit <- apc.fit.model(data,"od.poisson.response","AC")
ac.forecast <- apc.forecast.ac(ac.fit,quantiles=0.95)
Table4  <- ac.forecast$response.forecast.per[,c(1,6)]
Table4  <- rbind(Table4,ac.forecast$response.forecast.coh[,c(1,6)])
Table4  <- rbind(Table4,ac.forecast$response.forecast.all[,c(1,6)])
rownames(Table4)[19] <- "all"
Table4
@

Table 4 in the paper with the incorrect standard errors is reproduced as follows.

<<result=tex>>=
ac.fit <- apc.fit.model(data,"od.poisson.response","AC",replicate.version.1.3.1=TRUE)
ac.forecast <- apc.forecast.ac(ac.fit,quantiles=0.95)
Table4  <- ac.forecast$response.forecast.per[,c(1,6)]
Table4  <- rbind(Table4,ac.forecast$response.forecast.coh[,c(1,6)])
Table4  <- rbind(Table4,ac.forecast$response.forecast.all[,c(1,6)])
rownames(Table4)[19] <- "all"
Table4
@

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section*{References}
\begin{description}
  \item 
    Harnau, J.\ and Nielsen, B.\ (2016)
    Asymptotic theory for over-dispersed age-period-cohort and extended chain-ladder models.
    To appear in
    \textit{Journal of the American Statistical Association}.
    \textit{Download}:
    Discussion paper:
    \url{http://www.nuffield.ox.ac.uk/economics/papers/2017/HarnauNielsen2017apcDP.pdf}.
  \item 
    Kuang, D., Nielsen, B. and Nielsen, J.P. (2008a)
    Identification of the age-period-cohort model and the extended chain ladder model.
    \textit{Biometrika} 95, 979-986.
    \textit{Download}:
    Earlier version:
    \url{http://www.nuffield.ox.ac.uk/economics/papers/2007/w5/KuangNielsenNielsen07.pdf}.
  \item 
    Kuang, D., Nielsen, B. and Nielsen, J.P. (2008b)
    Forecasting with the age-period-cohort model and the extended chain-ladder model.
    \textit{Biometrika} 95, 987-991.
    \textit{Download}:
    Earlier version:
    \url{http://www.nuffield.ox.ac.uk/economics/papers/2008/w9/KuangNielsenNielsen_Forecast.pdf}.  
  \item
    Mart\'{i}nez Miranda, M.D., Nielsen, B. and Nielsen, J.P. (2015)
    Inference and forecasting in the age-period-cohort model with unknown exposure with an application to mesothelioma mortality.
    \textit{Journal of the Royal Statistical Society} A 178, 29-55.
    \textit{Download}:
    \url{http://www.nuffield.ox.ac.uk/economics/papers/2013/Asbestos8mar13.pdf}.
  \item
    Mart\'{i}nez-Miranda, M.D., Nielsen, B. and Nielsen, J.P. (2016)
    A simple benchmark for mesothelioma projection for Great Britain.
    To appear in
    \textit{Occupational and Environmental Medicine}.
    \textit{Download}:
    \url{https://www.nuffield.ox.ac.uk/economics/papers/2016/MartinezMirandaNielsenNielsen_AsbestosBenchmark.pdf}.
  \item 
    Nielsen, B.\ (2014)
    Deviance analysis of age-period-cohort models. 
    \textit{Download}:
    \url{http://www.nuffield.ox.ac.uk/economics/papers/2014/apc_deviance.pdf}.
  \item
    Nielsen, B.\ (2015)
    apc: An R package for age-period-cohort analysis.
    \textit{R Journal} 7, 52-64.
    \textit{Download}:
    \url{https://journal.r-project.org/archive/2015-2/nielsen.pdf}.
  \item
    Taylor, G.C.\ and Ashe, F.R. (1983)
    Second moments of estimates of outstanding claims
    \textit{Journal of Econometrics} 23, 37-61.
  \item  
    Verrall, R.J. (1991)                                                     
    On the estimation of reserves from loglinear models                      
    \textit{Insurance: Mathematics and Economics} 10, 75-80.                           
\end{description}


\end{document}