Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down at the end of May, and its replacement, **statalist.org** is already up and running.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

From |
Misha Spisok <misha.spisok@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
st: gradient in -ml- producing type mismatch? |

Date |
Sat, 8 May 2010 03:16:14 -0700 |

Hello, Statalist! I'm getting the following error (-ml- program and example that reproduces error is included below; extra details about the function being maximized and its gradient are at the end): type mismatch r(109); end of do-file r(109); after running my maximum likelihood function. I suspect it's due to the gradient. Can I use -mlvecsum- to form pieces that will be used subsequently? If so, how? For example, (borrowed heavily from -multin- by P. Guimaraes), given that -mlvecsum- multiplies the expression by x_{ij} (as stated in the manual), consider the following: program define mycondlog version 10.1 args todo b lnf g negH tempvar theta sumvij prob ysum a1 a2 partone newpiece sumexb myfrac obsg mleval `theta'=`b' local by $GROUP /* GROUP includes all choices for agents of a given type */ sort `by' quietly { by `by': egen double `sumvij' = sum(exp(`theta')) gen double `prob' = 1/(exp(-`theta')*`sumvij') mlsum `lnf' = $ML_y1*(`theta'-log(`sumvij')) if (`todo' == 0 | `lnf' >= .) exit /* if I could use mlvecsum to form pieces... */ mlvecsum `lnf' `partone' = $ML_y1 /* This forms the first part */ mlvecsum `lnf' `newpiece' = exp(`theta') /* which forms x_{ij}*exp(x_{ij}b) */ by `by': egen double `sumexb'= sum(`newpiece') gen double `myfrac' = `sumexb'/`sumvij' gen double `obsg' = `partone' - $ML_y1*`myfrac' matrix `g' = `obsg' } end Now I can do the following: use http://www.stata-press.com/data/r11/clogitid, clear expand 2 bys id: replace x2 = x2/count if _n==2 multin count x1 x2, gr(ycount) /* This assigns $GROUP; could also write: global GROUP ycount */ poisson ycount x1 x2, nocons mat b0 = e(b) local initopt init(`b0', skip) ml model d1 mycondlog (ycount = x1 x2, nocons), init(`b0') ml maximize type mismatch r(109); end of do-file r(109); -----Extra Details----- For the conditional logit, p_{ij} = exp(x_{ij}b)/[sum_{l} exp(x_{il}b)], where i indexes choice-making agents and j indexes alternatives, so that this is the probability that agent i chooses alternative j. The log-likelihood for the conditional logit is ll = y_{ij}*p_{ij} ll = y_{ij} * ln( exp(x_{ij}b)/[sum_{l} exp(x_{il}b)] ) The derivative of p_{ij} with respect to the parameter vector, b, can be written as p_{ij}*[ x_{ij} - xbar_{i} ] where xbar_{i} = sum_{l} p_{il}*x_{il}. The gradient can be written as g = sum_{i}sum_{j} y_{ij}*[ x_{ij} - xbar_{i} ] so for a given observation, it is g_{ij} = y_{ij}*[ x_{ij} - xbar_{i} ] (This is all from Cameron and Trivedi, Microeconometrics, p.524.) Another way to write the gradient is g = sum_{i}sum_{j} y_{ij}*[ x_{ij} - (sum_{l}x_{il}exp(x_{il}b))/(sum_{l}exp(x_{il}b)) ] so for a given observation, it is g_{ij} = y_{ij}*[ x_{ij} - (sum_{l}x_{il}exp(x_{il}b))/(sum_{l}exp(x_{il}b)) ] -mlvecsum- would work for the first part of g, y_{ij}*x_{ij}, but what about the second part (in either formulation)? Thanks for your time. Misha * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/

**Follow-Ups**:**Re: st: gradient in -ml- producing type mismatch?***From:*Maarten buis <maartenbuis@yahoo.co.uk>

- Prev by Date:
**RE: st: looping with geodist** - Next by Date:
**RE: st: looping with geodist** - Previous by thread:
**st: Delete missing** - Next by thread:
**Re: st: gradient in -ml- producing type mismatch?** - Index(es):