In statistics, there is rejection method to generate a random variable. (but it is not main problem)
I finished the sampling, and want to draw graphics in one screen.
I thought it was an 'add=TRUE' option, and I set it up.
But it showed me a new screen.
I do not know what is wrong.
I'm sorry to post a trivial question, but I have no other place to ask.
nsample=10000
fx = function(x){2*exp(-(x^2)/2)/sqrt(2*pi)}
gx = function(x){exp(-x)}
fxdivgx = function(x){(2*exp(-(x^2)/2)/sqrt(2*pi)/exp(-x))}
grid=seq(0.0,5.0, length.out =1000)
which(fxdivgx(grid)==max(fxdivgx(grid)))
grid[201] #x valuable which fx/gx has maximum value
const = fxdivgx(grid[201])
const
fn = function(x){((2*exp(-(x^2)/2)/sqrt(2*pi))/(exp(-x)*const))}
result2 = list()
result2$candi=rexp(nsample)
result2$targetDen=fn(result2$candi)
result2$keep=ifelse(runif(nsample)<result2$targetDen, TRUE, FALSE)
hist(result2$candi[result2$keep], freq=F, breaks=100)
curve(2*exp(-(x^2)/2)/sqrt(2*pi), add='TRUE', col='red')
This is my code.
When I run this code, the results are printed on different screens.
How do I print two pictures in one screen?
hist(result$candi[result$keep], breaks=100)
curve((2*exp(-(x^2)/2)/sqrt(2*pi)), add=TRUE, col='red')