0

I want to compute a posterior density plot with conjugate prior

I have data with known parameters (mean =30 , sd =10)

I have two priors one with normal distribution with known parameter ( mean =10 , sd=5) and other with t distribution with same mean and sd but degree of freedom 4

I want a graph with density plots for prior,data and posterior ?

can you help me with r code for this problem ?

Plus I am getting wrong density function for posterior in my opinion..Here is my code so far

x=seq(from=-90, to=90, by= 1)
data=dnorm(x,mean=30,sd =10)
prior = dnorm(x,mean=10,sd =5)
posterior = dnorm(x,mean=10,sd =5)*dnorm(x,mean=30,sd =10) # prior* data  #Prior*data

plot(x,data , type="l", col="blue")
lines(x,prior, type="l", col="red")
lines(x,posterior , type="l", col="green")
anupam
  • 135
  • 1
  • 2
  • 7
  • Please make this question [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), and then edit this question with updates (specifically including *what code have you tried so far*?). – r2evans Jul 29 '17 at 07:53
  • Hi I have added code to make example reproducible – anupam Jul 29 '17 at 09:16

1 Answers1

1

You need to add the two distributions together not multiply. I attach an example below that uses equal weight between the two distributions:

x <- seq(from = -90, to = 90, by = 1)
data <- dnorm(x, mean = 30, sd = 10)
prior <- dnorm(x, mean = 10, sd = 5)
posterior <- 0.5 * dnorm(x, mean = 10, sd = 5) + 0.5 * dnorm(x, mean = 30, sd = 10)

plot(x, prior, type = "l", col = "red")
lines(x, posterior, type = "l", col = "green")
lines(x, data , type = "l", col = "blue")

enter image description here

gcons
  • 411
  • 2
  • 6
  • 1
    The posterior looks like a mixture distribution. I don't think that is the case. The posterior can be calculated analytically as well though. – anupam Jul 29 '17 at 10:42