Background

Major depressive disorder (MDD) and type 2 diabetes (T2D) are substantial global health burdens, occurring together at twice the frequency expected by chance [1]. Adults with MDD have a 37% higher risk of developing T2D [2], and people with T2D face a 15% higher risk of developing MDD [3].

For individuals with T2D, comorbid depression is associated with elevated risk of diabetic complications [4] and all-cause mortality [5]. The underlying mechanisms between these disorders and outcomes remain poorly understood, but could include lifestyle factors, non-adherence to T2D treatment, use of antidepressant medication or genetic factors [1].

One potential link between MDD and adverse outcomes in T2D is glycaemic control, with glycated haemoglobin (HbA1c) representing a reliable measure of long-term glycemia. Elevated and more variable HbA1c levels are associated with increased risk of long-term diabetic complications, such as stroke and cardiovascular disease [6,7,8]. Prior studies have shown mixed support for the association between MDD and HbA1c [1, 9], but many are limited to a cross-sectional design. A meta-analysis that considered longitudinal effects of depressive symptoms on HbA1c found that depressive symptoms associated with higher HbA1c, and so poorer glycaemic control, across a mean follow-up of three years (n = 3683 across six studies) [9].

Furthermore, the relative timing of MDD and T2D diagnoses may also impact T2D clinical characteristics. A cross-sectional study showed that depression diagnosed after T2D is linked with poorer glycaemic control, and a higher prevalence of diabetic complications, compared to T2D patients with no or with pre-existing depression [10].

Understanding the impact of MDD on glycaemic control across time is crucial for delivering appropriate clinical care to individuals living with both MDD and T2D. Longitudinal studies to date have not explored the relationship between the relative diagnostic timings of these disorders and HbA1c trends over time. To address this gap, our study uses UK Biobank (UKB) primary care records to perform extensive longitudinal modelling of HbA1c in people with T2D in a retrospective, observational study, using information on MDD diagnosis. In the UK, HbA1c is measured by a general practitioner (GP) every 3–6 months in individuals with T2D [11]. Therefore, the linked primary care data available in UKB provides a unique opportunity to test the longitudinal relationship between HbA1c (mean levels and variability) and MDD over a 10-year period following T2D diagnosis. Our analysis incorporates four MDD exposures:

  1. 1)

    pre-T2D MDD diagnosis (ever diagnosed with MDD prior to T2D),

  2. 2)

    time between pre-T2D MDD diagnosis and T2D,

  3. 3)

    time-varying post-T2D MDD (newly diagnosed with MDD during follow-up period), and

  4. 4)

    time since post-T2D MDD diagnosis.

By considering these exposures, we aim to gain a deeper understanding of how MDD and its timing in relation to T2D affect glycaemic control.

Methods

Study population

The UKB is a health study of ~ 500,000 individuals recruited between 2006 and 2010 in the United Kingdom, aged 40–70 [12]. Linked primary care records are available for ~ 230,000 individuals (46%), encompassing clinical events, blood test results and prescriptions, providing longitudinal patient information [13].

T2D Classification and Validation

UKB participants with primary care records were classified into T2D cases and controls. T2D cases met specific T2D diagnostic criteria (detailed below), while controls did not. The T2D diagnostic criteria were validated using T2D polygenic scores (PGSs) in European ancestry participants meeting genetic quality control criteria [14, 15] (Additional file 1: Methods S1).

Type 2 diabetes (T2D) diagnostic criteria

T2D cases were identified based on the presence of any two of the following: 1) a primary care diagnosis code for T2D (Additional file 2: Table S1), 2) an ICD9/ICD10 diagnosis code for T2D (Additional file 2: Table S2), 3) any HbA1c measurement > 48 mmol/mol (6.5%), 4) any prescription for glucose lowering medication (Additional file 1: Methods S2 [16], Additional file 2: Table S3), and 5) a self-reported diagnosis for T2D with reported age at onset > 35 years. T2D diagnosis date was then the earliest occurrence among the identified criteria. T2D cases were excluded if they had a primary care code specific to type 1 diabetes, an insulin prescription within a year of T2D diagnosis, or a prescription for multiple diabetic medications at T2D diagnosis (Additional file 1: Methods S2-S3). Individuals prescribed one diabetic medication (monotherapy) at T2D diagnosis were not excluded.

Exclusion criteria for longitudinal analysis

For the longitudinal analysis, we excluded participants meeting any of the following criteria: 1) were a T2D control, 2) had fewer than two HbA1c measurements after T2D diagnosis, 3) had age at T2D diagnosis < 18 years, 4) had no recorded HbA1c measurements > 38 mmol/mol (3.5%) [17] within a six-month window of T2D diagnosis, 5) had diagnostic codes for bipolar, psychotic or substance-use disorders [18], 6) had a MDD diagnosis without a diagnosis date, or 7) were missing self-reported ethnicity (Fig. 1).

Fig. 1
figure 1

Flow diagram of UK Biobank (UKB) participant selection. T2D = type 2 diabetes. MDD = major depressive disorder. HES = hospital episode statistics. HbA1c = glycated haemaglobin. Validation of T2D diagnosis sample = T2D case and controls of European ancestry, meeting eligibility criteria outlined in Methods, including individual-level genetic analysis inclusion criteria (described in Additional File 1: Methods S1). Imputed datasets used for analyses (2) and (3) (adjusted mixed effects model for HbA1c over time and within-individual HbA1c variation)

Outcome measures

For the T2D diagnostic criteria validation, the outcome was T2D case–control status for UKB participants of European ancestry with primary care records available. For the longitudinal analysis, the outcome was repeated measures of HbA1c (mmol/mol) after T2D diagnosis. HbA1c data were taken from: 1) primary care records up to 2017 (Additional file 2: Table S4), where older observations recorded in %-units were converted to mmol/mol [19], and 2) all UKB biomarker assessments (2006–2016), where a validated correction was applied to account for lower average HbA1c values from the UKB biomarker panel compared to primary care [20] (Additional file 1: Methods S4). The indexing date was T2D diagnosis, with a maximum follow-up period of 10 years.

Exposures

In the longitudinal analysis, four MDD exposure variables were considered simultaneously. Two were related to individuals diagnosed with MDD prior to T2D (pre-T2D MDD), and two were related to individuals diagnosed with MDD after their T2D diagnosis (post-T2D MDD).

Pre-T2D MDD exposures: (1) History of MDD at T2D diagnosis (MDD_index). This binary variable indicates whether an individual had ever received a MDD diagnosis at the index date. (2) Pre-T2D MDD duration at index (years). This semi-continuous variable quantifies the time between MDD diagnosis and T2D diagnosis when MDD_index equals 1, and is 0 otherwise. These time-invariant exposures were used to examine the impact of pre-T2D MDD on HbA1c, including interactions with T2D disease duration.

Post-T2D MDD exposures: (1) Change in MDD diagnosis (MDD_change). A time-varying binary variable indicating, at each observation time (t), whether an individual has been diagnosed with MDD between t and the index date. It captures information about individuals diagnosed with MDD during follow-up. For individuals with no MDD diagnosis occurring before t, and those diagnosed prior to T2D diagnosis, this variable is set to 0. (2) Post-T2D MDD duration (years). A time-varying, semi-continuous variable equalling the time between MDD diagnosis and t if MDD_change equals 1, and 0 otherwise. This variable allows post-T2D MDD participants to have different HbA1c time-slopes after their MDD diagnosis. Note, UKB participants classified as having a MDD diagnosis required at least two diagnostic codes for a depressive disorder or episode diagnosis in the linked primary care records [18] (Additional file 1: Methods S5).

Covariates

Covariates were extracted from UKB assessments and/or primary care data. Covariates extracted from UKB initial assessments were: sex, assessment centre, self-reported ethnicity, Townend Deprivation Index (TDI), qualifications, ever smoked, and never consumed alcohol. Covariates extracted from both UKB assessments and primary care data were: age at T2D diagnosis, HbA1c at T2D diagnosis, body mass index (BMI) at T2D diagnosis (Additional file 1: Methods S6 [21, 22]), systolic (SBP) and diastolic (DBP) blood pressure at T2D diagnosis (Additional file 1: Methods S6), number of HbA1c, BMI and blood pressure measurements taken prior to T2D diagnosis, and T2D disease duration (time since diagnosis). Covariates extracted from primary care only were diabetic medications. Glucose lowering medication at each HbA1c observation was identified using prescription records up to three months prior to HbA1c measurement (Additional file 1: Methods S2). This information was grouped into four medication categories: 1) M0 (‘no medication’), 2) M1 (‘metformin or a single medication’/ monotherapy), 3) M2 (‘two medications’/ dual-therapy), and 4) M3 (either ‘3 or more medications’ or ‘insulin’). Two medication variables were then created. Firstly, a binary variable indicating monotherapy at T2D diagnosis versus no prescribed T2D medication (M0 vs M1 at diagnosis; recall exclusion criteria removed individuals prescribed multiple medications at diagnosis). Secondly, a time-varying medication variable using the four categories M0–M3.

Apart from T2D disease duration and time-varying medication, covariates were treated as baseline measurements, but it is important to note that the index date and UKB assessment dates are different. For example, measurements from UKB initial assessments (TDI, qualifications, etc.) were collected between 2006 and 2010, and 56% of individuals were diagnosed outside of this timeframe. However, results from models with and without UKB initial assessment covariates yielded similar conclusions (Additional file 2: Table S5). Further details on covariates are available in Additional file 2: Table S6 [18,19,20,21,22,23].

Statistical analysis

All analyses were performed using R version 4.2.2 and visualised using ggplot2.

Validation of T2D diagnostic criteria

To validate the T2D definition, we tested whether PGSs for T2D [24] predicted T2D case–control status. PGSs, calculated using PRSice v2 [25, 26] at eleven P-value thresholds, were tested for association with T2D case–control status, adjusting for six genetic ancestry principal components, assessment centre and genetic batch effect (Additional file 1: Methods S7).

Longitudinal analysis

We employed linear mixed effects models (MEMs) to investigate longitudinal associations between HbA1c and MDD using the nlme package [27, 28]. All models incorporated random intercepts and time slopes, with a continuous-time autoregressive 1 (CAR1) residual correlation structure to account for autocorrelation. A series of three analyses were performed.

(1) Unadjusted model with selection

This analysis focused on the primary fixed effects of time (T2D disease duration) and the MDD exposures. Time was included using a restricted cubic spline (RCS) with four knots to allow for a non-linear temporal trend in mean HbA1c [29, 30]. Interactions between the pre-T2D MDD exposures and time-splines were considered, with the impact of post-T2D MDD on HbA1c time-slopes captured by post-T2D MDD duration. Selection of the final model was determined by Akaike Information Criterion (AIC) and likelihood ratio tests (LRTs). Models with semi-continuous exposures required their corresponding binary indicator to be included.

(2) Adjusted model

The selected unadjusted model was extended to include covariates, plus interactions between the time-splines and HbA1c at T2D diagnosis, BMI at T2D diagnosis and the medication variables.

(3) Residual within-subject variation in HbA1c

To explore the association between within-subject variation and MDD, the adjusted model was updated to allow residual variation to differ by MDD diagnosis variables. Firstly, we assessed if pre-T2D MDD (MDD_index) individuals had different residual variation compared to other participants. Secondly, we used MDD_change to investigate if post-T2D MDD individuals had different residual variation after their MDD diagnosis. Thirdly, a retrospective, time-invariant binary indicator for post-T2D MDD individuals was introduced to assess any differences in within-subject variation over all of follow-up. Please see Additional file 1: Methods S8 [27,28,29,30] for a mathematical explanation of these models. Likelihood ratio tests (LRTs) were used to test for MDD diagnosis-related heterogeneity in residual variation.

HbA1c at T2D diagnosis had a high level of missingness (40%), but all included participants had a HbA1c measurement within a six-month window of this date. Multiple imputation (MI) with the Amelia package [31] generated 50 MI datasets (Additional file 1: Methods S9) that were used in analyses (2) and (3). MEM estimates were pooled using Rubin’s method, with pooled Wald-like P-values presented for the fixed effects [32, 33]. Parameter estimates for RCSs are difficult to interpret. Therefore, in addition to a summary of the MDD exposure fixed effects (including P-values from t-tests), results are presented using plots of predicted HbA1c [34, 35]. Details of covariates included in MI and/ or MEMs are given in Additional file 2: Tables S6-S7.

Results

Validation of T2D diagnostic criteria

The eligible UKB sample consisted of 17,712 individuals with T2D (Fig. 2), with 24% of individuals having evidence of T2D from all five data sources considered. Participants with T2D tended to be male (61%) with an average age at T2D diagnosis of 57 years (IQR: 51–64 years). Applying genetic quality control criteria to participants of European ancestry provided 13,799 T2D cases and 73,002 controls. T2D PGSs were significantly associated with T2D case–control status at all P-value thresholds considered and explained up to 2.8% of T2D liability (Additional file 3: Table S1).

Fig. 2
figure 2

Contributions of each input to the T2D phenotype definition. Horizontal bars indicate the number of individuals who met criteria for the corresponding T2D sub-phenotypes. Vertical bars indicate the number of individuals endorsing combinations of the five T2D sub-phenotypes

Longitudinal analysis

In total, 11,837 T2D cases were included, with a median follow-up time of 6.9 years (IQR: 3.5–9.3 years). Table 1 presents sample characteristics stratified by MDD subgroup: ‘no MDD’ (89% without an MDD diagnosis), ‘pre-T2D MDD’ (9% with MDD diagnosed prior to T2D) and ‘post-T2D MDD’ (2% diagnosed with MDD during the 10-year follow-up period). Groups with a diagnosis of MDD include a higher proportion of females and have higher median TDI, which is particularly notable in the post-T2D MDD group (Additional File 4: Figure S1). Additionally, the post-T2D MDD group had an earlier median age at T2D diagnosis, leading to a longer median follow-up time (Additional File 4: Figures S2-S3). This group also had the highest proportion of patients in medication group M3 (insulin and/or three or more T2D medications) by the end of follow-up. A higher proportion of the post-T2D MDD group are missing HbA1c at diagnosis (52% compared to 43% for the pre-T2D MDD group and 42% for the no MDD group). See Additional file 4: Figures S1-S2 and S4-S8 for density plots of continuous covariates included in the MEMs.

Table 1 Characteristics of the longitudinal analysis study participants, stratified by MDD subgroups. Columns present N (%) for categorical variables and median (IQR) for continuous variables

Unadjusted model

The selected unadjusted model included time-splines and the four MDD exposures, with no significant interaction between the pre-T2D MDD exposures and time (p = 0.3072; Table 2). This implies the difference in mean HbA1c between pre-T2D MDD individuals and individuals without MDD depends on the time between MDD and T2D diagnoses, the effect of which is constant over T2D disease duration. Having a history of MDD prior to T2D is associated with a 0.92 mmol/mol increase (95% CI: [0.00, 1.84]) in HbA1c, but earlier onset of pre-T2D MDD is associated with lower HbA1c (Table 3). To illustrate this, Fig. 3 plots predicted HbA1c (mmol/mol) over time for four example individuals with either no MDD or with MDD diagnosed 2.2 years, 10.7 years, or 27.5 years before T2D (10th, 50th and 90th percentile of pre-T2D MDD duration at index respectively). Differences in HbA1c between these three pre-T2D MDD individuals and the no MDD individual at any point during follow-up are 0.72 (95% CI: [-0.11, 1.55]), -0.06 (95% CI: [-0.65, 0.54]) and -1.60 (95 CI: [-2.63, -0.57]) for the 10th, 50th and 90th percentile of pre-T2D MDD duration respectively. Those diagnosed with MDD ~ 10 years before T2D have HbA1c levels over time equivalent to those without MDD, with those diagnosed earlier having lower HbA1c and those diagnosed closer to their T2D diagnosis having higher HbA1c. Our example shows that only individuals diagnosed with MDD many decades before T2D have a 95% CI for difference in HbA1c relative to no MDD that excludes 0 (Additional file 3: Table S2a).

Table 2 Unadjusted model selection summary. Model 2, with mean HbA1c being a function of time (four knot restricted cubic spline for T2D disease duration), the pre-T2D MDD exposures and the post-T2D exposures, has the lowest AIC and is selected using likelihood ratio tests (LRTs)
Table 3 Fixed effect estimates for MDD exposure variables from the unadjusted and pooled adjusted linear mixed effects models
Fig. 3
figure 3

Predicted HbA1c (mmol/mol) with 95% confidence intervals from the unadjusted model, for four example individuals: no MDD and three pre-T2D MDD individuals diagnosed with MDD 2.2 (10th percentile), 10.7 (median) and 27.5 (90th percentile) years before T2D

For post-T2D MDD individuals, the unadjusted model included a small, non-significant decrease in HbA1c of -0.50 mmol/mol (95% CI: [-1.63, 0.64]) upon MDD diagnosis (Table 3). This initial MDD diagnosis effect is retained in the model due to the significant semi-continuous post-T2D MDD duration variable, capturing time since post-T2D MDD diagnosis (Table 3). For two post-T2D MDD individuals where MDD is diagnosed one year apart, HbA1c is expected to be 0.53 mmol/mol higher (95% CI: [0.19, 0.87]) in the individual diagnosed with MDD one year earlier (Table 3).

Figure 4 presents predicted HbA1c over time for an individual without MDD and three example individuals diagnosed with MDD 1 year, 3.8 years and 7.1 years after T2D (10th, 50th and 90th percentile respectively). The smaller sample size for the post-T2D MDD group is reflected in wider 95% CI after MDD diagnosis. An initial decrease in HbA1c at MDD diagnosis is followed by a steeper increase in HbA1c, so that a post-MDD individual will have a higher HbA1c three years after MDD diagnosis compared to an individual with no MDD diagnosis (i.e. the 95% CI for the difference between predicted HbA1c for a post-T2D MDD individual and an individual without MDD is expected to exclude 0 after three years). Individuals diagnosed with MDD earlier in their follow-up are expected to have higher HbA1c levels after a 10-year T2D disease duration compared to those with later onset MDD or those without MDD diagnoses (Additional file 3: Table S2b). For example, at the end of the 10-year follow-up, an individual diagnosed with MDD one year after T2D is predicted to have a HbA1c of 60.49 (95% CI: [57.83, 63.15]) compared to 56.19 (95% CI: [55.78, 56.61]) for those without MDD.

Fig. 4
figure 4

Predicted HbA1c (mmol/mol) with 95% confidence intervals from the unadjusted model, for four example individuals: no MDD and three post-T2D MDD individuals diagnosed with MDD 1 (10th percentile), 3.8 (median) and 7.1 (90th percentile) years after T2D

Adjusted model

After adjustment for all covariates, the absolute effect sizes for all MDD exposure variables were reduced (Table 3). In particular, the pre-T2D MDD duration effect size is no longer significant (p = 0.1637) suggesting that, given the included covariates, a history of MDD prior to T2D has no effect on HbA1c trends over T2D disease duration.

For the post-T2D MDD exposures, the pooled results from the adjusted model are similar to those from the unadjusted model. The adjusted model suggests a small, non-significant decrease in HbA1c after a diagnosis with MDD during follow-up (p = 0.2555). The adjusted effect size for post-T2D MDD duration is 0.51 (95% CI: [0.17, 0.86]), showing that earlier MDD diagnosis during follow-up is still associated with higher HbA1c. Full model summaries can be found in Additional file 3: Tables S3 and S4 for the unadjusted and adjusted model respectively.

Within-patient HbA1c variability

Non-convergence in the MI datasets was common for models allowing residual variation to differ by MDD_index (24%) and MDD_change (48%). Pooled results from converged models showed that within-patient variability in HbA1c was approximately the same when comparing pre-T2D MDD individuals (using MDD_index) to all others, and post-T2D MDD (using MDD_change) to all others (Table 4). However, when looking retrospectively for post-T2D MDD individuals (i.e. looking across all follow-up rather than only after MDD diagnosis), we found that within-patient HbA1c variation was 1.16 times higher (95% CI: [1.13. 1.19]) for the post-T2D MDD group compared to no MDD and pre-T2D MDD. This suggests that differences in HbA1c for those who develop MDD after T2D, compared to those with no or pre-existing MDD, start prior to MDD diagnosis.

Table 4 Pooled hypothesis testing for within-subject variability in HbA1c differing by MDD diagnosis. The presented parameter estimate for each MDD diagnosis variable represents the ratio of the residual variance estimate for the MDD level relative to the reference

Discussion

This study tested the hypothesis that people with both T2D and MDD have poorer diabetic control over the course of T2D, as assessed by routine primary care monitoring of HbA1c, focusing on the role of timing and history of MDD diagnosis relative to T2D. We examined whether UKB participants with T2D and MDD have higher and more variable HbA1c levels over time compared to those with T2D alone. To do so, we utilised exposure variables that differentiated between individuals with a history of MDD before their T2D diagnosis (pre-T2D MDD) and those who received their MDD diagnosis after their T2D diagnosis (post-T2D MDD), while also incorporating time since MDD diagnosis.

For individuals with pre-T2D MDD, longitudinal modelling across 10 years of follow-up found that the time between MDD and T2D diagnoses was informative about HbA1c levels across T2D disease duration. Specifically, individuals diagnosed with MDD decades prior to T2D had lower HbA1c over time compared to individuals without MDD and those diagnosed closer to their T2D diagnosis date. After adjusting for covariates, the pre-T2D MDD variables became non-significant, which may be partly ascribed to the correlation between the time gap from pre-T2D MDD diagnosis to T2D and the age at T2D diagnosis (Spearman’s \(\rho =0.23\)). Here, a longer gap between MDD and T2D diagnoses correlates with a later age at T2D diagnosis (Additional file 3: Table S5). However, the non-significance of pre-T2D MDD exposures after covariate adjustment may also be partly attributed to mediation rather than confounding. For example, increased numbers of pre-T2D measurements for blood pressure, BMI and HbA1c (a proxy for history of healthcare utilisation) were associated with lower HbA1c at T2D diagnosis (Additional file 3: Table S6). Given that an earlier diagnosis with MDD relative to T2D leads to more pre-T2D measurements (Additional file 3: Table S5), it is plausible that mediation is partially responsible for the loss of significance for pre-T2D MDD duration, through increased contact with healthcare professionals. Further work on the impact of pre-T2D MDD is therefore warranted, with extension to additional features of MDD beyond timing of initial diagnosis (e.g. most recent episode, antidepressant prescriptions and number of episodes).

For individuals diagnosed with MDD after T2D, both the unadjusted and adjusted models found that time since MDD diagnosis was important in shaping the trajectory of HbA1c. The earlier the diagnosis of MDD during follow-up, the greater the expected difference in HbA1c between post-T2D MDD individuals and those without MDD at the end of follow-up. Post-T2D individuals also demonstrated some differences in within-patient HbA1c variation. Results suggest that, given the model considered, after diagnosis with MDD during follow-up, within-patient variation does not differ from no MDD and pre-T2D MDD. However, if we look retrospectively across all follow-up and compare post-T2D MDD individuals to all others, we observe that within-patient HbA1c variation is 1.16 times higher for the post-T2D MDD individuals. This greater variability in HbA1c, driven by observations before MDD diagnosis, merits further investigation to ensure appropriate public health and clinical advice is available. Post-T2D MDD individuals may indeed have higher HbA1c variability before MDD diagnosis. This is important because increased variability is associated with increased likelihood of adverse outcomes, including microvascular disease [36]. However, this result may also arise from mean HbA1c trends for post-T2D MDD individuals deviating from those with no or pre-existing MDD before their MDD diagnosis. Given that MDD diagnosis requires the presence of symptoms for two or more weeks, it is plausible that the impact of MDD symptoms on HbA1c begin before formal diagnosis. Additionally, diagnostic delays, as demonstrated in a study of primary care records in Spain (mean delay of 9.89 weeks) [37] may be present. Therefore, exploring the feasibility of using HbA1c trends for MDD prediction in individuals with T2D could be an interesting avenue of research.

Previous work on mean HbA1c has provided inconclusive evidence for the impact of MDD [9]. Nevertheless, in line with our results, two larger studies did find associations between mean HbA1c over time and depression. A meta-analysis found a modest association between depressive symptoms and HbA1c (3-year mean follow-up) [9], and a prospective study in veterans with T2D found that HbA1c was slightly higher for individuals with depression (5-year follow-up) [38].

A previous study [10] showed differences in T2D clinical characteristics by timing of MDD and T2D diagnoses, with a higher rate of diabetic complications in the post-T2D MDD group, but not the pre-TDD MDD group, compared to the no MDD group. Differences in glycaemic control between these MDD subgroups is a possible explanation for the observed clinical differences. The observed increase in HbA1c levels with time since MDD diagnosis for post-T2D MDD individuals, along with the retrospectively identified higher variability across follow-up, supports this hypothesis, demonstrating the potential importance of the relative timing of MDD onset.

Our analysis revealed a linear effect for time since post-T2D MDD diagnosis, meaning the impact of post-T2D MDD increases with years since MDD diagnosis. Due to the smaller sample size of the post-T2D MDD group (n = 226), this study may have been underpowered to detect non-linear trends for this post-T2D MDD duration variable (Additional file 1: Methods S10). Future work using larger samples and different methods of capturing the time between T2D and MDD diagnoses (e.g. joint models for HbA1c over time and time-to-MDD diagnosis) may identify such trends and determine if patients diagnosed with MDD after T2D require closer monitoring, or whether a specific time window is crucial for glycaemic control.

MDD episodes after T2D diagnosis are hypothesised to have a greater effect on glycaemic control, which we have confirmed by showing greater mean and within-subject variability in HbA1c levels for post-T2D MDD individuals. These individuals visited their GP after their T2D diagnosis and had a MDD code recorded. Multiple pathways, particularly behavioural, could explain this association. Several studies have shown that patients with both MDD and T2D have worse T2D self-management, are less able to keep the medical appointments, are less physically active and less able to adhere to dietary requirements, possibly leading to hyperglycaemia [39,40,41]. Our study highlights the importance of diagnosis timing, with possible need for targeted interventions based on clinical history.

This study has several limitations. Firstly, primary care data are collected when patients visit their GP, and between-patient differences in this visiting process can bias results [42, 43]. Individuals with T2D should see their GP every 3–6 months [11] and differences may affect results. For example, less healthy individuals may interact with GPs more frequently and contribute more observations. The models used attempted to reduce this potential bias, as the CAR1 error structure ensured that observations measured closely in time provided less information than those taken far apart. However, T2D cases with MDD may miss more scheduled appointments, and our analysis does not account for this.

Secondly, as a retrospective observational study, confounding may bias results. For example, antidepressant medication can lead to weight gain [44], which can negatively affect HbA1c levels [45]. While we adjusted for confounders such as BMI and blood pressure, these covariates were time invariant, and the timing of measurement differed for each participant (Additional file 1: Methods S6). Future studies could consider time-varying confounding and include additional risk factors (e.g. antidepressant medications, cardiovascular conditions).

Thirdly, HbA1c at T2D diagnosis had a high level of missingness, which disproportionately affected individuals in the post-T2D MDD group and highlights the challenges of working with real-world data. While we utilised MI, this approach has limitations and depends on the model selected and data availability. However, results for the post-T2D MDD exposures were similar in the unadjusted model (no MI required) and the adjusted model (MI used).

The validity of the age at MDD onset, determined using the date of the first MDD diagnostic code, presents a further limitation. For older individuals the validity of their MDD onset is unknown, given primary care records are only available after 1990, while mean age at diagnosis of MDD is around 30 years [46]. Replication in the Clinical Practice Research Datalink, which has GP records from 1987 and no age limits, would therefore be useful [47].

Conclusions

This study, utilising UKB primary care records, highlights the importance of considering the temporal relationships between T2D and MDD in the context of glycaemic control. Findings reveal a non-linear trend in HbA1c levels over time and demonstrated that within a 10-year window, those who develop MDD after T2D diagnosis had increased HbA1c levels and, prior to MDD diagnosis, greater variability, supporting the finding that individuals with both conditions have poorer health outcomes. Regular routine screening for depression, integrated mental health support and closer monitoring may reduce the adverse consequences associated with both T2D and MDD.