# Pointwise Nonparametric Estimation of Odds Ratio Curves with R: Introducing the flexOR Package
## 2 The Additive Model
This function utilizes an iterative process, starting from an initial value for the smoothing degree, and through three or more steps, we arrive at a value for the smoothing degree in the covariates adjusted in the model. Within the mgcv R package for GAMs, the estimation of smoothing parameters is achieved by maximizing the Restricted Maximum Likelihood (REML) score. The REML score is intricately connected to the model likelihood augmented by a penalty term. The likelihood term is contingent upon the distributional assumption of the response variable, such as binomial for binary responses. On the other hand, the penalty term encompasses the smoothing parameters associated with the smooth terms embedded in the model. The method \"GCV.Cp\" in package mgcv uses GCV for unknown scale parameters and Mallows' Cp/UBRE/AIC for known scale. The GCV formula for a GAM is the following:  
\[\text{GCV}=\frac{1}{n}\sum_{i=1}^{n}\left(\frac{y_{i}-\hat{f}(x_{i})}{1-\text{ trace}(\mathbf{S}(\lambda))/n}\right)^{2}\]  
where \(\lambda\) is the smoothing parameter that controls the amount of smoothing, \(n\) is the number of observations, \(y_{i}\) is the observed response for the \(i\)-th observation, \(\hat{f}(x_{i})\) is the predicted value for the \(i\)-th observation and \(\mathbf{S}(\lambda)\) is the smoother matrix associated with the penalty parameter \(\lambda\). The GCV criterion seeks to balance model fit and complexity by penalizing the model for the number of parameters estimated. REML and GCV.Cp are optimization problems. We utilize the mgcv package, which, in turn, employs numerical optimization techniques to determine optimal values for smoothing parameters. These values are obtained by either maximizing the REML score or minimizing the GCV.Cp criterion. The REML may exhibit a higher susceptibility to oversmoothing, and the choice among these methods depends on the specific characteristics and requirements of the data and modeling context. The criteria, implemented in the package, encompass Restricted Maximum Likelihood (REML) and a method known as GCV.Cp. GCV.Cp employs Generalized Cross-Validation (GCV) when dealing with an unknown scale parameter or an UnBiased Risk Estimator (UBRE) for a known scale parameter. UBRE essentially corresponds to a scaled AIC in the generalized case or Mallows' Cp in the additive model case. The REML approach aims to maximize the restricted likelihood of the model given the data, while GCV.Cp strives to strike a balance between the goodness of fit and the complexity of the model. It is important to note that, in our case, the confidence bands for the odds ratio (\(\widehat{\text{OR}}(x_{i},x_{i,ref})\)) are obtained considering a reference value that always needs to be defined by the user, unless predefined values such as minimum or maximum are used. Therefore, the graphical representations obtained will be different from those provided by other tools currently available, such as those found in the gam or mgcv libraries of the R software (version 4.4.0). The asymptotic variance of \(\text{Ln}\widehat{\text{OR}}(x_{i},x_{i,ref})\) can be expressed in terms of the covariance matrix of the smoother \(\hat{f}_{i}(x_{i})\): \(Var(\text{Ln}\widehat{\text{OR}}(x_{i},x_{i,ref}))=Var(\hat{f}_{i}(x_{i}))+ Var(\hat{f}_{i}(x_{i,ref}))-2 Cov(\hat{f}_{i}(x_{i}),\hat{f}_{i}(x_{i,ref}))\). Following [PERSON] and [PERSON] [5; 14], the estimate \(\hat{f}_{i}(x_{i})\) can be given by \(\hat{f}_{i}(x_{i})=S\cdot z(x_{i})=(H+G)\cdot z(x_{i})=H\cdot z(x_{i})+G\cdot z (x_{i})=\hat{\theta}x_{i}+\hat{\phi}(x_{i})\) In our context, the matrix \(S\) represents the (weighted) smoother matrix applied to the working response \(z(x_{i})\), which is obtained from the Fisher Scoring fit. Specifically, the \(S\) matrix we typically consider is based on cubic smoothing splines. This matrix \(S\) is orthogonally decomposed into a sum of two matrices: \(H\) and \(G\).