confidence_intervals_derivation
Source:vignettes/confidence_intervals_derivation.Rmd
confidence_intervals_derivation.RmdOverview
Given an outcome with a set of predictors a simple linear regression model is of the form,
where is a normally distributed i.i.d. random variate. Generalizing to multiple predictors in martix notation this can be described as,
Where in Equation @ref(eq:matrix-linear-regression) is the design matrix of form,
The vector of parameters is
and the vector of responses is
Combining with the pseudo-inverse leads to the following estimator for the coefficients,
where is the design matrix and is the vector of responses. Given this estimator is composed of the random variate a natural question is determining its variance. We can apply the variance operator to Equation @ref(eq:beta-estimator) and using the property of the variance, for a constant matrix ,
where we have also applied the IID assumption of the error terms with variance . Since this value is unknown it can be directly estimated from the residuals producing the final estimate of the variance,
Given that the estimator for the response is , the variance of the response estimator is,
Variance calculation for a generalized linear model
A generalized linear model is an extension of a linear model that includes a link function to define the expected value of the response according to the following relationship,
The results for the variance estimator of the coefficients follows as above, however in order estimate the variance of the response estimator we need to apply the delta method for a non-linear function applied to a random variable ,
For a Poisson or negative binomial model is the exponential function and it follows that,
This can be similarly calculated for other mean functions used for other distributions. for example for the Bernoulli, Binomial, categorical, or multinomial distributions the link function is a Logit and the gradient for the corresponding is,
For an exponential of gamma distribution model the corresponding gradient of is,
Incorporating a generalized additive model term
Generalized Additative Models (GAMs) in addition to linear terms fit general spline terms based on a series of basis functions . As each term in the design matrix is transformed by these basis functions it is straightforward to extend the above estimate of the variance for to a GAM with the final estimate,
Where is the transformed design matrix.
Calculating the variance of the difference of random variables
The variance of the difference for two random variables that are not independent (say and ),
This can be calculated directly from Equation @ref(eq:hat-var-y) using the constant vector as the following quadratic form,
In general a statement on the variance of some linear combination of the estimated value of the response can be calculated as a quadratic form of the variance estimator for and a constant vector . For example if the goal was to calculate the difference in the sum of one set of responses to another set of responses and given the ordering of the responses as , the appropriate vector is,
The above idea can be extended given a variance-covariance matrix of the predictor for a vector of predicted values, where the first is treated as a baseline comparator and the subsequent responses are treated as the comparisons i.e.
and we wish to estimate the variance for where then we may extend Equation @ref(eq:quad-u) to its matrix form,
where
In long form the matrix has the following structure,
the variance of the vector of differences is then the diagonal,
In Einstein notation this is,
Given that matches with the vector for the difference between and we see the above vector correctly matches with the required vector of variances.
Estimating the total difference under a counterfactual scenario
When estimating for a counterfactual scenario we can apply Equation @ref(eq:quad-u) to a set of random variables representing the baseline scenario and another set of random variables representing the counterfactual scenario. Generate a vector of random variables representing the baseline scenario by setting the counterfactual terms to zero and generating a random variable of the predicted value for at each time-point and the equivalent for the predicted value under the counterfactual ,
The vector can subsequently be defined as,
Estimation of the variance for a relative difference
for relative difference estimates the delta method approach can be extended to account for the non-linear function of the ratio of two random variables which is applied when calculating the relative difference i.e. variance estimates of the form
Define the ratio function . Performing a second order Taylor expansion around the mean for and provides the following approaximation,
For estimation of the variance we can take the first order approximation and given the definition,
where we use the approximation . Applying the first order Taylor approximation to the variance gives,
Given that and we arrive at the final result,