On Thu, 17 May 2012, Nrz Mrva wrote:
I am starting to learn gretl. Can you help me: may I forecast logit
multinomial model in gretl?
Gretl's "fcast" command is not available for multinomial logit
at present, partly because it's not very clear what a forecast
should look like for that sort of model. However, it's not
very difficult to write a script that will produce the
estimated probability for each outcome, given the estimated
parameters and some data. You can then pick the outcome with
the highest probability as the "forecast" if you like.
Here's an example script ("hansl" is gretl's scripting
language):
<hansl>
function matrix mlogit_probs (matrix b, matrix X, matrix y)
scalar m = rows(y)
scalar nb = rows(b) / (m-1)
scalar T = rows(X)
matrix probs = zeros(T, m)
matrix num = ones(1, m)
loop t=1..T --quiet
scalar den = 1
k = 1
loop j=2..m --quiet
matrix bj = b[k:k+nb-1]
num[j] = exp(X[t,]*bj)
den += num[j]
k += nb
endloop
probs[t,] = num / den
endloop
return probs
end function
open keane.gdt
smpl year==87 --restrict
list Xlist = const educ exper expersq black
matrix yvals = values(status)
# estimate, reserving the last 20 observations
smpl ; -20
logit status Xlist --multinomial
matrix theta = $coeff
# now forecast for the last 20 obs
smpl ; +20
sstart = $nobs - 19
smpl sstart ;
matrix X = {Xlist}
matrix probs = mlogit_probs(theta, X, yvals)
# show the probabilities per outcome
print probs
# show the most probable outcome
matrix fc = imaxr(probs)
print fc
</hansl>
Allin Cottrell