Let us have two linear models. Let $\alpha_i$ be real numbers same in both.
$$ LM1: Y=\alpha_0 + \alpha_1X_1+\alpha_2X_2 + \varepsilon $$
$$ LM2: Y^\prime=\alpha_0 + \alpha_1X^\prime_1+\alpha_2X^\prime_2 + \varepsilon` $$
The difference between them is only in the fact that $X_i, X^\prime_i$ are different. Now, I want to test a hypothesis that the noise distribution remains the same $H_0: \varepsilon\overset{D}{=}\varepsilon`$ (we assume noise has a zero mean and finite variance). We have available random samples, from the first one of length $n$: $(Y_1, X_1)^\top, \dots, (Y_{n}, X_{n})^\top$, and from the second one with length $m$: $(Y`_1, X`_1)^\top, \dots, (Y`_{m}, X`_{m})^\top$.
My approach: Based on these samples, we estimate $\hat{\alpha_i},\hat{\alpha_i}^\prime $ where e.g. $\hat{\alpha_i}^\prime$ is the estimation using LSE from the LM2. Now, compute the residuals. So $\hat{\varepsilon}=Y-\hat{\alpha_0}-\hat{\alpha_1}X_1 - \hat{\alpha_2}X_2$ and similarly for the second model $\hat{\varepsilon}^\prime$.
Asymptotically $\hat{\varepsilon}\overset{D}{=}\hat{\varepsilon}^\prime$, as $n,m\to\infty$ because estimates of the coefficients are consistent. BUT, for finite $n$, we did not converge yet to these limits. If I do e.g. KStest ($ks.test(rez1, rez2)$), it will reject this hypothesis too often even if it is true. Is it a correct test at least asymptotically? Do you know about some other test comparing these distributions with a proper $p-$ value?
My interest is more academic and I want a test that provably works.
Here is also some idea of the sample code I used
\documentclass{article}
\usepackage{listings}
\begin{document}
\begin{verbatim}
n=1e+04 f <- function(X1, X2){return(-1 + 1X1 + 2X2)}
X11=rnorm(n, 0,1);X12=rnorm(n, -1, 2) Y1= f(X11, X12) + rLaplace(n)
fit=lm(Y1~X11+X12, data= data.frame(Y1, X11, X12)) rez1=fit$residuals
X21=rexp(n, 1);X22=rexp(n,2) Y2= f(X21, X22) + rLaplace(n)
fit2=lm(Y2~X21+X22, data= data.frame(Y2, X21, X22)) rez2=fit2$residuals
ks.test(rez1, rez2))
\end{verbatim}
\end{document}
ks.test(x1-mean(x1), x2-mean(x2))
you would experience identical problems. $\endgroup$