# How to get first difference of count variable for poisson regression

Poisson regression using Panel data requires the dependent variable ($$y_{it}$$) to be a non-negative count variable. I need to take the first difference of the dependent variable to deal with reverse causality of the regressors. Cameron and Trivedi's Microeconometrics using Stata outline a transformation

$$\frac{\lambda_{i,t-1}}{\lambda_{i,t}}*y_{i,t}-y_{i,t-1}$$

where

$$\lambda_{i,t}=exp(x_{it}'\beta)$$

which, they state, can be used to eliminate the FE and as the basis for estimation of dynamic panel count models. My question is how can one actually estimate $$\lambda_{it}$$ (ideally in Stata) so that it can then be used to create first differences?

It seems that I need to use the gmm program evaluator to take the differencing as required in the question. I wrote the following program but it's not running and giving the error

could not calculate numerical derivatives -- flat or discontinuous region encountered

the program that I wrote is as follows

capture program drop gmm_poifd
program gmm_poifd
version 11
syntax varlist if, at(name) myrhs(varlist) mylhs(varlist) myidvar(varlist)
quietly {
tempvar mu muLag yLag
gen double mu' = 0 if'
local j = 1
foreach var of varlist myrhs' {
replace mu' = mu' + var'*at'[1,j'] if'
local j = j' + 1
}
replace mu' = exp(mu')
gen double muLag' = L.mu' if'
gen double yLag' = L.mylhs' if'
replace varlist' = muLag'/mu' * mylhs' - yLag' if'
}
end

gmm gmm_poifd, mylhs(PostMile1) myrhs(Traffic $$Messages$$Time $$Weather) /// myidvar(MessageBoardID) nequations(1) parameters(Traffic$$Messages $$Time$$Weather) ///
instruments(Traffic $$Messages$$Time \$Weather) onestep