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 <[email protected]> To [email protected] Subject Re: st: Programming the fixed effect transformation in Mata with an unbalanced panel data Date Wed, 26 Jun 2013 08:35:10 -0400

```Matteo --

The following is a way of constructing a matrix like the one you have
- but you might run into allocational issues for larger problems,
because for large N and large T, M is a matrix with a lot of zeros and
can really eat up the space! I might also mention that my little code
here uses loops, and there might be a more efficient way to do this.

/* Begin example */

clear all
webuse union
keep if id<5

mata
st_view(id=.,.,"idcode")
M=J(rows(id),rows(id),0)

for (i=1;i<=rows(m);i++) {
ni=[i,2]-m[i,1]+1
M[m[i,1]::m[i,2],m[i,1]::m[i,2]]=I(ni)-J(ni,ni,1/ni)
}
end
/* End example */

Hope that helps!

Matt

On Mon, May 20, 2013 at 12:16 PM, Matteo Pazzona <[email protected]> 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/
```