Order of variables in R's lm I dont quite understand the answer given in
Order of variables in R lm model
in lm() function of R (and generally formulas) why changing the order of variable matters? My own guess is that the model first calculates the effect of first variable, and then uses the second variable for remaining variation in dependent variable and so on.
set.seed(1)
a = seq(1:100) + rnorm(100, sd=5)
    b = seq(0.01:0.99, by=0.01) + rnorm(100,sd=3)/100
    c = seq(1:100) + rnorm(100,sd=3)
    d = seq(1:100) + rnorm(100,sd=3)
    summary(lm(a~c+b+d))
    summary(lm(a~b+c+d))

 A: Here is a more formal answer (more elegant proofs that start with something like "consider the space spanned by the columns of $X$..." are surely possible) to the question of why just changing the order of the regressors does not matter. As I sketch at the end, the question you link to deals with a case where more than just permuting the columns is happening (as the accepted answer there, I believe, also explains quite well).
Consider changing the position of the variables in the $(n\times p)$ regressor matrix
$$
X=(X_1,\ldots,X_p),
$$
where $X_j=(x_{1j},\ldots,x_{nj})'$, $j=1,\ldots,p$, amounts to postmultiplying $X$ with a $(p\times p)$ permutation matrix $P$ that has a single entry 1 in each column $j$ that indicates the new column position of that regressor $X_j$.
For example, if the new columns are to be the old columns 2, 1 and 3, we have
$$P=\begin{pmatrix}
0&1&0\\
1&0&0\\
0&0&1
\end{pmatrix}$$
This matrix $P$ is invertible, being just a permuted version of the identity matrix.
Also, as it is easy to check that $P'P=I$, $P^{-1}$ is equal to the transpose of $P$, $P^{-1}=P'$.
Thus, the OLS coefficient of the regression of $y$ on the transformed regressors, call it $\hat\beta_t$, is
\begin{eqnarray*}
\hat\beta_t&=&((XP)'XP)^{-1}(XP)'y\\
&\stackrel{(AB)'=B'A'}{=}&(P'X'XP)^{-1}P'X'y\\
&\stackrel{(ABC)^{-1}=C^{-1}B^{-1}A^{-1}}{=}&P^{-1}(X'X)^{-1}\underbrace{(P')^{-1}P'}_{=I}X'y\\
&=&P^{-1}(X'X)^{-1}X'y\\
&=&P'(X'X)^{-1}X'y\\
&=&P'\hat\beta\\
\end{eqnarray*}
Here, $P'$ is a matrix that permutes the row elements of $\hat\beta$, and hence permutes the entries of the original coeffient estimator $\hat\beta$ according to the permutation of the columns.
To see why the question you link to addresses a slightly different situation in which something else happens than just permuting the columns, I suggest to inspect model.matrix(out_1) and model.matrix(out_2) in that code, which gives you the different regressor matrices in the two models.
A: Couple of additional points.

*

*your understanding of the calculation process is incorrect.


My own guess is that the model first calculates the effect of first variable, and then uses the second variable for remaining variation in dependent variable and so on.

No, Linear Regression calculating all the coefficients at the same time, but one by one. In R, lm is using QR decomposition. And changing the order is just switching columns in the matrix.


*When people say, order of the variable it may mean "Coding Systems for Categorical Variables in Regression" and reference level for categorical Variables.

For example if you are fitting a regression model, and one of the variable is education level. (high school, bachelor, master), If the reference level is different. Then the coefficient will be different.
