4
$\begingroup$

I have adjusted the parameters (lambda, mu, sigma) for a mixture of two normals fitted to my data. Now I would like to plot the cdf of this model using the explicit function instead of the ecdf. Is there any way to do this or I do I have to simulate data so then I can use again ecdf?

The explicit function is something like:

ipc_values_EM\$lambda[1] * dnorm(x, ipc_values_EM\$mu[1], ipc_values_EM\$sigma[1]) 
+
ipc_values_EM\$lambda[2] * dnorm(x, ipc_values_EM\$mu[2], ipc_values_EM\$sigma[2]) 

(as you can note, is the mixture of two normals different mus and different sigmas)

$\endgroup$

3 Answers 3

7
$\begingroup$

Like the title of the function ecdf() says, it is empirical and only runs on samples.

If you want the exact cdf of a Gaussian, the function you are looking for is pnorm(). Here is a demonstration.

x <- seq(from=-5, to=5, by=.1)
y <- pnorm(x)
plot(x, y, type='l')

If you replace dnorm() by pnorm() in your code, and x by the range of values you want to take the cdf over you should get the result you are looking for.

$\endgroup$
1
  • 1
    $\begingroup$ I tried a little different approach but with the same spirit in mind, just used the curve() function and the code is as follows: curve(ipc_values_EM\$lambda[1] * pnorm(x, ipc_values_EM\$mu[1], ipc_values_EM\$sigma[1]) + ipc_values_EM\$lambda[2] * pnorm(x, ipc_values_EM\$mu[2], ipc_values_EM\$sigma[2]), from=-0.10, to=0.07, add=TRUE, col="blue") $\endgroup$
    – natorro
    Commented May 21, 2012 at 10:12
6
$\begingroup$

You might be interested in using the distr package for plotting the theoretical distribution functions for mixture distributions. Here is a quick example:

library(distr)
tmp <- UnivarMixingDistribution( Norm(10,2), Norm(15,1), mixCoeff=c(1,2)/3)
plot(tmp)

enter image description here

$\endgroup$
0
$\begingroup$

I think this approach works for plotting a cdf of a standard normal. It seems to give almost the same answer as pnorm(x, 0, 1) with the standard normal and also allows the function to be modified.

sigma <- 1
mu    <- 0

integrand <- function(x) {(1/(sigma*sqrt(2*pi)))*(exp(1)^((-1*((x-mu)^2))/(2*(sigma^2))))}

my.cdf <- matrix(0, ncol=2, nrow=length(seq(-5,5,0.01)))

m <- 1

for(i in seq(-5,5,by=0.01)){

     my.cdf[m,1] <- i

     my.cdf[m,2] <- as.numeric(integrate(integrand, lower = -5, upper = i)[1])

     m <- m+1

}

plot(my.cdf[,1], my.cdf[,2])

x <- seq(-5,5,0.01)

my.cdf2 <- pnorm(x, 0, 1)

round(my.cdf[,2],5) - round(my.cdf2,5)

Here I modify the function to approximate what I suspect you want. I am not sure whether this gives the solution you are after:

myconstant1 <- 0.4
sigma1 <- 1
mu1    <- 0

myconstant2 <- 0.2
sigma2 <- 2
mu2    <- 3

integrand <- function(x) {myconstant1 * ((1/(sigma1*sqrt(2*pi)))*(exp(1)^((-1*((x-mu1)^2))/(2*(sigma1^2))))) +
                          myconstant2 * ((1/(sigma2*sqrt(2*pi)))*(exp(1)^((-1*((x-mu2)^2))/(2*(sigma2^2))))) }

my.cdf <- matrix(0, ncol=2, nrow=length(seq(-5,10,0.01)))

m <- 1

for(i in seq(-5,10,by=0.01)){

     my.cdf[m,1] <- i

     my.cdf[m,2] <- as.numeric(integrate(integrand, lower = -5, upper = i)[1])

     m <- m+1

}

jpeg(file="myplot.jpeg")
plot(my.cdf[,1], my.cdf[,2])
dev.off()

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.