% \VignetteIndexEntry{Knee Data - Sequential/Cumulative Random Effects Logit Models}
% \VignetteDepends{MASS, lme4, ordinal}
%\VignetteEngine{knitr::knitr}
%\VignetteEncoding{UTF-8}
\documentclass[a4paper]{article}

\title{Knee Data - Sequential/Cumulative Random Effects Logit Models}

\begin{document}

\maketitle


<<echo=FALSE,eval=FALSE>>=
options(width=80)
@

For the sequential and cumulative random effects logit models we use the knee data from "catdata". We load 
the data "kneesequential" and "kneecumulative" which are already transformed and ready for use in the sequential or 
cumulative model.

<<results='hide',eval=FALSE>>=
library(catdata)
data(kneesequential)
data(kneecumulative)
@

The covariate "Age" is centered around 30 years and a quadratic effect of "Age" is created for both data sets.

<<eval=FALSE>>=
kneesequential$Age <- kneesequential$Age - 30
kneesequential$Age2 <- kneesequential$Age^2

kneecumulative$Age <- kneecumulative$Age - 30
kneecumulative$Age2<-kneecumulative$Age^2
@

For the sequential random effects logit model with Gauss--Hermite--Quadrature the function "glmer" from "lme4" is used.

<<eval=FALSE>>=
library(lme4)
@

Now the sequential model with 25 quadrature points (option "nAGQ=25") and a random intercept is fitted.

<<eval=FALSE>>=
seqGH<-glmer(y~-1+Icept1+Icept2+Icept3+Icept4+Th+Age+Age2+(1|Person), 
             family=binomial(link=logit),data=kneesequential, nAGQ = 25)
summary(seqGH)
@

The sequential model with Penalized Quasi--Likelihood is fitted with the function "glmmPQL" from the "MASS" library.

<<eval=FALSE>>=
library(MASS)
@

Here the sequential model with Penalized Quasi--Likelihood is fitted.

<<eval=FALSE>>=
seqPQL<-glmmPQL(y ~-1+Icept1+Icept2+Icept3+Icept4+Th+Age+Age2, 
random=list(Person=~1), family=binomial(link=logit), data=kneesequential, niter=30)
summary(seqPQL)
@

The cumulative models will be fitted with "clmm" from the package "ordinal".

<<eval=FALSE>>=
library(ordinal)
@

For the sequential random effects logit model with Gauss--Hermite Quadrature the number of quadrature points is defined by the option "nAGQ=25". 
Now the model is fitted again with a random intercept as the only random effect.

<<eval=FALSE>>=
cumGH<-clmm2(as.factor(y)~1+Th+Age+Age2, random = as.factor(Person), data = 
kneecumulative, link = "logistic",nAGQ=25,start=c(-5,-3,3,5,rep(0.001,4)),Hess=TRUE)
summary(cumGH)
@

For Laplace--Approximation the option "nAGQ"  can be dropped.

<<eval=FALSE>>=
cumLP<-clmm2(as.factor(y)~1+Th+Age+Age2, random = as.factor(Person), data = 
kneecumulative, link = "logistic",start=c(-5,-3,3,5,rep(0.001,4)), Hess = TRUE)
summary(cumLP)
@

<<echo=FALSE,eval=FALSE>>=
detach(package:ordinal)
@

<<echo=FALSE,eval=FALSE>>=
detach(package:lme4)
@
\end{document}
