# In OLS, what does a within transformation in a panel do to individuals where a dummy only changes for some?

Say I have a treatment variable x that takes the following values for four individuals i1, i2, i3, i4 over two time periods t1, t2:

x:

    t1    t2
i1  0     1
i2  1     0
i3  0     0
i4  1     1


It could be a treatment variable denoting when patients get a medication for example. Some patients get the medication once (i1, i2), some never (i3), some always (i4). I within transform this sample such that:

$x^{transformed}_i = x_{it} - \bar{x_i}$

and get, if my intuition is correct:

$x^{transformed}_i$:

     t1       t2
i1  -0.5      0.5
i2   0.5     -0.5
i3   0        0
i4   0        0


This is what happens in a standard fixed effects panel I think. My question is the following: How are these 0's for i3 and i4 interpreted? If I'm not mistaken, OLS on this transformed data will simply treat them as realizations of a continuous random variable. So it will interpret a 0 as something between -0.5 and +0.5.

Looking at the OLS Estimator:

$(X'X)^{-1}X'y$

Where y is then a within transformed outcome variable $y^{transformed} = [y^{i1}_{t1}, y^{i1}_{t2}, y^{i2}_{t1}, y^{i2}_{t2}, y^{i3}_{t1}, y^{i3}_{t2}, y^{i4}_{t1}, y^{i4}_{t2}]'$, I see that the last 4 elements would not contribute to the estimation as they are multiplied with a 0 in the X vector $x^{transformed} = [-0.5, 0.5, 0.5, -0,5, 0, 0, 0, 0]'$. However, both Stata and R will report completely different results if I drop such observations. Also, my intuition tells me that such a treatment analysis should compare the x = 1 to the x = 0 and not place i3 and i4 somewhere along the scale between treated and untreated. What am I missing?

• Why do this transformation at all? Dec 2, 2017 at 13:04
• to get rid of individual specific constants. Its known as 'fixed effects' or 'individual specific intercepts' in econometrics. Think of a regression comparing GDP to happiness over different countries over time. It will be super positive if you have very poor (and unhappy) and very rich (and less unhappy) countries in the sample. Once you control for the individual intercept, the relationship might be more ambiguous. en.wikipedia.org/wiki/Fixed_effects_model Dec 2, 2017 at 13:30
• It sounds like you're concerned about controlling for mean differences between cross-sections wrt independent variables such as GDP, is that correct? If so, there are other workarounds to this issue that don't involve transformation of the dummy variables. These include taking a natural log transformation of the continuous independent variables to 'level the playing field'. Unless I'm missing something in your explanation. Dec 2, 2017 at 14:56
• I solved the problem by now and the post is actually correct, the problem was that I programmed nonsense into Stata and R... Should I delete the question? Thanks so much for your time and sorry for the bother... Dec 3, 2017 at 19:15
• Don't worry about it. Dec 3, 2017 at 19:18

I solved the 'problem', the analysis in the question is correct, I simply programmed R and Stata badly.

So the OLS estimator can be calculated by hand, say I have this y vector and I just want to consider the data for i1 and i3 for simplicity (the first two observations are i1):

$x' = [0, 1, 0, 0]$

$y' = [1, 4, 5, 6]$

within transformed version with i3:

$y^* = [-1.5, 1.5, -0.5, 0.5]$

$x^* = [-0.5, 0.5, 0, 0]$

$\beta^{OLS}= (x^{*'}x^{*})^{-1}x^{*'}y^{*} = 1.5/0.5 = 3$

demeaned version without i3:

$y^* = [-1.5, 1.5]$ $x^* = [-0.5, 0.5]$

$\beta^{OLS}= (x^{*'}x^{*})^{-1}x^{*'}y^{*} = 3$

or in R:

x <- c(-0.5, 0.5,0,0)
y <- c(-1.5, 1.5, -0.5, 0.5)
solve(t(x)%*%x)%*%t(x)%*%y

x <- c(-0.5, 0.5)
y <- c(-1.5, 1.5)
solve(t(x)%*%x)%*%t(x)%*%y