{'fraction_non_alphanumeric': 0.0405178145410458, 'fraction_numerical': 0.011772067738276819, 'mean_word_length': 4.5963011599912456, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 6, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
mecor: An R package for measurement error correction in linear regression models with a continuous outcome

Measurement error in a covariate or the outcome of regression models is common, but is often ignored, even though measurement error can lead to substantial bias in the estimated covariate-outcome association. While several texts on measurement error correction methods are available, these methods remain seldomly applied. To improve the use of measurement error correction methodology, we developed mecor, an R package that implements measurement error correction methods for regression models with continuous outcomes. Measurement error correction requires information about the measurement error model and its parameters. This information can be obtained from four types of studies, used to estimate the parameters of the measurement error model: an internal validation study, a replicates study, a calibration study and an external validation study. In the package mecor, regression calibration methods and a maximum likelihood method are implemented to correct for measurement error in a continuous covariate in regression analyses. Additionally, methods of moments methods are implemented to correct for measurement error in the continuous outcome in regression analyses. Variance estimation of the corrected estimators is provided in closed form and using the bootstrap.


Introduction
Measurement error is common across research fields, affecting the measurement of outcomes as well as important covariates. When left uncorrected, this can lead to severely biased and inefficient estimates of associations between covariates and outcome variables. Several texts have been published describing the impact of measurement error and, measurement error correction methodology [1,2,3,4]. However, recent reviews by Brakenhoff et al. [5] and Shaw et al. [6] show that, in biomedical research, measurement error correction methods remain seldomly applied. Keogh et al. [7] suggest that one of the main barriers to the use of correction methods may be the lack of accessible software. Moreover, as exemplified in [8], measurement is not only common in biomedical research, but in bioinformatics, chemistry, astronomy and econometrics as well. Therefore, to facilitate and encourage the use of measurement error correction methodology, we developed mecor, an R package that provides measurement error correction methods for linear models with continuous outcomes.
In R [19], covariate measurement error correction by means of SIMEX is implemented in the package simex by Lederer et al. [20]. The R package simexaft by He et al. [21] provides SIMEX covariate measurement error correction for accelerated failure time models. A special issue of the Stata [22] Journal was published in 2003 and dedicated to measurement error models [23]. Three different methods were introduced for correction of measurement error in covariates in a generalized linear model. The rcal and eivreg procedure were introduced for RC by Hardin et al. [24], the simex and simexplot procedure were introduced for SIMEX by Hardin et al. [25] and, the cme procedure was introduced by Rabe-Hesketh et al. [26] for measurement error correction using a maximum likelihood approach. In SAS, multiple macros have been developed for measurement error correction. These macros include %blinplus, implementing the method by Rosner et al. [17]), %relibpls8, implementing the method by Rosner et al. [27], and %rrc, implementing the method by Liao et al. [28]), and the NCI method macros, implementing the methods by Kipnis et al. [29]. An overview of available software including useful web links can be found in Table 4 and 5 of the paper by Keogh et al. [7]. Although several measurement error correction methods are available in Stata and SAS, to date RC-like methods for measurement error correction in a covariate have not been implemented in an R package. Moreover, no method for measurement error correction in a continuous outcome has been implemented in R.
In this paper we present and describe mecor, an R package for measurement error correction in linear regression models with a continuous outcome. Several methods (i.e., RC, MM and maximum likelihood) are implemented to correct covariate-outcome associations for measurement error in a covariate, or in the outcome. The package mecor is flexible regarding the information that can be used to enable the measurement error correction, which can be of either of four types of measurement validation studies: an internal validation study, a replicates study, a calibration study and an external validation study. For each of these types of validation studies, standard RC, validation RC, efficient RC by Spiegelman et al. [30] and a maximum likelihood approach by Bartlett et al. [12] are implemented for measurement error correction in a covariate. For outcome measurement error correction, standard MM [1] and efficient MM [31] are available, for all different types of validation studies except replicates studies. The package mecor allows for random or systematic measurement error in a covariate, systematic measurement error in the outcome and, additionally, differential outcome measurement error in a univariable analysis. This broad spectrum of validation study types, measurement error models and correction methods in our easy-to-use software package should improve the application of measurement error corrections in research practice.
This paper is organized as follows. Section 2 introduces several measurement error models and the data structures of the four validation study types that can be used to estimate the parameters of the measurement error model. Section 3 outlines the measurement error correction methods. Section 4 introduces the functions in the package mecor. Section 5 demonstrates how the package mecor can be used in different settings using simulated example data.

Measurement error: notation, types and data structures
In this section, we introduce notation, derive expressions for the impact of measurement error on covariate-outcome associations and introduce the data structure of four different types of studies, that provide input for measurement error correction methods. Throughout, it is assumed that there is a continuous outcome Y , a continuous covariate X and a vector of k other covariates Z = (Z 1 , Z 2 , Z 3 , . . . , Z k ). We consider measurement error in one variable at a time, i.e., in the covariate, X, or in the outcome, Y and assume that the other variables in the model are measured without error. Since our focus is on studies in which we aim to estimate the covariate-outcome association, the covariate X could be the main exposure of interest or a variable that confounds the relation between the main exposure and the outcome (one of the Z variables). The parameters of interest are β = (β X , β 0 , β Z ) (with β Z a 1 × k matrix) from the linear model, where we assume that E(e) = 0 and Cov(e, X) = Cov(e, Z) = 0. This model will be referred to as the outcome model.

Types of measurement error and their impact
To quantify the impact of measurement error, we first define the assumed measurement error models. Subsequently, we outline the impact of measurement error in a covariate and the outcome on the estimates of the outcome model parameters, separately.

Covariate measurement error
Let X * denote the error-prone substitute measure of the error-free reference measure X, following the measurement error model, and assume that E(U) = 0 and Cov(U, X) = 0. We assume non-differential covariate measurement error (i.e., X * |= Y |X, Z or, equivalently, that the errors U are independent of the errors e in equation (1)). The measurement error is called 'classical' or 'random' if θ 0 = 0 and θ 1 = 1. The terms classical measurement error and random measurement error are used interchangeably in the literature. In this paper, we use the term random measurement error to refer to this type of measurement error. The measurement error is called 'systematic' for all other values of θ 0 and θ 1 .
Suppose that there is one covariate Z = Z 1 in the outcome model in (1), and that data on Y , X * and Z 1 are available to fit the linear model, In this model, the least squares estimatorsβ * = (β * X ,β * 0 ,β * Z ), are biased for β, and consistent and unbiased estimators for βΛ where Λ is the 3 × 3 calibration model matrix: A well-known special case of the calibration model matrix is the attenuation factor. In particular, when there is random measurement error in the substitute error-prone measure X * , we have β * X = λ X * β, where λ X * is called the attenuation factor [32] or regression dilution factor [33,34]. When there is more than one Z covariate in the outcome model defined by equation (1), the calibration model matrix generalizes to the following (2 + k) × (2 + k) matrix: where λ Z is a 1 × k matrix, 0 is a (1 + k) × 1 null matrix and I is a (1 + k) × (1 + k) identity matrix.

Outcome measurement error
Let Y * denote the error-prone substitute measure of the error-free reference measure Y , following the measurement error model, and assume that E(U) = 0 and Cov(U, Y ) = 0. We assume non-differential outcome measurement error (i.e., Y * |= X|Y, Z or, equivalently, that the errors U are independent of the errors e in equation (1)), unless specified otherwise. Random and systematic outcome measurement error are defined analogously to random and systematic covariate measurement error, respectively [35,36].
Suppose, again, that there is one covariate Z = Z 1 in the outcome model in (1) and that data on Y * , X and Z 1 are available to fit the linear model, If the measurement error in Y * is random, the least squares estimatorsβ * = (β * X ,β * 0 ,β * Z ) are unbiased for β. In contrast, if the error in Y * is systematic, the least squares estimatorsβ * = (β * X ,β * 0 ,β * Z ) are biased for β [1,31,36]. In order to identify consistent estimators for β by matrix multiplication, we add the integer 1 to the vectorβ * . Then, (β * , 1) are consistent and unbiased estimators for (β, 1)Θ where Θ is the 4 × 4 outcome measurement error model matrix: When there is more than one Z covariate in the outcome model defined in equation (1), the calibration model matrix generalizes to the following (2 + k + 1) × (2 + k + 1) outcome measurement error model matrix: whereΘ contains all zero's except on the diagonal and the (2 + k + 1, 2)th element.

Differential outcome measurement error in univariable analyses
We assume non-differential measurement error in the outcome in all but the following special case. Suppose exposure X is binary (e.g., in a two-arm controlled randomised trial) and that there are no other covariates Z in the outcome model defined by equation (1). Further, suppose that the measurement error in Y is differential such that the measurement error in the unexposed individuals (i.e., X = 0) is different from the measurement error in the exposed individuals (i.e., X = 1). Equivalently, let Y * be the error-prone substitute measure of the error-free reference measure Y , with mean E(Y * |Y, X) = θ X0 + θ X1 Y and variance τ 2 , for X = 0, 1. Suppose now that data on Y * and X are available to fit the linear model, In this model, the least squares estimatorsβ * = (β * X ,β * 0 ) are biased for β [31,36]. In order to identify consistent estimators for β by matrix multiplication, we again add the integer 1 to the vectorβ * . Then, (β * , 1) are consistent and unbiased estimators for (β, 1)Θ where, Θ is the following 3 × 3 differential outcome measurement error model matrix:

Validation study data structures for measurement error correction
Four types of validation studies can be used to estimate the calibration model matrix or outcome measurement error model matrix defined in section 2.1: an internal validation study, a replicates study, a calibration study or an external validation study [37,7]. The first three validation studies make use of information internal to the study cohort, whereas the fourth makes use of information external to the study cohort.

Internal validation study
In an internal validation study, the error-free reference covariate values X or outcome values Y are observed in a subset of individuals (Table 1). Table 1a shows the structure of an internal validation study for covariate measurement error. In the main study, the outcome Y , the error-prone substitute covariate X * and the covariates Z are measured in all n individuals. Additionally, in n sub individuals (n sub < n) the true covariate X is measured, assumed a random subset of the main study. As an example, suppose the true exposure of interest is visceral adipose tissue measurements (i.e., X) but that this is too expensive to obtain on all study participants and the error-prone substitute measure of waist circumference is instead collected for everyone (i.e, X * ). The same structure holds for an internal validation study for outcome measurement error, as shown in Table 1b. x n z n -

Replicates study
A replicates study can be used if the measurement error in a covariate is random, denoted by X * r . We will only use this type of study for covariate measurement error since random measurement error in an outcome does not result in biased association estimates (section 2.1). In a replicates study, the error-prone substitute covariate X * r is repeatedly measured (i.e., m times, where m ≥ 2) in all or in a random subset of individuals ( Table 2). The repeated measures are denoted by X * r 1 , . . . , X * r m . We assume that, in each individual, the same number of repeated measures was observed. Further, we assume that the measurement error in the replicates is jointly independent. Table  2a and 2b show the structure of a replicates study with full and partial replicates, respectively. In the main study, the outcome Y , the error-prone substitute covariate X * r 1 and the covariates Z are measured in all n individuals. Additionally, n sub ≤ n individuals have m replicates of the error-prone substitute measure X * r j for j = 2 . . . m. An example is the repeated measurement of several coronary risk factors in the Framingham Heart study, such as serum cholesterol, blood glucose, and systolic blood pressure [27].

Calibration study
A calibration study is a special type of sub-study where two types of error-prone substitute measurement methods are used to measure the covariate or outcome: substitute measurement prone to systematic measurement error and a substitute measurement prone to random measurement error (Table 3). Table 3a shows the structure of a calibration study for covariate measurement error. All n individuals in the main study have obtained measures of the outcome Y , the error-prone substitute covariate X * s and the covariates Z. The error-prone substitute covariate X * s is systematically different from X, or, E(X * s |X) = X (systematic measurement error). Additionally, a random subset of n sub individuals (n sub < n) have m replicates of the error-prone substitute measure X * r j , where E(X * r j |X) = X for j = 1 . . . m (random measurement error). The same structure holds for a calibration study for outcome measurement error, as shown in Table 3b. An example of an calibration study for outcome measurement error is a Table 3: Data structure of calibration studies. Two types of error-prone measurement methods are used to measure the covariate or outcome. The superscripts * r and * s indicate random and systematic measurement error, respectively.

External validation study
In an external validation study the error-free reference covariate values X or outcome values Y are observed in a small set of individuals not included in the main study ( Table 4). Table 4a shows the structure of an external validation study for covariate measurement error. In all n individuals in the main study measures are obtained of outcome Y , the error-prone substitute covariate X * and the covariates Z. Additionally, there is an external data set comprising of individuals on whom measures are obtained of the error-free reference covariate X, the error-prone substitute covariate X * and the other covariates Z. Table 4b shows the structure of an external validation study for outcome measurement error. In this setting, there is an external data set comprising of individuals of whom measures are obtained of the error-free reference outcome Y and the error-prone substitute outcome Y * . The external data set does not need to comprise measures of the covariates. An example of an external validation study for outcome measurement error is a trial designed to study the efficacy of iron supplementation in pregnant women where haemoglobin is measured in capillary blood samples (error-prone substitute measure) instead of in venous blood samples (error-free reference measure) [36].

Measurement error correction
In section 2.1, the calibration model matrix Λ and the measurement error model matrix Θ were introduced. These matrices quantify the bias in the naive analysis, i.e., the analysis that does not take the measurement error in X * or Y * into account. In the following sections, measurement error correction methods are introduced that utilize the matrices Λ and Θ. The standard method for covariate measurement error correction that uses the calibration model matrix Λ is standard regression calibration (RC) [14,15]. Standard RC Table 4: Data structure of external validation studies. An error-prone covariate or outcome is measured in the main study and the true covariate or outcome is measured in a small external set. The superscript * indicates that there is random or systematic measurement error in the variables Main study can be applied in all four types of studies from the previous section. In addition, validation RC, an adapted version of standard RC for internal validation studies, is the standard covariate measurement error correction method for internal validation studies [2]. Further, the standard method for outcome measurement error correction that uses the measurement error model matrix Θ is standard method of moments (MM) [1]. Standard MM can be applied in internal and external validation studies, and calibration studies. Standard RC and standard MM do not make the most efficient use of the information available in internal validation studies and calibration studies [2]. More efficient methods for measurement error correction methods are therefore implemented in mecor. A more efficient RC estimator, called efficient RC, was introduced by Spiegelman et al. [30]. A more efficient MM estimator was introduced by Keogh et al. [31], which is called the Buonaccorsi approach using the method of moments. For simplicity, we will refer to this method as efficient MM.
Likewise, in replicates studies, standard RC does not make the most efficient use of the information available [33]. The standard RC method is sub-optimal in terms of efficiency, since the method depends on the ordering of the replicate measurements [33]. This can be intuitively understood as follows. The standard RC regresses the mean of all but the first replicate on the first replicate, but this could as easily be exchanged with the second replicate. Therefore, different approaches are possible (e.g., maximum likelihood) [33]. [12] showed how a standard random-intercepts model can be used to obtain maximum likelihood (ML) estimates that are more efficient than standard RC, at the cost of some additional parametric assumptions, discussed in section 3.3. Section 3.1 introduces standard RC and validation RC for covariate measurement error correction, and standard MM for outcome measurement error correction. Efficient RC and efficient MM are introduced in section 3.2 and the maximum likelihood approach for replicates studies is introduced in section 3.3. When no information is available to estimate the parameters of the measurement error model, a sensitivity analysis or quantitative bias analysis can be used to analyse the sensitivity of study results to measurement error [38,39]. An approach for conducting sensitivity analyses is discussed in section 3.4.

Covariate measurement error
In standard RC, the biased least squares estimatorβ * is multiplied by the inverse of an estimate of the calibration model matrix Λ to give a consistent and unbiased estimator of β, denotedβ RC :β Standard RC can be applied using all four types of validation studies (section 2.2).
To construct the calibration model matrix Λ (see equation (4)), we estimate its components λ = (λ X * , λ 0 , λ Z ), from the linear calibration model: using least squares. Here, λ Z is a 1 × k matrix. Throughout, we assume that the calibration model matrix is correctly specified. To obtain estimates of the parameters of interest λ in an internal validation study (Table 1a) and external validation study (Table 4a), the error-free reference measure X is regressed on the error-prone substitute measure X * and the other covariates Z. To obtain estimates of the parameters of interest λ in a replicates study (Table 2a), the mean of all replicates except the first replicate (i.e., X * r 2 , . . . , X * r m ) is regressed on the first replicate X * 1 and the other covariates Z. To obtain estimates of the parameters of interest λ in a calibration study (Table 3a), the mean of the replicates X * r 1 , . . . , X * r m with random measurement error is regressed on the measurement X * s with systematic measurement error and the other covariates Z.
An adapted version of standard RC in internal validation studies is validation RC [2]. In validation RC, the outcome Y is regressed on the calibrated values X cal and Z.
The calibrated values X cal are constructed as follows: if X is observed, X cal = X, and if X is not observed, X cal = E(X|X * , Z). The parameters from the regression of Y on X cal and Z are estimates of our parameters of interest β in equation (5). Note that standard RC described above is identical to using X cal = E(X|X * , Z) for all X [7].

Outcome measurement error
In standard MM, the biased least squares estimatorβ * is multiplied by the inverse of an estimate of the outcome measurement error model matrix Θ to give a consistent and unbiased estimator of β, denotedβ MM : Standard MM can be applied using internal and external validation studies, and calibration studies (section 2.2).
To construct the outcome measurement error model matrix Θ (see equation (7)), we estimate its components θ = (θ 0 , θ 1 ) from the linear measurement error model E(Y * |Y ) = θ 0 + θ 1 Y using least squares. Throughout, we assume that the measurement error model matrix is correctly specified. To obtain estimates of the parameters of interest θ in an internal validation study (Table 1b) and an external validation study (Table 4b), the error-prone substitute measurement Y * is regressed on the error-free reference measurement Y . To obtain estimates of the parameters of interest θ in a calibration study (Table 3b), the measurement Y * s with systematic measurement error is regressed on the mean of the replicates Y * r 1 , . . . , Y * r m with random measurement error.

Differential outcome measurement error in univariable analyses
For the special case of differential measurement error, the outcome measurement error model matrix Θ (see equation (8)), can be constructed as follows. We estimate its components θ = (θ 00 , θ 01 , θ 10 , θ 11 ) from the measurement error model E(Y * |Y, X) = θ 00 + (θ 01 − θ 00 )X + θ 10 Y + (θ 11 − θ 10 )XY . This model can be fitted directly in an internal validation study (Table 1b), provided that the random internal subset includes exposed (i.e., X = 1) and non-exposed individuals (i.e., X = 0). The model can be fitted in an external validation study (Table 4b), provided that X is measured, and that exposed and non-exposed individuals are included in the external set. In a calibration study (Table 3b), the measurement with systematic measurement error is regressed on the mean of the replicates Y * r 1 , . . . , Y * r m with random measurement error and the covariate X (again, provided that the random subset with replicates with random measurement error includes exposed and non-exposed individuals).

Variance estimation
The variance of the standard RC estimator can be estimated using the multivariate delta method [17] or the zero-variance method [40]. Confidence intervals can then be obtained by constructing Wald-type confidence intervals using one of the former two methods. Additionally, confidence intervals can be obtained by the stratified bootstrap, by sampling the observations in the internal subset separately from the observations outside the internal subset. The variance of the standard MM estimator can also be estimated with the multivariate delta method, the zero-variance method or the stratified bootstrap. Additionally, for standard RC, confidence intervals forβ X RC (the first element of theβ RC ) can be obtained by the Fieller method [33]. For standard MM, confidence intervals forβ X MM andβ Z MM (the first two elements of theβ MM ) can be obtained by the Fieller method [36]. Details of these procedures can be found in the appendix section A.1.

Covariate measurement error
Efficient RC can be used in internal validation studies or calibration studies [30]. It pools the standard RC estimate with an internal estimate for β obtained in the internal validation study or calibration study.
In internal validation studies, the error-free reference covariate X is obtained in an internal subset of the main study (Table 1a). By regressing the outcome Y on X and the other covariates Z using least squares in the internal subset, one obtains an unbiased estimate for our parameters of interest β. Denote this estimator byβ I . This internal estimatorβ I can then be combined with the standard RC estimatorβ RC defined in equation (9), by taking the inverse variance weighted mean of the two estimates: whereΣ −1 β RC is the variance-covariance matrix obtained from the multivariate delta method andΣ β I is the standard variance-covariance matrix of a least squares estimator. The efficient RC estimator defined above is an unbiased, consistent and the most efficient estimator for β if sampling into the internal validation set is unbiased (e.g., if the validation study is a random subset of participants) [30].
In calibration studies, the covariate X is observed with random measurement error in an internal subset of the main study (Table 3a). If at least 2 replicates are available, an unbiased estimator for β can be obtained by using the standard RC estimator for a replicates study (see section 3.1) in the internal subset. Again, denote this estimator bŷ β I . Then, the estimate obtained from the internal subset can be pooled with the standard RC estimate following equation (12). Alternatively, an unbiased estimator for β using the replicates in the internal subset can be obtained by using the ML estimation discussed in section 3.3. Again, this estimate can then be pooled with the standard RC estimate following equation (12).

Outcome measurement error
Efficient MM can be used in internal validation studies or calibration studies [31]. It pools the standard MM estimate with an internal estimate for β obtained in the internal validation study or calibration study.
In internal validation studies, the error-free reference outcome Y is obtained in an internal subset of the main study (Table 1b). By regressing Y on the covariates X and Z using least squares in the internal subset, one obtains an unbiased estimator for β. Denote this estimator byβ I . In calibration studies, the outcome is observed with random measurement error in an internal subset of the main study (Table 3b). The internal estimatorβ I is obtained by regressing the outcome Y * ,r with random measurement error on the covariates X and Z using least squares in the internal subset. Using the outcome with random measurement error will lead to the unbiased estimation of the association under study since random outcome measurement error does not bias the association. A single measurement with random measurement error (i.e., m = 1 in Table  1b) is sufficient to obtain an internal estimate. However, if the outcome with random measurement error is observed more than once, the mean of the measures Y * r 1 , . . . , Y * r m can be used and regressed on the covariates X and Z. Subsequently, the estimate obtained from the internal subset in an internal validation study or calibration study can be pooled with the standard MM estimate following equation (12), by replacing the standard RC estimate with the standard MM estimate in the equation.

Differential outcome measurement error in univariable analyses
In internal validation studies, the internal estimatorβ I can be obtained by regressing Y on the covariates X and Z using least squares. In calibration studies, the internal estimatorβ I can be obtained by regressing the outcome Y * ,r with random measurement error on the covariates X and Z. A single measurement with random measurement error (i.e., m = 1 in Table 1b) is sufficient to obtain an internal estimate. However, if the outcome with random measurement error is observed more than once, the mean of the measures Y * r 1 , . . . , Y * r m can be used and regressed on the covariates X and Z. We assume that the internal subset is a random subset of the main study, and hence that exposed and unexposed are included in the internal subset. Subsequently, the estimate obtained from the internal subset in an internal validation study or calibration study can be pooled with the standard MM estimate following equation (12), by replacing the standard RC estimate with the standard MM estimate in the equation.

Variance estimation
The variance of the efficient RC estimator can be obtained from the following: The variance of the efficient RC estimator can also be obtained by stratified bootstrapping, by sampling the observations in the internal subset separately from the observations outside the internal subset. Confidence intervals can be obtained by constructing Wald-type confidence intervals using one of the former two variances or by stratified percentile bootstrap. The same applies for the efficient MM estimator.

Maximum likelihood estimation for replicates studies
The use of a standard random-intercepts model to obtain maximum likelihood (ML) estimates for β in replicates studies was introduced by [12]. To explain the ML method for replicates studies, we add the index i = 1, . . . , n to our notation in the outcome model: where we again assume that E(e i ) = 0 and Cov(e i , X i ) = Cov(e i , Z i ) = 0. Further, Z i = (Z i1 , . . . , Z ik ) and β Z is again a 1 × k matrix. On top of these assumptions, we also assume that the e i are normal and independently distributed. Additionally, assume that X i is normally distributed given Z i , with, In a replicates study, X i is not observed. Instead, m replicates of the error-prone measurement X * r i = (X * r i1 , . . . , X * r im ) are observed, for i = 1, . . . , n. In a full-replicates study (Table 2a), we assume that the number of replicate measurements m ≥ 2 is constant for every individual. In a partial-replicates study (Table 2b), we assume that the number of replicates m ≥ 2 is constant in the replicate sub-study and m = 1 in the main study. These measurements are assumed to follow the following random measurement error model: where we again assume that E(U ij ) = 0, Cov(U ij , X i ) = 0, and that the measurement error in non-differential, i.e., the errors U ij are independent of the errors e i in the outcome model described above. In addition, we also assume that the errors U ij are normal and independently distributed.
We consider the likelihood function when only Y i , X * r i and Z i are observed. The log likelihood can be factorized as follows: where θ = (β X , β 0 , β Z , σ 2 , ρ 0 , ρ Z , σ 2 X|Z , τ 2 ). From the assumptions that X i |Z i is normally distributed, the e i are normally distributed and that X i |Z i and e i are independent, [12] show that Y i given Z i is normal with mean δ 0 + δ Z Z i and variance σ 2 Y |Z , where δ Z is a 1 × k matrix. Furthermore, since X i |Z i and Y i |Z i are jointly normal, X i |Y i , Z i is also normal. [12] show that we can therefore write: . Then, since X * ij = X i + U ij , it follows from the above equation that, [12] and κ Z is a 1 × k matrix. Hence, X * r i given Y i and Z i follows a random-intercepts model with fixed effects of Y i and Z i , random intercepts variance σ 2 X|Y,Z and within subject variance τ 2 . The parameter vector ζ = (δ 0 , δ Z is a one-to-one function of the original model parameter vector θ = (β X , β 0 , β Z , σ 2 , ρ 0 , ρ Z , σ 2 X|Z , τ 2 ). Accordingly, [12] show that the ML estimate for ζ can be obtained by maximizing the two likelihood components of equation (13) separately. The likelihood component corresponding to f (Y i |Z i , ζ) in equation (13) can be maximized by fitting the least squares regression of Y i on Z i . The likelihood component corresponding to f (X * r i |Y i , Z i , ζ) in equation (13) can be maximized by fitting a random-intercepts model for X * r i given Y i and Z i .
An ML estimate for β can now be obtained by the following formulas: The estimatorβ ML = (β X ML ,β 0 ML ,β Z ML ) can be obtained by replacing the parameters from parameter vector ζ by their estimates in the above equations.

Variance estimation
The variance of the maximum likelihood estimator can be estimated with the multivariate delta method [12]. Confidence intervals can then be obtained by constructing Wald-type confidence intervals. Confidence intervals can also be obtained by stratified bootstrap, by sampling the observations in the internal subset separately from the observations outside the internal subset. Details of these procedures can be found in the appendix section A.2.

Sensitivity analyses
Information from a validation study may not always be available. In that case, a formal correction is not possible. Nevertheless, when measurement error in a covariate or the outcome is expected, one may check how sensitive study results are to that measurement error. Literature or expert knowledge can be used to inform this sensitivity analysis, e.g., by hypothesizing possible ranges for the parameter values of the measurement model. When random covariate measurement error is expected, speculation is needed of the values of τ 2 , i.e., the variance of the random measurement error. Additionally, when systematic covariate measurement error is suspected, speculation is needed about the parameter values of the calibration model described by equation (10). When systematic outcome measurement error is suspected, speculation is needed about the parameter values of the outcome measurement error model, described in equation (5).

The R package mecor
The R package mecor offers functionality to correct for measurement error in a continuous covariate or outcome in linear models with a continuous outcome. The main model fitting function in mecor is mecor: The function fits the linear model defined in formula, corrected for the measurement error in one of the variables. The arguments are as follows: • formula a formula object, with the response on the left of a '∼' operator and the terms, separated by + operators, on the right. This argument takes the form outcome MeasError(substitute, reference, replicate, differential) + covariates for covariate measurement error, and MeasError(substitute, reference, replicate, differential) covariates for outcome measurement error. The MeasError object can be used for measurement error correction in internal validation, replicates and calibration studies. For external validation studies or sensitivity analyses of systematic measurement error, the object MeasErrorExt(substitute, model) is used instead of a MeasError object. For sensitivity analyses of random measurement error, the object MeasErrorRandom(substitute, error) is used.
• method specifies the method used for measurement error correction. The options are "standard" for standard RC and standard MM, "valregcal" for validation RC, "efficient" for efficient RC and efficient MM, and "mle" for maximum likelihood estimation.
• B number of bootstrap samples used for standard error estimation. The default is set to 0.
An object of class mecor can be summarised using the summary function: summary(object, alpha, zerovar, fieller) The arguments are as follows: • object an object of class mecor.
• alpha a numeric indicating the probability of obtaining a type II error. Defaults to 0.05.
• zerovar a boolean indicating whether confidence intervals using the zero-variance method [40] must be printed. Only available for mecor objects fitted with method equal to "standard". Defaults to FALSE.
• fieller a boolean indicating whether confidence intervals using the fieller method [33,36] must be printed. Only available for mecor objects fitted with method equal to "standard". Defaults to FALSE.
The default summary object of an object of class mecor prints standard errors and confidence intervals obtained by the delta method. See the various 'Variance estimation' paragraphs in section 3 for a description of the methods for variance estimation.
The formula argument in mecor contains a MeasError object, a MeasErrorExt object or a MeasErrorRandom object. All three objects are described below.

The MeasError object
To correct for measurement error using an internal validation study, a replicates study or a calibration study, the formula argument in mecor contains a MeasError object on the right-hand side (covariate measurement error) or left-hand side (outcome measurement error). The MeasError object can be used for random and systematic measurement error correction, depending on the method used to correct for the measurement error in mecor: MeasError(substitute, reference, replicate, differential) with the arguments being described as follows: • substitute the error-prone substitute measurement; • reference the gold-standard reference measurement, to be used in case of an internal validation study, else NULL; • replicate (a vector of) the replicate measurement of the error-prone substitute measurement, to be used in case of a replicates study or calibration study, else NULL; • differential the binary exposure on which the outcome measurement error structure is dependent, to be used for differential outcome measurement error in univariable analyses, else NULL.
Depending on the type of validation study used, either argument reference (internal validation study) or replicate (replicates study or calibration study) should be used, but never both.

The MeasErrorExt object
To correct for measurement error using an external validation study, the formula object in mecor contains a MeasErrorExt object on the right-hand side (covariate measurement error) or left-hand side (outcome measurement error): MeasErrorExt(substitute, model) with the arguments being described as follows: • substitute the error-prone measurement; • model a fitted lm object of the calibration model in equation (10) (covariate measurement error) or the measurement error model in equation (5) (outcome measurement error). Or alternatively, a list with named arguments coef containing a vector of the coefficients of the calibration model or measurement error model and named argument vcov containing a matrix of the corresponding variancecovariance matrix. The argument vcov is not required.
The argument model is also used for conducting a sensitivity analysis by making informed guesses about the parameters of the calibration model (covariate measurement error) or measurement error model (outcome measurement error).

The MeasErrorRandom object
When random measurement error in a covariate is suspected but cannot be quantified, the MeasErrorRandom object can be used to conduct a sensitivity analysis:

MeasErrorRandom(substitute, variance)
with the arguments being described as follows: • substitute the error-prone measurement; • variance a numeric indicating the random measurement error variance in the substitute measurement, i.e., the parameter value of τ 2 in equation (2).

Examples
Seven simulated datasets are included in the package mecor. These datasets represent the data structures described in section 2.2. There is an internal validation study with covariate measurement error (icvs), an internal validation study with outcome measurement error (iovs), an internal validation study with differential measurement error (iovs diff), a replicates study (rs) and a calibration study with covariate measurement error (ccs). The dataset ecvs provides an external validation study for the icvs dataset, and the dataset eovs provides an external validation study for the iovs dataset. These datasets are described and analysed in the following sections.

Internal validation study
The dataset icvs is a simulated dataset, representing the structure of the internal covariatevalidation study shown in Table 1a. The dataset contains 1,000 observations of the continuous outcome Y, an error-prone continuous exposure X star and a continuous covariate Z. The error-free measurement of the exposure X is available in approximately 25% of the study population.
R> data("icvs", package = "mecor") R> head(icvs) The coefficients of this model will however be biased due to the measurement error in X star. The measurement error in X star can be corrected for using standard RC as follows: R> mecor(Y˜MeasError(X_star, reference = X) + Z, + data = icvs, + method = "standard") Call: mecor(formula = Y˜MeasError(X_star, reference = X) + Z, data = icvs, method = "standard") Stratified percentile bootstrap confidence intervals of the coefficients of the corrected model can be obtained by using the argument B in the function mecor. To obtain standard errors and confidence intervals using the Fieller method or zero-variance method, the arguments zerovar and fieller of the summary object are set to TRUE: In addition to standard RC, efficient RC (method = "efficient") or validation RC (method = "valregcal") can also be used to correct for the measurement error in the error-prone covariate X star. The dataset iovs is a simulated dataset, representing the structure of the internal outcome-validation study shown in Table 1b. The dataset contains 1,000 observations of the error-prone continuous outcome Y star, an continuous exposure X and a continuous covariate Z. The error-free measurement of the outcome Y is taken in approximately 25% of the study population. In addition to standard MM, efficient MM (method = "efficient") can also be used to correct for the measurement error in the error-prone outcome Y star.
The dataset iovs diff is a simulated dataset, again representing the structure of an internal outcome-validation study. The dataset contains 1,000 observations of the error-prone continuous outcome Y star with differential measurement error and a binary exposure X. The error-free measurement of the outcome Y is taken in approximately 25% of the study population. The differential measurement error in Y star can be accounted for by using the following: Efficient MM (method = "efficient") can also be used to correct for the differential measurement error in the error-prone outcome Y star.

Replicates study
The dataset rs is a simulated dataset, representing the structure of a replicates study, shown in Table 2a. The dataset contains 1,000 observations of the continuous outcome Y, three replicate measures of a continuous error-prone exposure X star 1, X star 2 and X star 3, and covariates Z1 and Z2.

R> data("rs", package = "mecor") R> head(rs)
The measurement error in the error-prone exposure X star 1 can be accounted for as follows: Maximum likelihood estimation (method = "mle") can also be used to correct for the measurement error in the error-prone exposure X1 star. Note that, in this example dataset, the coefficients of the corrected model using standard RC will differ when MeasError(X2 star, replicate = cbind(X1 star, X3 star) is used instead of MeasError(X1 star replicate = cbind(X2 star, X3 star). In contrast, the corrected estimated coefficients obtained using maximum likelihood estimation will not change when the order of replicates is changed.

Calibration study
The dataset ccs is a simulated dataset, representing the structure of the covariate calibration study, shown in Table 3b. The dataset contains 1,000 observations of the continuous outcome Y, an error-prone continuous exposure X star measured with systematic measurement error, two replicate measures of the same error-prone exposure X star 1 and X star 2 with random measurement error, and a covariate Z. The replicate measures X star 1 and X star 2 are only observed in the first 500 study participants. [c(1, 500, 501, 1000 A pooled estimate of the two estimates obtained by maximum likelihood estimation and standard regression calibration can be obtained by using method = "efficient".

External validation study
The dataset ecvs is a simulated dataset, representing the structure of the external part of the external covariate-validation study shown in Table 4a. The dataset contains 100 observations of the error-free continuous exposure X, the error-prone exposure X star and a covariate Z.
R> data("ecvs", package = "mecor") R> head(ecvs) Suppose that in the dataset icvs, the gold-standard measure X had not been observed. Using dataset ecvs, we can correct for the measurement error in X star in icvs. The first step is to fit the calibration model in the external validation study as follows:

Sensitivity analyses
Suppose that there is no error-free measure and no external validation study available for dataset icvs. To investigate the sensitivity of study results to measurement error in variable X star, informed guesses of the coefficients of the calibration model are needed. Suppose one assumes that E(X|X * , Z) = 0.9X * + 0.2Z. A sensitivity analysis can then be conducted as follows: R> data("icvs", package = "mecor") R> mecor_fit_sens <-mecor(Y˜MeasErrorExt (X_star, list(coef = c(0, 0.9, 0.2))) + Z + data = icvs, + method = "standard") R> mecor_fit_sens Call: mecor(formula = Y˜MeasErrorExt(X_star, list(coef = c(0, 0.9, 0.2))) + Z, data = icvs, method = "standard") The calibration model matrix used to correct for the measurement error in X star, is again saved as matrix in the corfit object attached to mecor fit sens: Lambda2 Lambda3 Lambda1 0.8119518 -0.01377731 0.1942796 Lambda2 0.0000000 1.00000000 0.0000000 Lambda3 0.0000000 0.00000000 1.0000000 The sensitivity analyses can be expanded to ranges of possible coefficients of the calibration model or assumed variance of the random measurement error.

Conclusion
We demonstrated how measurement error correction methods can be applied using our R package mecor. These correction methods can be used in linear models with a continuous outcome when there is measurement error in the outcome or in a continuous covariate. The package accommodates measurement error correction methodology for a wide range of data structures: internal and external validation studies, replicates studies, and calibration studies. Various measurement error correction methods are implemented in the package: standard RC, efficient RC, validation RC and correction based on maximum likelihood estimation. For standard error estimation, the delta method and bootstrap are implemented for all methods. The package also facilitates sensitivity analysis or quantitative bias analysis when no data are available to estimate the parameters of the measurement error model, but the assumption of no measurement error is not warranted. We focused on studies in which interest lies in estimating a covariateoutcome association. In other types of studies, e.g., prediction studies, considerations for measurement error correction are different and may not even require corrections [41,42]. In future updates of the package, the measurement error correction methods may be extended to time-to-event [16] and binary outcomes, and multiple variables with measurement error [17,27].

Computational details
The results in this paper were obtained using R 4.0.2. R itself and mecor are available from the Comprehensive R Archive Network (CRAN) at . matrix obtained from the uncorrected regression defined by model (6) and the last row and column contain zeros. Further,Σ B,j 1 ,j 2 is the (k + 3) × (k + 3) matrix relating the j 1 th and j 2 th column of B (similar to [17]). The so-called zero-variance variancecovariance matrix forβ can be estimated by B ′Σ (β * ,1) B. A 100(1 − α) percent confidence interval can be obtained from equation (15). Further, 100(1 − α) percent confidence intervals forφ andγ can be approximated by the Fieller method as defined in model 16, where f 0 =φ * − z 2 α/2 VAR(φ * ), f 1 = φ * /θ 1 − z 2 α/2 COV(φ * , 1/θ 1 ), f 2 = 1/λ 2 1 − z 2 α/2 VAR(1/λ 1 ) and idem forγ. Additionally, bootstrap can be used to construct confidence intervals forβ RC . Bootstrap confidence intervals are obtained by sampling the individuals in the internal adjustment set separately from the individuals not included in the internal adjustment set and taking the (100 − α) percentiles of the obtained distribution.
Differential outcome measurement error in univariable analyses. The variance-covariance matrix for the standard regression estimator (β RC , 1) can be estimated similar to non-differential outcome measurement error defined above (by using the measurement error matrices for differential outcome measurement error). Confidence intervals can then be obtained from equation (15). Bootstrap confidence intervals are obtained by sampling the individuals in the internal adjustment set separately from the individuals not included in the internal adjustment set and taking the (100 − α) percentiles of the obtained distribution.

A.2 Maximum Likelihood for replicates studies
The variance-covariance matrix for the maximum likelihood estimatorβ MLE can be approximated by the multivariate delta method [12]. Denote ζ * = (δ 0 , δ Z , σ 2 Y |Z , κ 0 , κ Y , κ Z , σ 2 X|Y,Z ), leaving the τ 2 from ζ in the main text (see section 3.3) out as this parameter is not needed for the estimation of β = (α, φ, γ). A standard result from linear mixed models is that the estimators of fixed parameters are asymptotically uncorrelated with the estimators of the variance component parameters [12]. If one further assumes that the estimators from the linear model of Y given Z are uncorrelated with the parameters estimated in the linear mixed model, it follows for large samples thatζ * is multivariate normal with mean ζ and variance covariance matrix VAR(ζ) equal to: If g : R 5+2k → R 2+k is the function that transforms ζ * to β ML = (α ML , φ ML , γ ML ), as defined in the main text, then by the multivariate delta method it follows that in large samples:β ML ∼ N(β ML , J gVAR(ζ)(Jg) ′ ),