Reputation: 27
I am wondering how to plot a joint distribution in R for a normal distribution. For example, if the normal distribution f(x) is comprised of two functions:
f_1(x) ~ Normal(0, 1)
f_2(x) ~ Normal(2, 1)
then how can I add an argument in R to portray this? I'm looking for an argument like the "shape1" type in the beta distribution, but can't figure out how to expand the regular dnorm argument to make it a joint distribution. Any suggestions?
Thank you!
Upvotes: 0
Views: 3313
Reputation: 57696
It looks like you want to create a distribution that is the mixture of 2 normals. The density of the mixture is just the (weighted) sum of the component densities, so you can do the following.
f <- function(x, p1 = 0.5, p2 = 1 - p1, m1, m2)
p1 * dnorm(x, m1) + p2 * dnorm(x, m2)
x <- seq(-2, 4, len=101)
dens <- f(x, p1 = 0.5, m1=0, m2=2)
plot(x, dens, type = "l")
Upvotes: 2
Reputation: 27
Here's my code: Its not quite right, but I'll edit it in the answer when it is right. I'm still not getting two local maxima in the graph.
f <- function(x, p1=0.5, p2=1-p1, m1, m2)
p1*dnorm(x, m1) + p2*dnorm(x,m2)
x <- seq(-2, 4, len=100)
f(x, m1=0, m2=2)
curve(f(x, m1=0, m2=0))
curve(f(x, m1=1, m2=0,), col="red", add=T)
curve(f(x, m1=2, m2=0,), col="green", add=T)
curve(f(x, m1=3, m2=0,), col="blue", add=T)
curve(f(x, m1=4, m2=0,), col="orange", add=T)
Upvotes: 0