mvrnorm

104 views
Skip to first unread message

Yakir Gagnon

unread,
Feb 2, 2015, 7:28:02 PM2/2/15
to julia...@googlegroups.com
Is there a function or functionality equivalent or similar to R's mvrnorm?

I want to fit a linear model to a bunch of `x` and `y` points, and then generate new *random* `x2` and `y2` points that would follow the distribution of the fitted model. So unlike `predict`, I need the new points to be random and not **just** follow the linear model's regression line. These point's randomness should be similar to that of the original `x` and `y` data. 

Andreas Noack

unread,
Feb 2, 2015, 7:51:19 PM2/2/15
to julia...@googlegroups.com
I think that MvNormal might do what you want

julia> X = MvNormal(ones(2), [1 0.5;0.5 1])

FullNormal(

dim: 2

μ: [1.0,1.0]

Σ: 2x2 Array{Float64,2}:

 1.0  0.5

 0.5  1.0

)



julia> rand(X, 4)

2x4 Array{Float64,2}:

 0.228904  0.326973   2.19096   -0.130484

 2.49441   0.872328  -0.283637  -0.397549


2015-02-02 19:28 GMT-05:00 Yakir Gagnon <12.y...@gmail.com>:
Is there a function or functionality equivalent or similar to R's mvrnorm?

I want to fit a linear model to a bunch of `x` and `y` points, and then generate new *random* `x2` and `y2` points that would follow the distribution of the fitted model. So unlike `predict`, I need the new points to be random and not **just** follow the linear model's regression line. These point's randomness should be similar to that of the original `x` and `y` data. 

--
You received this message because you are subscribed to the Google Groups "julia-stats" group.
To unsubscribe from this group and stop receiving emails from it, send an email to julia-stats...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Yakir Gagnon

unread,
Feb 2, 2015, 8:06:26 PM2/2/15
to julia...@googlegroups.com
That looks right! Thanks! I'll look into how I can feed the results from the GLM fit into MvNormal to get what I want. Thanks a lot!

Yakir Gagnon

unread,
Feb 2, 2015, 8:27:49 PM2/2/15
to julia...@googlegroups.com
Thanks again, this is what I needed.
Here's a bit of code to help anyone struggling to start with this:
using GLM, ASCIIPlots, DataFrames
n
= 25
x
= linspace(0,10,n)
y
= 2x .+ 3randn(n)
d
= DataFrame(X = x, Y = y)
f
= lm(Y ~ X, d)
f2
= MvNormal(coef(f),vcov(f))
nl
= 10000
ba
= rand(f2,nl)
b
= vec(ba[1,:])
a
= vec(ba[2,:])
xl
= linspace(0,10,nl)
yl
= a.*xl .+ b
scatterplot
(x,y, sym = '*')
scatterplot
(xl,yl, sym = '*')

Reply all
Reply to author
Forward
0 new messages