library(MCMCpack) library(MASS) library(ggplot2) # Generate some data # parameters for the DGP mu <- 30 sigma <- sqrt(5) n <- 50 # data x <- rnorm(n=n, mean=mu, sd=sigma) # summary(x) # ML estimates xbar <- mean(x) sdev <- sd s2 <- var(x) # Algorithmus 2 # Bayes S <- 5000 # number of draws #step 1 sigma2_sim <- rinvgamma(n=S, (n-1)/2, (n-1)*s2/2) # step2 mu_sim <- rnorm(n=S, mean=xbar, sd= sqrt(sigma2_sim/n)) plot(density(mu_sim)) plot(density(sigma2_sim)) h <- kde2d(mu_sim, sigma2_sim) contour(h$x,h$y,h$z) image(h$x,h$y,h$z)