Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: Programming the fixed effect transformation in Mata with an unbalanced panel data

 From Matthew Baker To statalist@hsphsun2.harvard.edu Subject Re: st: Programming the fixed effect transformation in Mata with an unbalanced panel data Date Wed, 22 May 2013 11:38:22 -0400

```Matteo --

While there might be a better solution, you could always loop over
your panels and demean the variables as you go along, while also
creating a variable that can be used to pick out just the last
observation. This might not be terrible because presumably you'll only
have to do this once!

Anyways, an example:

/* Begin the example */

webuse nlswork
mata
st_view(id=.,.,"id")
st_view(y=.,.,"ln_w")

m=panelsetup(id,1)

yh     =J(rows(y),1,.)
notLast=J(rows(y),1,1)

for (i=1;i<=rows(m);i++) {
yp=panelsubmatrix(y,i,m)
yh[m[i,1]::m[i,2],1]=yp:-mean(y)
notLast[m[i,2],1]=0
}

/* Use only the first N-1 observations */

yhNotLast=select(yh,notLast)
end

/* End example */

In the above, yh is a placeholder for the demeaned variable, while
notLast is a variable that takes on the value zero for the last
observation. The mata select command can then be used to pick out
everything but this last variable.

Hope that helps!

Matt Baker

On Mon, May 20, 2013 at 12:16 PM, Matteo Pazzona <M.Pazzona@bath.ac.uk> wrote:
> Dear Statalist,
>
> I have an unbalanced panel data and I need to program the fixed effect
> transformation with MATA. The code I am trying to program is the usual (
> let's call the matrix for all the panel M):
>
> M = (I(N) #  (I(T) - 1/T(ee'))
>
> where N stands for unit observations ,T the total number of years, e =
> (1,1,...,1) is 1xT vector of ones and # is the kronecker product. Given that
> I have an unbalanced panel data, I have a varying number of years.  I am
> able to program it with balanced panel data but not with unbalanced data. I
> have checked on the FAQs but I could not find a clear answer to that.
> Secondly, after having computed the fixed effect transformation I need to
> delete the last observation for each group. So, if Y is the vector of
> independent variables , then I want to delete the Tth observation ( last
> observation) for each group of the matrix MY . Again, since the number of
> years is not fixed I am not able how to do that.
> If you could help me out on this too, it would be very helpful.
>
>
> Best,
>
> Matteo
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/

--
Dr. Matthew J. Baker
Department of Economics
Hunter College and the Graduate Center, CUNY
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/
```