Reputation: 31
I'm trying to use Julia's GLM package. Based on my experience in R, I have always loved its simulate
function
simulate(object, nsim = 1, seed = NULL, ...)
which draws response from a fitted model. Is there any method similar in Julia that can apply to its fitted GLM model?
Upvotes: 3
Views: 118
Reputation: 69909
AFAIK there is no such function, but it should be relatively easy to implement it for your special case.
Here is an example for unweighted OLS with Gaussian (family Normal()
) assumption:
function simulate(m, n=1)
predict(m) .+ randn(Int(nobs(m)), n)*sqrt(deviance(m)/dof_residual(m))
end
which is equivalent to R implementation.
As a side note: in my experience instead from the distribution used by simulate
in R usually mean response or predicted response distributions are more useful depending on what you need.
EDIT:
Probably a more Julian way to do it would be to avoid generating the sample but to return an object that you can sample from like this:
simdist(m) = Normal.(predict(m), sqrt(deviance(m)/dof_residual(m)))
now you can use rand
to perform sampling eg.:
sdm = simdist(m)
rand.(sdm)
rand.(sdm, 10)
This way you often can avoid allocation of large memory chunks. E.g. if you wanted nobs=10^9
in R you would most probably run out of memory and using simdist
you can efficiently generate what you need on the go.
Upvotes: 1